Created
November 11, 2021 22:40
-
-
Save nakamasato/66a5e6baa33e1f46d093cee587e25a20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import ( | |
appsv1 "k8s.io/api/apps/v1" // added | |
corev1 "k8s.io/api/core/v1" // added | |
"k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // added | |
"k8s.io/apimachinery/pkg/types" // added | |
"context" | |
... | |
) | |
... | |
//+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds,verbs=get;list;watch;create;update;patch;delete | |
//+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds/status,verbs=get;update;patch | |
//+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds/finalizers,verbs=update | |
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete // added | |
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list; // added | |
... | |
func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | |
log := r.Log.WithValues("memcached", req.NamespacedName) | |
// 1. Fetch the Memcached instance | |
// ...already implemented in the previous section | |
// 2. Check if the deployment already exists, if not create a new one | |
found := &appsv1.Deployment{} | |
err = r.Get(ctx, types.NamespacedName{Name: memcached.Name, Namespace: memcached.Namespace}, found) | |
if err != nil && errors.IsNotFound(err) { | |
// Define a new deployment | |
dep := r.deploymentForMemcached(memcached) | |
log.Info("2. Check if the deployment already exists, if not create a new one. Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name) | |
err = r.Create(ctx, dep) | |
if err != nil { | |
log.Error(err, "2. Check if the deployment already exists, if not create a new one. Failed to create new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name) | |
return ctrl.Result{}, err | |
} | |
// Deployment created successfully - return and requeue | |
return ctrl.Result{Requeue: true}, nil | |
} else if err != nil { | |
log.Error(err, "2. Check if the deployment already exists, if not create a new one. Failed to get Deployment") | |
return ctrl.Result{}, err | |
} | |
return ctrl.Result{}, nil | |
} | |
// deploymentForMemcached returns a memcached Deployment object | |
func (r *MemcachedReconciler) deploymentForMemcached(m *cachev1alpha1.Memcached) *appsv1.Deployment { | |
ls := labelsForMemcached(m.Name) | |
replicas := m.Spec.Size | |
dep := &appsv1.Deployment{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: m.Name, | |
Namespace: m.Namespace, | |
}, | |
Spec: appsv1.DeploymentSpec{ | |
Replicas: &replicas, | |
Selector: &metav1.LabelSelector{ | |
MatchLabels: ls, | |
}, | |
Template: corev1.PodTemplateSpec{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Labels: ls, | |
}, | |
Spec: corev1.PodSpec{ | |
Containers: []corev1.Container{{ | |
Image: "memcached:1.4.36-alpine", | |
Name: "memcached", | |
Command: []string{"memcached", "-m=64", "-o", "modern", "-v"}, | |
Ports: []corev1.ContainerPort{{ | |
ContainerPort: 11211, | |
Name: "memcached", | |
}}, | |
}}, | |
}, | |
}, | |
}, | |
} | |
// Set Memcached instance as the owner and controller | |
ctrl.SetControllerReference(m, dep, r.Scheme) | |
return dep | |
} | |
// labelsForMemcached returns the labels for selecting the resources | |
// belonging to the given memcached CR name. | |
func labelsForMemcached(name string) map[string]string { | |
return map[string]string{"app": "memcached", "memcached_cr": name} | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment