Skip to content

Commit

Permalink
Bundle data description is actually keyed with (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored May 9, 2024
1 parent fce2a80 commit 56bb6da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
24 changes: 15 additions & 9 deletions internal/controller/constraint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}
}
Expand Down

0 comments on commit 56bb6da

Please sign in to comment.