Skip to content

Commit

Permalink
Drop '-client' suffix from rmq service
Browse files Browse the repository at this point in the history
- rename ClientService override to Service override
- rename client_service.go to service.go
  • Loading branch information
ChunyiLyu committed Nov 6, 2020
1 parent bc8bc2f commit b5d7713
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 128 deletions.
14 changes: 7 additions & 7 deletions api/v1beta1/rabbitmqcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ type RabbitmqClusterSpec struct {
}

type RabbitmqClusterOverrideSpec struct {
StatefulSet *StatefulSet `json:"statefulSet,omitempty"`
ClientService *ClientService `json:"clientService,omitempty"`
StatefulSet *StatefulSet `json:"statefulSet,omitempty"`
Service *Service `json:"service,omitempty"`
}

type ClientService struct {
type Service struct {
// +optional
*EmbeddedLabelsAnnotations `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the behavior of a service.
Expand Down Expand Up @@ -151,7 +151,7 @@ type StatefulSetSpec struct {
UpdateStrategy *appsv1.StatefulSetUpdateStrategy `json:"updateStrategy,omitempty" protobuf:"bytes,7,opt,name=updateStrategy"`
}

// It is used in ClientService and StatefulSet
// It is used in Service and StatefulSet
type EmbeddedLabelsAnnotations struct {
// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects. May match selectors of replication controllers
Expand Down Expand Up @@ -277,12 +277,12 @@ type RabbitmqClusterPersistenceSpec struct {
Storage *k8sresource.Quantity `json:"storage,omitempty"`
}

// Settable attributes for the Client Service resource.
// Settable attributes for the Service resource.
type RabbitmqClusterServiceSpec struct {
// +kubebuilder:validation:Enum=ClusterIP;LoadBalancer;NodePort
// +kubebuilder:default:="ClusterIP"
Type corev1.ServiceType `json:"type,omitempty"`
// Annotations to add to the Client Service.
// Annotations to add to the Service.
Annotations map[string]string `json:"annotations,omitempty"`
}

Expand Down Expand Up @@ -396,7 +396,7 @@ type RabbitmqClusterList struct {
}

func (cluster RabbitmqCluster) ChildResourceName(name string) string {
return strings.Join([]string{cluster.Name, name}, "-")
return strings.TrimSuffix(strings.Join([]string{cluster.Name, name}, "-"), "-")
}

func init() {
Expand Down
56 changes: 28 additions & 28 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions charts/tsmgr/bind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

template: |
local clientFilterFunc(j) = std.length(std.findSubstr("rabbitmq-client", j.name)) > 0;
local clientService = std.filter(clientFilterFunc, $.services)[0];
local service = std.filter(clientFilterFunc, $.services)[0];
local secretFilterFunc(j) = std.length(std.findSubstr("rabbitmq-default-user", j.name)) > 0;
local defaultUser = std.filter(secretFilterFunc, $.secrets);
local vhost = "%2F";
local ingress = if clientService.spec.type == "LoadBalancer" then
if std.objectHas(clientService.status.loadBalancer.ingress[0], "hostname") then
clientService.status.loadBalancer.ingress[0].hostname
local ingress = if service.spec.type == "LoadBalancer" then
if std.objectHas(service.status.loadBalancer.ingress[0], "hostname") then
service.status.loadBalancer.ingress[0].hostname
else
clientService.status.loadBalancer.ingress[0].ip
service.status.loadBalancer.ingress[0].ip
else
clientService.spec.clusterIP;
service.spec.clusterIP;
local defaultUserUsername = defaultUser[0].data['username'];
local defaultUserPassword = defaultUser[0].data['password'];
local mgmtURI = "http://" + ingress + ":15672";
Expand Down
6 changes: 3 additions & 3 deletions config/crd/bases/rabbitmq.com_rabbitmqclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ spec:
type: array
override:
properties:
clientService:
service:
properties:
metadata:
properties:
Expand Down Expand Up @@ -3700,12 +3700,12 @@ spec:
service:
default:
type: ClusterIP
description: Settable attributes for the Client Service resource.
description: Settable attributes for the Service resource.
properties:
annotations:
additionalProperties:
type: string
description: Annotations to add to the Client Service.
description: Annotations to add to the Service.
type: object
type:
default: ClusterIP
Expand Down
2 changes: 1 addition & 1 deletion controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (r *RabbitmqClusterReconciler) getChildResources(ctx context.Context, rmq r
}

if err := r.Client.Get(ctx,
types.NamespacedName{Name: rmq.ChildResourceName("client"), Namespace: rmq.Namespace},
types.NamespacedName{Name: rmq.ChildResourceName(resource.ServiceSuffix), Namespace: rmq.Namespace},
endPoints); err != nil && !errors.IsNotFound(err) {
return nil, err
} else if errors.IsNotFound(err) {
Expand Down
58 changes: 29 additions & 29 deletions controllers/rabbitmqcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ var _ = Describe("RabbitmqClusterController", func() {
Expect(secret.OwnerReferences[0].Name).To(Equal(cluster.Name))
})

By("creating a rabbitmq client service", func() {
svc := service(ctx, cluster, "client")
Expect(svc.Name).To(Equal(cluster.ChildResourceName("client")))
By("creating a rabbitmq service", func() {
svc := service(ctx, cluster, "")
Expect(svc.Name).To(Equal(cluster.ChildResourceName("")))
Expect(svc.OwnerReferences[0].Name).To(Equal(cluster.Name))
Expect(svc.Spec.Type).To(Equal(corev1.ServiceTypeClusterIP))
})
Expand Down Expand Up @@ -148,7 +148,7 @@ var _ = Describe("RabbitmqClusterController", func() {
By("recording SuccessfulCreate events for all child resources", func() {
allEventMsgs := aggregateEventMsgs(ctx, cluster, "SuccessfulCreate")
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.StatefulSet", cluster.ChildResourceName("server")))
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.Service", cluster.ChildResourceName("client")))
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.Service", cluster.ChildResourceName("")))
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.Service", cluster.ChildResourceName("nodes")))
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.ConfigMap", cluster.ChildResourceName("plugins-conf")))
Expect(allEventMsgs).To(ContainSubstring("created resource %s of Type *v1.ConfigMap", cluster.ChildResourceName("server-conf")))
Expand Down Expand Up @@ -259,10 +259,10 @@ var _ = Describe("RabbitmqClusterController", func() {
})
})

Context("Client service configurations", func() {
Context("Service configurations", func() {
AfterEach(func() {
Expect(client.Delete(ctx, cluster)).To(Succeed())
Expect(clientSet.CoreV1().Services(cluster.Namespace).Delete(ctx, cluster.ChildResourceName("client"), metav1.DeleteOptions{}))
Expect(clientSet.CoreV1().Services(cluster.Namespace).Delete(ctx, cluster.ChildResourceName(""), metav1.DeleteOptions{}))
})

It("creates the service type and annotations as configured in instance spec", func() {
Expand All @@ -277,7 +277,7 @@ var _ = Describe("RabbitmqClusterController", func() {

Expect(client.Create(ctx, cluster)).To(Succeed())

clientSvc := service(ctx, cluster, "client")
clientSvc := service(ctx, cluster, "")
Expect(clientSvc.Spec.Type).Should(Equal(corev1.ServiceTypeLoadBalancer))
Expect(clientSvc.Annotations).Should(HaveKeyWithValue("annotations", "cr-annotation"))
})
Expand Down Expand Up @@ -352,8 +352,8 @@ var _ = Describe("RabbitmqClusterController", func() {

Context("Custom Resource updates", func() {
var (
clientServiceName string
stsName string
svcName string
stsName string
)
BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
Expand All @@ -362,7 +362,7 @@ var _ = Describe("RabbitmqClusterController", func() {
Namespace: defaultNamespace,
},
}
clientServiceName = cluster.ChildResourceName("client")
svcName = cluster.ChildResourceName("")
stsName = cluster.ChildResourceName("server")

Expect(client.Create(ctx, cluster)).To(Succeed())
Expand All @@ -380,14 +380,14 @@ var _ = Describe("RabbitmqClusterController", func() {
})).To(Succeed())

Eventually(func() map[string]string {
clientServiceName := cluster.ChildResourceName("client")
service, _ := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, clientServiceName, metav1.GetOptions{})
return service.Annotations
svcName := cluster.ChildResourceName("")
svc, _ := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, svcName, metav1.GetOptions{})
return svc.Annotations
}, 3).Should(HaveKeyWithValue("test-key", "test-value"))

// verify that SuccessfulUpdate event is recorded for the client service
// verify that SuccessfulUpdate event is recorded for the service
Expect(aggregateEventMsgs(ctx, cluster, "SuccessfulUpdate")).To(
ContainSubstring("updated resource %s of Type *v1.Service", cluster.ChildResourceName("client")))
ContainSubstring("updated resource %s of Type *v1.Service", cluster.ChildResourceName("")))
})

It("the CPU and memory requirements are updated", func() {
Expand Down Expand Up @@ -452,7 +452,7 @@ var _ = Describe("RabbitmqClusterController", func() {
})).To(Succeed())

Eventually(func() map[string]string {
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, clientServiceName, metav1.GetOptions{})
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, svcName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return service.Labels
}, 3).Should(HaveKeyWithValue("foo", "bar"))
Expand Down Expand Up @@ -482,7 +482,7 @@ var _ = Describe("RabbitmqClusterController", func() {
}, 3).Should(HaveKeyWithValue(annotationKey, annotationValue))

Eventually(func() map[string]string {
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, cluster.ChildResourceName("client"), metav1.GetOptions{})
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, cluster.ChildResourceName(""), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return service.Annotations
}, 3).Should(HaveKeyWithValue(annotationKey, annotationValue))
Expand Down Expand Up @@ -570,7 +570,7 @@ var _ = Describe("RabbitmqClusterController", func() {
})).To(Succeed())

Eventually(func() string {
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, cluster.ChildResourceName("client"), metav1.GetOptions{})
service, err := clientSet.CoreV1().Services(cluster.Namespace).Get(ctx, cluster.ChildResourceName(""), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return string(service.Spec.Type)
}, 3).Should(Equal("NodePort"))
Expand Down Expand Up @@ -623,7 +623,7 @@ var _ = Describe("RabbitmqClusterController", func() {

Context("Recreate child resources after deletion", func() {
var (
clientServiceName string
svcName string
headlessServiceName string
stsName string
configMapName string
Expand All @@ -635,7 +635,7 @@ var _ = Describe("RabbitmqClusterController", func() {
Namespace: defaultNamespace,
},
}
clientServiceName = cluster.ChildResourceName("client")
svcName = cluster.ChildResourceName("")
headlessServiceName = cluster.ChildResourceName("nodes")
stsName = cluster.ChildResourceName("server")
configMapName = cluster.ChildResourceName("server-conf")
Expand All @@ -652,15 +652,15 @@ var _ = Describe("RabbitmqClusterController", func() {
oldConfMap, err := clientSet.CoreV1().ConfigMaps(defaultNamespace).Get(ctx, configMapName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())

oldClientSvc := service(ctx, cluster, "client")
oldClientSvc := service(ctx, cluster, "")

oldHeadlessSvc := service(ctx, cluster, "nodes")

oldSts := statefulSet(ctx, cluster)

Expect(clientSet.AppsV1().StatefulSets(defaultNamespace).Delete(ctx, stsName, metav1.DeleteOptions{})).NotTo(HaveOccurred())
Expect(clientSet.CoreV1().ConfigMaps(defaultNamespace).Delete(ctx, configMapName, metav1.DeleteOptions{})).NotTo(HaveOccurred())
Expect(clientSet.CoreV1().Services(defaultNamespace).Delete(ctx, clientServiceName, metav1.DeleteOptions{})).NotTo(HaveOccurred())
Expect(clientSet.CoreV1().Services(defaultNamespace).Delete(ctx, svcName, metav1.DeleteOptions{})).NotTo(HaveOccurred())
Expect(clientSet.CoreV1().Services(defaultNamespace).Delete(ctx, headlessServiceName, metav1.DeleteOptions{})).NotTo(HaveOccurred())

Eventually(func() bool {
Expand All @@ -672,7 +672,7 @@ var _ = Describe("RabbitmqClusterController", func() {
}, 5).Should(BeTrue())

Eventually(func() bool {
clientSvc, err := clientSet.CoreV1().Services(defaultNamespace).Get(ctx, clientServiceName, metav1.GetOptions{})
clientSvc, err := clientSet.CoreV1().Services(defaultNamespace).Get(ctx, svcName, metav1.GetOptions{})
if err != nil {
return false
}
Expand Down Expand Up @@ -1038,7 +1038,7 @@ var _ = Describe("RabbitmqClusterController", func() {
})
})

Context("Client Service Override", func() {
Context("Service Override", func() {

BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
Expand All @@ -1051,7 +1051,7 @@ var _ = Describe("RabbitmqClusterController", func() {
Type: "LoadBalancer",
},
Override: rabbitmqv1beta1.RabbitmqClusterOverrideSpec{
ClientService: &rabbitmqv1beta1.ClientService{
Service: &rabbitmqv1beta1.Service{
Spec: &corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Expand Down Expand Up @@ -1081,11 +1081,11 @@ var _ = Describe("RabbitmqClusterController", func() {
waitForClusterDeletion(ctx, cluster, client)
})

It("creates a Client Service with the override applied", func() {
It("creates a Service with the override applied", func() {
amqpTargetPort := intstr.IntOrString{IntVal: int32(5672)}
managementTargetPort := intstr.IntOrString{IntVal: int32(15672)}
additionalTargetPort := intstr.IntOrString{IntVal: int32(15535)}
svc := service(ctx, cluster, "client")
svc := service(ctx, cluster, "")
Expect(svc.Spec.Type).To(Equal(corev1.ServiceTypeClusterIP))
Expect(svc.Spec.Ports).To(ConsistOf(
corev1.ServicePort{
Expand Down Expand Up @@ -1114,11 +1114,11 @@ var _ = Describe("RabbitmqClusterController", func() {

It("updates", func() {
Expect(updateWithRetry(cluster, func(r *rabbitmqv1beta1.RabbitmqCluster) {
cluster.Spec.Override.ClientService.Spec.Type = "LoadBalancer"
cluster.Spec.Override.Service.Spec.Type = "LoadBalancer"
})).To(Succeed())

Eventually(func() corev1.ServiceType {
svc := service(ctx, cluster, "client")
svc := service(ctx, cluster, "")
return svc.Spec.Type
}, 5).Should(Equal(corev1.ServiceTypeLoadBalancer))
})
Expand Down
2 changes: 1 addition & 1 deletion controllers/reconcile_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (r *RabbitmqClusterReconciler) setDefaultUserStatus(ctx context.Context, rm
defaultUserStatus := &rabbitmqv1beta1.RabbitmqClusterDefaultUser{}

serviceRef := &rabbitmqv1beta1.RabbitmqClusterServiceReference{
Name: rmq.ChildResourceName("client"),
Name: rmq.ChildResourceName(""),
Namespace: rmq.Namespace,
}
defaultUserStatus.ServiceReference = serviceRef
Expand Down

0 comments on commit b5d7713

Please sign in to comment.