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

Expose provisioned service binding #615

Merged
merged 3 commits into from
Feb 23, 2021
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
5 changes: 5 additions & 0 deletions api/v1beta1/rabbitmqcluster_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type RabbitmqClusterStatus struct {

// Identifying information on internal resources
DefaultUser *RabbitmqClusterDefaultUser `json:"defaultUser,omitempty"`

// Binding exposes a secret containing the binding information for this
// RabbitmqCluster. It implements the service binding Provisioned Service
// duck type. See: https://k8s-service-bindings.github.io/spec/#provisioned-service
Binding *corev1.LocalObjectReference `json:"binding,omitempty"`
}

// Contains references to resources created with the RabbitmqCluster resource.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

7 changes: 7 additions & 0 deletions config/crd/bases/rabbitmq.com_rabbitmqclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,13 @@ spec:
status:
description: Status presents the observed state of RabbitmqCluster
properties:
binding:
description: 'Binding exposes a secret containing the binding information for this RabbitmqCluster. It implements the service binding Provisioned Service duck type. See: https://k8s-service-bindings.github.io/spec/#provisioned-service'
properties:
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
clusterStatus:
description: Unused.
type: string
Expand Down
5 changes: 4 additions & 1 deletion controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// only StatefulSetBuilder returns true
if builder.UpdateMayRequireStsRecreate() {
if builder.UpdateMayRequireStsRecreate() {
if err = r.reconcilePVC(ctx, builder, rabbitmqCluster, resource); err != nil {
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionFalse, "FailedReconcilePVC", err.Error())
if statusErr := r.Status().Update(ctx, rabbitmqCluster); statusErr != nil {
Expand Down Expand Up @@ -201,6 +201,9 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if err := r.setDefaultUserStatus(ctx, rabbitmqCluster); err != nil {
return ctrl.Result{}, err
}
if err := r.setBinding(ctx, rabbitmqCluster); err != nil {
return ctrl.Result{}, err
}

// By this point the StatefulSet may have finished deploying. Run any
// post-deploy steps if so, or requeue until the deployment is finished.
Expand Down
18 changes: 18 additions & 0 deletions controllers/reconcile_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/resource"
corev1 "k8s.io/api/core/v1"
"reflect"
)

Expand Down Expand Up @@ -36,3 +37,20 @@ func (r *RabbitmqClusterReconciler) setDefaultUserStatus(ctx context.Context, rm

return nil
}

// Status.Binding exposes the default user secret which contains the binding
// information for this RabbitmqCluster.
// Default user secret implements the service binding Provisioned Service
// See: https://k8s-service-bindings.github.io/spec/#provisioned-service
func (r *RabbitmqClusterReconciler) setBinding(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster) error {
binding := &corev1.LocalObjectReference{
Name: rmq.ChildResourceName(resource.DefaultUserSecretName),
}
if !reflect.DeepEqual(rmq.Status.Binding, binding) {
rmq.Status.Binding = binding
if err := r.Status().Update(ctx, rmq); err != nil {
return err
}
}
return nil
}
15 changes: 15 additions & 0 deletions controllers/reconcile_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers_test
import (
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/resource"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -71,5 +72,19 @@ var _ = Describe("Reconcile status", func() {

Expect(serviceRef.Name).To(Equal(rmq.ChildResourceName("")))
Expect(serviceRef.Namespace).To(Equal(rmq.Namespace))

By("setting Status.Binding")
rmq = &rabbitmqv1beta1.RabbitmqCluster{}
binding := &corev1.LocalObjectReference{}
Eventually(func() *corev1.LocalObjectReference {
client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
if rmq.Status.Binding != nil {
binding = rmq.Status.Binding
return binding
}
return nil
}, 5).ShouldNot(BeNil())

Expect(binding.Name).To(Equal(rmq.ChildResourceName(resource.DefaultUserSecretName)))
})
})
1 change: 1 addition & 0 deletions docs/api/rabbitmq.com.ref.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Status presents the observed state of RabbitmqCluster
| *`clusterStatus`* __string__ | Unused.
| *`conditions`* __xref:{anchor_prefix}-github.aaakk.us.kg-rabbitmq-cluster-operator-internal-status-rabbitmqclustercondition[$$RabbitmqClusterCondition$$] array__ | Set of Conditions describing the current state of the RabbitmqCluster
| *`defaultUser`* __xref:{anchor_prefix}-github.aaakk.us.kg-rabbitmq-cluster-operator-api-v1beta1-rabbitmqclusterdefaultuser[$$RabbitmqClusterDefaultUser$$]__ | Identifying information on internal resources
| *`binding`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#localobjectreference-v1-core[$$LocalObjectReference$$]__ | Binding exposes a secret containing the binding information for this RabbitmqCluster. It implements the service binding Provisioned Service duck type. See: https://k8s-service-bindings.github.io/spec/#provisioned-service
|===


Expand Down
6 changes: 6 additions & 0 deletions internal/resource/default_user_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

const (
DefaultUserSecretName = "default-user"
bindingProvider = "rabbitmq"
bindingType = "rabbitmq"
)

type DefaultUserSecretBuilder struct {
Expand Down Expand Up @@ -62,7 +64,11 @@ func (builder *DefaultUserSecretBuilder) Build() (client.Object, error) {
Namespace: builder.Instance.Namespace,
},
Type: corev1.SecretTypeOpaque,
// Default user secret implements the service binding Provisioned Service
// See: https://k8s-service-bindings.github.io/spec/#provisioned-service
Data: map[string][]byte{
"provider": []byte(bindingProvider),
"type": []byte(bindingType),
"username": []byte(username),
"password": []byte(password),
"default_user.conf": defaultUserConf,
Expand Down
12 changes: 12 additions & 0 deletions internal/resource/default_user_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ var _ = Describe("DefaultUserSecret", func() {
Expect(cfg.Section("").Key("default_user").Value()).To(Equal(string(username)))
Expect(cfg.Section("").Key("default_pass").Value()).To(Equal(string(password)))
})

By("setting 'data.provider' to 'rabbitmq' ", func() {
provider, ok := secret.Data["provider"]
Expect(ok).NotTo(BeFalse(), "Failed to find key 'provider' ")
Expect(string(provider)).To(Equal("rabbitmq"))
})

By("setting 'data.type' to 'rabbitmq' ", func() {
t, ok := secret.Data["type"]
Expect(ok).NotTo(BeFalse(), "Failed to find key 'type' ")
Expect(string(t)).To(Equal("rabbitmq"))
})
})
})

Expand Down
1 change: 0 additions & 1 deletion internal/resource/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (builder *RoleBuilder) Build() (client.Object, error) {
}, nil
}


func (builder *RoleBuilder) UpdateMayRequireStsRecreate() bool {
return false
}
Expand Down
1 change: 0 additions & 1 deletion internal/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (builder *ServiceBuilder) Build() (client.Object, error) {
}, nil
}


func (builder *ServiceBuilder) UpdateMayRequireStsRecreate() bool {
return false
}
Expand Down
1 change: 0 additions & 1 deletion internal/resource/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (builder *ServiceAccountBuilder) Build() (client.Object, error) {
}, nil
}


func (builder *ServiceAccountBuilder) UpdateMayRequireStsRecreate() bool {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion system_tests/system_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ CONSOLE_LOG=new`
})

Context("Persistence expansion", func() {
var cluster *rabbitmqv1beta1.RabbitmqCluster
var cluster *rabbitmqv1beta1.RabbitmqCluster

AfterEach(func() {
Expect(rmqClusterClient.Delete(context.TODO(), cluster)).To(Succeed())
Expand Down