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

Reuse common labels #89

Merged
merged 3 commits into from
Mar 28, 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
22 changes: 22 additions & 0 deletions internal/controller/factory/labels_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package factory

type LabelsBuilder map[string]string

func NewLabelsBuilder() LabelsBuilder {
return make(map[string]string)
}

func (b LabelsBuilder) WithName() LabelsBuilder {
b["app.kubernetes.io/name"] = "etcd"
return b
}

func (b LabelsBuilder) WithInstance(name string) LabelsBuilder {
b["app.kubernetes.io/instance"] = name
return b
}

func (b LabelsBuilder) WithManagedBy() LabelsBuilder {
b["app.kubernetes.io/managed-by"] = "etcd-operator"
return b
}
40 changes: 40 additions & 0 deletions internal/controller/factory/labels_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package factory

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Labels builder", func() {
Context("When ensuring a labels builder", func() {
It("constructor returns empty initialized map", func() {
builder := NewLabelsBuilder()
Expect(builder).To(Equal(make(LabelsBuilder)))
})
It("WithName sets correct key and value", func() {
builder := NewLabelsBuilder()
builder.WithName()
Expect(builder["app.kubernetes.io/name"]).To(Equal("etcd"))
})
It("WithManagedBy sets correct key and value", func() {
builder := NewLabelsBuilder()
builder.WithManagedBy()
Expect(builder["app.kubernetes.io/managed-by"]).To(Equal("etcd-operator"))
})
It("WithInstance sets correct key and value", func() {
builder := NewLabelsBuilder()
builder.WithInstance("local")
Expect(builder["app.kubernetes.io/instance"]).To(Equal("local"))
})
It("Chaining methods builds correct map", func() {
builder := NewLabelsBuilder()
builder.WithName().WithManagedBy().WithInstance("local")
expected := map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": "local",
"app.kubernetes.io/managed-by": "etcd-operator",
}
Expect(builder).To(Equal(LabelsBuilder(expected)))
})
})
})
12 changes: 2 additions & 10 deletions internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ func CreateOrUpdateStatefulSet(
rscheme *runtime.Scheme,
) error {
podMetadata := metav1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
Labels: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
}

if cluster.Spec.PodSpec.PodMetadata != nil {
Expand Down Expand Up @@ -88,11 +84,7 @@ func CreateOrUpdateStatefulSet(
ServiceName: cluster.Name,
PodManagementPolicy: appsv1.ParallelPodManagement,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
MatchLabels: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: podMetadata,
Expand Down
30 changes: 7 additions & 23 deletions internal/controller/factory/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,16 @@ func CreateOrUpdateClusterService(
ObjectMeta: metav1.ObjectMeta{
Name: cluster.Name,
Namespace: cluster.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
Labels: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Name: "peer", TargetPort: intstr.FromInt32(2380), Port: 2380, Protocol: corev1.ProtocolTCP},
{Name: "client", TargetPort: intstr.FromInt32(2379), Port: 2379, Protocol: corev1.ProtocolTCP},
},
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "None",
Selector: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "None",
Selector: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
PublishNotReadyAddresses: true,
},
}
Expand All @@ -83,22 +75,14 @@ func CreateOrUpdateClientService(
ObjectMeta: metav1.ObjectMeta{
Name: GetClientServiceName(cluster),
Namespace: cluster.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
Labels: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Name: "client", TargetPort: intstr.FromInt32(2379), Port: 2379, Protocol: corev1.ProtocolTCP},
},
Type: corev1.ServiceTypeClusterIP,
Selector: map[string]string{
"app.kubernetes.io/name": "etcd",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/managed-by": "etcd-operator",
},
Type: corev1.ServiceTypeClusterIP,
Selector: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
},
}

Expand Down