@@ -2,39 +2,49 @@ package perconaservermongodb
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
6
- psmdbv1alpha1 "github.com/Percona-Lab/percona-server-mongodb-operator/pkg/apis/psmdb/v1alpha1"
7
- corev1 "k8s.io/api/core/v1"
8
7
"k8s.io/apimachinery/pkg/api/errors"
9
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
8
"k8s.io/apimachinery/pkg/runtime"
11
- "k8s.io/apimachinery/pkg/types"
12
9
"sigs.k8s.io/controller-runtime/pkg/client"
13
10
"sigs.k8s.io/controller-runtime/pkg/controller"
14
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
15
11
"sigs.k8s.io/controller-runtime/pkg/handler"
16
12
"sigs.k8s.io/controller-runtime/pkg/manager"
17
13
"sigs.k8s.io/controller-runtime/pkg/reconcile"
18
14
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
19
15
"sigs.k8s.io/controller-runtime/pkg/source"
20
- )
21
16
22
- var log = logf .Log .WithName ("controller_perconaservermongodb" )
17
+ api "github.com/Percona-Lab/percona-server-mongodb-operator/pkg/apis/psmdb/v1alpha1"
18
+ "github.com/Percona-Lab/percona-server-mongodb-operator/version"
19
+ )
23
20
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
- */
21
+ var log = logf .Log .WithName ("controller_psmdb" )
28
22
29
23
// Add creates a new PerconaServerMongoDB Controller and adds it to the Manager. The Manager will set fields on the Controller
30
24
// and Start it when the Manager is Started.
31
25
func Add (mgr manager.Manager ) error {
32
- return add (mgr , newReconciler (mgr ))
26
+ r , err := newReconciler (mgr )
27
+ if err != nil {
28
+ return err
29
+ }
30
+
31
+ return add (mgr , r )
33
32
}
34
33
35
34
// newReconciler returns a new reconcile.Reconciler
36
- func newReconciler (mgr manager.Manager ) reconcile.Reconciler {
37
- return & ReconcilePerconaServerMongoDB {client : mgr .GetClient (), scheme : mgr .GetScheme ()}
35
+ func newReconciler (mgr manager.Manager ) (reconcile.Reconciler , error ) {
36
+ sv , err := version .Server ()
37
+ if err != nil {
38
+ return nil , fmt .Errorf ("get server version: %v" , err )
39
+ }
40
+
41
+ log .Info ("server version" , "platform" , sv .Platform , "version" , sv .Info )
42
+
43
+ return & ReconcilePerconaServerMongoDB {
44
+ client : mgr .GetClient (),
45
+ scheme : mgr .GetScheme (),
46
+ serverVersion : sv ,
47
+ }, nil
38
48
}
39
49
40
50
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -46,17 +56,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
46
56
}
47
57
48
58
// Watch for changes to primary resource PerconaServerMongoDB
49
- err = c .Watch (& source.Kind {Type : & psmdbv1alpha1.PerconaServerMongoDB {}}, & 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 PerconaServerMongoDB
56
- err = c .Watch (& source.Kind {Type : & corev1.Pod {}}, & handler.EnqueueRequestForOwner {
57
- IsController : true ,
58
- OwnerType : & psmdbv1alpha1.PerconaServerMongoDB {},
59
- })
59
+ err = c .Watch (& source.Kind {Type : & api.PerconaServerMongoDB {}}, & handler.EnqueueRequestForObject {})
60
60
if err != nil {
61
61
return err
62
62
}
@@ -72,12 +72,12 @@ type ReconcilePerconaServerMongoDB struct {
72
72
// that reads objects from the cache and writes to the apiserver
73
73
client client.Client
74
74
scheme * runtime.Scheme
75
+
76
+ serverVersion * version.ServerVersion
75
77
}
76
78
77
79
// Reconcile reads that state of the cluster for a PerconaServerMongoDB object and makes changes based on the state read
78
80
// and what is in the PerconaServerMongoDB.Spec
79
- // TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates
80
- // a Pod as an example
81
81
// Note:
82
82
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
83
83
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
@@ -86,8 +86,8 @@ func (r *ReconcilePerconaServerMongoDB) Reconcile(request reconcile.Request) (re
86
86
reqLogger .Info ("Reconciling PerconaServerMongoDB" )
87
87
88
88
// Fetch the PerconaServerMongoDB instance
89
- instance := & psmdbv1alpha1 .PerconaServerMongoDB {}
90
- err := r .client .Get (context .TODO (), request .NamespacedName , instance )
89
+ cr := & api .PerconaServerMongoDB {}
90
+ err := r .client .Get (context .TODO (), request .NamespacedName , cr )
91
91
if err != nil {
92
92
if errors .IsNotFound (err ) {
93
93
// Request object not found, could have been deleted after reconcile request.
@@ -99,54 +99,31 @@ func (r *ReconcilePerconaServerMongoDB) Reconcile(request reconcile.Request) (re
99
99
return reconcile.Result {}, err
100
100
}
101
101
102
- // Define a new Pod object
103
- pod := newPodForCR (instance )
104
-
105
- // Set PerconaServerMongoDB instance as the owner and controller
106
- if err := controllerutil .SetControllerReference (instance , pod , r .scheme ); err != nil {
107
- return reconcile.Result {}, err
108
- }
109
-
110
- // Check if this Pod already exists
111
- found := & corev1.Pod {}
112
- err = r .client .Get (context .TODO (), types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, found )
113
- if err != nil && errors .IsNotFound (err ) {
114
- reqLogger .Info ("Creating a new Pod" , "Pod.Namespace" , pod .Namespace , "Pod.Name" , pod .Name )
115
- err = r .client .Create (context .TODO (), pod )
116
- if err != nil {
117
- return reconcile.Result {}, err
118
- }
119
-
120
- // Pod created successfully - don't requeue
121
- return reconcile.Result {}, nil
122
- } else if err != nil {
123
- return reconcile.Result {}, err
124
- }
102
+ // // Define a new Pod object
103
+ // pod := newPodForCR(instance)
104
+
105
+ // // Set PerconaServerMongoDB instance as the owner and controller
106
+ // if err := controllerutil.SetControllerReference(instance, pod, r.scheme); err != nil {
107
+ // return reconcile.Result{}, err
108
+ // }
109
+
110
+ // // Check if this Pod already exists
111
+ // found := &corev1.Pod{}
112
+ // err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found)
113
+ // if err != nil && errors.IsNotFound(err) {
114
+ // reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
115
+ // err = r.client.Create(context.TODO(), pod)
116
+ // if err != nil {
117
+ // return reconcile.Result{}, err
118
+ // }
119
+
120
+ // // Pod created successfully - don't requeue
121
+ // return reconcile.Result{}, nil
122
+ // } else if err != nil {
123
+ // return reconcile.Result{}, err
124
+ // }
125
125
126
126
// Pod already exists - don't requeue
127
- reqLogger .Info ("Skip reconcile: Pod already exists" , "Pod.Namespace" , found .Namespace , "Pod.Name" , found .Name )
127
+ // reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
128
128
return reconcile.Result {}, nil
129
129
}
130
-
131
- // newPodForCR returns a busybox pod with the same name/namespace as the cr
132
- func newPodForCR (cr * psmdbv1alpha1.PerconaServerMongoDB ) * corev1.Pod {
133
- labels := map [string ]string {
134
- "app" : cr .Name ,
135
- }
136
- return & corev1.Pod {
137
- ObjectMeta : metav1.ObjectMeta {
138
- Name : cr .Name + "-pod" ,
139
- Namespace : cr .Namespace ,
140
- Labels : labels ,
141
- },
142
- Spec : corev1.PodSpec {
143
- Containers : []corev1.Container {
144
- {
145
- Name : "busybox" ,
146
- Image : "busybox" ,
147
- Command : []string {"sleep" , "3600" },
148
- },
149
- },
150
- },
151
- }
152
- }
0 commit comments