/
namespaces.go
40 lines (34 loc) · 1.02 KB
/
namespaces.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
package validationwebhooks
import (
"context"
"fmt"
. "github.com/toughnoah/melon/internal/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type NamespaceValidator struct {
Client client.Client
decoder *admission.Decoder
ConfPath string
}
func (v *NamespaceValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
namespace := &corev1.Namespace{}
err := v.decoder.Decode(req, namespace)
if err != nil {
klog.Errorf(decodeError, err.Error())
return admission.Errored(http.StatusBadRequest, err)
}
err = ValidateNaming(namespace.Name, v.ConfPath, NamespaceNamingKind)
if err != nil {
klog.Errorf(namingCheckError, NamespaceNamingKind, err.Error())
return admission.Denied(fmt.Sprintf(namingCheckError, NamespaceNamingKind, err.Error()))
}
return admission.Allowed("")
}
func (v *NamespaceValidator) InjectDecoder(d *admission.Decoder) error {
v.decoder = d
return nil
}