-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Webhooks for external APIs #4176
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -40,6 +40,10 @@ type Webhook struct { // nolint:maligned | |
// Define value for AdmissionReviewVersions marker | ||
AdmissionReviewVersions string | ||
|
||
// If the webhook is for an external resource. External resources need to be | ||
// imported. | ||
ExternalAPI bool | ||
|
||
Force bool | ||
} | ||
|
||
|
@@ -97,16 +101,27 @@ import ( | |
{{- if .Resource.HasValidationWebhook }} | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
{{- end }} | ||
{{- if .ExternalAPI }} | ||
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}" | ||
{{- end }} | ||
) | ||
|
||
// nolint:unused | ||
// log is for logging in this package. | ||
var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind }}-resource") | ||
|
||
// SetupWebhookWithManager will setup the manager to manage the webhooks. | ||
{{- if .ExternalAPI }} | ||
func SetupWebhookFor{{ .Resource.Kind }}WithManager(mgr ctrl.Manager) error { | ||
{{- else }} | ||
func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
{{- end }} | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
{{- if .ExternalAPI }} | ||
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here should be if the Resource.Path is not empty |
||
{{- else }} | ||
For(r). | ||
{{- end }} | ||
{{- if .Resource.HasValidationWebhook }} | ||
WithValidator(&{{ .Resource.Kind }}CustomValidator{}). | ||
{{- end }} | ||
|
@@ -137,7 +152,11 @@ var _ webhook.CustomDefaulter = &{{ .Resource.Kind }}CustomDefaulter{} | |
|
||
// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind {{ .Resource.Kind }}. | ||
func (d *{{ .Resource.Kind }}CustomDefaulter) Default(ctx context.Context, obj runtime.Object) error { | ||
{{- if .ExternalAPI }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}) | ||
{{- else }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.Kind }}) | ||
{{- end }} | ||
if !ok { | ||
return fmt.Errorf("expected an {{ .Resource.Kind }} object but got %T", obj) | ||
} | ||
|
@@ -170,7 +189,11 @@ var _ webhook.CustomValidator = &{{ .Resource.Kind }}CustomValidator{} | |
|
||
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type {{ .Resource.Kind }}. | ||
func (v *{{ .Resource.Kind }}CustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
{{- if .ExternalAPI }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}) | ||
{{- else }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.Kind }}) | ||
{{- end }} | ||
if !ok { | ||
return nil, fmt.Errorf("expected a {{ .Resource.Kind }} object but got %T", obj) | ||
} | ||
|
@@ -183,7 +206,11 @@ func (v *{{ .Resource.Kind }}CustomValidator) ValidateCreate(ctx context.Context | |
|
||
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type {{ .Resource.Kind }}. | ||
func (v *{{ .Resource.Kind }}CustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { | ||
{{- if .ExternalAPI }} | ||
{{ lower .Resource.Kind }}, ok := newObj.(*{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}) | ||
{{- else }} | ||
{{ lower .Resource.Kind }}, ok := newObj.(*{{ .Resource.Kind }}) | ||
{{- end }} | ||
if !ok { | ||
return nil, fmt.Errorf("expected a {{ .Resource.Kind }} object but got %T", newObj) | ||
} | ||
|
@@ -196,7 +223,11 @@ func (v *{{ .Resource.Kind }}CustomValidator) ValidateUpdate(ctx context.Context | |
|
||
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type {{ .Resource.Kind }}. | ||
func (v *{{ .Resource.Kind }}CustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
{{- if .ExternalAPI }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}) | ||
{{- else }} | ||
{{ lower .Resource.Kind }}, ok := obj.(*{{ .Resource.Kind }}) | ||
{{- end }} | ||
if !ok { | ||
return nil, fmt.Errorf("expected a {{ .Resource.Kind }} object but got %T", obj) | ||
} | ||
|
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
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 |
---|---|---|
|
@@ -96,12 +96,17 @@ func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error { | |
" --programmatic-validation and --conversion to be true", p.commandName) | ||
} | ||
|
||
// check if resource exist to create webhook | ||
if r, err := p.config.GetResource(p.resource.GVK); err != nil { | ||
return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName) | ||
} else if r.Webhooks != nil && !r.Webhooks.IsEmpty() && !p.force { | ||
if p.force { | ||
return nil | ||
} | ||
Comment on lines
+99
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not the same of what was implemented before. |
||
|
||
r, err := p.config.GetResource(p.resource.GVK) | ||
if err == nil && r.Webhooks != nil && !r.Webhooks.IsEmpty() { | ||
return fmt.Errorf("webhook resource already exists") | ||
} | ||
if err == nil && r.API == nil || err != nil && res.Path == "" { | ||
return fmt.Errorf("%s create webhook requires a previously created API or a core API", p.commandName) | ||
} | ||
|
||
return nil | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those changes will not be required after : #4150