Skip to content

Commit ac9e368

Browse files
authored
Merge pull request #318 from rabbitmq/small-refactoring
Small refactor
2 parents 0677077 + 686e9ad commit ac9e368

File tree

10 files changed

+35
-47
lines changed

10 files changed

+35
-47
lines changed

api/v1beta1/rabbitmqcluster_types.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
corev1 "k8s.io/api/core/v1"
1919
k8sresource "k8s.io/apimachinery/pkg/api/resource"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
runtime "k8s.io/apimachinery/pkg/runtime"
21+
"k8s.io/apimachinery/pkg/runtime"
2222
)
2323

2424
const (
@@ -325,13 +325,13 @@ func (cluster *RabbitmqCluster) AdditionalPluginEnabled(plugin Plugin) bool {
325325
return false
326326
}
327327

328-
func (rmqStatus *RabbitmqClusterStatus) SetConditions(resources []runtime.Object) {
328+
func (clusterStatus *RabbitmqClusterStatus) SetConditions(resources []runtime.Object) {
329329
var oldAllPodsReadyCondition *status.RabbitmqClusterCondition
330330
var oldClusterAvailableCondition *status.RabbitmqClusterCondition
331331
var oldNoWarningsCondition *status.RabbitmqClusterCondition
332332
var oldReconcileCondition *status.RabbitmqClusterCondition
333333

334-
for _, condition := range rmqStatus.Conditions {
334+
for _, condition := range clusterStatus.Conditions {
335335
switch condition.Type {
336336
case status.AllReplicasReady:
337337
oldAllPodsReadyCondition = condition.DeepCopy()
@@ -355,20 +355,20 @@ func (rmqStatus *RabbitmqClusterStatus) SetConditions(resources []runtime.Object
355355
reconciledCondition = status.ReconcileSuccessCondition(corev1.ConditionUnknown, "Initialising", "")
356356
}
357357

358-
rmqStatus.Conditions = []status.RabbitmqClusterCondition{
358+
clusterStatus.Conditions = []status.RabbitmqClusterCondition{
359359
allReplicasReadyCond,
360360
clusterAvailableCond,
361361
noWarningsCond,
362362
reconciledCondition,
363363
}
364364
}
365365

366-
func (rmqStatus *RabbitmqClusterStatus) SetCondition(condType status.RabbitmqClusterConditionType,
366+
func (clusterStatus *RabbitmqClusterStatus) SetCondition(condType status.RabbitmqClusterConditionType,
367367
condStatus corev1.ConditionStatus, reason string, messages ...string) {
368-
for i := range rmqStatus.Conditions {
369-
if rmqStatus.Conditions[i].Type == condType {
370-
rmqStatus.Conditions[i].UpdateState(condStatus)
371-
rmqStatus.Conditions[i].UpdateReason(reason, messages...)
368+
for i := range clusterStatus.Conditions {
369+
if clusterStatus.Conditions[i].Type == condType {
370+
clusterStatus.Conditions[i].UpdateState(condStatus)
371+
clusterStatus.Conditions[i].UpdateReason(reason, messages...)
372372
break
373373
}
374374
}
@@ -383,8 +383,8 @@ type RabbitmqClusterList struct {
383383
Items []RabbitmqCluster `json:"items"`
384384
}
385385

386-
func (r RabbitmqCluster) ChildResourceName(name string) string {
387-
return strings.Join([]string{r.Name, "rabbitmq", name}, "-")
386+
func (cluster RabbitmqCluster) ChildResourceName(name string) string {
387+
return strings.Join([]string{cluster.Name, "rabbitmq", name}, "-")
388388
}
389389

390390
func init() {

api/v1beta1/rabbitmqcluster_types_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
corev1 "k8s.io/api/core/v1"
1717
"k8s.io/apimachinery/pkg/api/resource"
1818
k8sresource "k8s.io/apimachinery/pkg/api/resource"
19-
runtime "k8s.io/apimachinery/pkg/runtime"
19+
"k8s.io/apimachinery/pkg/runtime"
2020

2121
"golang.org/x/net/context"
2222
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -163,7 +163,7 @@ var _ = Describe("RabbitmqCluster", func() {
163163
Image: "rabbitmq-image-from-cr",
164164
ImagePullSecret: "my-super-secret",
165165
Service: RabbitmqClusterServiceSpec{
166-
Type: corev1.ServiceType("this-is-a-service"),
166+
Type: "this-is-a-service",
167167
Annotations: map[string]string{
168168
"myannotation": "is-set",
169169
},
@@ -186,7 +186,7 @@ var _ = Describe("RabbitmqCluster", func() {
186186
NodeAffinity: &corev1.NodeAffinity{
187187
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
188188
NodeSelectorTerms: []corev1.NodeSelectorTerm{
189-
corev1.NodeSelectorTerm{
189+
{
190190
MatchExpressions: []corev1.NodeSelectorRequirement{
191191
{
192192
Key: "somekey",
@@ -201,7 +201,7 @@ var _ = Describe("RabbitmqCluster", func() {
201201
},
202202
},
203203
Tolerations: []corev1.Toleration{
204-
corev1.Toleration{
204+
{
205205
Key: "mykey",
206206
Operator: "NotEqual",
207207
Value: "myvalue",
@@ -325,15 +325,15 @@ var _ = Describe("RabbitmqCluster", func() {
325325

326326
It("updates an arbitrary condition", func() {
327327
someCondition := status.RabbitmqClusterCondition{}
328-
someCondition.Type = status.RabbitmqClusterConditionType("a-type")
328+
someCondition.Type = "a-type"
329329
someCondition.Reason = "whynot"
330-
someCondition.Status = corev1.ConditionStatus("perhaps")
330+
someCondition.Status = "perhaps"
331331
someCondition.LastTransitionTime = metav1.Unix(10, 0)
332332
rmqStatus := RabbitmqClusterStatus{
333333
Conditions: []status.RabbitmqClusterCondition{someCondition},
334334
}
335335

336-
rmqStatus.SetCondition(status.RabbitmqClusterConditionType("a-type"),
336+
rmqStatus.SetCondition("a-type",
337337
corev1.ConditionTrue, "some-reason", "my-message")
338338

339339
updatedCondition := rmqStatus.Conditions[0]

controllers/rabbitmqcluster_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,13 @@ func (r *RabbitmqClusterReconciler) prepareForDeletion(ctx context.Context, rabb
459459
}
460460
// Add label on all Pods to be picked up in pre-stop hook via Downward API
461461
if err := r.addRabbitmqDeletionLabel(ctx, rabbitmqCluster); err != nil {
462-
return fmt.Errorf("Failed to add deletion markers to RabbitmqCluster Pods: %s", err.Error())
462+
return fmt.Errorf("failed to add deletion markers to RabbitmqCluster Pods: %s", err.Error())
463463
}
464464
// Delete StatefulSet immediately after changing pod labels to minimize risk of them respawning.
465465
// There is a window where the StatefulSet could respawn Pods without the deletion label in this order.
466466
// But we can't delete it before because the DownwardAPI doesn't update once a Pod enters Terminating.
467467
if err := r.Client.Delete(ctx, sts); client.IgnoreNotFound(err) != nil {
468-
return fmt.Errorf("Cannot delete StatefulSet: %s", err.Error())
468+
return fmt.Errorf("cannot delete StatefulSet: %s", err.Error())
469469
}
470470

471471
return nil
@@ -508,7 +508,7 @@ func (r *RabbitmqClusterReconciler) addRabbitmqDeletionLabel(ctx context.Context
508508
pod := &pods.Items[i]
509509
pod.Labels[resource.DeletionMarker] = "true"
510510
if err := r.Client.Update(ctx, pod); client.IgnoreNotFound(err) != nil {
511-
return fmt.Errorf("Cannot Update Pod %s in Namespace %s: %s", pod.Name, pod.Namespace, err.Error())
511+
return fmt.Errorf("cannot Update Pod %s in Namespace %s: %s", pod.Name, pod.Namespace, err.Error())
512512
}
513513
}
514514

internal/metadata/metadata_suite_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,3 @@ func TestResource(t *testing.T) {
2020
RegisterFailHandler(Fail)
2121
RunSpecs(t, "Metadata Suite")
2222
}
23-
24-
func testLabels(labels map[string]string) {
25-
ExpectWithOffset(1, labels).To(SatisfyAll(
26-
HaveKeyWithValue("foo", "bar"),
27-
HaveKeyWithValue("rabbitmq", "is-great"),
28-
HaveKeyWithValue("foo/app.kubernetes.io", "edgecase"),
29-
Not(HaveKey("app.kubernetes.io/foo")),
30-
))
31-
}

internal/resource/client_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (builder *ClientServiceBuilder) Update(object runtime.Object) error {
5252
service := object.(*corev1.Service)
5353
builder.setAnnotations(service)
5454
service.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
55-
service.Spec.Type = corev1.ServiceType(builder.Instance.Spec.Service.Type)
55+
service.Spec.Type = builder.Instance.Spec.Service.Type
5656
service.Spec.Selector = metadata.LabelSelector(builder.Instance.Name)
5757

5858
service.Spec.Ports = builder.updatePorts(service.Spec.Ports)

internal/resource/client_service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ var _ = Context("ClientServices", func() {
334334
It("preserves the same node ports after updating from LoadBalancer to NodePort", func() {
335335
svc.Spec.Type = corev1.ServiceTypeLoadBalancer
336336
svc.Spec.Ports = []corev1.ServicePort{
337-
corev1.ServicePort{
337+
{
338338
Protocol: corev1.ProtocolTCP,
339339
Port: 5672,
340340
Name: "amqp",
341341
NodePort: 12345,
342342
},
343-
corev1.ServicePort{
343+
{
344344
Protocol: corev1.ProtocolTCP,
345345
Port: 15672,
346346
Name: "management",
@@ -372,7 +372,7 @@ var _ = Context("ClientServices", func() {
372372
It("unsets nodePort after updating from NodePort to ClusterIP", func() {
373373
svc.Spec.Type = corev1.ServiceTypeNodePort
374374
svc.Spec.Ports = []corev1.ServicePort{
375-
corev1.ServicePort{
375+
{
376376
Protocol: corev1.ProtocolTCP,
377377
Port: 5672,
378378
Name: "amqp",
@@ -399,7 +399,7 @@ var _ = Context("ClientServices", func() {
399399
It("unsets the service type and node ports when service type is deleted from CR spec", func() {
400400
svc.Spec.Type = corev1.ServiceTypeNodePort
401401
svc.Spec.Ports = []corev1.ServicePort{
402-
corev1.ServicePort{
402+
{
403403
Protocol: corev1.ProtocolTCP,
404404
Port: 5672,
405405
Name: "amqp",

internal/resource/statefulset_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ func generateRabbitmqCluster() rabbitmqv1beta1.RabbitmqCluster {
14091409
Image: "rabbitmq-image-from-cr",
14101410
ImagePullSecret: "my-super-secret",
14111411
Service: rabbitmqv1beta1.RabbitmqClusterServiceSpec{
1412-
Type: corev1.ServiceType("this-is-a-service"),
1412+
Type: "this-is-a-service",
14131413
Annotations: map[string]string{},
14141414
},
14151415
Persistence: rabbitmqv1beta1.RabbitmqClusterPersistenceSpec{
@@ -1430,7 +1430,7 @@ func generateRabbitmqCluster() rabbitmqv1beta1.RabbitmqCluster {
14301430
NodeAffinity: &corev1.NodeAffinity{
14311431
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
14321432
NodeSelectorTerms: []corev1.NodeSelectorTerm{
1433-
corev1.NodeSelectorTerm{
1433+
{
14341434
MatchExpressions: []corev1.NodeSelectorRequirement{
14351435
{
14361436
Key: "somekey",
@@ -1445,7 +1445,7 @@ func generateRabbitmqCluster() rabbitmqv1beta1.RabbitmqCluster {
14451445
},
14461446
},
14471447
Tolerations: []corev1.Toleration{
1448-
corev1.Toleration{
1448+
{
14491449
Key: "mykey",
14501450
Operator: "NotEqual",
14511451
Value: "myvalue",

internal/status/status_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ var _ = Describe("Status", func() {
2020
BeforeEach(func() {
2121
someConditionTime = metav1.Unix(1, 1)
2222
someCondition = RabbitmqClusterCondition{
23-
Type: RabbitmqClusterConditionType("a-type"),
24-
Status: corev1.ConditionStatus("some-status"),
23+
Type: "a-type",
24+
Status: "some-status",
2525
LastTransitionTime: (*someConditionTime.DeepCopy()),
2626
Reason: "reasons",
2727
Message: "ship-it",
2828
}
2929
})
3030

3131
It("changes the status and transition time", func() {
32-
someCondition.UpdateState(corev1.ConditionStatus("maybe"))
32+
someCondition.UpdateState("maybe")
3333
Expect(someCondition.Status).To(Equal(corev1.ConditionStatus("maybe")))
3434

3535
Expect(someCondition.LastTransitionTime).NotTo(Equal(someConditionTime))
@@ -38,7 +38,7 @@ var _ = Describe("Status", func() {
3838
})
3939

4040
It("preserves the status and transtion time", func() {
41-
someCondition.UpdateState(corev1.ConditionStatus("some-status"))
41+
someCondition.UpdateState("some-status")
4242
Expect(someCondition.Status).To(Equal(corev1.ConditionStatus("some-status")))
4343
Expect(someCondition.LastTransitionTime).To(Equal(someConditionTime))
4444
})

system_tests/system_tests.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import (
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
)
2525

26-
const (
27-
clientServiceSuffix = "client"
28-
statefulSetSuffix = "server"
29-
)
26+
const statefulSetSuffix = "server"
3027

3128
var _ = Describe("Operator", func() {
3229
var (

system_tests/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func createTLSSecret(secretName, secretNamespace, hostname string) string {
600600
// generate and write cert and key to file
601601
Expect(createCertificateChain(hostname, caCertFile, serverCertFile, serverKeyFile)).To(Succeed())
602602
// create k8s tls secret
603-
Expect(k8sCreateSecretTLS("rabbitmq-tls-test-secret", secretNamespace, serverCertPath, serverKeyPath)).To(Succeed())
603+
Expect(k8sCreateSecretTLS(secretName, secretNamespace, serverCertPath, serverKeyPath)).To(Succeed())
604604

605605
// remove server files
606606
Expect(os.Remove(serverKeyPath)).To(Succeed())

0 commit comments

Comments
 (0)