@@ -17,31 +17,22 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
- "context"
21
20
"flag"
22
- "fmt"
23
- "net/http"
24
21
"os"
25
22
26
- "github.com/go-logr/logr"
27
-
28
23
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
29
24
appsv1 "k8s.io/api/apps/v1"
30
25
corev1 "k8s.io/api/core/v1"
31
- "k8s.io/apimachinery/pkg/api/errors"
32
26
apitypes "k8s.io/apimachinery/pkg/types"
33
27
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
34
- "sigs.k8s.io/controller-runtime/pkg/client"
35
28
"sigs.k8s.io/controller-runtime/pkg/client/config"
36
29
"sigs.k8s.io/controller-runtime/pkg/controller"
37
30
"sigs.k8s.io/controller-runtime/pkg/handler"
38
31
"sigs.k8s.io/controller-runtime/pkg/manager"
39
- "sigs.k8s.io/controller-runtime/pkg/reconcile"
40
32
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
41
33
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
42
34
"sigs.k8s.io/controller-runtime/pkg/source"
43
35
"sigs.k8s.io/controller-runtime/pkg/webhook"
44
- "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
45
36
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
46
37
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
47
38
)
@@ -112,21 +103,21 @@ func main() {
112
103
as , err := webhook .NewServer ("foo-admission-server" , mgr , webhook.ServerOptions {
113
104
Port : 443 ,
114
105
CertDir : "/tmp/cert" ,
115
- Client : mgr .GetClient (),
116
- KVMap : map [string ]interface {}{"foo" : "bar" },
106
+ // Client: mgr.GetClient(),
107
+ KVMap : map [string ]interface {}{"foo" : "bar" },
117
108
BootstrapOptions : & webhook.BootstrapOptions {
118
109
Secret : & apitypes.NamespacedName {
119
110
Namespace : "default" ,
120
111
Name : "foo-admission-server-secret" ,
121
112
},
122
113
123
- Service : & apitypes. NamespacedName {
114
+ Service : & webhook. Service {
124
115
Namespace : "default" ,
125
116
Name : "foo-admission-server-service" ,
126
- },
127
- // Labels should select the pods that runs this webhook server.
128
- Labels : map [ string ] string {
129
- "app" : "foo-admission-server" ,
117
+ // Selectors should select the pods that runs this webhook server.
118
+ Selectors : map [ string ] string {
119
+ "app" : "foo-admission-server" ,
120
+ } ,
130
121
},
131
122
},
132
123
})
@@ -145,122 +136,3 @@ func main() {
145
136
os .Exit (1 )
146
137
}
147
138
}
148
-
149
- // reconcileReplicaSet reconciles ReplicaSets
150
- type reconcileReplicaSet struct {
151
- // client can be used to retrieve objects from the APIServer.
152
- client client.Client
153
- log logr.Logger
154
- }
155
-
156
- // Implement reconcile.Reconciler so the controller can reconcile objects
157
- var _ reconcile.Reconciler = & reconcileReplicaSet {}
158
-
159
- func (r * reconcileReplicaSet ) Reconcile (request reconcile.Request ) (reconcile.Result , error ) {
160
- // set up a convinient log object so we don't have to type request over and over again
161
- log := r .log .WithValues ("request" , request )
162
-
163
- // Fetch the ReplicaSet from the cache
164
- rs := & appsv1.ReplicaSet {}
165
- err := r .client .Get (context .TODO (), request .NamespacedName , rs )
166
- if errors .IsNotFound (err ) {
167
- log .Error (nil , "Could not find ReplicaSet" )
168
- return reconcile.Result {}, nil
169
- }
170
-
171
- if err != nil {
172
- log .Error (err , "Could not fetch ReplicaSet" )
173
- return reconcile.Result {}, err
174
- }
175
-
176
- // Print the ReplicaSet
177
- log .Info ("Reconciling ReplicaSet" , "container name" , rs .Spec .Template .Spec .Containers [0 ].Name )
178
-
179
- // Set the label if it is missing
180
- if rs .Labels == nil {
181
- rs .Labels = map [string ]string {}
182
- }
183
- if rs .Labels ["hello" ] == "world" {
184
- return reconcile.Result {}, nil
185
- }
186
-
187
- // Update the ReplicaSet
188
- rs .Labels ["hello" ] = "world"
189
- err = r .client .Update (context .TODO (), rs )
190
- if err != nil {
191
- log .Error (err , "Could not write ReplicaSet" )
192
- return reconcile.Result {}, err
193
- }
194
-
195
- return reconcile.Result {}, nil
196
- }
197
-
198
- // podAnnotator annotates Pods
199
- type podAnnotator struct {
200
- client client.Client
201
- decoder admission.Decoder
202
- }
203
-
204
- // Implement admission.Handler so the controller can handle admission request.
205
- var _ admission.Handler = & podAnnotator {}
206
-
207
- // podAnnotator adds an annotation to every incoming pods.
208
- func (a * podAnnotator ) Handle (_ context.Context , req admission.Request ) admission.Response {
209
- pod := & corev1.Pod {}
210
-
211
- err := a .decoder .Decode (req , pod )
212
- if err != nil {
213
- return admission .ErrorResponse (http .StatusBadRequest , err )
214
- }
215
- copy := pod .DeepCopy ()
216
-
217
- err = mutatePodsFn (copy )
218
- if err != nil {
219
- return admission .ErrorResponse (http .StatusInternalServerError , err )
220
- }
221
- return admission .PatchResponse (pod , copy )
222
- }
223
-
224
- // mutatePodsFn add an annotation to the given pod
225
- func mutatePodsFn (pod * corev1.Pod ) error {
226
- anno := pod .GetAnnotations ()
227
- anno ["example-mutating-admission-webhhok" ] = "foo"
228
- pod .SetAnnotations (anno )
229
- return nil
230
- }
231
-
232
- // podValidator validates Pods
233
- type podValidator struct {
234
- client client.Client
235
- decoder admission.Decoder
236
- }
237
-
238
- // Implement admission.Handler so the controller can handle admission request.
239
- var _ admission.Handler = & podValidator {}
240
-
241
- // podValidator admits a pod iff a specific annotation exists.
242
- func (v * podValidator ) Handle (_ context.Context , req admission.Request ) admission.Response {
243
- pod := & corev1.Pod {}
244
-
245
- err := v .decoder .Decode (req , pod )
246
- if err != nil {
247
- return admission .ErrorResponse (http .StatusBadRequest , err )
248
- }
249
-
250
- allowed , reason , err := validatePodsFn (pod )
251
- if err != nil {
252
- return admission .ErrorResponse (http .StatusInternalServerError , err )
253
- }
254
- return admission .ValidationResponse (allowed , reason )
255
- }
256
-
257
- func validatePodsFn (pod * corev1.Pod ) (bool , string , error ) {
258
- anno := pod .GetAnnotations ()
259
- key := "example-mutating-admission-webhhok"
260
- _ , found := anno [key ]
261
- if found {
262
- return found , "" , nil
263
- } else {
264
- return found , fmt .Sprintf ("failed to find annotation with key: %v" , key ), nil
265
- }
266
- }
0 commit comments