Skip to content

Commit

Permalink
Merge pull request #2105 from songrx1997/CheckBackendConfigAnnotation
Browse files Browse the repository at this point in the history
Add CheckBackendConfigAnnotation rule to check-gke-ingress
  • Loading branch information
k8s-ci-robot authored May 10, 2023
2 parents 94a9c31 + 2dea130 commit d32cf45
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package ingress

import (
"context"
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
"k8s.io/ingress-gce/pkg/annotations"
)

func CheckServiceExistence(namespace, name string, client clientset.Interface) (*corev1.Service, string, string) {
Expand All @@ -37,3 +39,28 @@ func CheckServiceExistence(namespace, name string, client clientset.Interface) (
}
return svc, report.Passed, fmt.Sprintf("Service %s/%s found", namespace, name)
}

func CheckBackendConfigAnnotation(svc *corev1.Service) (*annotations.BackendConfigs, string, string) {
val, ok := getBackendConfigAnnotation(svc)
if !ok {
return nil, report.Skipped, fmt.Sprintf("Service %s/%s does not have backendconfig annotation", svc.Namespace, svc.Name)
}
beConfigs := &annotations.BackendConfigs{}
if err := json.Unmarshal([]byte(val), beConfigs); err != nil {
return nil, report.Failed, fmt.Sprintf("BackendConfig annotation is invalid in service %s/%s", svc.Namespace, svc.Name)
}
if beConfigs.Default == "" && beConfigs.Ports == nil {
return nil, report.Failed, fmt.Sprintf("BackendConfig annotation is missing both `default` and `ports` field in service %s/%s", svc.Namespace, svc.Name)
}
return beConfigs, report.Passed, fmt.Sprintf("BackendConfig annotation is valid in service %s/%s", svc.Namespace, svc.Name)
}

func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) {
for _, bcKey := range []string{annotations.BackendConfigKey, annotations.BetaBackendConfigKey} {
val, ok := svc.Annotations[bcKey]
if ok {
return val, ok
}
}
return "", false
}
68 changes: 68 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
"k8s.io/ingress-gce/pkg/annotations"
)

func TestCheckServiceExistence(t *testing.T) {
Expand Down Expand Up @@ -71,3 +72,70 @@ func TestCheckServiceExistence(t *testing.T) {
}
}
}

func TestCheckBackendConfigAnnotation(t *testing.T) {
for _, tc := range []struct {
desc string
svc corev1.Service
expect string
}{
{
desc: "empty input",
expect: report.Skipped,
},
{
desc: "service without beconfig annotation",
svc: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "test",
},
},
expect: report.Skipped,
},
{
desc: "service with beconfig annotation",
svc: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "test",
Annotations: map[string]string{
annotations.BackendConfigKey: `{"ports": {"port1": "beconfig"}}`,
},
},
},
expect: report.Passed,
},
{
desc: "service with malformed default field in beconfig annotation",
svc: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "test",
Annotations: map[string]string{
annotations.BackendConfigKey: `{"default": {"port1": "beconfig"}}`,
},
},
},
expect: report.Failed,
},
{
desc: "service with malformed ports field in beconfig annotation",
svc: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "test",
Annotations: map[string]string{
annotations.BackendConfigKey: `{"port1": "beconfig1", "port2": "beconfig2"}`,
},
},
},
expect: report.Failed,
},
} {
_, res, _ := CheckBackendConfigAnnotation(&tc.svc)
if res != tc.expect {
t.Errorf("For test case %q, expect check result = %s, but got %s", tc.desc, tc.expect, res)
}
}
}

0 comments on commit d32cf45

Please sign in to comment.