diff --git a/cmd/check-gke-ingress/app/ingress/rule.go b/cmd/check-gke-ingress/app/ingress/rule.go index 7f09cdf1b0..348137fa72 100644 --- a/cmd/check-gke-ingress/app/ingress/rule.go +++ b/cmd/check-gke-ingress/app/ingress/rule.go @@ -28,7 +28,9 @@ import ( "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" + feconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/frontendconfig/v1beta1" beconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned" + feconfigclient "k8s.io/ingress-gce/pkg/frontendconfig/client/clientset/versioned" ) func CheckServiceExistence(namespace, name string, client clientset.Interface) (*corev1.Service, string, string) { @@ -81,6 +83,19 @@ func CheckHealthCheckTimeout(beConfig *beconfigv1.BackendConfig, svcName string) return report.Passed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s healthcheck configuration is valid", beConfig.Namespace, beConfig.Name, beConfig.Namespace, svcName) } +// CheckFrontendConfigExistence checks whether a FrontendConfig exists. +func CheckFrontendConfigExistence(namespace, name string, client feconfigclient.Interface) (*feconfigv1beta1.FrontendConfig, string, string) { + feConfig, err := client.NetworkingV1beta1().FrontendConfigs(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + return nil, report.Failed, fmt.Sprintf("FrontendConfig %s/%s does not exist", namespace, name) + } + return nil, report.Failed, fmt.Sprintf("Failed to get frontendConfig %s/%s", namespace, name) + } + return feConfig, report.Passed, fmt.Sprintf("FrontendConfig %s/%s found", namespace, name) +} + +// getBackendConfigAnnotation gets the BackendConfig annotation from a service. func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) { for _, bcKey := range []string{annotations.BackendConfigKey, annotations.BetaBackendConfigKey} { val, ok := svc.Annotations[bcKey] diff --git a/cmd/check-gke-ingress/app/ingress/rule_test.go b/cmd/check-gke-ingress/app/ingress/rule_test.go index 110b741776..a7e26c5c54 100644 --- a/cmd/check-gke-ingress/app/ingress/rule_test.go +++ b/cmd/check-gke-ingress/app/ingress/rule_test.go @@ -28,7 +28,9 @@ import ( "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" + feconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/frontendconfig/v1beta1" fakebeconfig "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned/fake" + fakefeconfig "k8s.io/ingress-gce/pkg/frontendconfig/client/clientset/versioned/fake" ) func TestCheckServiceExistence(t *testing.T) { @@ -265,3 +267,48 @@ func TestCheckHealthCheckConfig(t *testing.T) { } } } + +func TestCheckFrontendConfigExistence(t *testing.T) { + client := fakefeconfig.NewSimpleClientset() + client.NetworkingV1beta1().FrontendConfigs("test").Create(context.TODO(), &feconfigv1beta1.FrontendConfig{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test", + Name: "foo-feconfig", + }, + }, 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-feconfig", + expect: report.Passed, + }, + { + desc: "correct namespace and wrong name", + namespace: "test", + name: "bar-feconfig", + expect: report.Failed, + }, + { + desc: "wrong namespace and correct name", + namespace: "namespace2", + name: "foo-feconfig", + expect: report.Failed, + }, + } { + _, res, _ := CheckFrontendConfigExistence(tc.namespace, tc.name, client) + if res != tc.expect { + t.Errorf("For test case %q, expect check result = %s, but got %s", tc.desc, tc.expect, res) + } + } +}