diff --git a/cmd/check-gke-ingress/app/ingress/rule.go b/cmd/check-gke-ingress/app/ingress/rule.go index 348137fa72..8377e56ffc 100644 --- a/cmd/check-gke-ingress/app/ingress/rule.go +++ b/cmd/check-gke-ingress/app/ingress/rule.go @@ -22,6 +22,7 @@ import ( "fmt" corev1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clientset "k8s.io/client-go/kubernetes" @@ -33,6 +34,7 @@ import ( feconfigclient "k8s.io/ingress-gce/pkg/frontendconfig/client/clientset/versioned" ) +// CheckServiceExistence checks whether a service exists. func CheckServiceExistence(namespace, name string, client clientset.Interface) (*corev1.Service, string, string) { svc, err := client.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { @@ -44,6 +46,11 @@ func CheckServiceExistence(namespace, name string, client clientset.Interface) ( return svc, report.Passed, fmt.Sprintf("Service %s/%s found", namespace, name) } +// CheckBackendConfigAnnotation checks the BackendConfig annotation of a +// service for: +// +// whether the annotation is a valid BackendConfig json object. +// whether the annotation has `default` or `ports` field. func CheckBackendConfigAnnotation(svc *corev1.Service) (*annotations.BackendConfigs, string, string) { val, ok := getBackendConfigAnnotation(svc) if !ok { @@ -59,6 +66,7 @@ func CheckBackendConfigAnnotation(svc *corev1.Service) (*annotations.BackendConf return beConfigs, report.Passed, fmt.Sprintf("BackendConfig annotation is valid in service %s/%s", svc.Namespace, svc.Name) } +// CheckBackendConfigExistence checks whether a BackendConfig exists. 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 { @@ -70,6 +78,8 @@ func CheckBackendConfigExistence(ns string, beConfigName string, svcName string, return beConfig, report.Passed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s found", ns, beConfigName, ns, svcName) } +// CheckHealthCheckTimeout checks whether timeout time is smaller than check +// interval in backendconfig health check configuration. func CheckHealthCheckTimeout(beConfig *beconfigv1.BackendConfig, svcName string) (string, string) { if beConfig.Spec.HealthCheck == nil { return report.Skipped, fmt.Sprintf("BackendConfig %s/%s in service %s/%s does not have healthcheck specified", beConfig.Namespace, beConfig.Name, beConfig.Namespace, svcName) @@ -83,6 +93,14 @@ 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) } +// CheckIngressRule checks whether an ingress rule has the http field. +func CheckIngressRule(ingressRule *networkingv1.IngressRule) (*networkingv1.HTTPIngressRuleValue, string, string) { + if ingressRule.HTTP == nil { + return nil, report.Failed, "IngressRule has no field `http`" + } + return ingressRule.HTTP, report.Passed, "IngressRule has field `http`" +} + // 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{}) diff --git a/cmd/check-gke-ingress/app/ingress/rule_test.go b/cmd/check-gke-ingress/app/ingress/rule_test.go index a7e26c5c54..54c9620e21 100644 --- a/cmd/check-gke-ingress/app/ingress/rule_test.go +++ b/cmd/check-gke-ingress/app/ingress/rule_test.go @@ -22,6 +22,7 @@ import ( "github.com/google/go-cmp/cmp" corev1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -312,3 +313,40 @@ func TestCheckFrontendConfigExistence(t *testing.T) { } } } + +func TestCheckIngressRule(t *testing.T) { + + for _, tc := range []struct { + desc string + ingressRule networkingv1.IngressRule + expect string + }{ + { + desc: "Ingress rule with http field", + ingressRule: networkingv1.IngressRule{ + IngressRuleValue: networkingv1.IngressRuleValue{ + HTTP: &networkingv1.HTTPIngressRuleValue{ + Paths: []networkingv1.HTTPIngressPath{ + { + Path: "/*", + }, + }, + }, + }, + }, + expect: report.Passed, + }, + { + desc: "Ingress rule without http field", + ingressRule: networkingv1.IngressRule{ + IngressRuleValue: networkingv1.IngressRuleValue{}, + }, + expect: report.Failed, + }, + } { + _, res, _ := CheckIngressRule(&tc.ingressRule) + if diff := cmp.Diff(tc.expect, res); diff != "" { + t.Errorf("For test case %s, (-want +got):\n%s", tc.desc, diff) + } + } +}