Skip to content

Commit

Permalink
Fix for missing rules for VS with HostAliases and hostGroup (#3317)
Browse files Browse the repository at this point in the history
  • Loading branch information
arzzon committed Mar 1, 2024
1 parent 5fe5279 commit 1a7c645
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions pkg/controller/nativeResourceWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func (ctlr *Controller) prepareABRouteLTMRules(
uri := route.Spec.Host + route.Spec.Path
path := route.Spec.Path
appRoot := "/"
ruleName := formatVirtualServerRuleName(route.Spec.Host, route.Namespace, path, poolName)
ruleName := formatVirtualServerRuleName(route.Spec.Host, route.Namespace, path, poolName, false)
rl, err := createRule(uri, poolName, ruleName, allowSourceRange, wafPolicy, true)
if nil != err {
log.Errorf("Error configuring rule: %v", err)
Expand Down Expand Up @@ -681,7 +681,7 @@ func (ctlr *Controller) prepareRouteLTMRules(
// Handle app-root annotation
appRootPath, appRootOk := route.Annotations[resource.F5VsAppRootAnnotation]
if appRootOk {
ruleName := formatVirtualServerRuleName(route.Spec.Host, "", "redirectto", appRootPath)
ruleName := formatVirtualServerRuleName(route.Spec.Host, "", "redirectto", appRootPath, false)
rl, err := createRedirectRule(route.Spec.Host+appRoot, appRootPath, ruleName, allowSourceRange)
if nil != err {
log.Errorf("Error configuring redirect rule: %v", err)
Expand All @@ -694,7 +694,7 @@ func (ctlr *Controller) prepareRouteLTMRules(
}
}

ruleName := formatVirtualServerRuleName(route.Spec.Host, route.Namespace, path, poolName)
ruleName := formatVirtualServerRuleName(route.Spec.Host, route.Namespace, path, poolName, false)
rl, err := createRule(uri, poolName, ruleName, allowSourceRange, wafPolicy, false)
if nil != err {
log.Errorf("Error configuring rule: %v", err)
Expand Down Expand Up @@ -730,7 +730,7 @@ func (ctlr *Controller) prepareRouteLTMRules(

if rlMap[route.Spec.Host] == nil && len(redirects) == 2 {
rl := &Rule{
Name: formatVirtualServerRuleName(route.Spec.Host, route.Namespace, "", redirects[1].Actions[0].Pool),
Name: formatVirtualServerRuleName(route.Spec.Host, route.Namespace, "", redirects[1].Actions[0].Pool, false),
FullURI: route.Spec.Host,
Actions: redirects[1].Actions,
Conditions: []*condition{
Expand Down
13 changes: 10 additions & 3 deletions pkg/controller/resourceConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"sort"
"strings"

"github.com/F5Networks/k8s-bigip-ctlr/v2/pkg/clustermanager"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -119,11 +120,11 @@ var _ = Describe("Resource Config Tests", func() {
Expect(name).To(Equal("svc1_default_foo_com_path_http_80"), "Invalid Monitor Name")
})
It("Rule Name", func() {
name := formatVirtualServerRuleName("test.com", "", "", "sample_pool")
name := formatVirtualServerRuleName("test.com", "", "", "sample_pool", false)
Expect(name).To(Equal("vs_test_com_sample_pool"))
name = formatVirtualServerRuleName("test.com", "exams.com", "", "sample_pool")
name = formatVirtualServerRuleName("test.com", "exams.com", "", "sample_pool", false)
Expect(name).To(Equal("vs_exams_com_sample_pool"))
name = formatVirtualServerRuleName("test.com", "", "/foo", "sample_pool")
name = formatVirtualServerRuleName("test.com", "", "/foo", "sample_pool", false)
Expect(name).To(Equal("vs_test_com_foo_sample_pool"))

})
Expand Down Expand Up @@ -335,6 +336,7 @@ var _ = Describe("Resource Config Tests", func() {
cisapiv1.VirtualServerSpec{
Host: "test.com",
HostAliases: []string{"test1.com", "test2.com"},
HostGroup: "hg1",
Pools: []cisapiv1.VSPool{
{
Path: "/foo",
Expand Down Expand Up @@ -381,6 +383,11 @@ var _ = Describe("Resource Config Tests", func() {
for _, rule := range rsCfg.Policies[0].Rules {
_, ok := urisInRules[rule.FullURI]
Expect(ok).To(BeTrue(), "Incorrect rules defined for VirtualServer with hostAliases")
// Verify correct ruleName is generated in case HostGroup is defined along with hostAliases
uriArray := strings.Split(rule.FullURI, "/")
Expect(len(uriArray)).To(BeNumerically(">=", 1), "Incorrect rules defined for VirtualServer with hostAliases")
formatedUri := strings.ReplaceAll(uriArray[0], ".", "_")
Expect(rule.Name).To(ContainSubstring("vs_hg1_"+formatedUri), "Incorrect rules defined for VirtualServer with hostAliases")
}
})

Expand Down
19 changes: 13 additions & 6 deletions pkg/controller/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ func (ctlr *Controller) prepareVirtualServerRules(

// Consider the primary host as well as the host aliases
hosts := getUniqueHosts(vs.Spec.Host, vs.Spec.HostAliases)

hostAliasesUsed := false
if len(vs.Spec.HostAliases) > 0 {
hostAliasesUsed = true
}
if vs.Spec.RewriteAppRoot != "" {
for _, host := range hosts {
ruleName := formatVirtualServerRuleName(host, vs.Spec.HostGroup, "redirectto", vs.Spec.RewriteAppRoot)
ruleName := formatVirtualServerRuleName(host, vs.Spec.HostGroup, "redirectto", vs.Spec.RewriteAppRoot, hostAliasesUsed)
rl, err := createRedirectRule(host+appRoot, vs.Spec.RewriteAppRoot, ruleName, rsCfg.Virtual.AllowSourceRange)
if nil != err {
log.Errorf("Error configuring redirect rule: %v", err)
Expand Down Expand Up @@ -95,7 +98,7 @@ func (ctlr *Controller) prepareVirtualServerRules(
vs.Spec.Host,
backend,
)
ruleName := formatVirtualServerRuleName(host, vs.Spec.HostGroup, path, poolName)
ruleName := formatVirtualServerRuleName(host, vs.Spec.HostGroup, path, poolName, hostAliasesUsed)
var err error
rl, err := createRule(uri, poolName, ruleName, rsCfg.Virtual.AllowSourceRange, wafPolicy, skipPool)
if nil != err {
Expand Down Expand Up @@ -157,7 +160,7 @@ func (ctlr *Controller) prepareVirtualServerRules(

if rlMap[vs.Spec.Host] == nil && len(hosts) != 0 && len(redirects) == 2*len(hosts) {
rl := &Rule{
Name: formatVirtualServerRuleName(vs.Spec.Host, vs.Spec.HostGroup, "", redirects[1].Actions[0].Pool),
Name: formatVirtualServerRuleName(vs.Spec.Host, vs.Spec.HostGroup, "", redirects[1].Actions[0].Pool, hostAliasesUsed),
FullURI: vs.Spec.Host,
Actions: redirects[1].Actions,
Conditions: []*condition{
Expand Down Expand Up @@ -198,15 +201,19 @@ func (ctlr *Controller) prepareVirtualServerRules(
}

// format the rule name for VirtualServer
func formatVirtualServerRuleName(hostname, hostGroup, path, pool string) string {
func formatVirtualServerRuleName(hostname, hostGroup, path, pool string, hostAliases bool) string {
var rule string
host := hostname
//if wildcard vs
if strings.HasPrefix(host, "*") {
host = strings.Replace(host, "*", "wildcard", 1)
}
if hostGroup != "" {
host = hostGroup
if !hostAliases {
host = hostGroup
} else {
host = hostGroup + "_" + host
}
}
if path == "" {
rule = fmt.Sprintf("vs_%s_%s", host, pool)
Expand Down

0 comments on commit 1a7c645

Please sign in to comment.