-
Notifications
You must be signed in to change notification settings - Fork 21
/
validate.go
172 lines (145 loc) · 5.43 KB
/
validate.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
164
165
166
167
168
169
170
171
172
/*
Copyright AppsCode Inc. and 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 validator
import (
"context"
"fmt"
"strings"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"github.com/pkg/errors"
"gomodules.xyz/x/arrays"
core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
)
func ValidateStorage(client kubernetes.Interface, storageType api.StorageType, spec *core.PersistentVolumeClaimSpec, storageSpecPath ...string) error {
if storageType == api.StorageTypeEphemeral {
return nil
}
storagePath := "spec.storage"
if len(storageSpecPath) != 0 {
storagePath = strings.Join(storageSpecPath, ".")
}
if spec == nil {
return fmt.Errorf(`%v is missing for durable storage type`, storagePath)
}
if spec.StorageClassName != nil {
if _, err := client.StorageV1().StorageClasses().Get(context.TODO(), *spec.StorageClassName, metav1.GetOptions{}); err != nil {
if kerr.IsNotFound(err) {
return fmt.Errorf(`%v.storageClassName "%v" not found`, storagePath, *spec.StorageClassName)
}
return err
}
}
if val, found := spec.Resources.Requests[core.ResourceStorage]; found {
if val.Value() <= 0 {
return errors.New("invalid ResourceStorage request")
}
} else {
return errors.New("missing ResourceStorage request")
}
return nil
}
// ValidateMonitorSpec validates the Monitoring spec after all the defaulting is done.
func ValidateMonitorSpec(agent *mona.AgentSpec) error {
if agent.Agent == "" {
return fmt.Errorf(`object 'Agent' is missing in '%+v'`, agent)
}
if !mona.IsKnownAgentType(agent.Agent) {
return fmt.Errorf("unknown monitoring agent type %s", agent.Agent)
}
if agent.Agent.Vendor() == mona.VendorPrometheus {
if agent.Prometheus != nil &&
agent.Prometheus.Exporter.Port >= 1024 &&
agent.Prometheus.Exporter.Port <= 65535 {
return nil
}
return fmt.Errorf(`invalid 'monitor.prometheus' in '%+v'. prometheus.exporter.port value must be between 1024 and 65535, inclusive`, agent)
}
return fmt.Errorf(`invalid 'Agent' in '%+v'`, agent)
}
func IsStorageTypeCompatibleWithSpec(storageType api.StorageType, storage *core.PersistentVolumeClaimSpec, ephemeralStorage *core.EmptyDirVolumeSource) error {
if storageType == api.StorageTypeEphemeral && storage != nil {
return fmt.Errorf("'spec.storage' is not supported for Ephemeral storage type, use 'spec.ephemeralStorage' to configure Ephemeral storage type")
}
if storageType == api.StorageTypeDurable && ephemeralStorage != nil {
return fmt.Errorf("'spec.ephemeralStorage' is not supported for Durable storage type, use 'spec.storage' to configure Durable storage type")
}
return nil
}
func CheckSecretsExist(client kubernetes.Interface, secNames []string, namespace string) error {
for _, sec := range secNames {
_, err := client.CoreV1().Secrets(namespace).Get(context.TODO(), sec, metav1.GetOptions{})
if err != nil {
return err
}
}
return nil
}
func ValidateEnvVar(envs []core.EnvVar, forbiddenEnvs []string, resourceType string) error {
for _, env := range envs {
present, _ := arrays.Contains(forbiddenEnvs, env.Name)
if present {
return fmt.Errorf("environment variable %s is forbidden to use in %s spec", env.Name, resourceType)
}
}
return nil
}
func ValidateInternalUsers(users map[string]api.ElasticsearchUserSpec, allowedInternalUsers []string, resourceType string) error {
for user := range users {
present, _ := arrays.Contains(allowedInternalUsers, user)
if !present {
return fmt.Errorf("Internal user %s is forbidden to use in %s spec", user, resourceType)
}
}
return nil
}
func ValidateHealth(health *api.HealthCheckSpec) error {
if health.PeriodSeconds != nil && *health.PeriodSeconds <= 0 {
return fmt.Errorf(`spec.healthCheck.periodSeconds: can not be less than 1`)
}
if health.TimeoutSeconds != nil && *health.TimeoutSeconds <= 0 {
return fmt.Errorf(`spec.healthCheck.timeoutSeconds: can not be less than 1`)
}
if health.FailureThreshold != nil && *health.FailureThreshold <= 0 {
return fmt.Errorf(`spec.healthCheck.failureThreshold: can not be less than 1`)
}
return nil
}
func ValidateVolumes(volumes []core.Volume, reservedVolumeNames []string) error {
for _, rv := range reservedVolumeNames {
for _, ugv := range volumes {
if ugv.Name == rv {
return errors.New("Cannot use a reserve volume name: " + rv)
}
}
}
return nil
}
func ValidateMountPaths(volumeMounts []core.VolumeMount, reservedMountPaths []string) error {
trimPrefixSuffix := func(s string) string {
return strings.TrimSuffix(strings.TrimPrefix(s, "/"), "/") + "/"
}
for _, vm := range volumeMounts {
givenPath := trimPrefixSuffix(vm.MountPath)
for _, our := range reservedMountPaths {
ourPath := trimPrefixSuffix(our)
if strings.HasPrefix(givenPath, ourPath) {
return errors.New("Cannot use mountPath " + vm.MountPath + " because of reservedMountPaths path " + our)
}
}
}
return nil
}