-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mengqi Yu
committed
Feb 4, 2019
1 parent
2027a41
commit b2ea9dc
Showing
38 changed files
with
341 additions
and
4,875 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package admission | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
|
||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/internal/webhookgenerator/types" | ||
) | ||
|
||
// Webhook represents each individual webhook. | ||
type Webhook struct { | ||
// Name is the name of the webhook | ||
Name string | ||
// Type is the webhook type, i.e. mutating, validating | ||
Type types.WebhookType | ||
// Path is the path this webhook will serve. | ||
Path string | ||
// Rules maps to the Rules field in admissionregistrationv1beta1.Webhook | ||
Rules []admissionregistrationv1beta1.RuleWithOperations | ||
// FailurePolicy maps to the FailurePolicy field in admissionregistrationv1beta1.Webhook | ||
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server. | ||
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147 | ||
FailurePolicy *admissionregistrationv1beta1.FailurePolicyType | ||
// NamespaceSelector maps to the NamespaceSelector field in admissionregistrationv1beta1.Webhook | ||
// This optional. | ||
NamespaceSelector *metav1.LabelSelector | ||
|
||
once sync.Once | ||
} | ||
|
||
func (w *Webhook) setDefaults() { | ||
if len(w.Path) == 0 { | ||
if len(w.Rules) == 0 || len(w.Rules[0].Resources) == 0 { | ||
// can't do defaulting, skip it. | ||
return | ||
} | ||
if w.Type == types.WebhookTypeMutating { | ||
w.Path = "/mutate-" + w.Rules[0].Resources[0] | ||
} else if w.Type == types.WebhookTypeValidating { | ||
w.Path = "/validate-" + w.Rules[0].Resources[0] | ||
} | ||
} | ||
if len(w.Name) == 0 { | ||
reg := regexp.MustCompile("[^a-zA-Z0-9]+") | ||
processedPath := strings.ToLower(reg.ReplaceAllString(w.Path, "")) | ||
w.Name = processedPath + ".example.com" | ||
} | ||
} | ||
|
||
// GetName returns the name of the webhook. | ||
func (w *Webhook) GetName() string { | ||
w.once.Do(w.setDefaults) | ||
return w.Name | ||
} | ||
|
||
// GetPath returns the path that the webhook registered. | ||
func (w *Webhook) GetPath() string { | ||
w.once.Do(w.setDefaults) | ||
return w.Path | ||
} | ||
|
||
// GetType returns the type of the webhook. | ||
func (w *Webhook) GetType() types.WebhookType { | ||
w.once.Do(w.setDefaults) | ||
return w.Type | ||
} | ||
|
||
// Validate validates if the webhook is valid. | ||
func (w *Webhook) Validate() error { | ||
w.once.Do(w.setDefaults) | ||
if len(w.Rules) == 0 { | ||
return errors.New("field Rules should not be empty") | ||
} | ||
if len(w.Name) == 0 { | ||
return errors.New("field Name should not be empty") | ||
} | ||
if w.Type != types.WebhookTypeMutating && w.Type != types.WebhookTypeValidating { | ||
return fmt.Errorf("unsupported Type: %v, only WebhookTypeMutating and WebhookTypeValidating are supported", w.Type) | ||
} | ||
if len(w.Path) == 0 { | ||
return errors.New("field Path should not be empty") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.