|
| 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 admission |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "regexp" |
| 23 | + "strings" |
| 24 | + "sync" |
| 25 | + |
| 26 | + admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/internal/webhookgenerator/types" |
| 29 | +) |
| 30 | + |
| 31 | +// Webhook represents each individual webhook. |
| 32 | +type Webhook struct { |
| 33 | + // Name is the name of the webhook |
| 34 | + Name string |
| 35 | + // Type is the webhook type, i.e. mutating, validating |
| 36 | + Type types.WebhookType |
| 37 | + // Path is the path this webhook will serve. |
| 38 | + Path string |
| 39 | + // Rules maps to the Rules field in admissionregistrationv1beta1.Webhook |
| 40 | + Rules []admissionregistrationv1beta1.RuleWithOperations |
| 41 | + // FailurePolicy maps to the FailurePolicy field in admissionregistrationv1beta1.Webhook |
| 42 | + // This optional. If not set, will be defaulted to Ignore (fail-open) by the server. |
| 43 | + // More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147 |
| 44 | + FailurePolicy *admissionregistrationv1beta1.FailurePolicyType |
| 45 | + // NamespaceSelector maps to the NamespaceSelector field in admissionregistrationv1beta1.Webhook |
| 46 | + // This optional. |
| 47 | + NamespaceSelector *metav1.LabelSelector |
| 48 | + |
| 49 | + once sync.Once |
| 50 | +} |
| 51 | + |
| 52 | +func (w *Webhook) setDefaults() { |
| 53 | + if len(w.Path) == 0 { |
| 54 | + if len(w.Rules) == 0 || len(w.Rules[0].Resources) == 0 { |
| 55 | + // can't do defaulting, skip it. |
| 56 | + return |
| 57 | + } |
| 58 | + if w.Type == types.WebhookTypeMutating { |
| 59 | + w.Path = "/mutate-" + w.Rules[0].Resources[0] |
| 60 | + } else if w.Type == types.WebhookTypeValidating { |
| 61 | + w.Path = "/validate-" + w.Rules[0].Resources[0] |
| 62 | + } |
| 63 | + } |
| 64 | + if len(w.Name) == 0 { |
| 65 | + reg := regexp.MustCompile("[^a-zA-Z0-9]+") |
| 66 | + processedPath := strings.ToLower(reg.ReplaceAllString(w.Path, "")) |
| 67 | + w.Name = processedPath + ".example.com" |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// GetName returns the name of the webhook. |
| 72 | +func (w *Webhook) GetName() string { |
| 73 | + w.once.Do(w.setDefaults) |
| 74 | + return w.Name |
| 75 | +} |
| 76 | + |
| 77 | +// GetPath returns the path that the webhook registered. |
| 78 | +func (w *Webhook) GetPath() string { |
| 79 | + w.once.Do(w.setDefaults) |
| 80 | + return w.Path |
| 81 | +} |
| 82 | + |
| 83 | +// GetType returns the type of the webhook. |
| 84 | +func (w *Webhook) GetType() types.WebhookType { |
| 85 | + w.once.Do(w.setDefaults) |
| 86 | + return w.Type |
| 87 | +} |
| 88 | + |
| 89 | +// Validate validates if the webhook is valid. |
| 90 | +func (w *Webhook) Validate() error { |
| 91 | + w.once.Do(w.setDefaults) |
| 92 | + if len(w.Rules) == 0 { |
| 93 | + return errors.New("field Rules should not be empty") |
| 94 | + } |
| 95 | + if len(w.Name) == 0 { |
| 96 | + return errors.New("field Name should not be empty") |
| 97 | + } |
| 98 | + if w.Type != types.WebhookTypeMutating && w.Type != types.WebhookTypeValidating { |
| 99 | + return fmt.Errorf("unsupported Type: %v, only WebhookTypeMutating and WebhookTypeValidating are supported", w.Type) |
| 100 | + } |
| 101 | + if len(w.Path) == 0 { |
| 102 | + return errors.New("field Path should not be empty") |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
0 commit comments