Skip to content

Commit

Permalink
Merge pull request #2130 from songrx1997/CheckRuleHostOverwrite
Browse files Browse the repository at this point in the history
Add CheckRuleHostOverwrite to check-gke-ingress
  • Loading branch information
k8s-ci-robot authored May 19, 2023
2 parents 8f60f52 + 07c1407 commit a8eb42d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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)
}
}
}

0 comments on commit a8eb42d

Please sign in to comment.