|
| 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 | +/* |
| 18 | +Package builder provides methods to build admission webhooks. |
| 19 | +
|
| 20 | +The following are 2 examples for building mutating webhook and validating webhook. |
| 21 | +
|
| 22 | + webhook1, err := NewWebhookBuilder(). |
| 23 | + Mutating(). |
| 24 | + Operations(admissionregistrationv1beta1.Create). |
| 25 | + ForType(&corev1.Pod{}). |
| 26 | + WithManager(mgr). |
| 27 | + Handlers(mutatingHandler11, mutatingHandler12). |
| 28 | + Build() |
| 29 | + if err != nil { |
| 30 | + // handle error |
| 31 | + } |
| 32 | +
|
| 33 | + webhook2, err := NewWebhookBuilder(). |
| 34 | + Validating(). |
| 35 | + Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update). |
| 36 | + ForType(&appsv1.Deployment{}). |
| 37 | + WithManager(mgr). |
| 38 | + Handlers(validatingHandler21). |
| 39 | + Build() |
| 40 | + if err != nil { |
| 41 | + // handle error |
| 42 | + } |
| 43 | +
|
| 44 | +Note: To build a webhook for a CRD, you need to ensure the manager uses the scheme that understands your CRD. |
| 45 | +This is necessary, because if the scheme doesn't understand your CRD types, the decoder won't be able to decode |
| 46 | +the CR object from the admission review request. |
| 47 | +
|
| 48 | +The following snippet shows how to register CRD types with manager's scheme. |
| 49 | +
|
| 50 | + mgr, err := manager.New(cfg, manager.Options{}) |
| 51 | + if err != nil { |
| 52 | + // handle error |
| 53 | + } |
| 54 | + // SchemeGroupVersion is group version used to register these objects |
| 55 | + SchemeGroupVersion = schema.GroupVersion{Group: "crew.k8s.io", Version: "v1"} |
| 56 | + // SchemeBuilder is used to add go types to the GroupVersionKind scheme |
| 57 | + SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} |
| 58 | + // Register your CRD types. |
| 59 | + SchemeBuilder.Register(&Kraken{}, &KrakenList{}) |
| 60 | + // Register your CRD types with the manager's scheme. |
| 61 | + err = SchemeBuilder.AddToScheme(mgr.GetScheme()) |
| 62 | + if err != nil { |
| 63 | + // handle error |
| 64 | + } |
| 65 | +
|
| 66 | +There are more options for configuring a webhook. e.g. Name, Path, FailurePolicy, NamespaceSelector. |
| 67 | +Here is another example: |
| 68 | +
|
| 69 | + webhook3, err := NewWebhookBuilder(). |
| 70 | + Name("foo.example.com"). |
| 71 | + Path("/mutatepods"). |
| 72 | + Mutating(). |
| 73 | + Operations(admissionregistrationv1beta1.Create). |
| 74 | + ForType(&corev1.Pod{}). |
| 75 | + FailurePolicy(admissionregistrationv1beta1.Fail). |
| 76 | + WithManager(mgr). |
| 77 | + Handlers(mutatingHandler31, mutatingHandler32). |
| 78 | + Build() |
| 79 | + if err != nil { |
| 80 | + // handle error |
| 81 | + } |
| 82 | +
|
| 83 | +For most users, we recommend to use Operations and ForType instead of Rules to construct a webhook, |
| 84 | +since it is more intuitive and easier to pass the target operations to Operations method and |
| 85 | +a empty target object to ForType method than passing a complex RuleWithOperations struct to Rules method. |
| 86 | +
|
| 87 | +Rules may be useful for some more advanced use cases like subresources, wildcard resources etc. |
| 88 | +Here is an example: |
| 89 | +
|
| 90 | + webhook4, err := NewWebhookBuilder(). |
| 91 | + Validating(). |
| 92 | + Rules(admissionregistrationv1beta1.RuleWithOperations{ |
| 93 | + Operations: []admissionregistrationv1beta1.OperationType{admissionregistrationv1beta1.Create}, |
| 94 | + Rule: admissionregistrationv1beta1.Rule{ |
| 95 | + APIGroups: []string{"apps", "batch"}, |
| 96 | + APIVersions: []string{"v1"}, |
| 97 | + Resources: []string{"*"}, |
| 98 | + }, |
| 99 | + }). |
| 100 | + WithManager(mgr). |
| 101 | + Handlers(validatingHandler41). |
| 102 | + Build() |
| 103 | + if err != nil { |
| 104 | + // handle error |
| 105 | + } |
| 106 | +*/ |
| 107 | +package builder |
0 commit comments