-
Notifications
You must be signed in to change notification settings - Fork 10
/
webhooks.go
133 lines (117 loc) · 4.21 KB
/
webhooks.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
/*
* Copyright (C) 2022 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"bytes"
"context"
"fmt"
"os"
admissionv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/appvia/terranetes-controller/pkg/register"
"github.com/appvia/terranetes-controller/pkg/schema"
"github.com/appvia/terranetes-controller/pkg/utils"
"github.com/appvia/terranetes-controller/pkg/utils/kubernetes"
)
// registerWebhooks is responsible for registering the webhooks
func (s *Server) registerWebhooks(ctx context.Context) error {
cc, err := client.New(s.cfg, client.Options{Scheme: schema.GetScheme()})
if err != nil {
return err
}
// @step: read the certificate authority
ca, err := os.ReadFile(s.config.TLSAuthority)
if err != nil {
return fmt.Errorf("failed to read the certificate authority file, %w", err)
}
documents, err := utils.YAMLDocuments(bytes.NewReader(register.MustAsset("webhooks/manifests.yaml")))
if err != nil {
return fmt.Errorf("failed to decode the webhooks manifests, %w", err)
}
// @step: register the validating webhooks
for _, x := range documents {
o, err := schema.DecodeYAML([]byte(x))
if err != nil {
return fmt.Errorf("failed to decode the webhook, %w", err)
}
switch o := o.(type) {
case *admissionv1.ValidatingWebhookConfiguration:
for i := 0; i < len(o.Webhooks); i++ {
o.Webhooks[i].ClientConfig.CABundle = ca
o.Webhooks[i].ClientConfig.Service.Namespace = os.Getenv("KUBE_NAMESPACE")
o.Webhooks[i].ClientConfig.Service.Name = "controller"
o.Webhooks[i].ClientConfig.Service.Port = pointer.Int32(443)
}
case *admissionv1.MutatingWebhookConfiguration:
for i := 0; i < len(o.Webhooks); i++ {
o.Webhooks[i].ClientConfig.CABundle = ca
o.Webhooks[i].ClientConfig.Service.Namespace = os.Getenv("KUBE_NAMESPACE")
o.Webhooks[i].ClientConfig.Service.Name = "controller"
o.Webhooks[i].ClientConfig.Service.Port = pointer.Int32(443)
}
default:
return fmt.Errorf("expected a validating or mutating webhook, got %T", o)
}
if err := kubernetes.CreateOrForceUpdate(ctx, cc, o); err != nil {
return fmt.Errorf("failed to create / update the webhook, %w", err)
}
}
// @step: create a webhook for intercepting the namespaces
decision := admissionv1.Fail
sideEffects := admissionv1.SideEffectClassNone
wh := &admissionv1.ValidatingWebhookConfiguration{}
wh.Name = "validating-webhook-namespace"
wh.Webhooks = []admissionv1.ValidatingWebhook{
{
AdmissionReviewVersions: []string{"v1"},
ClientConfig: admissionv1.WebhookClientConfig{
Service: &admissionv1.ServiceReference{
Name: "controller",
Namespace: os.Getenv("KUBE_NAMESPACE"),
Path: pointer.String("/validate/terraform.appvia.io/namespaces"),
Port: pointer.Int32(443),
},
CABundle: ca,
},
FailurePolicy: &decision,
Name: "namespaces.terraform.appvia.io",
Rules: []admissionv1.RuleWithOperations{
{
Operations: []admissionv1.OperationType{"DELETE"},
Rule: admissionv1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"namespaces"},
},
},
},
SideEffects: &sideEffects,
},
}
switch s.config.EnableNamespaceProtection {
case true:
if err := kubernetes.CreateOrForceUpdate(ctx, cc, wh); err != nil {
return fmt.Errorf("failed to create / update the namespace webhook, %w", err)
}
default:
if err := kubernetes.DeleteIfExists(ctx, cc, wh); err != nil {
return fmt.Errorf("failed to delete the namespace webhook, %w", err)
}
}
return nil
}