@@ -2,7 +2,6 @@ package commands
2
2
3
3
import (
4
4
"bytes"
5
- "encoding/json"
6
5
"fmt"
7
6
"os"
8
7
"path/filepath"
@@ -23,10 +22,6 @@ import (
23
22
"sigs.k8s.io/yaml"
24
23
)
25
24
26
- const (
27
- legacyMigrationMessage = " are set with legacy annotations, this functionality will be removed in a future release. Please migrate to OPA Metadata annotations. See konstraint convert."
28
- )
29
-
30
25
func newCreateCommand () * cobra.Command {
31
26
cmd := cobra.Command {
32
27
Use : "create <dir>" ,
@@ -88,7 +83,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
88
83
}
89
84
90
85
cmd .PersistentFlags ().StringP ("output" , "o" , "" , "Specify an output directory for the Gatekeeper resources" )
91
- cmd .PersistentFlags ().BoolP ("dryrun" , "d" , false , "Set the enforcement action of the constraints to dryrun, overriding the @ enforcement tag " )
86
+ cmd .PersistentFlags ().BoolP ("dryrun" , "d" , false , "Set the enforcement action of the constraints to dryrun, overriding the enforcement setting " )
92
87
cmd .PersistentFlags ().Bool ("skip-constraints" , false , "Skip generation of constraints" )
93
88
cmd .PersistentFlags ().String ("constraint-template-version" , "v1" , "Set the version of ConstraintTemplates" )
94
89
cmd .PersistentFlags ().Bool ("partial-constraints" , false , "Generate partial Constraints for policies with parameters" )
@@ -152,7 +147,7 @@ func runCreateCommand(path string) error {
152
147
}
153
148
154
149
// Skip Constraint generation if there are parameters on the template.
155
- if ! viper .GetBool ("partial-constraints" ) && ( len (violation .Parameters ()) > 0 || len ( violation . AnnotationParameters ()) > 0 ) {
150
+ if ! viper .GetBool ("partial-constraints" ) && len (violation .AnnotationParameters ()) > 0 {
156
151
logger .Warn ("Skipping constraint generation due to use of parameters" )
157
152
continue
158
153
}
@@ -247,7 +242,7 @@ func renderTemplate(violation rego.Rego, appliedTemplate []byte) ([]byte, error)
247
242
return buf .Bytes (), nil
248
243
}
249
244
250
- func getConstraintTemplatev1 (violation rego.Rego , logger * log.Entry ) * v1.ConstraintTemplate {
245
+ func getConstraintTemplatev1 (violation rego.Rego , _ * log.Entry ) * v1.ConstraintTemplate {
251
246
constraintTemplate := v1.ConstraintTemplate {
252
247
TypeMeta : metav1.TypeMeta {
253
248
APIVersion : "templates.gatekeeper.sh/v1" ,
@@ -274,20 +269,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
274
269
},
275
270
}
276
271
277
- if len (violation .Parameters ()) > 0 {
278
- logger .Warn ("Parameters" + legacyMigrationMessage )
279
- constraintTemplate .Spec .CRD .Spec .Validation = & v1.Validation {
280
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
281
- Properties : violation .GetOpenAPISchemaProperties (),
282
- Type : "object" ,
283
- },
284
- }
285
- }
286
-
287
272
if len (violation .AnnotationParameters ()) > 0 {
288
- if constraintTemplate .Spec .CRD .Spec .Validation != nil {
289
- logger .Warn ("Parameters already set with legacy annotations, overwriting the parameters using values from OPA Metadata" )
290
- }
291
273
constraintTemplate .Spec .CRD .Spec .Validation = & v1.Validation {
292
274
OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
293
275
Properties : violation .AnnotationParameters (),
@@ -299,7 +281,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
299
281
return & constraintTemplate
300
282
}
301
283
302
- func getConstraintTemplatev1beta1 (violation rego.Rego , logger * log.Entry ) * v1beta1.ConstraintTemplate {
284
+ func getConstraintTemplatev1beta1 (violation rego.Rego , _ * log.Entry ) * v1beta1.ConstraintTemplate {
303
285
constraintTemplate := v1beta1.ConstraintTemplate {
304
286
TypeMeta : metav1.TypeMeta {
305
287
APIVersion : "templates.gatekeeper.sh/v1beta1" ,
@@ -326,19 +308,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
326
308
},
327
309
}
328
310
329
- if len (violation .Parameters ()) > 0 {
330
- logger .Warn ("Parameters" + legacyMigrationMessage )
331
- constraintTemplate .Spec .CRD .Spec .Validation = & v1beta1.Validation {
332
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
333
- Properties : violation .GetOpenAPISchemaProperties (),
334
- },
335
- }
336
- }
337
-
338
311
if len (violation .AnnotationParameters ()) > 0 {
339
- if constraintTemplate .Spec .CRD .Spec .Validation != nil {
340
- logger .Warn ("Parameters already set with legacy annotations, overwriting the parameters using values from OPA Metadata" )
341
- }
342
312
constraintTemplate .Spec .CRD .Spec .Validation = & v1beta1.Validation {
343
313
OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
344
314
Properties : violation .AnnotationParameters (),
@@ -349,7 +319,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
349
319
return & constraintTemplate
350
320
}
351
321
352
- func getConstraint (violation rego.Rego , logger * log.Entry ) (* unstructured.Unstructured , error ) {
322
+ func getConstraint (violation rego.Rego , _ * log.Entry ) (* unstructured.Unstructured , error ) {
353
323
gvk := schema.GroupVersionKind {
354
324
Group : "constraints.gatekeeper.sh" ,
355
325
Version : "v1beta1" ,
@@ -382,68 +352,14 @@ func getConstraint(violation rego.Rego, logger *log.Entry) (*unstructured.Unstru
382
352
}
383
353
}
384
354
385
- matchers , err := violation .Matchers ()
386
- if err != nil {
387
- return nil , fmt .Errorf ("get matchers: %w" , err )
388
- }
389
-
390
- if len (matchers .KindMatchers ) > 0 {
391
- logger .Warn ("Kind Matchers" + legacyMigrationMessage )
392
- if err := setKindMatcher (& constraint , matchers .KindMatchers ); err != nil {
393
- return nil , fmt .Errorf ("set kind matcher: %w" , err )
394
- }
395
- }
396
-
397
- if len (matchers .MatchLabelsMatcher ) > 0 {
398
- logger .Warn ("Match Labels Matchers" + legacyMigrationMessage )
399
- if err := setMatchLabelsMatcher (& constraint , matchers .MatchLabelsMatcher ); err != nil {
400
- return nil , fmt .Errorf ("set match labels matcher: %w" , err )
401
- }
402
- }
403
-
404
- if len (matchers .MatchExpressionsMatcher ) > 0 {
405
- logger .Warn ("Match Expressions Matchers" + legacyMigrationMessage )
406
- if err := setMatchExpressionsMatcher (& constraint , matchers .MatchExpressionsMatcher ); err != nil {
407
- return nil , fmt .Errorf ("set match expressions matcher: %w" , err )
408
- }
409
- }
410
-
411
- if len (matchers .NamespaceMatcher ) > 0 {
412
- logger .Warn ("Namespace Matchers" + legacyMigrationMessage )
413
- if err := setNestedStringSlice (& constraint , matchers .NamespaceMatcher , "spec" , "match" , "namespaces" ); err != nil {
414
- return nil , fmt .Errorf ("set namespace matcher: %w" , err )
415
- }
416
- }
417
-
418
- if len (matchers .ExcludedNamespaceMatcher ) > 0 {
419
- logger .Warn ("Excluded Namespace Matchers" + legacyMigrationMessage )
420
- if err := setNestedStringSlice (& constraint , matchers .ExcludedNamespaceMatcher , "spec" , "match" , "excludedNamespaces" ); err != nil {
421
- return nil , fmt .Errorf ("set namespace matcher: %w" , err )
422
- }
423
- }
424
-
425
355
metadataMatchers , err := violation .GetAnnotation ("matchers" )
426
356
if err == nil {
427
- if len (matchers .KindMatchers ) > 0 ||
428
- len (matchers .MatchLabelsMatcher ) > 0 ||
429
- len (matchers .MatchExpressionsMatcher ) > 0 ||
430
- len (matchers .NamespaceMatcher ) > 0 ||
431
- len (matchers .ExcludedNamespaceMatcher ) > 0 {
432
- logger .Warn ("Overwriting matchers set with legacy annotations using matchers from OPA Metadata." )
433
- }
434
-
435
357
if err := unstructured .SetNestedField (constraint .Object , metadataMatchers , "spec" , "match" ); err != nil {
436
358
return nil , fmt .Errorf ("set matchers from metadata annotation: %w" , err )
437
359
}
438
360
}
439
361
440
362
if viper .GetBool ("partial-constraints" ) {
441
- if len (violation .Parameters ()) > 0 {
442
- logger .Warn ("Parameters" + legacyMigrationMessage )
443
- if err := addParametersToConstraintLegacy (& constraint , violation .Parameters ()); err != nil {
444
- return nil , fmt .Errorf ("add parameters %v to constraint: %w" , violation .Parameters (), err )
445
- }
446
- }
447
363
if len (violation .AnnotationParameters ()) > 0 {
448
364
if err := addParametersToConstraint (& constraint , violation .AnnotationParameters ()); err != nil {
449
365
return nil , fmt .Errorf ("add parameters %v to constraint: %w" , violation .AnnotationParameters (), err )
@@ -466,52 +382,6 @@ func addParametersToConstraint(constraint *unstructured.Unstructured, parameters
466
382
return nil
467
383
}
468
384
469
- func addParametersToConstraintLegacy (constraint * unstructured.Unstructured , parameters []rego.Parameter ) error {
470
- params := make (map [string ]interface {}, len (parameters ))
471
- for _ , p := range parameters {
472
- params [p .Name ] = nil
473
- }
474
- if err := unstructured .SetNestedField (constraint .Object , params , "spec" , "parameters" ); err != nil {
475
- return fmt .Errorf ("set parameters map: %w" , err )
476
- }
477
-
478
- return nil
479
- }
480
-
481
- func setKindMatcher (constraint * unstructured.Unstructured , kindMatchers rego.KindMatchers ) error {
482
- if err := unstructured .SetNestedSlice (constraint .Object , kindMatchers .ToSpec (), "spec" , "match" , "kinds" ); err != nil {
483
- return fmt .Errorf ("set constraint kinds matchers: %w" , err )
484
- }
485
- return nil
486
- }
487
-
488
- func setMatchLabelsMatcher (constraint * unstructured.Unstructured , matcher rego.MatchLabelsMatcher ) error {
489
- if err := unstructured .SetNestedStringMap (constraint .Object , matcher , "spec" , "match" , "labelSelector" , "matchLabels" ); err != nil {
490
- return fmt .Errorf ("set constraint labelSelector.matchLabels matchers: %w" , err )
491
- }
492
- return nil
493
- }
494
-
495
- func setMatchExpressionsMatcher (constraint * unstructured.Unstructured , matcher []rego.MatchExpressionMatcher ) error {
496
- marshaled , err := json .Marshal (matcher )
497
- if err != nil {
498
- return err
499
- }
500
- var unmarshaled []interface {}
501
- if err := json .Unmarshal (marshaled , & unmarshaled ); err != nil {
502
- return err
503
- }
504
- return unstructured .SetNestedSlice (constraint .Object , unmarshaled , "spec" , "match" , "labelSelector" , "matchExpressions" )
505
- }
506
-
507
- func setNestedStringSlice (constraint * unstructured.Unstructured , slice []string , path ... string ) error {
508
- var values []interface {}
509
- for _ , s := range slice {
510
- values = append (values , interface {}(s ))
511
- }
512
- return unstructured .SetNestedSlice (constraint .Object , values , path ... )
513
- }
514
-
515
385
func isValidEnforcementAction (action string ) bool {
516
386
for _ , a := range []string {"deny" , "dryrun" , "warn" } {
517
387
if a == action {
0 commit comments