forked from slackhq/simple-kubernetes-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_validator.go
39 lines (31 loc) · 926 Bytes
/
name_validator.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
package validation
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
// nameValidator is a container for validating the name of pods
type nameValidator struct {
Logger logrus.FieldLogger
}
// nameValidator implements the podValidator interface
var _ podValidator = (*nameValidator)(nil)
// Name returns the name of nameValidator
func (n nameValidator) Name() string {
return "name_validator"
}
// Validate inspects the name of a given pod and returns validation.
// The returned validation is only valid if the pod name does not contain some
// bad string.
func (n nameValidator) Validate(pod *corev1.Pod) (validation, error) {
badString := "offensive"
if strings.Contains(pod.Name, badString) {
v := validation{
Valid: false,
Reason: fmt.Sprintf("pod name contains %q", badString),
}
return v, nil
}
return validation{Valid: true, Reason: "valid name"}, nil
}