Skip to content

Commit

Permalink
Add CheckHealthCheckConfig rule to check-gke-ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed May 13, 2023
1 parent c7c7171 commit 6721d51
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
33 changes: 23 additions & 10 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ 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)
}

func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) {
for _, bcKey := range []string{annotations.BackendConfigKey, annotations.BetaBackendConfigKey} {
val, ok := svc.Annotations[bcKey]
if ok {
return val, ok
}
}
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 {
Expand All @@ -77,3 +67,26 @@ 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)
}

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)
}
if beConfig.Spec.HealthCheck.TimeoutSec == nil || beConfig.Spec.HealthCheck.CheckIntervalSec == nil {
return report.Skipped, fmt.Sprintf("BackendConfig %s/%s in service %s/%s does not have timeoutSec or checkIntervalSec specified", beConfig.Namespace, beConfig.Name, beConfig.Namespace, svcName)
}
if *beConfig.Spec.HealthCheck.TimeoutSec > *beConfig.Spec.HealthCheck.CheckIntervalSec {
return report.Failed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s has healthcheck timeoutSec greater than checkIntervalSec", beConfig.Namespace, beConfig.Name, beConfig.Namespace, svcName)
}
return report.Passed, fmt.Sprintf("BackendConfig %s/%s in service %s/%s healthcheck configuration is valid", beConfig.Namespace, beConfig.Name, beConfig.Namespace, svcName)
}

func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) {
for _, bcKey := range []string{annotations.BackendConfigKey, annotations.BetaBackendConfigKey} {
val, ok := svc.Annotations[bcKey]
if ok {
return val, ok
}
}
return "", false
}
79 changes: 79 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report"
"k8s.io/ingress-gce/pkg/annotations"
Expand Down Expand Up @@ -186,3 +188,80 @@ func TestCheckBackendConfigExistence(t *testing.T) {
}
}
}

func TestCheckHealthCheckConfig(t *testing.T) {

thirtyVar := int64(30)
twentyVar := int64(20)
for _, tc := range []struct {
desc string
spec beconfigv1.BackendConfigSpec
expect string
}{
{
desc: "TimeoutSec equals to CheckIntervalSec",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
CheckIntervalSec: &thirtyVar,
TimeoutSec: &thirtyVar,
},
},
expect: report.Passed,
},
{
desc: "TimeoutSec smaller than CheckIntervalSec",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
CheckIntervalSec: &thirtyVar,
TimeoutSec: &twentyVar,
},
},
expect: report.Passed,
},
{
desc: "TimeoutSec larger than CheckIntervalSec",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
CheckIntervalSec: &twentyVar,
TimeoutSec: &thirtyVar,
},
},
expect: report.Failed,
},
{
desc: "No healthCheck specified",
spec: beconfigv1.BackendConfigSpec{},
expect: report.Skipped,
},
{
desc: "TimeoutSec not specidfied",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
CheckIntervalSec: &twentyVar,
},
},
expect: report.Skipped,
},
{
desc: "CheckIntervalSec not specidfied",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
TimeoutSec: &twentyVar,
},
},
expect: report.Skipped,
},
} {
beconfig := beconfigv1.BackendConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "foo-beconfig",
},
Spec: tc.spec,
}
res, _ := CheckHealthCheckTimeout(&beconfig, "")
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 6721d51

Please sign in to comment.