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

fix sort order for backend switching mapping #29

Merged
merged 1 commit into from
Jan 23, 2024
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
2 changes: 1 addition & 1 deletion controllers/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,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)
sort.Sort(sort.Reverse(sort.StringSlice(mappings)))
files[rules.Backend.RegexMapping.FilePath()] = strings.Join(mappings, "\n")
}
}
Expand Down
77 changes: 69 additions & 8 deletions controllers/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
var _ = Describe("Reconcile", Label("controller"), func() {
Context("Reconcile", func() {
var (
scheme *runtime.Scheme
ctx context.Context
proxy *proxyv1alpha1.Instance
frontend, frontendCustomCerts, frontendCustomCerts2, frontendCustomCertsEmpty *configv1alpha1.Frontend
backend, backend2 *configv1alpha1.Backend
listen *configv1alpha1.Listen
resolver *configv1alpha1.Resolver
initObjs []client.Object
scheme *runtime.Scheme
ctx context.Context
proxy *proxyv1alpha1.Instance
frontend, frontendCustomCerts, frontendCustomCerts2, frontendCustomCertsEmpty, frontendWithBackendSwitching *configv1alpha1.Frontend
backend, backend2 *configv1alpha1.Backend
listen *configv1alpha1.Listen
resolver *configv1alpha1.Resolver
initObjs []client.Object
)

customCert := "Certificate"
Expand Down Expand Up @@ -174,13 +174,54 @@ var _ = Describe("Reconcile", Label("controller"), func() {
},
}

be := configv1alpha1.BackendReference{
RegexMapping: &configv1alpha1.RegexBackendMapping{
Name: "be-https-passthrough",
Parameter: "req.ssl_sni,lower",
LabelSelector: metav1.LabelSelector{
MatchLabels: labels,
},
},
}
frontendWithBackendSwitching = &configv1alpha1.Frontend{
ObjectMeta: metav1.ObjectMeta{
Name: "fe-https-with-backend-switching",
Namespace: "foo",
Labels: labels,
},
Spec: configv1alpha1.FrontendSpec{
Binds: []configv1alpha1.Bind{
{
Address: "unix@/var/lib/haproxy/run/local.sock",
Port: 9443,
Name: "https",
AcceptProxy: ptr.To(true),
Hidden: ptr.To(true),
SSL: &configv1alpha1.SSL{
Enabled: true,
},
},
},
BackendSwitching: []configv1alpha1.BackendSwitchingRule{
{
Rule: configv1alpha1.Rule{
ConditionType: "if",
Condition: be.RegexMapping.FoundCondition(),
},
Backend: be,
},
},
},
}

backend = &configv1alpha1.Backend{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-back",
Namespace: "foo",
Labels: labels,
},
Spec: configv1alpha1.BackendSpec{
HostRegex: "aaaa\\.com/\\.?(:[0-9]+)?(/.*)?",
HostCertificate: &configv1alpha1.CertificateListElement{
Certificate: configv1alpha1.SSLCertificate{
Name: "route.name",
Expand Down Expand Up @@ -224,6 +265,7 @@ var _ = Describe("Reconcile", Label("controller"), func() {
Labels: labels,
},
Spec: configv1alpha1.BackendSpec{
HostRegex: "zzzz\\.com/\\.?(:[0-9]+)?(/.*)?",
HostCertificate: &configv1alpha1.CertificateListElement{
Certificate: configv1alpha1.SSLCertificate{
Name: "route.name2",
Expand Down Expand Up @@ -459,5 +501,24 @@ var _ = Describe("Reconcile", Label("controller"), func() {
Ω(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"))
})
It("should create backend mapping", func() {
cli := fake.NewClientBuilder().WithScheme(scheme).WithObjects(append(initObjs, frontendWithBackendSwitching)...).WithStatusSubresource(append(initObjs, frontendWithBackendSwitching)...).Build()
r := instance.Reconciler{
Client: cli,
Scheme: scheme,
}
result, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: proxy.Name, Namespace: proxy.Namespace}})
Ω(err).ShouldNot(HaveOccurred())
Ω(result).ShouldNot(BeNil())

Ω(cli.Get(ctx, client.ObjectKeyFromObject(proxy), proxy)).ShouldNot(HaveOccurred())
fmt.Println(proxy.Status)
Ω(proxy.Status.Phase).Should(Equal(proxyv1alpha1.InstancePhaseRunning))
Ω(proxy.Status.Error).Should(BeEmpty())

secret := &corev1.Secret{}
Ω(cli.Get(ctx, client.ObjectKey{Namespace: proxy.Namespace, Name: "bar-foo-haproxy-config"}, secret)).ShouldNot(HaveOccurred())
Ω(string(secret.Data["be-https-passthrough.map"])).Should(Equal("^zzzz\\.com/\\.?(:[0-9]+)?(/.*)?$ foo-back2\n^aaaa\\.com/\\.?(:[0-9]+)?(/.*)?$ foo-back"))
})
})
})