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 CheckRuleHostOverwrite to check-gke-ingress #2130

Merged
merged 1 commit into from
May 19, 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
12 changes: 12 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func CheckFrontendConfigExistence(namespace, name string, client feconfigclient.
return feConfig, report.Passed, fmt.Sprintf("FrontendConfig %s/%s found", namespace, name)
}

// CheckRuleHostOverwrite checks whether hosts of ingress rules are unique.
func CheckRuleHostOverwrite(rules []networkingv1.IngressRule) (string, string) {
hostSet := make(map[string]struct{})
for _, rule := range rules {
if _, ok := hostSet[rule.Host]; ok {
return report.Failed, fmt.Sprintf("Ingress rules have identical host: %s", rule.Host)
}
hostSet[rule.Host] = struct{}{}
}
return report.Passed, fmt.Sprintf("Ingress rule hosts are unique")
}

// getBackendConfigAnnotation gets the BackendConfig annotation from a service.
func getBackendConfigAnnotation(svc *corev1.Service) (string, bool) {
for _, bcKey := range []string{annotations.BackendConfigKey, annotations.BetaBackendConfigKey} {
Expand Down
43 changes: 43 additions & 0 deletions cmd/check-gke-ingress/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,46 @@ func TestCheckIngressRule(t *testing.T) {
}
}
}

func TestCheckRuleHostOverwrite(t *testing.T) {
for _, tc := range []struct {
desc string
rules []networkingv1.IngressRule
expect string
}{
{
desc: "Empty rules",
rules: []networkingv1.IngressRule{},
expect: report.Passed,
},
{
desc: "Rules with identical host",
rules: []networkingv1.IngressRule{
{
Host: "foo.bar.com",
},
{
Host: "foo.bar.com",
},
},
expect: report.Failed,
},
{
desc: "Rules with unique hosts",
rules: []networkingv1.IngressRule{
{
Host: "foo.bar.com",
},
{
Host: "abc.xyz.com",
},
},
expect: report.Passed,
},
} {
res, _ := CheckRuleHostOverwrite(tc.rules)
if res != tc.expect {
t.Errorf("For test case %q, expect check result = %s, but got %s", tc.desc, tc.expect, res)
}
}
}