1
1
package commands
2
2
3
3
import (
4
- "encoding/json"
5
4
"fmt"
6
5
"os"
7
6
"path/filepath"
@@ -20,10 +19,6 @@ import (
20
19
"sigs.k8s.io/yaml"
21
20
)
22
21
23
- const (
24
- legacyMigrationMessage = " are set with legacy annotations, this functionality will be removed in a future release. Please migrate to OPA Metadata annotations. See konstraint convert."
25
- )
26
-
27
22
func newCreateCommand () * cobra.Command {
28
23
cmd := cobra.Command {
29
24
Use : "create <dir>" ,
@@ -64,7 +59,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
64
59
}
65
60
66
61
cmd .PersistentFlags ().StringP ("output" , "o" , "" , "Specify an output directory for the Gatekeeper resources" )
67
- cmd .PersistentFlags ().BoolP ("dryrun" , "d" , false , "Sets the enforcement action of the constraints to dryrun, overriding the @ enforcement tag " )
62
+ cmd .PersistentFlags ().BoolP ("dryrun" , "d" , false , "Sets the enforcement action of the constraints to dryrun, overriding the enforcement setting " )
68
63
cmd .PersistentFlags ().Bool ("skip-constraints" , false , "Skip generation of constraints" )
69
64
cmd .PersistentFlags ().String ("constraint-template-version" , "v1beta1" , "Set the version of ConstraintTemplates" )
70
65
cmd .PersistentFlags ().Bool ("partial-constraints" , false , "Generate partial Constraints for policies with parameters" )
@@ -132,7 +127,7 @@ func runCreateCommand(path string) error {
132
127
}
133
128
134
129
// Skip Constraint generation if there are parameters on the template.
135
- if ! viper .GetBool ("partial-constraints" ) && ( len (violation .Parameters ()) > 0 || len ( violation . AnnotationParameters ()) > 0 ) {
130
+ if ! viper .GetBool ("partial-constraints" ) && len (violation .AnnotationParameters ()) > 0 {
136
131
logger .Warn ("Skipping constraint generation due to use of parameters" )
137
132
continue
138
133
}
@@ -157,7 +152,7 @@ func runCreateCommand(path string) error {
157
152
return nil
158
153
}
159
154
160
- func getConstraintTemplatev1 (violation rego.Rego , logger * log.Entry ) * v1.ConstraintTemplate {
155
+ func getConstraintTemplatev1 (violation rego.Rego , _ * log.Entry ) * v1.ConstraintTemplate {
161
156
constraintTemplate := v1.ConstraintTemplate {
162
157
TypeMeta : metav1.TypeMeta {
163
158
APIVersion : "templates.gatekeeper.sh/v1" ,
@@ -184,20 +179,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
184
179
},
185
180
}
186
181
187
- if len (violation .Parameters ()) > 0 {
188
- logger .Warn ("Parameters" + legacyMigrationMessage )
189
- constraintTemplate .Spec .CRD .Spec .Validation = & v1.Validation {
190
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
191
- Properties : violation .GetOpenAPISchemaProperties (),
192
- Type : "object" ,
193
- },
194
- }
195
- }
196
-
197
182
if len (violation .AnnotationParameters ()) > 0 {
198
- if constraintTemplate .Spec .CRD .Spec .Validation != nil {
199
- logger .Warn ("Parameters already set with legacy annotations, overwriting the parameters using values from OPA Metadata" )
200
- }
201
183
constraintTemplate .Spec .CRD .Spec .Validation = & v1.Validation {
202
184
OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
203
185
Properties : violation .AnnotationParameters (),
@@ -209,7 +191,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
209
191
return & constraintTemplate
210
192
}
211
193
212
- func getConstraintTemplatev1beta1 (violation rego.Rego , logger * log.Entry ) * v1beta1.ConstraintTemplate {
194
+ func getConstraintTemplatev1beta1 (violation rego.Rego , _ * log.Entry ) * v1beta1.ConstraintTemplate {
213
195
constraintTemplate := v1beta1.ConstraintTemplate {
214
196
TypeMeta : metav1.TypeMeta {
215
197
APIVersion : "templates.gatekeeper.sh/v1beta1" ,
@@ -236,19 +218,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
236
218
},
237
219
}
238
220
239
- if len (violation .Parameters ()) > 0 {
240
- logger .Warn ("Parameters" + legacyMigrationMessage )
241
- constraintTemplate .Spec .CRD .Spec .Validation = & v1beta1.Validation {
242
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
243
- Properties : violation .GetOpenAPISchemaProperties (),
244
- },
245
- }
246
- }
247
-
248
221
if len (violation .AnnotationParameters ()) > 0 {
249
- if constraintTemplate .Spec .CRD .Spec .Validation != nil {
250
- logger .Warn ("Parameters already set with legacy annotations, overwriting the parameters using values from OPA Metadata" )
251
- }
252
222
constraintTemplate .Spec .CRD .Spec .Validation = & v1beta1.Validation {
253
223
OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
254
224
Properties : violation .AnnotationParameters (),
@@ -259,7 +229,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
259
229
return & constraintTemplate
260
230
}
261
231
262
- func getConstraint (violation rego.Rego , logger * log.Entry ) (* unstructured.Unstructured , error ) {
232
+ func getConstraint (violation rego.Rego , _ * log.Entry ) (* unstructured.Unstructured , error ) {
263
233
gvk := schema.GroupVersionKind {
264
234
Group : "constraints.gatekeeper.sh" ,
265
235
Version : "v1beta1" ,
@@ -292,68 +262,14 @@ func getConstraint(violation rego.Rego, logger *log.Entry) (*unstructured.Unstru
292
262
}
293
263
}
294
264
295
- matchers , err := violation .Matchers ()
296
- if err != nil {
297
- return nil , fmt .Errorf ("get matchers: %w" , err )
298
- }
299
-
300
- if len (matchers .KindMatchers ) > 0 {
301
- logger .Warn ("Kind Matchers" + legacyMigrationMessage )
302
- if err := setKindMatcher (& constraint , matchers .KindMatchers ); err != nil {
303
- return nil , fmt .Errorf ("set kind matcher: %w" , err )
304
- }
305
- }
306
-
307
- if len (matchers .MatchLabelsMatcher ) > 0 {
308
- logger .Warn ("Match Labels Matchers" + legacyMigrationMessage )
309
- if err := setMatchLabelsMatcher (& constraint , matchers .MatchLabelsMatcher ); err != nil {
310
- return nil , fmt .Errorf ("set match labels matcher: %w" , err )
311
- }
312
- }
313
-
314
- if len (matchers .MatchExpressionsMatcher ) > 0 {
315
- logger .Warn ("Match Expressions Matchers" + legacyMigrationMessage )
316
- if err := setMatchExpressionsMatcher (& constraint , matchers .MatchExpressionsMatcher ); err != nil {
317
- return nil , fmt .Errorf ("set match expressions matcher: %w" , err )
318
- }
319
- }
320
-
321
- if len (matchers .NamespaceMatcher ) > 0 {
322
- logger .Warn ("Namespace Matchers" + legacyMigrationMessage )
323
- if err := setNestedStringSlice (& constraint , matchers .NamespaceMatcher , "spec" , "match" , "namespaces" ); err != nil {
324
- return nil , fmt .Errorf ("set namespace matcher: %w" , err )
325
- }
326
- }
327
-
328
- if len (matchers .ExcludedNamespaceMatcher ) > 0 {
329
- logger .Warn ("Excluded Namespace Matchers" + legacyMigrationMessage )
330
- if err := setNestedStringSlice (& constraint , matchers .ExcludedNamespaceMatcher , "spec" , "match" , "excludedNamespaces" ); err != nil {
331
- return nil , fmt .Errorf ("set namespace matcher: %w" , err )
332
- }
333
- }
334
-
335
265
metadataMatchers , ok := violation .GetAnnotation ("matchers" )
336
266
if ok {
337
- if len (matchers .KindMatchers ) > 0 ||
338
- len (matchers .MatchLabelsMatcher ) > 0 ||
339
- len (matchers .MatchExpressionsMatcher ) > 0 ||
340
- len (matchers .NamespaceMatcher ) > 0 ||
341
- len (matchers .ExcludedNamespaceMatcher ) > 0 {
342
- logger .Warn ("Overwriting matchers set with legacy annotations using matchers from OPA Metadata." )
343
- }
344
-
345
267
if err := unstructured .SetNestedField (constraint .Object , metadataMatchers , "spec" , "match" ); err != nil {
346
268
return nil , fmt .Errorf ("set matchers from metadata annotation: %w" , err )
347
269
}
348
270
}
349
271
350
272
if viper .GetBool ("partial-constraints" ) {
351
- if len (violation .Parameters ()) > 0 {
352
- logger .Warn ("Parameters" + legacyMigrationMessage )
353
- if err := addParametersToConstraintLegacy (& constraint , violation .Parameters ()); err != nil {
354
- return nil , fmt .Errorf ("add parameters %v to constraint: %w" , violation .Parameters (), err )
355
- }
356
- }
357
273
if len (violation .AnnotationParameters ()) > 0 {
358
274
if err := addParametersToConstraint (& constraint , violation .AnnotationParameters ()); err != nil {
359
275
return nil , fmt .Errorf ("add parameters %v to constraint: %w" , violation .AnnotationParameters (), err )
@@ -376,52 +292,6 @@ func addParametersToConstraint(constraint *unstructured.Unstructured, parameters
376
292
return nil
377
293
}
378
294
379
- func addParametersToConstraintLegacy (constraint * unstructured.Unstructured , parameters []rego.Parameter ) error {
380
- params := make (map [string ]interface {}, len (parameters ))
381
- for _ , p := range parameters {
382
- params [p .Name ] = nil
383
- }
384
- if err := unstructured .SetNestedField (constraint .Object , params , "spec" , "parameters" ); err != nil {
385
- return fmt .Errorf ("set parameters map: %w" , err )
386
- }
387
-
388
- return nil
389
- }
390
-
391
- func setKindMatcher (constraint * unstructured.Unstructured , kindMatchers rego.KindMatchers ) error {
392
- if err := unstructured .SetNestedSlice (constraint .Object , kindMatchers .ToSpec (), "spec" , "match" , "kinds" ); err != nil {
393
- return fmt .Errorf ("set constraint kinds matchers: %w" , err )
394
- }
395
- return nil
396
- }
397
-
398
- func setMatchLabelsMatcher (constraint * unstructured.Unstructured , matcher rego.MatchLabelsMatcher ) error {
399
- if err := unstructured .SetNestedStringMap (constraint .Object , matcher , "spec" , "match" , "labelSelector" , "matchLabels" ); err != nil {
400
- return fmt .Errorf ("set constraint labelSelector.matchLabels matchers: %w" , err )
401
- }
402
- return nil
403
- }
404
-
405
- func setMatchExpressionsMatcher (constraint * unstructured.Unstructured , matcher []rego.MatchExpressionMatcher ) error {
406
- marshalled , err := json .Marshal (matcher )
407
- if err != nil {
408
- return err
409
- }
410
- var unmarshalled []interface {}
411
- if err := json .Unmarshal (marshalled , & unmarshalled ); err != nil {
412
- return err
413
- }
414
- return unstructured .SetNestedSlice (constraint .Object , unmarshalled , "spec" , "match" , "labelSelector" , "matchExpressions" )
415
- }
416
-
417
- func setNestedStringSlice (constraint * unstructured.Unstructured , slice []string , path ... string ) error {
418
- var values []interface {}
419
- for _ , s := range slice {
420
- values = append (values , interface {}(s ))
421
- }
422
- return unstructured .SetNestedSlice (constraint .Object , values , path ... )
423
- }
424
-
425
295
func isValidEnforcementAction (action string ) bool {
426
296
for _ , a := range []string {"deny" , "dryrun" , "warn" } {
427
297
if a == action {
0 commit comments