Skip to content

Commit e76823f

Browse files
committed
storage-operator: generate Volume controller
``` $ operator-sdk add controller --api-version storage.metalk8s.scality.com/v1alpha1 --kind Volume $ go mod vendor ```
1 parent 22933ac commit e76823f

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

storage-operator/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
go.uber.org/multierr v1.1.0 // indirect
2929
go.uber.org/zap v1.9.1 // indirect
3030
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
31+
k8s.io/api v0.0.0-20190222213804-5cb15d344471
3132
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628
3233
k8s.io/client-go v2.0.0-alpha.0.0.20181126152608-d082d5923d3c+incompatible
3334
k8s.io/code-generator v0.0.0-20180823001027-3dcf91f64f63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package controller
2+
3+
import (
4+
"github.com/scality/metalk8s/storage-operator/pkg/controller/volume"
5+
)
6+
7+
func init() {
8+
// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
9+
AddToManagerFuncs = append(AddToManagerFuncs, volume.Add)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package volume
2+
3+
import (
4+
"context"
5+
6+
storagev1alpha1 "github.com/scality/metalk8s/storage-operator/pkg/apis/storage/v1alpha1"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/api/errors"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/types"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
"sigs.k8s.io/controller-runtime/pkg/controller"
14+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
15+
"sigs.k8s.io/controller-runtime/pkg/handler"
16+
"sigs.k8s.io/controller-runtime/pkg/manager"
17+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
18+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
19+
"sigs.k8s.io/controller-runtime/pkg/source"
20+
)
21+
22+
var log = logf.Log.WithName("controller_volume")
23+
24+
/**
25+
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
26+
* business logic. Delete these comments after modifying this file.*
27+
*/
28+
29+
// Add creates a new Volume Controller and adds it to the Manager. The Manager will set fields on the Controller
30+
// and Start it when the Manager is Started.
31+
func Add(mgr manager.Manager) error {
32+
return add(mgr, newReconciler(mgr))
33+
}
34+
35+
// newReconciler returns a new reconcile.Reconciler
36+
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
37+
return &ReconcileVolume{client: mgr.GetClient(), scheme: mgr.GetScheme()}
38+
}
39+
40+
// add adds a new Controller to mgr with r as the reconcile.Reconciler
41+
func add(mgr manager.Manager, r reconcile.Reconciler) error {
42+
// Create a new controller
43+
c, err := controller.New("volume-controller", mgr, controller.Options{Reconciler: r})
44+
if err != nil {
45+
return err
46+
}
47+
48+
// Watch for changes to primary resource Volume
49+
err = c.Watch(&source.Kind{Type: &storagev1alpha1.Volume{}}, &handler.EnqueueRequestForObject{})
50+
if err != nil {
51+
return err
52+
}
53+
54+
// TODO(user): Modify this to be the types you create that are owned by the primary resource
55+
// Watch for changes to secondary resource Pods and requeue the owner Volume
56+
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
57+
IsController: true,
58+
OwnerType: &storagev1alpha1.Volume{},
59+
})
60+
if err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
67+
// blank assignment to verify that ReconcileVolume implements reconcile.Reconciler
68+
var _ reconcile.Reconciler = &ReconcileVolume{}
69+
70+
// ReconcileVolume reconciles a Volume object
71+
type ReconcileVolume struct {
72+
// This client, initialized using mgr.Client() above, is a split client
73+
// that reads objects from the cache and writes to the apiserver
74+
client client.Client
75+
scheme *runtime.Scheme
76+
}
77+
78+
// Reconcile reads that state of the cluster for a Volume object and makes changes based on the state read
79+
// and what is in the Volume.Spec
80+
// TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates
81+
// a Pod as an example
82+
// Note:
83+
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
84+
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
85+
func (r *ReconcileVolume) Reconcile(request reconcile.Request) (reconcile.Result, error) {
86+
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
87+
reqLogger.Info("Reconciling Volume")
88+
89+
// Fetch the Volume instance
90+
instance := &storagev1alpha1.Volume{}
91+
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
92+
if err != nil {
93+
if errors.IsNotFound(err) {
94+
// Request object not found, could have been deleted after reconcile request.
95+
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
96+
// Return and don't requeue
97+
return reconcile.Result{}, nil
98+
}
99+
// Error reading the object - requeue the request.
100+
return reconcile.Result{}, err
101+
}
102+
103+
// Define a new Pod object
104+
pod := newPodForCR(instance)
105+
106+
// Set Volume instance as the owner and controller
107+
if err := controllerutil.SetControllerReference(instance, pod, r.scheme); err != nil {
108+
return reconcile.Result{}, err
109+
}
110+
111+
// Check if this Pod already exists
112+
found := &corev1.Pod{}
113+
err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found)
114+
if err != nil && errors.IsNotFound(err) {
115+
reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
116+
err = r.client.Create(context.TODO(), pod)
117+
if err != nil {
118+
return reconcile.Result{}, err
119+
}
120+
121+
// Pod created successfully - don't requeue
122+
return reconcile.Result{}, nil
123+
} else if err != nil {
124+
return reconcile.Result{}, err
125+
}
126+
127+
// Pod already exists - don't requeue
128+
reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
129+
return reconcile.Result{}, nil
130+
}
131+
132+
// newPodForCR returns a busybox pod with the same name/namespace as the cr
133+
func newPodForCR(cr *storagev1alpha1.Volume) *corev1.Pod {
134+
labels := map[string]string{
135+
"app": cr.Name,
136+
}
137+
return &corev1.Pod{
138+
ObjectMeta: metav1.ObjectMeta{
139+
Name: cr.Name + "-pod",
140+
Namespace: cr.Namespace,
141+
Labels: labels,
142+
},
143+
Spec: corev1.PodSpec{
144+
Containers: []corev1.Container{
145+
{
146+
Name: "busybox",
147+
Image: "busybox",
148+
Command: []string{"sleep", "3600"},
149+
},
150+
},
151+
},
152+
}
153+
}

0 commit comments

Comments
 (0)