Skip to content
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
16 changes: 6 additions & 10 deletions controllers/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package instance

import (
"context"
"fmt"

configv1alpha1 "github.com/six-group/haproxy-operator/apis/config/v1alpha1"
proxyv1alpha1 "github.com/six-group/haproxy-operator/apis/proxy/v1alpha1"
Expand All @@ -11,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -133,7 +135,7 @@ func (r *Reconciler) handleError(ctx context.Context, instance *proxyv1alpha1.In

func (r *Reconciler) patchPods(ctx context.Context, instance *proxyv1alpha1.Instance, checksum string) error {
ls := client.MatchingLabels{
corev1.LabelMetadataName: utils.GetServiceAndStatefulsetName(instance),
"app.kubernetes.io/name": utils.GetServiceAndStatefulsetName(instance),
}

l := &corev1.PodList{}
Expand All @@ -142,16 +144,10 @@ func (r *Reconciler) patchPods(ctx context.Context, instance *proxyv1alpha1.Inst
return err
}

for i := range l.Items {
pod := &l.Items[i]
original := pod.DeepCopy()

if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pod.Annotations["haproxy.operator/checksum"] = checksum
patch := fmt.Appendf(nil, `{"metadata":{"annotations":{"haproxy.operator/checksum": "%s"}}}`, checksum)

err = r.Patch(ctx, pod, client.MergeFrom(original))
for i := range l.Items {
err = r.Patch(ctx, &l.Items[i], client.RawPatch(types.MergePatchType, patch))
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ var _ = Describe("Reconcile", Label("controller"), func() {
Name: "haproxy-0",
Namespace: "foo",
Labels: map[string]string{
corev1.LabelMetadataName: utils.GetServiceAndStatefulsetName(proxy),
"app.kubernetes.io/name": utils.GetServiceAndStatefulsetName(proxy),
},
},
}
Expand All @@ -483,7 +483,7 @@ var _ = Describe("Reconcile", Label("controller"), func() {
Name: "haproxy-1",
Namespace: "foo",
Labels: map[string]string{
corev1.LabelMetadataName: utils.GetServiceAndStatefulsetName(proxy),
"app.kubernetes.io/name": utils.GetServiceAndStatefulsetName(proxy),
},
},
}
Expand All @@ -509,15 +509,15 @@ var _ = Describe("Reconcile", Label("controller"), func() {
Ω(cli.Get(ctx, client.ObjectKey{Namespace: proxy.Namespace, Name: utils.GetServiceAndStatefulsetName(proxy)}, service)).ShouldNot(HaveOccurred())
Ω(service.Spec.Type).Should(Equal(corev1.ServiceTypeLoadBalancer))
Ω(service.Annotations["service.beta.kubernetes.io/aws-load-balancer-scheme"]).Should(Equal("internet-facing"))
Ω(service.Spec.Selector[corev1.LabelMetadataName]).Should(Equal(proxy.Name + "-haproxy"))
Ω(service.Spec.Selector["app.kubernetes.io/name"]).Should(Equal(proxy.Name + "-haproxy"))

secret := &corev1.Secret{}
Ω(cli.Get(ctx, client.ObjectKey{Namespace: proxy.Namespace, Name: "bar-foo-haproxy-config"}, secret)).ShouldNot(HaveOccurred())
Ω(string(secret.Data["haproxy.cfg"])).Should(Equal(haproxyConfig))

statefulSet := &appsv1.StatefulSet{}
Ω(cli.Get(ctx, client.ObjectKey{Namespace: proxy.Namespace, Name: "bar-foo-haproxy"}, statefulSet)).ShouldNot(HaveOccurred())
Ω(statefulSet.Spec.Template.ObjectMeta.Labels[corev1.LabelMetadataName]).Should(Equal(proxy.Name + "-haproxy"))
Ω(statefulSet.Spec.Template.ObjectMeta.Labels["app.kubernetes.io/name"]).Should(Equal(proxy.Name + "-haproxy"))
Ω(statefulSet.Spec.Template.ObjectMeta.Labels["label-test"]).Should(Equal("ok"))
Ω(statefulSet.Spec.Template.Spec.InitContainers).Should(HaveLen(1))
Ω(statefulSet.Spec.Template.Spec.InitContainers[0].Name).Should(Equal(proxy.Spec.InitContainers[0].Name))
Expand Down
3 changes: 1 addition & 2 deletions controllers/instance/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
configv1alpha1 "github.com/six-group/haproxy-operator/apis/config/v1alpha1"
proxyv1alpha1 "github.com/six-group/haproxy-operator/apis/proxy/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/uuid"
Expand Down Expand Up @@ -82,7 +81,7 @@ var _ = Describe("Reconcile", Label("controller"), func() {

statefulSet := &appsv1.StatefulSet{}
Ω(cli.Get(ctx, client.ObjectKey{Namespace: proxy.Namespace, Name: "bar-foo-haproxy"}, statefulSet)).ShouldNot(HaveOccurred())
Ω(statefulSet.Spec.Template.ObjectMeta.Labels[corev1.LabelMetadataName]).Should(Equal(proxy.Name + "-haproxy"))
Ω(statefulSet.Spec.Template.ObjectMeta.Labels["app.kubernetes.io/name"]).Should(Equal(proxy.Name + "-haproxy"))
Ω(statefulSet.Spec.Template.ObjectMeta.Labels["label-test"]).Should(Equal("ok"))
Ω(statefulSet.Spec.Template.Spec.InitContainers).Should(HaveLen(1))
Ω(statefulSet.Spec.Template.Spec.InitContainers[0].Args[0]).Should(ContainSubstring("10.158.182.27"))
Expand Down
10 changes: 10 additions & 0 deletions helm/haproxy-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ rules:
- update
- watch
- delete
- apiGroups:
- ''
resources:
- pods
verbs:
- get
- list
- patch
- update
- watch
- apiGroups:
- route.openshift.io
resources:
Expand Down
3 changes: 1 addition & 2 deletions pkg/utils/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package utils

import (
"github.com/six-group/haproxy-operator/apis/proxy/v1alpha1"
corev1 "k8s.io/api/core/v1"
)

func GetAppSelectorLabels(instance *v1alpha1.Instance) map[string]string {
return map[string]string{
corev1.LabelMetadataName: GetServiceAndStatefulsetName(instance),
"app.kubernetes.io/name": GetServiceAndStatefulsetName(instance),
}
}

Expand Down