parse multiple documents defined in one yaml file
package apply
import (
+ "bytes"
"encoding/json"
"fmt"
"io"
@@ -22,11 +23,15 @@ import (
"k8s.io/client-go/restmapper"
)
-const applyExample = ` # Apply a policy to the resource.
+const (
+ applyExample = ` # Apply a policy to the resource.
kyverno apply @policy.yaml @resource.yaml
kyverno apply @policy.yaml @resourceDir/
kyverno apply @policy.yaml @resource.yaml --kubeconfig=$PATH_TO_KUBECONFIG_FILE`
+ defaultYamlSeparator = "---"
+)
+
// NewCmdApply returns the apply command for kyverno
func NewCmdApply(in io.Reader, out, errout io.Writer) *cobra.Command {
var kubeconfig string
@@ -153,8 +158,11 @@ func extractResource(fileDir, kubeconfig string) ([]*resourceInfo, error) {
continue
}
+ dd := bytes.Split(data, []byte(defaultYamlSeparator))
+
+ for _, d := range dd {
decode := scheme.Codecs.UniversalDeserializer().Decode
- obj, gvk, err := decode([]byte(data), nil, nil)
+ obj, gvk, err := decode([]byte(d), nil, nil)
if err != nil {
fmt.Printf("Warning: error while decoding YAML object, err: %s\n", err)
continue
@@ -175,6 +183,7 @@ func extractResource(fileDir, kubeconfig string) ([]*resourceInfo, error) {
gvkInfo := &metav1.GroupVersionKind{Group: gvk.Group, Version: gvk.Version, Kind: gvk.Kind}
resources = append(resources, &resourceInfo{rawResource: raw, gvk: gvkInfo})
}
+ }
return resources, err
}
