Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CheckHealthCheckConfig rule to check-gke-ingress #2112

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
ruixiansong marked this conversation as resolved.
Show resolved Hide resolved
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
}{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test what happens when hc config is nil, and when the fields are nil as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks!

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 specified",
spec: beconfigv1.BackendConfigSpec{
HealthCheck: &beconfigv1.HealthCheckConfig{
CheckIntervalSec: &twentyVar,
},
},
expect: report.Skipped,
},
{
desc: "CheckIntervalSec not specified",
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)
}
}
}