Skip to content

Commit

Permalink
Merge pull request #2120 from songrx1997/CheckFrontendConfigExistence
Browse files Browse the repository at this point in the history
Add CheckFrontendConfigExistence to check-gke-ingress
  • Loading branch information
k8s-ci-robot authored May 18, 2023
2 parents 14d6715 + 2d0d1fe commit e9a6588
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]
Expand Down
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 @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit e9a6588

Please sign in to comment.