1
+ /*
2
+ Copyright 2018 The Kubernetes Authors.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ package main
18
+
19
+ import (
20
+ "context"
21
+ "github.com/go-logr/logr"
22
+ "k8s.io/apimachinery/pkg/runtime/schema"
23
+ "sigs.k8s.io/controller-runtime/pkg/handler"
24
+
25
+ appsv1 "k8s.io/api/apps/v1"
26
+ corev1 "k8s.io/api/core/v1"
27
+ "k8s.io/apimachinery/pkg/api/errors"
28
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/apimachinery/pkg/types"
30
+
31
+ "sigs.k8s.io/controller-runtime/pkg/client"
32
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
33
+ )
34
+
35
+ // reconcileReplicaSet reconciles ReplicaSets
36
+ type reconcileReplicaSet struct {
37
+ // client can be used to retrieve objects from the APIServer.
38
+ client client.Client
39
+ log logr.Logger
40
+ }
41
+
42
+ // Implement reconcile.Reconciler so the controller can reconcile objects
43
+ var _ reconcile.Reconciler = & reconcileReplicaSet {}
44
+
45
+ func (r * reconcileReplicaSet ) Reconcile (request reconcile.Request ) (reconcile.Result , error ) {
46
+ // set up a convenient log object so we don't have to type request over and over again
47
+ log := r .log .WithValues ("request" , request )
48
+
49
+ // Fetch the ReplicaSet from the cache
50
+ rs := & appsv1.ReplicaSet {}
51
+ err := r .client .Get (context .TODO (), request .NamespacedName , rs )
52
+ if errors .IsNotFound (err ) {
53
+ log .Error (nil , "Could not find ReplicaSet" )
54
+ return reconcile.Result {}, nil
55
+ }
56
+
57
+ if err != nil {
58
+ log .Error (err , "Could not fetch ReplicaSet" )
59
+ return reconcile.Result {}, err
60
+ }
61
+
62
+ // Print the ReplicaSet
63
+ log .Info ("Reconciling ReplicaSet" , "container name" , rs .Spec .Template .Spec .Containers [0 ].Name )
64
+
65
+ // Check if the Pod already exists, if not create a new one
66
+ podRs := & corev1.Pod {}
67
+ err = r .client .Get (context .TODO (), types.NamespacedName {Name : rs .Name , Namespace : rs .Namespace }, podRs )
68
+ if err != nil && errors .IsNotFound (err ) {
69
+ // Define a new Deployment
70
+ pod := r .podForReplicasetWithWatchAnnotations (rs )
71
+ err = r .client .Create (context .TODO (), pod )
72
+ if err != nil {
73
+ log .Error (err , "Failed to create new Pod." , "Pod.Namespace" , pod .Namespace , "Pod.Name" , pod .Name )
74
+ return reconcile.Result {}, err
75
+ }
76
+ return reconcile.Result {Requeue : true }, nil
77
+ } else if err != nil {
78
+ log .Error (err , "Failed to get Pod." )
79
+ return reconcile.Result {}, err
80
+ }
81
+
82
+ // Set the label if it is missing
83
+ if rs .Labels == nil {
84
+ rs .Labels = map [string ]string {}
85
+ }
86
+
87
+ if rs .Labels ["hello" ] == "world" {
88
+ return reconcile.Result {}, nil
89
+ }
90
+
91
+ // Update the ReplicaSet
92
+ rs .Labels ["hello" ] = "world"
93
+ err = r .client .Update (context .TODO (), rs )
94
+ if err != nil {
95
+ log .Error (err , "Could not write ReplicaSet" )
96
+ return reconcile.Result {}, err
97
+ }
98
+
99
+ return reconcile.Result {}, nil
100
+ }
101
+
102
+ // podForReplicasetWithWatchAnnotations returns a pod object with the annotations required to be watched with.
103
+ func (r * reconcileReplicaSet ) podForReplicasetWithWatchAnnotations (rs * appsv1.ReplicaSet ) * corev1.Pod {
104
+ pod := & corev1.Pod {
105
+ ObjectMeta : metav1.ObjectMeta {
106
+ Name : rs .Name ,
107
+ Namespace : rs .Namespace ,
108
+ },
109
+ }
110
+ annotation := schema.GroupKind {Group : "ReplicaSet" , Kind : "apps" }
111
+ handler .SetWatchOwnerAnnotation (rs ,pod , annotation )
112
+ return pod
113
+ }
0 commit comments