Skip to content

Commit

Permalink
Use client.Object instead of runtime.Object when necessary
Browse files Browse the repository at this point in the history
- controller runtime helper functions use client.Object
in version 0.7
  • Loading branch information
ChunyiLyu committed Jan 5, 2021
1 parent 2f1c9fd commit 15d36f2
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (r *RabbitmqClusterReconciler) getRabbitmqCluster(ctx context.Context, name
}

func (r *RabbitmqClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
for _, resource := range []runtime.Object{&appsv1.StatefulSet{}, &corev1.ConfigMap{}, &corev1.Service{}} {
for _, resource := range []client.Object{&appsv1.StatefulSet{}, &corev1.ConfigMap{}, &corev1.Service{}} {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), resource, ownerKey, addResourceToIndex); err != nil {
return err
}
Expand All @@ -323,7 +323,7 @@ func (r *RabbitmqClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func addResourceToIndex(rawObj runtime.Object) []string {
func addResourceToIndex(rawObj client.Object) []string {
switch resourceObject := rawObj.(type) {
case *appsv1.StatefulSet:
owner := metav1.GetControllerOf(resourceObject)
Expand Down
3 changes: 1 addition & 2 deletions controllers/reconcile_rabbitmq_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientretry "k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -28,7 +27,7 @@ const (
// These annotations are temporary markers used in later reconcile loops to perform some action (such as restarting the StatefulSet or executing RabbitMQ CLI commands)
func (r *RabbitmqClusterReconciler) annotateIfNeeded(ctx context.Context, builder resource.ResourceBuilder, operationResult controllerutil.OperationResult, rmq *rabbitmqv1beta1.RabbitmqCluster) error {
var (
obj runtime.Object
obj client.Object
objName string
annotationKey string
)
Expand Down
2 changes: 1 addition & 1 deletion controllers/reconcile_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = Describe("Reconcile TLS", func() {

sts, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())

Expect(sts.Spec.Template.Spec.Volumes).To(ContainElement(corev1.Volume{
Name: "rabbitmq-tls",
VolumeSource: corev1.VolumeSource{
Expand Down
6 changes: 3 additions & 3 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package controllers

import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
)
Expand All @@ -17,7 +17,7 @@ func (r *RabbitmqClusterReconciler) exec(namespace, podName, containerName strin
return r.PodExecutor.Exec(r.Clientset, r.ClusterConfig, namespace, podName, containerName, command...)
}

func (r *RabbitmqClusterReconciler) deleteAnnotation(ctx context.Context, obj runtime.Object, annotation string) error {
func (r *RabbitmqClusterReconciler) deleteAnnotation(ctx context.Context, obj client.Object, annotation string) error {
accessor, err := meta.Accessor(obj)
if err != nil {
return err
Expand All @@ -31,7 +31,7 @@ func (r *RabbitmqClusterReconciler) deleteAnnotation(ctx context.Context, obj ru
return r.Update(ctx, obj)
}

func (r *RabbitmqClusterReconciler) updateAnnotation(ctx context.Context, obj runtime.Object, namespace, objName, key, value string) error {
func (r *RabbitmqClusterReconciler) updateAnnotation(ctx context.Context, obj client.Object, namespace, objName, key, value string) error {
return retry.OnError(
retry.DefaultRetry,
errorIsConflictOrNotFound, // StatefulSet needs time to be found after it got created
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package resource
import (
"bytes"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -64,7 +65,7 @@ func (builder *RabbitmqResourceBuilder) ServerConfigMap() *ServerConfigMapBuilde
}
}

func (builder *ServerConfigMapBuilder) Build() (runtime.Object, error) {
func (builder *ServerConfigMapBuilder) Build() (client.Object, error) {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: builder.Instance.ChildResourceName(ServerConfigMapName),
Expand All @@ -75,7 +76,7 @@ func (builder *ServerConfigMapBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *ServerConfigMapBuilder) Update(object runtime.Object) error {
func (builder *ServerConfigMapBuilder) Update(object client.Object) error {
configMap := object.(*corev1.ConfigMap)

ini.PrettySection = false // Remove trailing new line because rabbitmq.conf has only a default section.
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/default_user_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package resource
import (
"bytes"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -39,7 +40,7 @@ func (builder *RabbitmqResourceBuilder) DefaultUserSecret() *DefaultUserSecretBu
}
}

func (builder *DefaultUserSecretBuilder) Build() (runtime.Object, error) {
func (builder *DefaultUserSecretBuilder) Build() (client.Object, error) {
username, err := randomEncodedString(24)
if err != nil {
return nil, err
Expand Down Expand Up @@ -69,7 +70,7 @@ func (builder *DefaultUserSecretBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *DefaultUserSecretBuilder) Update(object runtime.Object) error {
func (builder *DefaultUserSecretBuilder) Update(object client.Object) error {
secret := object.(*corev1.Secret)
secret.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
secret.Annotations = metadata.ReconcileAndFilterAnnotations(secret.GetAnnotations(), builder.Instance.Annotations)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/erlang_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
Expand All @@ -38,7 +39,7 @@ func (builder *RabbitmqResourceBuilder) ErlangCookie() *ErlangCookieBuilder {
}
}

func (builder *ErlangCookieBuilder) Build() (runtime.Object, error) {
func (builder *ErlangCookieBuilder) Build() (client.Object, error) {
cookie, err := randomEncodedString(24)
if err != nil {
return nil, err
Expand All @@ -56,7 +57,7 @@ func (builder *ErlangCookieBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *ErlangCookieBuilder) Update(object runtime.Object) error {
func (builder *ErlangCookieBuilder) Update(object client.Object) error {
secret := object.(*corev1.Secret)
secret.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
secret.Annotations = metadata.ReconcileAndFilterAnnotations(secret.GetAnnotations(), builder.Instance.Annotations)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/headless_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package resource

import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/metadata"
Expand All @@ -37,7 +38,7 @@ func (builder *RabbitmqResourceBuilder) HeadlessService() *HeadlessServiceBuilde
}
}

func (builder *HeadlessServiceBuilder) Build() (runtime.Object, error) {
func (builder *HeadlessServiceBuilder) Build() (client.Object, error) {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: builder.Instance.ChildResourceName(headlessServiceSuffix),
Expand All @@ -46,7 +47,7 @@ func (builder *HeadlessServiceBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *HeadlessServiceBuilder) Update(object runtime.Object) error {
func (builder *HeadlessServiceBuilder) Update(object client.Object) error {
service := object.(*corev1.Service)
service.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
service.Annotations = metadata.ReconcileAndFilterAnnotations(service.GetAnnotations(), builder.Instance.Annotations)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/rabbitmq_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resource

import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -33,7 +34,7 @@ func (builder *RabbitmqResourceBuilder) RabbitmqPluginsConfigMap() *RabbitmqPlug
}
}

func (builder *RabbitmqPluginsConfigMapBuilder) Build() (runtime.Object, error) {
func (builder *RabbitmqPluginsConfigMapBuilder) Build() (client.Object, error) {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: builder.Instance.ChildResourceName(PluginsConfigName),
Expand All @@ -47,7 +48,7 @@ func (builder *RabbitmqPluginsConfigMapBuilder) Build() (runtime.Object, error)
}, nil
}

func (builder *RabbitmqPluginsConfigMapBuilder) Update(object runtime.Object) error {
func (builder *RabbitmqPluginsConfigMapBuilder) Update(object client.Object) error {
configMap := object.(*corev1.ConfigMap)

if configMap.Data == nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/rabbitmq_resource_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package resource
import (
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type RabbitmqResourceBuilder struct {
Expand All @@ -20,8 +21,8 @@ type RabbitmqResourceBuilder struct {
}

type ResourceBuilder interface {
Build() (runtime.Object, error)
Update(runtime.Object) error
Build() (client.Object, error)
Update(client.Object) error
}

func (builder *RabbitmqResourceBuilder) ResourceBuilders() ([]ResourceBuilder, error) {
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package resource

import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/metadata"
Expand All @@ -36,7 +37,7 @@ func (builder *RabbitmqResourceBuilder) Role() *RoleBuilder {
}
}

func (builder *RoleBuilder) Build() (runtime.Object, error) {
func (builder *RoleBuilder) Build() (client.Object, error) {
return &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Namespace: builder.Instance.Namespace,
Expand All @@ -45,7 +46,7 @@ func (builder *RoleBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *RoleBuilder) Update(object runtime.Object) error {
func (builder *RoleBuilder) Update(object client.Object) error {
role := object.(*rbacv1.Role)
role.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
role.Annotations = metadata.ReconcileAndFilterAnnotations(role.GetAnnotations(), builder.Instance.Annotations)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
Expand All @@ -36,7 +37,7 @@ func (builder *RabbitmqResourceBuilder) RoleBinding() *RoleBindingBuilder {
}
}

func (builder *RoleBindingBuilder) Update(object runtime.Object) error {
func (builder *RoleBindingBuilder) Update(object client.Object) error {
roleBinding := object.(*rbacv1.RoleBinding)
roleBinding.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
roleBinding.Annotations = metadata.ReconcileAndFilterAnnotations(roleBinding.GetAnnotations(), builder.Instance.Annotations)
Expand All @@ -58,7 +59,7 @@ func (builder *RoleBindingBuilder) Update(object runtime.Object) error {
return nil
}

func (builder *RoleBindingBuilder) Build() (runtime.Object, error) {
func (builder *RoleBindingBuilder) Build() (client.Object, error) {
return &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: builder.Instance.Namespace,
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package resource
import (
"encoding/json"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/strategicpatch"
Expand Down Expand Up @@ -40,7 +41,7 @@ func (builder *RabbitmqResourceBuilder) Service() *ServiceBuilder {
}
}

func (builder *ServiceBuilder) Build() (runtime.Object, error) {
func (builder *ServiceBuilder) Build() (client.Object, error) {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: builder.Instance.ChildResourceName(ServiceSuffix),
Expand All @@ -49,7 +50,7 @@ func (builder *ServiceBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *ServiceBuilder) Update(object runtime.Object) error {
func (builder *ServiceBuilder) Update(object client.Object) error {
service := object.(*corev1.Service)
builder.setAnnotations(service)
service.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package resource

import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/metadata"
Expand All @@ -36,7 +37,7 @@ func (builder *RabbitmqResourceBuilder) ServiceAccount() *ServiceAccountBuilder
}
}

func (builder *ServiceAccountBuilder) Build() (runtime.Object, error) {
func (builder *ServiceAccountBuilder) Build() (client.Object, error) {
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: builder.Instance.Namespace,
Expand All @@ -45,7 +46,7 @@ func (builder *ServiceAccountBuilder) Build() (runtime.Object, error) {
}, nil
}

func (builder *ServiceAccountBuilder) Update(object runtime.Object) error {
func (builder *ServiceAccountBuilder) Update(object client.Object) error {
serviceAccount := object.(*corev1.ServiceAccount)
serviceAccount.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
serviceAccount.Annotations = metadata.ReconcileAndFilterAnnotations(serviceAccount.GetAnnotations(), builder.Instance.Annotations)
Expand Down
5 changes: 3 additions & 2 deletions internal/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package resource
import (
"encoding/json"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"

"k8s.io/apimachinery/pkg/util/intstr"

Expand Down Expand Up @@ -46,7 +47,7 @@ func (builder *RabbitmqResourceBuilder) StatefulSet() *StatefulSetBuilder {
}
}

func (builder *StatefulSetBuilder) Build() (runtime.Object, error) {
func (builder *StatefulSetBuilder) Build() (client.Object, error) {
// PVC, ServiceName & Selector: can't be updated without deleting the statefulset
pvc, err := persistentVolumeClaim(builder.Instance, builder.Scheme)
if err != nil {
Expand Down Expand Up @@ -98,7 +99,7 @@ func (builder *StatefulSetBuilder) Build() (runtime.Object, error) {
return sts, nil
}

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

//Replicas
Expand Down
1 change: 0 additions & 1 deletion system_tests/system_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ CONSOLE_LOG=new`
return actualCert
}, 180, 10).ShouldNot(Equal(oldServerCert))


Eventually(func() []byte {
newServerCertificate := inspectServerCertificate(username, password, hostname, amqpsNodePort, caFilePath)
return newServerCertificate
Expand Down

0 comments on commit 15d36f2

Please sign in to comment.