Skip to content

Commit 0765172

Browse files
authored
Add a pkg/webhook package to standardize the way controllers create and register new webhooks (#26)
Adding a new package to systematize the way we create and register new webhooks. Introducing two files: - `webhook.go` which contains the webhook structure and defines the setup method that needs to be suplied by each CRD per APIVersion. - `register.go` similarily to what a controller `pkg/resource/register.go` does, this files exposes functions to register and list the registered webhooks. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c8458a2 commit 0765172

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

pkg/webhook/registry.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package webhook
15+
16+
import "fmt"
17+
18+
var (
19+
// webhookRegistry is a map of webhooks, keyed by their unique
20+
// identifier.
21+
webhooksRegistry map[string]*Webhook
22+
)
23+
24+
// GetWebhooks returns the list of webhooks that were registred with
25+
// RegisterWebhook function.
26+
func GetWebhooks() []*Webhook {
27+
webhooks := make([]*Webhook, 0, len(webhooksRegistry))
28+
for _, wh := range webhooksRegistry {
29+
webhooks = append(webhooks, wh)
30+
}
31+
return webhooks
32+
}
33+
34+
// RegisterWebhook registers a new webhook within the webhook registry.
35+
// This function will return an error if it tries to register two webhooks
36+
// with the same unique identifier.
37+
func RegisterWebhook(w *Webhook) error {
38+
if webhooksRegistry == nil {
39+
webhooksRegistry = make(map[string]*Webhook)
40+
}
41+
42+
_, ok := webhooksRegistry[w.UID()]
43+
if ok {
44+
return fmt.Errorf("webhook %s already registred", w.UID())
45+
}
46+
webhooksRegistry[w.UID()] = w
47+
return nil
48+
}

pkg/webhook/webhook.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package webhook
15+
16+
import (
17+
"fmt"
18+
19+
ctrlrt "sigs.k8s.io/controller-runtime"
20+
)
21+
22+
type WebhookType string
23+
24+
const (
25+
WebhookTypeUnknown WebhookType = "unknown"
26+
WebhookTypeConversion WebhookType = "conversion"
27+
//TODO(a-hilaly) add validating and defaulting types
28+
)
29+
30+
// Webhook contains information about a custom Webhook
31+
type Webhook struct {
32+
Type string
33+
CRDKind string
34+
APIVersion string
35+
// Setup is used to register the webhook within
36+
// the webhook manager
37+
Setup func(ctrlrt.Manager) error
38+
}
39+
40+
// UID returns a unique identifier for the webhook. Webhooks
41+
// must be unique per CRD and APIVersion.
42+
func (w *Webhook) UID() string {
43+
return fmt.Sprintf("%s/%s/%s", w.Type, w.CRDKind, w.APIVersion)
44+
}
45+
46+
// New instanciate a new webhook object pointer.
47+
func New(
48+
apiVersion string,
49+
crdKind string,
50+
type_ string,
51+
setupFunc func(ctrlrt.Manager) error,
52+
) *Webhook {
53+
return &Webhook{
54+
Type: type_,
55+
CRDKind: crdKind,
56+
APIVersion: apiVersion,
57+
Setup: setupFunc,
58+
}
59+
}

0 commit comments

Comments
 (0)