Skip to content

Commit fc02256

Browse files
Fix Ops type validating (#1517)
Signed-off-by: obaydullahmhs <obaydullah@appscode.com>
1 parent fed65f0 commit fc02256

24 files changed

Lines changed: 117 additions & 102 deletions

pkg/webhooks/ops/v1alpha1/cassandra.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func (w *CassandraOpsRequestCustomWebhook) ValidateDelete(ctx context.Context, o
8282
}
8383

8484
func (rv *CassandraOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.CassandraOpsRequest) error {
85+
if validType, _ := arrays.Contains(opsapi.CassandraOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
86+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
87+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Cassandra are %s", req.Spec.Type, strings.Join(opsapi.CassandraOpsRequestTypeNames(), ", ")))
88+
}
89+
8590
var allErr field.ErrorList
8691
switch req.GetRequestType().(opsapi.CassandraOpsRequestType) {
8792
case opsapi.CassandraOpsRequestTypeRestart:
@@ -136,10 +141,6 @@ func (rv *CassandraOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.C
136141
}
137142
}
138143

139-
if validType, _ := arrays.Contains(opsapi.CassandraOpsRequestTypeNames(), req.Spec.Type); !validType {
140-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
141-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Cassandra are %s", req.Spec.Type, strings.Join(opsapi.CassandraOpsRequestTypeNames(), ", "))))
142-
}
143144
if len(allErr) == 0 {
144145
return nil
145146
}

pkg/webhooks/ops/v1alpha1/clickhouse.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func (w *ClickHouseOpsRequestCustomWebhook) ValidateDelete(ctx context.Context,
7979
}
8080

8181
func (rv *ClickHouseOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.ClickHouseOpsRequest) error {
82+
if validType, _ := arrays.Contains(opsapi.ClickHouseOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
83+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
84+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for ClickHouse are %s", req.Spec.Type, strings.Join(opsapi.ClickHouseOpsRequestTypeNames(), ", ")))
85+
}
8286
var allErr field.ErrorList
8387
switch req.GetRequestType().(opsapi.ClickHouseOpsRequestType) {
8488
case opsapi.ClickHouseOpsRequestTypeRestart:
@@ -94,10 +98,7 @@ func (rv *ClickHouseOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.
9498
err.Error()))
9599
}
96100
}
97-
if validType, _ := arrays.Contains(opsapi.ClickHouseOpsRequestTypeNames(), req.Spec.Type); !validType {
98-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
99-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for ClickHouse are %s", req.Spec.Type, strings.Join(opsapi.ClickHouseOpsRequestTypeNames(), ", "))))
100-
}
101+
101102
if len(allErr) == 0 {
102103
return nil
103104
}

pkg/webhooks/ops/v1alpha1/druid.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func validateDruidOpsRequest(req *opsapi.DruidOpsRequest, oldReq *opsapi.DruidOp
110110
}
111111

112112
func (w *DruidOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.DruidOpsRequest) error {
113+
if validType, _ := arrays.Contains(opsapi.DruidOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
114+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
115+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Druid are %s", req.Spec.Type, strings.Join(opsapi.DruidOpsRequestTypeNames(), ", ")))
116+
}
113117
druid, err := w.hasDatabaseRef(req)
114118
if err != nil {
115119
return err
@@ -169,10 +173,6 @@ func (w *DruidOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.DruidO
169173
}
170174
}
171175

172-
if validType, _ := arrays.Contains(opsapi.DruidOpsRequestTypeNames(), req.Spec.Type); !validType {
173-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
174-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Druid are %s", req.Spec.Type, strings.Join(opsapi.DruidOpsRequestTypeNames(), ", "))))
175-
}
176176
if len(allErr) == 0 {
177177
return nil
178178
}

pkg/webhooks/ops/v1alpha1/elasticsearch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func (w *ElasticsearchOpsRequestCustomWebhook) validateOpensearchVersionCompatib
130130
}
131131

132132
func (w *ElasticsearchOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.ElasticsearchOpsRequest) error {
133+
if validType, _ := arrays.Contains(opsapi.ElasticsearchOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
134+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
135+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Elasticsearch are %s", req.Spec.Type, strings.Join(opsapi.ElasticsearchOpsRequestTypeNames(), ", ")))
136+
}
133137
var allErr field.ErrorList
134138
db := &dbapi.Elasticsearch{}
135139
err := w.DefaultClient.Get(context.TODO(), types.NamespacedName{
@@ -155,10 +159,6 @@ func (w *ElasticsearchOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsap
155159
}
156160
}
157161

158-
if validType, _ := arrays.Contains(opsapi.ElasticsearchOpsRequestTypeNames(), req.Spec.Type); !validType {
159-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
160-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Elasticsearch are %s", req.Spec.Type, strings.Join(opsapi.ElasticsearchOpsRequestTypeNames(), ", "))))
161-
}
162162
if len(allErr) == 0 {
163163
return nil
164164
}

pkg/webhooks/ops/v1alpha1/ferretdb.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func validateFerretDBOpsRequest(req *opsapi.FerretDBOpsRequest, oldReq *opsapi.F
101101
}
102102

103103
func (w *FerretDBOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.FerretDBOpsRequest) error {
104+
if validType, _ := arrays.Contains(opsapi.FerretDBOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
105+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
106+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for FerretDB are %s", req.Spec.Type, strings.Join(opsapi.FerretDBOpsRequestTypeNames(), ", ")))
107+
}
108+
104109
var allErr field.ErrorList
105110
switch req.GetRequestType().(opsapi.FerretDBOpsRequestType) {
106111
case opsapi.FerretDBOpsRequestTypeRestart:
@@ -124,10 +129,6 @@ func (w *FerretDBOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.Fer
124129
}
125130
}
126131

127-
if validType, _ := arrays.Contains(opsapi.FerretDBOpsRequestTypeNames(), req.Spec.Type); !validType {
128-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
129-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for FerretDB are %s", req.Spec.Type, strings.Join(opsapi.FerretDBOpsRequestTypeNames(), ", "))))
130-
}
131132
if len(allErr) == 0 {
132133
return nil
133134
}

pkg/webhooks/ops/v1alpha1/hazelcast.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func validateHazelcastOpsRequest(req *opsapi.HazelcastOpsRequest, oldReq *opsapi
105105
}
106106

107107
func (w *HazelcastOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.HazelcastOpsRequest) error {
108+
if validType, _ := arrays.Contains(opsapi.HazelcastOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
109+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
110+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Hazelcast are %s", req.Spec.Type, strings.Join(opsapi.HazelcastOpsRequestTypeNames(), ", ")))
111+
}
112+
108113
var allErr field.ErrorList
109114
switch req.GetRequestType().(opsapi.HazelcastOpsRequestType) {
110115
case opsapi.HazelcastOpsRequestTypeRestart:
@@ -157,10 +162,6 @@ func (w *HazelcastOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.Ha
157162
}
158163
}
159164

160-
if validType, _ := arrays.Contains(opsapi.HazelcastOpsRequestTypeNames(), req.Spec.Type); !validType {
161-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
162-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Hazelcast are %s", req.Spec.Type, strings.Join(opsapi.HazelcastOpsRequestTypeNames(), ", "))))
163-
}
164165
if len(allErr) == 0 {
165166
return nil
166167
}

pkg/webhooks/ops/v1alpha1/ignite.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func validateIgniteOpsRequest(req *opsapi.IgniteOpsRequest, oldReq *opsapi.Ignit
105105
}
106106

107107
func (rv *IgniteOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.IgniteOpsRequest) error {
108+
if validType, _ := arrays.Contains(opsapi.IgniteOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
109+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
110+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Ignite are %s", req.Spec.Type, strings.Join(opsapi.IgniteOpsRequestTypeNames(), ", ")))
111+
}
112+
108113
var allErr field.ErrorList
109114
switch req.GetRequestType().(opsapi.IgniteOpsRequestType) {
110115
case opsapi.IgniteOpsRequestTypeRestart:
@@ -157,10 +162,6 @@ func (rv *IgniteOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.Igni
157162
}
158163
}
159164

160-
if validType, _ := arrays.Contains(opsapi.IgniteOpsRequestTypeNames(), req.Spec.Type); !validType {
161-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
162-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Ignite are %s", req.Spec.Type, strings.Join(opsapi.IgniteOpsRequestTypeNames(), ", "))))
163-
}
164165
if len(allErr) == 0 {
165166
return nil
166167
}

pkg/webhooks/ops/v1alpha1/kafka.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func validateKafkaOpsRequest(req *opsapi.KafkaOpsRequest, oldReq *opsapi.KafkaOp
110110
}
111111

112112
func (w *KafkaOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.KafkaOpsRequest) error {
113+
if validType, _ := arrays.Contains(opsapi.KafkaOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
114+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
115+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Kafka are %s", req.Spec.Type, strings.Join(opsapi.KafkaOpsRequestTypeNames(), ", ")))
116+
}
117+
113118
var (
114119
err error
115120
kafka *dbapi.Kafka
@@ -164,10 +169,6 @@ func (w *KafkaOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.KafkaO
164169
}
165170
}
166171

167-
if validType, _ := arrays.Contains(opsapi.KafkaOpsRequestTypeNames(), req.Spec.Type); !validType {
168-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
169-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for kafka are %s", req.Spec.Type, strings.Join(opsapi.KafkaOpsRequestTypeNames(), ", "))))
170-
}
171172
if len(allErr) == 0 {
172173
return nil
173174
}

pkg/webhooks/ops/v1alpha1/mariadb.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func (w *MariaDBOpsRequestCustomWebhook) ValidateDelete(ctx context.Context, obj
9191
}
9292

9393
func (w *MariaDBOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.MariaDBOpsRequest) error {
94+
if validType, _ := arrays.Contains(opsapi.MariaDBOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
95+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
96+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for MariaDB are %s", req.Spec.Type, strings.Join(opsapi.MariaDBOpsRequestTypeNames(), ", ")))
97+
}
98+
9499
var allErr field.ErrorList
95100
switch req.GetRequestType().(opsapi.MariaDBOpsRequestType) {
96101
case opsapi.MariaDBOpsRequestTypeRestart:
@@ -144,10 +149,6 @@ func (w *MariaDBOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.Mari
144149

145150
}
146151

147-
if validType, _ := arrays.Contains(opsapi.MariaDBOpsRequestTypeNames(), req.Spec.Type); !validType {
148-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
149-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for MariaDB are %s", req.Spec.Type, strings.Join(opsapi.MariaDBOpsRequestTypeNames(), ", "))))
150-
}
151152
if len(allErr) == 0 {
152153
return nil
153154
}

pkg/webhooks/ops/v1alpha1/memcached.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ func validateMemcachedOpsRequest(req *opsapi.MemcachedOpsRequest, oldReq *opsapi
106106
}
107107

108108
func (c *MemcachedOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.MemcachedOpsRequest) error {
109+
if validType, _ := arrays.Contains(opsapi.MemcachedOpsRequestTypeNames(), string(req.Spec.Type)); !validType {
110+
return field.Invalid(field.NewPath("spec").Child("type"), req.Name,
111+
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Memcached are %s", req.Spec.Type, strings.Join(opsapi.MemcachedOpsRequestTypeNames(), ", ")))
112+
}
113+
109114
var allErr field.ErrorList
110115
switch req.GetRequestType().(opsapi.MemcachedOpsRequestType) {
111116
case opsapi.MemcachedOpsRequestTypeRestart:
@@ -153,10 +158,6 @@ func (c *MemcachedOpsRequestCustomWebhook) validateCreateOrUpdate(req *opsapi.Me
153158
}
154159
}
155160

156-
if validType, _ := arrays.Contains(opsapi.MemcachedOpsRequestTypeNames(), req.Spec.Type); !validType {
157-
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("type"), req.Name,
158-
fmt.Sprintf("defined OpsRequestType %s is not supported, supported types for Memcached are %s", req.Spec.Type, strings.Join(opsapi.MemcachedOpsRequestTypeNames(), ", "))))
159-
}
160161
if len(allErr) == 0 {
161162
return nil
162163
}

0 commit comments

Comments
 (0)