func (r *ReconcileRethinkDBCluster) reconcileAdminSecret(cr *rethinkdbv1alpha1.RethinkDBCluster) error {
	name := fmt.Sprintf("%s-%s", cr.ObjectMeta.Name, RethinkDBAdminKey)
	found := &corev1.Secret{}
	err := r.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: cr.Namespace}, found)
	if err != nil && errors.IsNotFound(err) {
		log.Info("creating new secret", "secret", name)
		secret, err := newUserSecret(cr, RethinkDBAdminKey)
		if err != nil {
			return err
		}
		if err = controllerutil.SetControllerReference(cr, secret, r.scheme); err != nil {
			return err
		}
		return r.client.Create(context.TODO(), secret)
	} else if err != nil {
		return err
	}
	log.Info("secret exists", "secret", found.Name)
	return nil
}
reconcileAdminSecret ensures the cluster admin user credentials are present.
package rethinkdbcluster
import (
	"context"
	"fmt"
	rethinkdbv1alpha1 "github.com/jmckind/rethinkdb-operator/pkg/apis/rethinkdb/v1alpha1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/rest"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_rethinkdbcluster")
func Add(mgr manager.Manager) error {
	return add(mgr, newReconciler(mgr))
}
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
	return &ReconcileRethinkDBCluster{client: mgr.GetClient(), config: mgr.GetConfig(), scheme: mgr.GetScheme()}
}
func add(mgr manager.Manager, r reconcile.Reconciler) error {
	c, err := controller.New("rethinkdbcluster-controller", mgr, controller.Options{Reconciler: r})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &rethinkdbv1alpha1.RethinkDBCluster{}}, &handler.EnqueueRequestForObject{})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForOwner{
		IsController: true,
		OwnerType:    &rethinkdbv1alpha1.RethinkDBCluster{},
	})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &corev1.PersistentVolumeClaim{}}, &handler.EnqueueRequestForOwner{
		IsController: true,
		OwnerType:    &rethinkdbv1alpha1.RethinkDBCluster{},
	})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
		IsController: true,
		OwnerType:    &rethinkdbv1alpha1.RethinkDBCluster{},
	})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{
		IsController: true,
		OwnerType:    &rethinkdbv1alpha1.RethinkDBCluster{},
	})
	if err != nil {
		return err
	}
	err = c.Watch(&source.Kind{Type: &corev1.Service{}}, &handler.EnqueueRequestForOwner{
		IsController: true,
		OwnerType:    &rethinkdbv1alpha1.RethinkDBCluster{},
	})
	if err != nil {
		return err
	}
	return nil
}
var _ reconcile.Reconciler = &ReconcileRethinkDBCluster{}
type ReconcileRethinkDBCluster struct {
	client client.Client
	config *rest.Config
	scheme *runtime.Scheme
}
func (r *ReconcileRethinkDBCluster) Reconcile(request reconcile.Request) (reconcile.Result, error) {
	reqLogger := log.WithValues("namespace", request.Namespace, "name", request.Name)
	reqLogger.Info("reconciling RethinkDBCluster")
	cluster := &rethinkdbv1alpha1.RethinkDBCluster{}
	err := r.client.Get(context.TODO(), request.NamespacedName, cluster)
	if err != nil {
		if errors.IsNotFound(err) {
			return reconcile.Result{}, nil
		}
		return reconcile.Result{}, err
	}
	if setDefaults(cluster) {
		reqLogger.Info("default spec values initialized")
		return reconcile.Result{Requeue: true}, r.client.Update(context.TODO(), cluster)
	}
	caSecret, err := r.reconcileCASecret(cluster)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile ca secret")
		return reconcile.Result{}, err
	}
	err = r.reconcileCAConfigMap(cluster, caSecret)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile ca configmap")
		return reconcile.Result{}, err
	}
	err = r.reconcileAdminService(cluster)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile admin service")
		return reconcile.Result{}, err
	}
	err = r.reconcileDriverService(cluster)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile driver service")
		return reconcile.Result{}, err
	}
	err = r.reconcileTLSSecrets(cluster, caSecret)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile tls secrets")
		return reconcile.Result{}, err
	}
	err = r.reconcileAdminSecret(cluster)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile admin secret")
		return reconcile.Result{}, err
	}
	err = r.reconcileServerPods(cluster)
	if err != nil {
		reqLogger.Error(err, "unable to reconcile server pods")
		return reconcile.Result{}, err
	}
	return reconcile.Result{}, nil
}
func (r *ReconcileRethinkDBCluster) addPVC(cr *rethinkdbv1alpha1.RethinkDBCluster) error {
	log.Info("creating new persistent volume claim")
	pvc := newPVC(cr)
	if err := controllerutil.SetControllerReference(cr, pvc, r.scheme); err != nil {
		return err
	}
	return r.client.Create(context.TODO(), newPVC(cr))
}
func (r *ReconcileRethinkDBCluster) addServer(cr *rethinkdbv1alpha1.RethinkDBCluster, members []corev1.Pod) error {
	log.Info("creating new server pod")
	pod := newPod(cr, members)
	if err := controllerutil.SetControllerReference(cr, pod, r.scheme); err != nil {
		return err
	}
	err := r.client.Create(context.TODO(), pod)
	if err != nil {
		return err
	}
	cr.Status.Servers = append(cr.Status.Servers, pod.Name)
	return r.client.Status().Update(context.TODO(), cr)
}
func (r *ReconcileRethinkDBCluster) listPVCs(cr *rethinkdbv1alpha1.RethinkDBCluster) ([]corev1.PersistentVolumeClaim, error) {
	found := &corev1.PersistentVolumeClaimList{}
	labelSelector := labels.SelectorFromSet(labelsForCluster(cr))
	listOps := &client.ListOptions{Namespace: cr.Namespace, LabelSelector: labelSelector}
	err := r.client.List(context.TODO(), listOps, found)
	if err != nil {
		log.Error(err, "failed to list persistent volume claims")
		return nil, err
	}
	return found.Items, nil
}
func (r *ReconcileRethinkDBCluster) listServers(cr *rethinkdbv1alpha1.RethinkDBCluster) ([]corev1.Pod, error) {
	found := &corev1.PodList{}
	labelSelector := labels.SelectorFromSet(labelsForCluster(cr))
	listOps := &client.ListOptions{Namespace: cr.Namespace, LabelSelector: labelSelector}
	err := r.client.List(context.TODO(), listOps, found)
	if err != nil {
		log.Error(err, "failed to list server pods")
		return nil, err
	}
	return found.Items, nil
}