Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
operator-sdk/example/kb-memcached-operator/memcached_controller.go.tmpl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
182 lines (162 sloc)
5.94 KB
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
/* | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package controllers | |
import ( | |
"context" | |
"reflect" | |
"github.com/go-logr/logr" | |
appsv1 "k8s.io/api/apps/v1" | |
corev1 "k8s.io/api/core/v1" | |
"k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/apimachinery/pkg/types" | |
ctrl "sigs.k8s.io/controller-runtime" | |
"sigs.k8s.io/controller-runtime/pkg/client" | |
cachev1alpha1 "github.com/example-inc/memcached-operator/api/v1alpha1" | |
) | |
// MemcachedReconciler reconciles a Memcached object | |
type MemcachedReconciler struct { | |
client.Client | |
Log logr.Logger | |
Scheme *runtime.Scheme | |
} | |
// +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=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete | |
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list; | |
func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | |
ctx := context.Background() | |
log := r.Log.WithValues("memcached", req.NamespacedName) | |
// Fetch the Memcached instance | |
memcached := &cachev1alpha1.Memcached{} | |
err := r.Get(ctx, req.NamespacedName, memcached) | |
if err != nil { | |
if errors.IsNotFound(err) { | |
// Request object not found, could have been deleted after reconcile request. | |
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. | |
// Return and don't requeue | |
log.Info("Memcached resource not found. Ignoring since object must be deleted") | |
return ctrl.Result{}, nil | |
} | |
// Error reading the object - requeue the request. | |
log.Error(err, "Failed to get Memcached") | |
return ctrl.Result{}, err | |
} | |
// 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("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name) | |
err = r.Create(ctx, dep) | |
if err != nil { | |
log.Error(err, "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, "Failed to get Deployment") | |
return ctrl.Result{}, err | |
} | |
// Ensure the deployment size is the same as the spec | |
size := memcached.Spec.Size | |
if *found.Spec.Replicas != size { | |
found.Spec.Replicas = &size | |
err = r.Update(ctx, found) | |
if err != nil { | |
log.Error(err, "Failed to update Deployment", "Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name) | |
return ctrl.Result{}, err | |
} | |
// Spec updated - return and requeue | |
return ctrl.Result{Requeue: true}, nil | |
} | |
// Update the Memcached status with the pod names | |
// List the pods for this memcached's deployment | |
podList := &corev1.PodList{} | |
listOpts := []client.ListOption{ | |
client.InNamespace(memcached.Namespace), | |
client.MatchingLabels(labelsForMemcached(memcached.Name)), | |
} | |
if err = r.List(ctx, podList, listOpts...); err != nil { | |
log.Error(err, "Failed to list pods", "Memcached.Namespace", memcached.Namespace, "Memcached.Name", memcached.Name) | |
return ctrl.Result{}, err | |
} | |
podNames := getPodNames(podList.Items) | |
// Update status.Nodes if needed | |
if !reflect.DeepEqual(podNames, memcached.Status.Nodes) { | |
memcached.Status.Nodes = podNames | |
err := r.Status().Update(ctx, memcached) | |
if err != nil { | |
log.Error(err, "Failed to update Memcached status") | |
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} | |
} | |
// getPodNames returns the pod names of the array of pods passed in | |
func getPodNames(pods []corev1.Pod) []string { | |
var podNames []string | |
for _, pod := range pods { | |
podNames = append(podNames, pod.Name) | |
} | |
return podNames | |
} | |
func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
return ctrl.NewControllerManagedBy(mgr). | |
For(&cachev1alpha1.Memcached{}). | |
Owns(&appsv1.Deployment{}). | |
Complete(r) | |
} |