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 9, 2023
1 parent 6cc961f commit d335475
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 @@ -25,6 +25,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
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 All @@ -37,3 +39,14 @@ func CheckServiceExistence(namespace, name string, client clientset.Interface) (
}
return svc, report.Passed, fmt.Sprintf("Service %s/%s found", namespace, name)
}

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 @@ -24,6 +24,8 @@ 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"
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 @@ -71,3 +73,48 @@ func TestCheckServiceExistence(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 d335475

Please sign in to comment.