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

Add CheckBackendConfigExistence to check-gke-ingress #2106

Merged
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
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)
}
}
}