Add Ingress TLS support (#1326)
This PR aims to add the Ingress TLS support by specifying the
TLS secret when creating/updating the HTTP trigger.
Command example:
fission route create --name foo \
--url /foo/{bar} --function foofn --createingress \
--ingressannotation "nginx.ingress.kubernetes.io/ssl-redirect=false" \
--ingressannotation "nginx.ingress.kubernetes.io/use-regex=true" \
--ingressrule "*=/foo/*"
--ingresstls "foobartls"
This commit is contained in:
@@ -24,7 +24,7 @@ import (
|
||||
)
|
||||
|
||||
// GetIngressConfig returns an IngressConfig based on user inputs; return error if any.
|
||||
func GetIngressConfig(annotations []string, rule string, fallbackRelativeURL string, oldIngressConfig *fv1.IngressConfig) (*fv1.IngressConfig, error) {
|
||||
func GetIngressConfig(annotations []string, rule string, tls string, fallbackRelativeURL string, oldIngressConfig *fv1.IngressConfig) (*fv1.IngressConfig, error) {
|
||||
|
||||
removeAnns, anns, err := getIngressAnnotations(annotations)
|
||||
if err != nil {
|
||||
@@ -34,6 +34,7 @@ func GetIngressConfig(annotations []string, rule string, fallbackRelativeURL str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
removeTLS, secret := getIngressTLS(tls)
|
||||
|
||||
if oldIngressConfig == nil {
|
||||
if isEmptyRule { // assign default value
|
||||
@@ -44,6 +45,7 @@ func GetIngressConfig(annotations []string, rule string, fallbackRelativeURL str
|
||||
Annotations: anns,
|
||||
Host: host,
|
||||
Path: path,
|
||||
TLS: secret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -73,6 +75,12 @@ func GetIngressConfig(annotations []string, rule string, fallbackRelativeURL str
|
||||
oldIngressConfig.Path = path
|
||||
}
|
||||
|
||||
if removeTLS {
|
||||
oldIngressConfig.TLS = ""
|
||||
} else if len(secret) > 0 {
|
||||
oldIngressConfig.TLS = secret
|
||||
}
|
||||
|
||||
return oldIngressConfig, nil
|
||||
}
|
||||
|
||||
@@ -116,3 +124,14 @@ func getIngressHostRule(rule string, fallbackPath string) (empty bool, host stri
|
||||
}
|
||||
return false, v[0], v[1], nil
|
||||
}
|
||||
|
||||
func getIngressTLS(secret string) (remove bool, tls string) {
|
||||
switch secret {
|
||||
case "-":
|
||||
return true, ""
|
||||
case "":
|
||||
return false, ""
|
||||
default:
|
||||
return false, secret
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ func Test_GetIngressConfig(t *testing.T) {
|
||||
annotations []string
|
||||
rule string
|
||||
fallbackRelativeURL string
|
||||
tls string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -248,10 +249,105 @@ func Test_GetIngressConfig(t *testing.T) {
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "tls-setup",
|
||||
args: args{
|
||||
ingressConfig: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "",
|
||||
},
|
||||
annotations: nil,
|
||||
rule: "",
|
||||
fallbackRelativeURL: "/test",
|
||||
tls: "dummy",
|
||||
},
|
||||
want: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "dummy",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "same-tls",
|
||||
args: args{
|
||||
ingressConfig: nil,
|
||||
annotations: nil,
|
||||
rule: "",
|
||||
fallbackRelativeURL: "/test",
|
||||
tls: "dummy",
|
||||
},
|
||||
want: &fv1.IngressConfig{
|
||||
Annotations: nil,
|
||||
Host: "*",
|
||||
Path: "/test",
|
||||
TLS: "dummy",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "replace-tls",
|
||||
args: args{
|
||||
ingressConfig: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "foobar",
|
||||
},
|
||||
annotations: nil,
|
||||
rule: "",
|
||||
fallbackRelativeURL: "/test",
|
||||
tls: "dummy",
|
||||
},
|
||||
want: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "dummy",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "remove-tls",
|
||||
args: args{
|
||||
ingressConfig: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "foobar",
|
||||
},
|
||||
annotations: nil,
|
||||
rule: "",
|
||||
fallbackRelativeURL: "/test",
|
||||
tls: "-",
|
||||
},
|
||||
want: &fv1.IngressConfig{
|
||||
Annotations: map[string]string{
|
||||
"a": "b",
|
||||
},
|
||||
Host: "test.com",
|
||||
Path: "/foo/bar",
|
||||
TLS: "",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetIngressConfig(tt.args.annotations, tt.args.rule, tt.args.fallbackRelativeURL, tt.args.ingressConfig)
|
||||
got, err := GetIngressConfig(tt.args.annotations, tt.args.rule, tt.args.tls, tt.args.fallbackRelativeURL, tt.args.ingressConfig)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("getIngressConfig() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
@@ -440,3 +536,51 @@ func Test_getIngressHostRule(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getIngressTLS(t *testing.T) {
|
||||
type args struct {
|
||||
secret string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantRemove bool
|
||||
wantTls string
|
||||
}{
|
||||
{
|
||||
name: "tls-setup",
|
||||
args: args{
|
||||
secret: "foobar",
|
||||
},
|
||||
wantRemove: false,
|
||||
wantTls: "foobar",
|
||||
},
|
||||
{
|
||||
name: "remove-tls",
|
||||
args: args{
|
||||
secret: "-",
|
||||
},
|
||||
wantRemove: true,
|
||||
wantTls: "",
|
||||
},
|
||||
{
|
||||
name: "empty-tls",
|
||||
args: args{
|
||||
secret: "",
|
||||
},
|
||||
wantRemove: false,
|
||||
wantTls: "",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotRemove, gotTls := getIngressTLS(tt.args.secret)
|
||||
if gotRemove != tt.wantRemove {
|
||||
t.Errorf("getIngressTLS() gotRemove = %v, want %v", gotRemove, tt.wantRemove)
|
||||
}
|
||||
if gotTls != tt.wantTls {
|
||||
t.Errorf("getIngressTLS() gotTls = %v, want %v", gotTls, tt.wantTls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user