-
Notifications
You must be signed in to change notification settings - Fork 156
/
mutation.go
155 lines (122 loc) · 5.05 KB
/
mutation.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
/*
Copyright 2022 The Kubermatic Kubernetes Platform contributors.
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 mutation
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/go-logr/logr"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/provider"
"k8c.io/kubermatic/v2/pkg/provider/kubernetes"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/api/equality"
ctrlruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// AdmissionHandler for mutating Kubermatic MLAAdminSetting CRD.
type AdmissionHandler struct {
log logr.Logger
decoder *admission.Decoder
seedGetter provider.SeedGetter
seedClientGetter provider.SeedClientGetter
}
// NewAdmissionHandler returns a new MLAAdminSetting AdmissionHandler.
func NewAdmissionHandler(seedGetter provider.SeedGetter, seedClientGetter provider.SeedClientGetter) *AdmissionHandler {
return &AdmissionHandler{
seedGetter: seedGetter,
seedClientGetter: seedClientGetter,
}
}
func (h *AdmissionHandler) SetupWebhookWithManager(mgr ctrlruntime.Manager) {
mgr.GetWebhookServer().Register("/mutate-kubermatic-k8c-io-v1-mlaadminsetting", &webhook.Admission{Handler: h})
}
func (h *AdmissionHandler) InjectLogger(l logr.Logger) error {
h.log = l.WithName("mlaadminsetting-mutation-handler")
return nil
}
func (h *AdmissionHandler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
return nil
}
func (h *AdmissionHandler) Handle(ctx context.Context, req webhook.AdmissionRequest) webhook.AdmissionResponse {
adminSetting := &kubermaticv1.MLAAdminSetting{}
switch req.Operation {
case admissionv1.Create:
if err := h.decoder.Decode(req, adminSetting); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
// apply defaults to the existing MLAAdminSetting
err := h.ensureClusterReference(ctx, adminSetting)
if err != nil {
h.log.Error(err, "MLAAdminSetting mutation failed")
return webhook.Errored(http.StatusInternalServerError, fmt.Errorf("MLAAdminSetting mutation request %s failed: %w", req.UID, err))
}
case admissionv1.Update:
oldSetting := &kubermaticv1.MLAAdminSetting{}
if err := h.decoder.Decode(req, adminSetting); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
if err := h.decoder.DecodeRaw(req.OldObject, oldSetting); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
err := h.validateUpdate(ctx, oldSetting, adminSetting)
if err != nil {
h.log.Error(err, "MLAAdminSetting mutation failed")
return webhook.Errored(http.StatusInternalServerError, fmt.Errorf("MLAAdminSetting mutation request %s failed: %w", req.UID, err))
}
case admissionv1.Delete:
return webhook.Allowed(fmt.Sprintf("no mutation done for request %s", req.UID))
default:
return admission.Errored(http.StatusBadRequest, fmt.Errorf("%s not supported on MLAAdminSetting resources", req.Operation))
}
mutatedSetting, err := json.Marshal(adminSetting)
if err != nil {
return webhook.Errored(http.StatusInternalServerError, fmt.Errorf("marshaling MLAAdminSetting object failed: %w", err))
}
return admission.PatchResponseFromRaw(req.Object.Raw, mutatedSetting)
}
func (h *AdmissionHandler) ensureClusterReference(ctx context.Context, adminSetting *kubermaticv1.MLAAdminSetting) error {
seed, err := h.seedGetter()
if err != nil {
return fmt.Errorf("failed to get current Seed: %w", err)
}
if seed == nil {
return errors.New("webhook not configured for a Seed cluster, cannot validate MLAAdminSetting resources")
}
client, err := h.seedClientGetter(seed)
if err != nil {
return fmt.Errorf("failed to get Seed client: %w", err)
}
cluster, err := kubernetes.ClusterFromNamespace(ctx, client, adminSetting.Namespace)
if err != nil {
return fmt.Errorf("failed to list Cluster objects: %w", err)
}
if cluster == nil {
return errors.New("MLAAdminSetting resources can only be created in cluster namespaces, but no matching Cluster was found")
}
if cluster.DeletionTimestamp != nil {
return fmt.Errorf("Cluster %s is in deletion already, cannot create a new MLAAdminSetting", cluster.Name)
}
adminSetting.Spec.ClusterName = cluster.Name
return nil
}
func (h *AdmissionHandler) validateUpdate(ctx context.Context, oldSetting *kubermaticv1.MLAAdminSetting, newSetting *kubermaticv1.MLAAdminSetting) error {
if !equality.Semantic.DeepEqual(oldSetting.Spec.ClusterName, newSetting.Spec.ClusterName) {
return errors.New("Cluster reference cannot be changed")
}
return nil
}