Skip to content

Commit

Permalink
sort file contents to avoid unnecessary reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
m-terra committed Jan 16, 2024
1 parent c467bf4 commit feab745
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions controllers/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"

haproxy "github.com/haproxytech/client-native/v5/configuration/options"
Expand Down Expand Up @@ -357,6 +358,7 @@ func (r *Reconciler) generateBackendMappingFiles(ctx context.Context, instance *
mappings = append(mappings, fmt.Sprintf("^%s$ %s", strings.TrimPrefix(strings.TrimSuffix(backend.Spec.HostRegex, "$"), "^"), backend.Name))
}

sort.Strings(mappings)
files[rules.Backend.RegexMapping.FilePath()] = strings.Join(mappings, "\n")
}
}
Expand Down Expand Up @@ -409,6 +411,7 @@ func (r *Reconciler) generateACLValuesFiles(_ context.Context, listens *configv1
for _, frontend := range frontends.Items {
for _, acl := range frontend.Spec.ACL {
if len(acl.Values) > defaults.MaxLineArgs-3 {
sort.Strings(acl.Values)
files[acl.FilePath()] = strings.Join(acl.Values, "\n")
}
}
Expand All @@ -417,6 +420,7 @@ func (r *Reconciler) generateACLValuesFiles(_ context.Context, listens *configv1
for _, backend := range backends.Items {
for _, acl := range backend.Spec.ACL {
if len(acl.Values) > defaults.MaxLineArgs-3 {
sort.Strings(acl.Values)
files[acl.FilePath()] = strings.Join(acl.Values, "\n")
}
}
Expand All @@ -425,6 +429,7 @@ func (r *Reconciler) generateACLValuesFiles(_ context.Context, listens *configv1
for _, listen := range listens.Items {
for _, acl := range listen.Spec.ACL {
if len(acl.Values) > defaults.MaxLineArgs-3 {
sort.Strings(acl.Values)
files[acl.FilePath()] = strings.Join(acl.Values, "\n")
}
}
Expand Down Expand Up @@ -566,6 +571,7 @@ func (r *Reconciler) generateCustomCertificatesFile(ctx context.Context, instanc
mappings = append(mappings, strings.Join([]string{element.Certificate.FilePath(), alpn, element.SNIFilter, "\n"}, " "))
}

sort.Strings(mappings)
files[bind.SSLCertificateList.FilePath()] = strings.Join(mappings, "")
}
}
Expand Down Expand Up @@ -596,6 +602,7 @@ func (r *Reconciler) generateCustomCertificatesFile(ctx context.Context, instanc
mappings = append(mappings, strings.Join([]string{element.Certificate.FilePath(), alpn, element.SNIFilter, "\n"}, " "))
}

sort.Strings(mappings)
files[bind.SSLCertificateList.FilePath()] = strings.Join(mappings, "")
}
}
Expand Down
19 changes: 11 additions & 8 deletions controllers/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,18 @@ var _ = Describe("Reconcile", Label("controller"), func() {
"backend foo-back2\n server server localhost:80 check ssl alpn h2,http/1.0 ca-file /usr/local/etc/haproxy/test-ca.crt inter 5000 verify required verifyhost routername.namespace.svc weight 256\n\n" +
"backend foo-listen\n server routeName routeName.routeNamespace.svc.cluster.local:8443 check ssl alpn http/1.1,h2 init-addr none inter 500 resolvers dns-routeNamespace verify required verifyhost routeName.routeName.svc weight 256\n"))

Ω(secret.Data["cert_list.map"]).Should(Equal([]byte("/usr/local/etc/haproxy/route.name4.crt [alpn h2,http/1.0] route.host4 \n" +
Ω(string(secret.Data["cert_list.map"])).Should(Equal(
"/usr/local/etc/haproxy/route.name.crt route.host \n" +
"/usr/local/etc/haproxy/route.name2.crt [alpn h2,http/1.0] route.host2 \n" +
"/usr/local/etc/haproxy/route.name.tcp.crt [alpn h2,http/1.0] route.host.tcp \n")))

Ω(secret.Data["route.name.crt"]).Should(Equal([]byte("Key\n\nCertificate\n\nCAcertificate")))
Ω(secret.Data["route.name2.crt"]).Should(Equal([]byte("Key2\n\nCertificate2\n\nCAcertificate2")))
Ω(secret.Data["route.name.tcp.crt"]).Should(Equal([]byte("Key2\n\nCertificate2\n\nCAcertificate2")))
Ω(secret.Data["route.name4.crt"]).Should(Equal([]byte("Key\n\nCertificate\n\nCAcertificate")))
"/usr/local/etc/haproxy/route.name.tcp.crt [alpn h2,http/1.0] route.host.tcp \n" +
"/usr/local/etc/haproxy/route.name2.crt [alpn h2,http/1.0] route.host2 \n" +
"/usr/local/etc/haproxy/route.name4.crt [alpn h2,http/1.0] route.host4 \n",
),
)

Ω(string(secret.Data["route.name.crt"])).Should(Equal("Key\n\nCertificate\n\nCAcertificate"))
Ω(string(secret.Data["route.name2.crt"])).Should(Equal("Key2\n\nCertificate2\n\nCAcertificate2"))
Ω(string(secret.Data["route.name.tcp.crt"])).Should(Equal("Key2\n\nCertificate2\n\nCAcertificate2"))
Ω(string(secret.Data["route.name4.crt"])).Should(Equal("Key\n\nCertificate\n\nCAcertificate"))
})
})
})

0 comments on commit feab745

Please sign in to comment.