Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy over annotations to ingress #7

Merged
merged 2 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,12 @@ func getClass(ingressType string) string {
func makeAnnotations(function *faasv1.FunctionIngress) map[string]string {
class := getClass(function.Spec.IngressType)
specJSON, _ := json.Marshal(function)
annotations := map[string]string{
"kubernetes.io/ingress.class": class,
"com.openfaas.spec": string(specJSON),
annotations := make(map[string]string)
for k, v := range function.ObjectMeta.Annotations {
annotations[k] = v
}
annotations["kubernetes.io/ingress.class"] = class
annotations["com.openfaas.spec"] = string(specJSON)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcusNoble should you take specJSON after setting the ingress.class?


switch class {

Expand Down
67 changes: 67 additions & 0 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package controller

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

faasv1 "github.com/openfaas-incubator/ingress-operator/pkg/apis/openfaas/v1alpha2"
)

func TestMakeAnnotations_AnnotationsCopied(t *testing.T) {
ingress := faasv1.FunctionIngress{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"test": "test",
"example": "example",
},
},
}

result := makeAnnotations(&ingress)

if _, ok := result["test"]; !ok {
t.Errorf("Failed to find expected annotation 'test'")
}
if _, ok := result["example"]; !ok {
t.Errorf("Failed to find expected annotation 'example'")
}
}

func TestMakeAnnotations_IngressClass(t *testing.T) {
wantIngressType := "nginx"
ingress := faasv1.FunctionIngress{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"kubernetes.io/ingress.class": "traefik",
},
},
Spec: faasv1.FunctionIngressSpec{
IngressType: wantIngressType,
},
}

result := makeAnnotations(&ingress)

if val, ok := result["kubernetes.io/ingress.class"]; !ok || val != wantIngressType {
t.Errorf("Failed to find expected ingress class annotation. Expected '%s' but got '%s'", wantIngressType, val)
}
}

func TestMakeAnnotations_IngressClassAdditionalAnnotations(t *testing.T) {
defaultRewriteAnnotation := "nginx.ingress.kubernetes.io/rewrite-target"
ingress := faasv1.FunctionIngress{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: faasv1.FunctionIngressSpec{
IngressType: "nginx",
},
}

result := makeAnnotations(&ingress)

if _, ok := result[defaultRewriteAnnotation]; !ok {
t.Errorf("Failed to find expected rewrite-target annotation")
}
}