Skip to content

Commit 914a3c9

Browse files
committed
Remove support for legacy annotations
1 parent fdfe524 commit 914a3c9

11 files changed

+66
-1121
lines changed

docs/cli/konstraint.md

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ A tool to create and manage Gatekeeper CRDs from Rego
1414

1515
### SEE ALSO
1616

17-
* [konstraint convert](konstraint_convert.md) - Convert legacy annotations to OPA Metadata Annotations
1817
* [konstraint create](konstraint_create.md) - Create Gatekeeper constraints from Rego policies
1918
* [konstraint doc](konstraint_doc.md) - Generate documentation from Rego policies
2019

docs/cli/konstraint_create.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
2323

2424
```
2525
--constraint-template-version string Set the version of ConstraintTemplates (default "v1beta1")
26-
-d, --dryrun Sets the enforcement action of the constraints to dryrun, overriding the @enforcement tag
26+
-d, --dryrun Sets the enforcement action of the constraints to dryrun, overriding the enforcement setting
2727
-h, --help help for create
2828
-o, --output string Specify an output directory for the Gatekeeper resources
2929
--partial-constraints Generate partial Constraints for policies with parameters

docs/constraint_creation.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ in the custom metadata section.
112112

113113
### Legacy annotations
114114

115-
Previously Konstraint had custom annotation format, such as `@title` or `@kinds`, which is a legacy format and will be removed in future releases.
116-
117-
To aid with transition to OPA Metadata format, a conversion tool is provided as `konstraint convert`
115+
Previously Konstraint had custom annotation format, such as `@title` or `@kinds`, which is a legacy format and were removed in release v0.39.0.
118116

119117
## Using Input Parameters
120118

internal/commands/convert.go

-123
This file was deleted.

internal/commands/create.go

+5-135
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
76
"path/filepath"
@@ -20,10 +19,6 @@ import (
2019
"sigs.k8s.io/yaml"
2120
)
2221

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-
2722
func newCreateCommand() *cobra.Command {
2823
cmd := cobra.Command{
2924
Use: "create <dir>",
@@ -64,7 +59,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
6459
}
6560

6661
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")
6863
cmd.PersistentFlags().Bool("skip-constraints", false, "Skip generation of constraints")
6964
cmd.PersistentFlags().String("constraint-template-version", "v1beta1", "Set the version of ConstraintTemplates")
7065
cmd.PersistentFlags().Bool("partial-constraints", false, "Generate partial Constraints for policies with parameters")
@@ -132,7 +127,7 @@ func runCreateCommand(path string) error {
132127
}
133128

134129
// 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 {
136131
logger.Warn("Skipping constraint generation due to use of parameters")
137132
continue
138133
}
@@ -157,7 +152,7 @@ func runCreateCommand(path string) error {
157152
return nil
158153
}
159154

160-
func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.ConstraintTemplate {
155+
func getConstraintTemplatev1(violation rego.Rego, _ *log.Entry) *v1.ConstraintTemplate {
161156
constraintTemplate := v1.ConstraintTemplate{
162157
TypeMeta: metav1.TypeMeta{
163158
APIVersion: "templates.gatekeeper.sh/v1",
@@ -184,20 +179,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
184179
},
185180
}
186181

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-
197182
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-
}
201183
constraintTemplate.Spec.CRD.Spec.Validation = &v1.Validation{
202184
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
203185
Properties: violation.AnnotationParameters(),
@@ -209,7 +191,7 @@ func getConstraintTemplatev1(violation rego.Rego, logger *log.Entry) *v1.Constra
209191
return &constraintTemplate
210192
}
211193

212-
func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1beta1.ConstraintTemplate {
194+
func getConstraintTemplatev1beta1(violation rego.Rego, _ *log.Entry) *v1beta1.ConstraintTemplate {
213195
constraintTemplate := v1beta1.ConstraintTemplate{
214196
TypeMeta: metav1.TypeMeta{
215197
APIVersion: "templates.gatekeeper.sh/v1beta1",
@@ -236,19 +218,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
236218
},
237219
}
238220

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-
248221
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-
}
252222
constraintTemplate.Spec.CRD.Spec.Validation = &v1beta1.Validation{
253223
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
254224
Properties: violation.AnnotationParameters(),
@@ -259,7 +229,7 @@ func getConstraintTemplatev1beta1(violation rego.Rego, logger *log.Entry) *v1bet
259229
return &constraintTemplate
260230
}
261231

262-
func getConstraint(violation rego.Rego, logger *log.Entry) (*unstructured.Unstructured, error) {
232+
func getConstraint(violation rego.Rego, _ *log.Entry) (*unstructured.Unstructured, error) {
263233
gvk := schema.GroupVersionKind{
264234
Group: "constraints.gatekeeper.sh",
265235
Version: "v1beta1",
@@ -292,68 +262,14 @@ func getConstraint(violation rego.Rego, logger *log.Entry) (*unstructured.Unstru
292262
}
293263
}
294264

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-
335265
metadataMatchers, ok := violation.GetAnnotation("matchers")
336266
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-
345267
if err := unstructured.SetNestedField(constraint.Object, metadataMatchers, "spec", "match"); err != nil {
346268
return nil, fmt.Errorf("set matchers from metadata annotation: %w", err)
347269
}
348270
}
349271

350272
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-
}
357273
if len(violation.AnnotationParameters()) > 0 {
358274
if err := addParametersToConstraint(&constraint, violation.AnnotationParameters()); err != nil {
359275
return nil, fmt.Errorf("add parameters %v to constraint: %w", violation.AnnotationParameters(), err)
@@ -376,52 +292,6 @@ func addParametersToConstraint(constraint *unstructured.Unstructured, parameters
376292
return nil
377293
}
378294

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-
425295
func isValidEnforcementAction(action string) bool {
426296
for _, a := range []string{"deny", "dryrun", "warn"} {
427297
if a == action {

internal/commands/default.go

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func NewDefaultCommand() *cobra.Command {
2424

2525
cmd.AddCommand(newCreateCommand())
2626
cmd.AddCommand(newDocCommand())
27-
cmd.AddCommand(newConvertCommand())
2827

2928
return &cmd
3029
}

0 commit comments

Comments
 (0)