Skip to content

Commit

Permalink
Support OpenShift
Browse files Browse the repository at this point in the history
1. Allow rabbitmq-cluster-operator-role to update
rabbitmqclusters/finalizers

2. Do not BlockOwnerDeletion for PVCs

Fixes #234
  • Loading branch information
ansd committed Sep 1, 2020
1 parent ced108a commit a235ccd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ rules:
- list
- update
- watch
- apiGroups:
- rabbitmq.com
resources:
- rabbitmqclusters/finalizers
verbs:
- update
- apiGroups:
- rabbitmq.com
resources:
Expand Down
4 changes: 2 additions & 2 deletions controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ type RabbitmqClusterReconciler struct {
// +kubebuilder:rbac:groups="",resources=pods/exec,verbs=create
// +kubebuilder:rbac:groups="",resources=pods,verbs=update;get;list;watch
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;watch
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;watch;list
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=list
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=rabbitmq.com,resources=rabbitmqclusters,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=rabbitmq.com,resources=rabbitmqclusters/status,verbs=get;update
// +kubebuilder:rbac:groups=rabbitmq.com,resources=rabbitmqclusters/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=events,verbs=get;create;patch
// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles,verbs=get;list;watch;create;update
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451
sigs.k8s.io/controller-runtime v0.6.2
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e // indirect
)
11 changes: 11 additions & 0 deletions internal/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -91,6 +92,7 @@ func (builder *StatefulSetBuilder) Build() (runtime.Object, error) {
if err := controllerutil.SetControllerReference(builder.Instance, &pvcList[i], builder.Scheme); err != nil {
return nil, fmt.Errorf("failed setting controller reference: %v", err)
}
disableBlockOwnerDeletion(pvcList[i])
}
sts.Spec.VolumeClaimTemplates = pvcList
}
Expand Down Expand Up @@ -121,10 +123,19 @@ func persistentVolumeClaim(instance *rabbitmqv1beta1.RabbitmqCluster, scheme *ru
if err := controllerutil.SetControllerReference(instance, &pvc, scheme); err != nil {
return []corev1.PersistentVolumeClaim{}, fmt.Errorf("failed setting controller reference: %v", err)
}
disableBlockOwnerDeletion(pvc)

return []corev1.PersistentVolumeClaim{pvc}, nil
}

// required for OpenShift compatibility, see https://github.com/rabbitmq/cluster-operator/issues/234
func disableBlockOwnerDeletion(pvc corev1.PersistentVolumeClaim) {
refs := pvc.OwnerReferences
for i := range refs {
refs[i].BlockOwnerDeletion = pointer.BoolPtr(false)
}
}

func (builder *StatefulSetBuilder) Update(object runtime.Object) error {
sts := object.(*appsv1.StatefulSet)

Expand Down
15 changes: 7 additions & 8 deletions internal/resource/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
)

var _ = Describe("StatefulSet", func() {
Expand Down Expand Up @@ -94,7 +95,6 @@ var _ = Describe("StatefulSet", func() {
})
Context("PVC template", func() {
It("creates the required PersistentVolumeClaim", func() {
truth := true
q, _ := k8sresource.ParseQuantity("10Gi")

obj, err := stsBuilder.Build()
Expand All @@ -116,8 +116,8 @@ var _ = Describe("StatefulSet", func() {
Kind: "RabbitmqCluster",
Name: instance.Name,
UID: "",
Controller: &truth,
BlockOwnerDeletion: &truth,
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(false),
},
},
Annotations: map[string]string{},
Expand Down Expand Up @@ -169,7 +169,6 @@ var _ = Describe("StatefulSet", func() {

It("overrides the PVC list", func() {
storageClass := "my-storage-class"
truth := true
builder.Instance.Spec.Override.StatefulSet = &rabbitmqv1beta1.StatefulSet{
Spec: &rabbitmqv1beta1.StatefulSetSpec{
VolumeClaimTemplates: []rabbitmqv1beta1.PersistentVolumeClaim{
Expand Down Expand Up @@ -220,8 +219,8 @@ var _ = Describe("StatefulSet", func() {
Kind: "RabbitmqCluster",
Name: instance.Name,
UID: "",
Controller: &truth,
BlockOwnerDeletion: &truth,
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(false),
},
},
},
Expand All @@ -244,8 +243,8 @@ var _ = Describe("StatefulSet", func() {
Kind: "RabbitmqCluster",
Name: instance.Name,
UID: "",
Controller: &truth,
BlockOwnerDeletion: &truth,
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(false),
},
},
},
Expand Down

0 comments on commit a235ccd

Please sign in to comment.