From 56bb6da85d9b51727f368ed4c32e3f8cb088d9b1 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 9 May 2024 02:16:38 -0400 Subject: [PATCH] Bundle data description is actually keyed with (#183) --- go.mod | 2 +- go.sum | 2 -- internal/controller/constraint_controller.go | 24 ++++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 24278164..7b153f79 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/yuin/gopher-lua v1.1.1 go.uber.org/zap v1.27.0 golang.org/x/net v0.23.0 + gopkg.in/yaml.v3 v3.0.1 helm.sh/helm/v3 v3.14.3 k8s.io/api v0.29.2 k8s.io/apiextensions-apiserver v0.29.0 @@ -223,7 +224,6 @@ require ( gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiserver v0.29.0 // indirect k8s.io/component-base v0.29.2 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect diff --git a/go.sum b/go.sum index bd0a5268..88f1d883 100644 --- a/go.sum +++ b/go.sum @@ -526,8 +526,6 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= -github.com/pluralsh/console-client-go v0.5.2 h1:vDiKzZ/vPFivr9TIXSSi/6Q1nOrH4y1huE5XkrCJ3D0= -github.com/pluralsh/console-client-go v0.5.2/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo= github.com/pluralsh/console-client-go v0.5.6 h1:8CUQco0vJehtKabVVNHAkFE4V9UI9MaMKvYNgQRrJdo= github.com/pluralsh/console-client-go v0.5.6/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo= github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E= diff --git a/internal/controller/constraint_controller.go b/internal/controller/constraint_controller.go index e2b27cfc..9fb428a8 100644 --- a/internal/controller/constraint_controller.go +++ b/internal/controller/constraint_controller.go @@ -2,9 +2,10 @@ package controller import ( "context" - "encoding/json" "fmt" + "gopkg.in/yaml.v3" + templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1" "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1" constraintstatusv1beta1 "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1" @@ -29,11 +30,11 @@ const ( ) type BundleData struct { - Description string `json:"description"` - Severity string `json:"severity"` - BundleName string `json:"bundleName"` - BundleDisplayName string `json:"bundleDisplayName"` - Remediation string `json:"remediation"` + Description string `json:"description" yaml:"description"` + Severity string `json:"severity" yaml:"severity"` + BundleName string `json:"bundleName" yaml:"bundleName"` + BundleDisplayName string `json:"bundleDisplayName" yaml:"bundleDisplayName"` + Remediation string `json:"remediation" yaml:"remediation"` } type StatusViolation struct { @@ -86,6 +87,8 @@ func (r *ConstraintReconciler) Reconcile(ctx context.Context, req ctrl.Request) if err != nil { return ctrl.Result{}, err } + + logger.Info("recording constraint", "name", pca.Name) r.Constraints[pca.Name] = pca res, err := r.ConsoleClient.UpsertConstraints(algorithms.MapValues[string, *console.PolicyConstraintAttributes](r.Constraints)) if err != nil { @@ -114,12 +117,15 @@ func GenerateAPIConstraint(instance *unstructured.Unstructured, template *templa }, } - if template.Annotations != nil { + if annotations := instance.GetAnnotations(); annotations != nil { var bundleData BundleData - if d, ok := template.Annotations[bundleDataAnnotation]; ok { - if err := json.Unmarshal([]byte(d), &bundleData); err != nil { + if d, ok := annotations[bundleDataAnnotation]; ok { + fmt.Printf("found bundle data: %s\n", d) + if err := yaml.Unmarshal([]byte(d), &bundleData); err != nil { pca.Description = lo.ToPtr(bundleData.Description) pca.Recommendation = lo.ToPtr(bundleData.Remediation) + } else { + fmt.Printf("Could not parse bundle data %s\n", err.Error()) } } }