-
Notifications
You must be signed in to change notification settings - Fork 71
/
webhook.go
163 lines (146 loc) · 4.3 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package webhook
import (
"errors"
"fmt"
"github.com/armory/spinnaker-operator/pkg/util"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"k8s.io/api/admissionregistration/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"os"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"strings"
)
const (
servicePort = 9876
)
var registrations = []registration{}
type registration struct {
kind schema.GroupVersionKind
h admission.Handler
p string
r string
}
func Register(kind schema.GroupVersionKind, resources string, h admission.Handler) {
registrations = append(registrations, registration{
kind: kind,
h: h,
p: generateValidatePath(kind),
r: resources,
})
}
func Start(m manager.Manager) error {
if len(registrations) == 0 {
return errors.New("no kind registered for validation")
}
ns, name, err := getOperatorNameAndNamespace()
if err != nil {
return err
}
// Create Kubernetes service for listening to requests from API server
rawClient := kubernetes.NewForConfigOrDie(m.GetConfig())
err = deployWebhookService(ns, name, servicePort, rawClient)
if err != nil {
return err
}
// Create or get certificates
c, err := getCertContext(ns, name)
if err != nil {
return err
}
hookServer := m.GetWebhookServer()
hookServer.CertDir = c.certDir
hookServer.Port = servicePort
for _, r := range registrations {
hookServer.Register(r.p, &webhook.Admission{Handler: r.h})
}
// Create validating webhook configuration for registering our webhook with the API server
return deployValidatingWebhookConfiguration(name, ns, rawClient, c.signingCert)
}
func getOperatorNameAndNamespace() (string, string, error) {
name, err := k8sutil.GetOperatorName()
if err != nil {
return "", "", err
}
ns, err := k8sutil.GetOperatorNamespace()
if err != nil {
envNs := os.Getenv("ADMISSION_PROXY_NAMESPACE")
if envNs == "" {
return "", "", fmt.Errorf("unable to determine operator namespace. Error: %s and ADMISSION_PROXY_NAMESPACE env var not set", err.Error())
}
ns = envNs
}
return ns, name, nil
}
func generateValidatePath(gvk schema.GroupVersionKind) string {
return "/validate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
gvk.Version + "-" + strings.ToLower(gvk.Kind)
}
func deployWebhookService(ns string, name string, port int, rawClient *kubernetes.Clientset) error {
selectorLabels := map[string]string{"name": name}
service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
Labels: selectorLabels,
},
Spec: v1.ServiceSpec{
Selector: selectorLabels,
Ports: []v1.ServicePort{
{
Name: "http",
Protocol: "TCP",
Port: 443,
TargetPort: intstr.FromInt(port),
},
},
},
}
return util.CreateOrUpdateService(service, rawClient)
}
func deployValidatingWebhookConfiguration(svcName, ns string, rawClient *kubernetes.Clientset, cert []byte) error {
webhookConfig := &v1beta1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "spinnakervalidatingwebhook",
Namespace: ns,
},
Webhooks: []v1beta1.Webhook{},
}
for i := range registrations {
r := registrations[i]
webhookConfig.Webhooks = append(webhookConfig.Webhooks, v1beta1.Webhook{
Name: fmt.Sprintf("webhook-%s-%s.%s", r.r, r.kind.Version, strings.ToLower(r.kind.Group)),
ClientConfig: v1beta1.WebhookClientConfig{
Service: &v1beta1.ServiceReference{
Namespace: ns,
Name: svcName,
Path: &r.p,
},
CABundle: cert,
},
Rules: []v1beta1.RuleWithOperations{{
Operations: []v1beta1.OperationType{
v1beta1.Create,
v1beta1.Update,
},
Rule: v1beta1.Rule{
APIGroups: []string{r.kind.Group},
APIVersions: []string{r.kind.Version},
Resources: []string{r.r}, // should be "spinnakerservices"
},
}},
SideEffects: sideEffect(v1beta1.SideEffectClassNone),
})
}
return util.CreateOrUpdateValidatingWebhookConfiguration(webhookConfig, rawClient)
}
func sideEffect(sideEffect v1beta1.SideEffectClass) *v1beta1.SideEffectClass {
s := new(v1beta1.SideEffectClass)
*s = sideEffect
return s
}