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

Enable bootstrapping statefulsets #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
74 changes: 72 additions & 2 deletions internal/controller/etcdcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ package controller
import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

operatorv1alpha1 "go.etcd.io/etcd-operator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EtcdClusterReconciler reconciles a EtcdCluster object
Expand All @@ -47,13 +52,78 @@ type EtcdClusterReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
log := log.FromContext(ctx)

// TODO(user): your logic here
// Fetch the etcd cluster resource
var etcdCluster operatorv1alpha1.EtcdCluster
if err := r.Get(ctx, req.NamespacedName, &etcdCluster); err != nil {
log.Error(err, "unable to fetch etcd cluster")
return ctrl.Result{}, nil
}
// Check if StatefulSet already exists
var sts appsv1.StatefulSet
err := r.Get(ctx, req.NamespacedName, &sts)
// If it doesn't exist and Size is specified > 0 then create it with 0 replicas
// End Reconcile loop otherwise
if apierrors.IsNotFound(err) {
if etcdCluster.Spec.Size > 0 {
log.Info("creating statefulset with replica = 0")
if err := r.CreateStatefulset(ctx, &etcdCluster); err != nil {
log.Error(err, "failed to create StatefulSet")
}
}
return ctrl.Result{}, nil
} else if err != nil {
log.Error(err, "failed to create StatefulSet")
return ctrl.Result{}, err
}
log.Info("StatefulSet exists")

return ctrl.Result{}, nil
}

// Create Statefulset
func (r *EtcdClusterReconciler) CreateStatefulset(ctx context.Context, etcdCluster *operatorv1alpha1.EtcdCluster) error {
sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: etcdCluster.ObjectMeta.Name,
Namespace: etcdCluster.ObjectMeta.Namespace,
},
Spec: appsv1.StatefulSetSpec{
Replicas: ptr.To(int32(0)),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": etcdCluster.ObjectMeta.Name,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": etcdCluster.ObjectMeta.Name,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "etcd",
Image: "quay.io/coreos/etcd:v2.3.8",
Ports: []corev1.ContainerPort{
{ContainerPort: 2379},
{ContainerPort: 2380},
},
},
},
},
},
},
}
// Set EtcdCluster as the owner and controller
if err := ctrl.SetControllerReference(etcdCluster, sts, r.Scheme); err != nil {
return err
}
return r.Create(ctx, sts)
}

// SetupWithManager sets up the controller with the Manager.
func (r *EtcdClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down