Skip to content

Commit

Permalink
Add CheckBackendConfigExistence to check-gke-ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed May 10, 2023
1 parent d32cf45 commit 90522f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
clientset "k8s.io/client-go/kubernetes"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
"k8s.io/ingress-gce/pkg/annotations"
beconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
beconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned"
)

func CheckServiceExistence(namespace, name string, client clientset.Interface) (*corev1.Service, string, string) {
Expand Down Expand Up @@ -64,3 +66,14 @@ func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) {
}
return "", false
}

func CheckBackendConfigExistence(ns string, beConfigName string, svcName string, client beconfigclient.Interface) (*beconfigv1.BackendConfig, string, string) {
beConfig, err := client.CloudV1().BackendConfigs(ns).Get(context.TODO(), beConfigName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil, report.Failed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s does not exist", ns, beConfigName, ns, svcName)
}
return nil, report.Failed, fmt.Sprintf("Failed to get backendConfig %s/%s in service %s/%s", ns, beConfigName, ns, svcName)
}
return beConfig, report.Passed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s found", ns, beConfigName, ns, svcName)
}
47 changes: 47 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
"k8s.io/ingress-gce/pkg/annotations"
beconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
fakebeconfig "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned/fake"
)

func TestCheckServiceExistence(t *testing.T) {
Expand Down Expand Up @@ -139,3 +141,48 @@ func TestCheckBackendConfigAnnotation(t *testing.T) {
}
}
}

func TestCheckBackendConfigExistence(t *testing.T) {
client := fakebeconfig.NewSimpleClientset()
client.CloudV1().BackendConfigs("test").Create(context.TODO(), &beconfigv1.BackendConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "foo-beconfig",
},
}, metav1.CreateOptions{})

for _, tc := range []struct {
desc string
namespace string
name string
expect string
}{
{
desc: "empty input",
expect: report.Failed,
},
{
desc: "correct namespace and correct name",
namespace: "test",
name: "foo-beconfig",
expect: report.Passed,
},
{
desc: "correct namespace and wrong name",
namespace: "test",
name: "bar-beconfig",
expect: report.Failed,
},
{
desc: "wrong namespace and correct name",
namespace: "namespace2",
name: "foo-beconfig",
expect: report.Failed,
},
} {
_, res, _ := CheckBackendConfigExistence(tc.namespace, tc.name, "svc-1", client)
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 90522f6

Please sign in to comment.