mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #95660 from rikatz/improve-create-ingress
Fix catch all regex and missing DryRun Options
This commit is contained in:
commit
2046f4212a
@ -40,13 +40,13 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
// Explaining the Regex below:
|
// Explaining the Regex below:
|
||||||
// ^(?P<host>.+) -> Indicates the host - 1-N characters
|
// ^(?P<host>[\w\*\-\.]*) -> Indicates the host - 0-N characters of letters, number, underscore, '-', '.' and '*'
|
||||||
// (?P<path>/.*) -> Indicates the path and MUST start with '/' - / + 0-N characters
|
// (?P<path>/.*) -> Indicates the path and MUST start with '/' - / + 0-N characters
|
||||||
// Separator from host/path to svcname:svcport -> "="
|
// Separator from host/path to svcname:svcport -> "="
|
||||||
// (?P<svcname>[\w\-]+) -> Service Name (letters, numbers, '-') -> 1-N characters
|
// (?P<svcname>[\w\-]+) -> Service Name (letters, numbers, '-') -> 1-N characters
|
||||||
// Separator from svcname to svcport -> ":"
|
// Separator from svcname to svcport -> ":"
|
||||||
// (?P<svcport>[\w\-]+) -> Service Port (letters, numbers, '-') -> 1-N characters
|
// (?P<svcport>[\w\-]+) -> Service Port (letters, numbers, '-') -> 1-N characters
|
||||||
regexHostPathSvc = `^(?P<host>.+)(?P<path>/.*)=(?P<svcname>[\w\-]+):(?P<svcport>[\w\-]+)`
|
regexHostPathSvc = `^(?P<host>[\w\*\-\.]*)(?P<path>/.*)=(?P<svcname>[\w\-]+):(?P<svcport>[\w\-]+)`
|
||||||
|
|
||||||
// This Regex is optional -> (....)?
|
// This Regex is optional -> (....)?
|
||||||
// (?P<istls>tls) -> Verify if the argument after "," is 'tls'
|
// (?P<istls>tls) -> Verify if the argument after "," is 'tls'
|
||||||
@ -66,8 +66,8 @@ var (
|
|||||||
# svc1:8080 with a tls secret "my-cert"
|
# svc1:8080 with a tls secret "my-cert"
|
||||||
kubectl create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"
|
kubectl create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"
|
||||||
|
|
||||||
# Create a catch all ingress pointing to service svc:port and Ingress Class as "otheringress"
|
# Create a catch all ingress of "/path" pointing to service svc:port and Ingress Class as "otheringress"
|
||||||
kubectl create ingress catch-all --class=otheringress --rule="_/=svc:port"
|
kubectl create ingress catch-all --class=otheringress --rule="/path=svc:port"
|
||||||
|
|
||||||
# Create an ingress with two annotations: ingress.annotation1 and ingress.annotations2
|
# Create an ingress with two annotations: ingress.annotation1 and ingress.annotations2
|
||||||
kubectl create ingress annotated --class=default --rule="foo.com/bar=svc:port" \
|
kubectl create ingress annotated --class=default --rule="foo.com/bar=svc:port" \
|
||||||
@ -156,6 +156,7 @@ func NewCmdCreateIngress(f cmdutil.Factory, ioStreams genericclioptions.IOStream
|
|||||||
|
|
||||||
cmdutil.AddApplyAnnotationFlags(cmd)
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
||||||
cmdutil.AddValidateFlags(cmd)
|
cmdutil.AddValidateFlags(cmd)
|
||||||
|
cmdutil.AddDryRunFlag(cmd)
|
||||||
cmd.Flags().StringVar(&o.IngressClass, "class", o.IngressClass, "Ingress Class to be used")
|
cmd.Flags().StringVar(&o.IngressClass, "class", o.IngressClass, "Ingress Class to be used")
|
||||||
cmd.Flags().StringArrayVar(&o.Rules, "rule", o.Rules, "Rule in format host/path=service:port[,tls=secretname]. Paths containing the leading character '*' are considered pathType=Prefix. tls argument is optional.")
|
cmd.Flags().StringArrayVar(&o.Rules, "rule", o.Rules, "Rule in format host/path=service:port[,tls=secretname]. Paths containing the leading character '*' are considered pathType=Prefix. tls argument is optional.")
|
||||||
cmd.Flags().StringVar(&o.DefaultBackend, "default-backend", o.DefaultBackend, "Default service for backend, in format of svcname:port")
|
cmd.Flags().StringVar(&o.DefaultBackend, "default-backend", o.DefaultBackend, "Default service for backend, in format of svcname:port")
|
||||||
@ -344,7 +345,7 @@ func (o *CreateIngressOptions) buildTLSRules() []networkingv1.IngressTLS {
|
|||||||
hostAlreadyPresent[host] = struct{}{}
|
hostAlreadyPresent[host] = struct{}{}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if host != "_" {
|
if host != "" {
|
||||||
ingressTLS.Hosts = append(ingressTLS.Hosts, host)
|
ingressTLS.Hosts = append(ingressTLS.Hosts, host)
|
||||||
}
|
}
|
||||||
if secret != "" {
|
if secret != "" {
|
||||||
@ -371,7 +372,7 @@ func (o *CreateIngressOptions) buildIngressRules() []networkingv1.IngressRule {
|
|||||||
ingressPath := buildHTTPIngressPath(hostSplit[1])
|
ingressPath := buildHTTPIngressPath(hostSplit[1])
|
||||||
ingressRule := networkingv1.IngressRule{}
|
ingressRule := networkingv1.IngressRule{}
|
||||||
|
|
||||||
if host != "_" {
|
if host != "" {
|
||||||
ingressRule.Host = host
|
ingressRule.Host = host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +108,12 @@ func TestCreateIngressValidation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expected: "rule foo.com=othersvc:8080 is invalid and should be in format host/path=svcname:svcport[,tls[=secret]]",
|
expected: "rule foo.com=othersvc:8080 is invalid and should be in format host/path=svcname:svcport[,tls[=secret]]",
|
||||||
},
|
},
|
||||||
|
"valid catch all rule": {
|
||||||
|
rules: []string{
|
||||||
|
"/path/subpath*=svc:redis,tls=blo",
|
||||||
|
},
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range tests {
|
for name, tc := range tests {
|
||||||
@ -144,7 +150,7 @@ func TestCreateIngress(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
"catch all host and default backend with default TLS returns empty TLS": {
|
"catch all host and default backend with default TLS returns empty TLS": {
|
||||||
rules: []string{
|
rules: []string{
|
||||||
"_/=catchall:8080,tls=",
|
"/=catchall:8080,tls=",
|
||||||
},
|
},
|
||||||
ingressclass: ingressClass,
|
ingressclass: ingressClass,
|
||||||
defaultbackend: "service1:https",
|
defaultbackend: "service1:https",
|
||||||
@ -195,6 +201,63 @@ func TestCreateIngress(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"catch all with path of type prefix and secret name": {
|
||||||
|
rules: []string{
|
||||||
|
"/path*=catchall:8080,tls=secret1",
|
||||||
|
},
|
||||||
|
ingressclass: ingressClass,
|
||||||
|
defaultbackend: "service1:https",
|
||||||
|
annotations: []string{},
|
||||||
|
expected: &networkingv1.Ingress{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: networkingv1.SchemeGroupVersion.String(),
|
||||||
|
Kind: "Ingress",
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: ingressName,
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
|
Spec: networkingv1.IngressSpec{
|
||||||
|
IngressClassName: &ingressClass,
|
||||||
|
DefaultBackend: &networkingv1.IngressBackend{
|
||||||
|
Service: &networkingv1.IngressServiceBackend{
|
||||||
|
Name: "service1",
|
||||||
|
Port: networkingv1.ServiceBackendPort{
|
||||||
|
Name: "https",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
TLS: []v1.IngressTLS{
|
||||||
|
{
|
||||||
|
SecretName: "secret1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Rules: []networkingv1.IngressRule{
|
||||||
|
{
|
||||||
|
Host: "",
|
||||||
|
IngressRuleValue: networkingv1.IngressRuleValue{
|
||||||
|
HTTP: &networkingv1.HTTPIngressRuleValue{
|
||||||
|
Paths: []networkingv1.HTTPIngressPath{
|
||||||
|
{
|
||||||
|
Path: "/path",
|
||||||
|
PathType: &pathTypePrefix,
|
||||||
|
Backend: networkingv1.IngressBackend{
|
||||||
|
Service: &networkingv1.IngressServiceBackend{
|
||||||
|
Name: "catchall",
|
||||||
|
Port: networkingv1.ServiceBackendPort{
|
||||||
|
Number: 8080,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
"mixed hosts with mixed TLS configuration and a default backend": {
|
"mixed hosts with mixed TLS configuration and a default backend": {
|
||||||
rules: []string{
|
rules: []string{
|
||||||
"foo.com/=foo-svc:8080,tls=",
|
"foo.com/=foo-svc:8080,tls=",
|
||||||
|
Loading…
Reference in New Issue
Block a user