Skip to content

Commit

Permalink
Merge pull request #2119 from songrx1997/CheckIngressRule
Browse files Browse the repository at this point in the history
Add CheckIngressRule to check-gke-ingress
  • Loading branch information
k8s-ci-robot authored May 19, 2023
2 parents 0e6c39c + 1c4bfa6 commit 925849b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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{})
Expand Down
38 changes: 38 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 925849b

Please sign in to comment.