Skip to content

Commit 2046f42

Browse files
authored
Merge pull request kubernetes#95660 from rikatz/improve-create-ingress
Fix catch all regex and missing DryRun Options
2 parents 86a208e + 45d6672 commit 2046f42

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ import (
4040

4141
var (
4242
// Explaining the Regex below:
43-
// ^(?P<host>.+) -> Indicates the host - 1-N characters
43+
// ^(?P<host>[\w\*\-\.]*) -> Indicates the host - 0-N characters of letters, number, underscore, '-', '.' and '*'
4444
// (?P<path>/.*) -> Indicates the path and MUST start with '/' - / + 0-N characters
4545
// Separator from host/path to svcname:svcport -> "="
4646
// (?P<svcname>[\w\-]+) -> Service Name (letters, numbers, '-') -> 1-N characters
4747
// Separator from svcname to svcport -> ":"
4848
// (?P<svcport>[\w\-]+) -> Service Port (letters, numbers, '-') -> 1-N characters
49-
regexHostPathSvc = `^(?P<host>.+)(?P<path>/.*)=(?P<svcname>[\w\-]+):(?P<svcport>[\w\-]+)`
49+
regexHostPathSvc = `^(?P<host>[\w\*\-\.]*)(?P<path>/.*)=(?P<svcname>[\w\-]+):(?P<svcport>[\w\-]+)`
5050

5151
// This Regex is optional -> (....)?
5252
// (?P<istls>tls) -> Verify if the argument after "," is 'tls'
@@ -66,8 +66,8 @@ var (
6666
# svc1:8080 with a tls secret "my-cert"
6767
kubectl create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"
6868
69-
# Create a catch all ingress pointing to service svc:port and Ingress Class as "otheringress"
70-
kubectl create ingress catch-all --class=otheringress --rule="_/=svc:port"
69+
# Create a catch all ingress of "/path" pointing to service svc:port and Ingress Class as "otheringress"
70+
kubectl create ingress catch-all --class=otheringress --rule="/path=svc:port"
7171
7272
# Create an ingress with two annotations: ingress.annotation1 and ingress.annotations2
7373
kubectl create ingress annotated --class=default --rule="foo.com/bar=svc:port" \
@@ -156,6 +156,7 @@ func NewCmdCreateIngress(f cmdutil.Factory, ioStreams genericclioptions.IOStream
156156

157157
cmdutil.AddApplyAnnotationFlags(cmd)
158158
cmdutil.AddValidateFlags(cmd)
159+
cmdutil.AddDryRunFlag(cmd)
159160
cmd.Flags().StringVar(&o.IngressClass, "class", o.IngressClass, "Ingress Class to be used")
160161
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.")
161162
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 {
344345
hostAlreadyPresent[host] = struct{}{}
345346
continue
346347
}
347-
if host != "_" {
348+
if host != "" {
348349
ingressTLS.Hosts = append(ingressTLS.Hosts, host)
349350
}
350351
if secret != "" {
@@ -371,7 +372,7 @@ func (o *CreateIngressOptions) buildIngressRules() []networkingv1.IngressRule {
371372
ingressPath := buildHTTPIngressPath(hostSplit[1])
372373
ingressRule := networkingv1.IngressRule{}
373374

374-
if host != "_" {
375+
if host != "" {
375376
ingressRule.Host = host
376377
}
377378

staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ func TestCreateIngressValidation(t *testing.T) {
108108
},
109109
expected: "rule foo.com=othersvc:8080 is invalid and should be in format host/path=svcname:svcport[,tls[=secret]]",
110110
},
111+
"valid catch all rule": {
112+
rules: []string{
113+
"/path/subpath*=svc:redis,tls=blo",
114+
},
115+
expected: "",
116+
},
111117
}
112118

113119
for name, tc := range tests {
@@ -144,7 +150,7 @@ func TestCreateIngress(t *testing.T) {
144150
}{
145151
"catch all host and default backend with default TLS returns empty TLS": {
146152
rules: []string{
147-
"_/=catchall:8080,tls=",
153+
"/=catchall:8080,tls=",
148154
},
149155
ingressclass: ingressClass,
150156
defaultbackend: "service1:https",
@@ -195,6 +201,63 @@ func TestCreateIngress(t *testing.T) {
195201
},
196202
},
197203
},
204+
"catch all with path of type prefix and secret name": {
205+
rules: []string{
206+
"/path*=catchall:8080,tls=secret1",
207+
},
208+
ingressclass: ingressClass,
209+
defaultbackend: "service1:https",
210+
annotations: []string{},
211+
expected: &networkingv1.Ingress{
212+
TypeMeta: metav1.TypeMeta{
213+
APIVersion: networkingv1.SchemeGroupVersion.String(),
214+
Kind: "Ingress",
215+
},
216+
ObjectMeta: metav1.ObjectMeta{
217+
Name: ingressName,
218+
Annotations: map[string]string{},
219+
},
220+
Spec: networkingv1.IngressSpec{
221+
IngressClassName: &ingressClass,
222+
DefaultBackend: &networkingv1.IngressBackend{
223+
Service: &networkingv1.IngressServiceBackend{
224+
Name: "service1",
225+
Port: networkingv1.ServiceBackendPort{
226+
Name: "https",
227+
},
228+
},
229+
},
230+
TLS: []v1.IngressTLS{
231+
{
232+
SecretName: "secret1",
233+
},
234+
},
235+
Rules: []networkingv1.IngressRule{
236+
{
237+
Host: "",
238+
IngressRuleValue: networkingv1.IngressRuleValue{
239+
HTTP: &networkingv1.HTTPIngressRuleValue{
240+
Paths: []networkingv1.HTTPIngressPath{
241+
{
242+
Path: "/path",
243+
PathType: &pathTypePrefix,
244+
Backend: networkingv1.IngressBackend{
245+
Service: &networkingv1.IngressServiceBackend{
246+
Name: "catchall",
247+
Port: networkingv1.ServiceBackendPort{
248+
Number: 8080,
249+
},
250+
},
251+
},
252+
},
253+
},
254+
},
255+
},
256+
},
257+
},
258+
},
259+
},
260+
},
198261
"mixed hosts with mixed TLS configuration and a default backend": {
199262
rules: []string{
200263
"foo.com/=foo-svc:8080,tls=",

0 commit comments

Comments
 (0)