mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
feat: create a policy utils package (#5473)
* feat: create a policy utils package Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * added comment Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
8547c8ff8c
commit
4984c5c878
3 changed files with 30 additions and 16 deletions
|
@ -1,7 +1,6 @@
|
|||
package oci
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -11,9 +10,9 @@ import (
|
|||
securejoin "github.com/cyphar/filepath-securejoin"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
policyutils "github.com/kyverno/kyverno/pkg/utils/policy"
|
||||
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
var dir string
|
||||
|
@ -97,13 +96,9 @@ kyverno oci pull -i <imgref> -d policies`,
|
|||
return fmt.Errorf("unmarshaling layer blob: %v", err)
|
||||
}
|
||||
for _, policy := range policies {
|
||||
policyJsonBytes, err := json.Marshal(policy)
|
||||
policyBytes, err := policyutils.ToYaml(policy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting policy to json: %v", err)
|
||||
}
|
||||
policyBytes, err := yaml.JSONToYAML(policyJsonBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting json to yaml: %v", err)
|
||||
return fmt.Errorf("converting policy to yaml: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, policy.GetName()+".yaml"), policyBytes, 0o600); err != nil {
|
||||
return fmt.Errorf("creating file: %v", err)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package oci
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -15,9 +14,9 @@ import (
|
|||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
|
||||
"github.com/kyverno/kyverno/pkg/openapi"
|
||||
policyvalidation "github.com/kyverno/kyverno/pkg/policy"
|
||||
policyutils "github.com/kyverno/kyverno/pkg/utils/policy"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/multierr"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
var policyRef string
|
||||
|
@ -65,13 +64,9 @@ kyverno oci push -p policies. -i <imgref>`,
|
|||
} else {
|
||||
fmt.Println("Adding cluster policy", policy.GetName(), "...")
|
||||
}
|
||||
policyJsonBytes, err := json.Marshal(policy)
|
||||
policyBytes, err := policyutils.ToYaml(policy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting policy to json: %v", err)
|
||||
}
|
||||
policyBytes, err := yaml.JSONToYAML(policyJsonBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting json to yaml: %v", err)
|
||||
return fmt.Errorf("converting policy to yaml: %v", err)
|
||||
}
|
||||
policyLayer := static.NewLayer(policyBytes, policyLayerMediaType)
|
||||
img, err = mutate.Append(img, mutate.Addendum{
|
||||
|
|
24
pkg/utils/policy/mashal.go
Normal file
24
pkg/utils/policy/mashal.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package policy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// ToJson marshals a policy into corresponding json bytes.
|
||||
func ToJson(policy kyvernov1.PolicyInterface) ([]byte, error) {
|
||||
return json.Marshal(policy)
|
||||
}
|
||||
|
||||
// ToYaml marshals a policy into corresponding yaml bytes.
|
||||
// If firsts converts the policy to json because some internal structures have
|
||||
// custom json marshalling functions, then it converts json to yaml.
|
||||
func ToYaml(policy kyvernov1.PolicyInterface) ([]byte, error) {
|
||||
jsonBytes, err := ToJson(policy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return yaml.JSONToYAML(jsonBytes)
|
||||
}
|
Loading…
Add table
Reference in a new issue