@@ -18,11 +18,46 @@ import (
1818 "sigs.k8s.io/kustomize/kyaml/yaml"
1919)
2020
21+ //
22+ // Kubeconform.
23+
2124type Kubeconform struct {
22- Input * io.PipeReader
23- Output bytes.Buffer
25+ IO io.ReadWriter
26+ Config * config.Config
27+ }
28+
29+ // LoadResourceListItems links between Kustomize KRM input and Kubeconform input.
30+ func (kc * Kubeconform ) loadResourceListItems (rlItems []* yaml.RNode ) {
31+ err := (& kio.ByteWriter {Writer : kc .IO }).Write (rlItems )
32+ if err != nil {
33+ log .Fatal ("failed to load ResourceList items: " , err )
34+ }
35+ }
36+
37+ func (kc * Kubeconform ) configure (kcvSpec * kubeconformValidatorSpec ) (config.Config , string ) {
38+ cfg , out , err := config .FromFlags (os .Args [0 ], kcvSpec .Args )
39+ if err != nil {
40+ log .Fatal ("failed to parse args: " , err )
41+ }
42+
43+ // Override config using KubeconformValidator spec.config.
44+ // That means "spec.config" has a priority over "spec.args".
45+ if err := mergo .Merge (& cfg , kcvSpec .Config , mergo .WithOverride ); err != nil {
46+ log .Fatal ("failed to merge config: " , err )
47+ }
48+
49+ cfg .LoadNGConfig ()
50+
51+ // Configure Kubeconform IO.
52+ cfg .Stream .Input = kc .IO
53+ cfg .Stream .Output = kc .IO
54+
55+ return cfg , out
2456}
2557
58+ //
59+ // KubeconformValidator.
60+
2661type KubeconformValidator struct {
2762 Kind string `yaml:"kind" json:"kind"`
2863 Metadata struct {
@@ -32,16 +67,16 @@ type KubeconformValidator struct {
3267}
3368
3469type kubeconformValidatorSpec struct {
35- Args []string `yaml:"args" json:"args"`
36- Config config.Config `yaml:"config" json:"config"`
70+ Args []string `yaml:"args" json:"args"`
71+ Config * config.Config `yaml:"config" json:"config"`
3772}
3873
3974//go:embed plugin-schema.yaml
4075var kubeconformValidatorDefinition string
4176
4277func (kcv * KubeconformValidator ) Schema () (* spec.Schema , error ) {
4378 schema , err := framework .SchemaFromFunctionDefinition (
44- resid .NewGvk ("kubeconformvalidator .aabouzaid.com" , "v1alpha1" , "KubeconformValidator" ),
79+ resid .NewGvk ("validators.kustomize .aabouzaid.com" , "v1alpha1" , "KubeconformValidator" ),
4580 kubeconformValidatorDefinition )
4681 return schema , errors .WrapPrefixf (err , "failed to parse KubeconformValidator schema" )
4782}
@@ -56,49 +91,14 @@ func (kcv *KubeconformValidator) Validate() error {
5691// return nil
5792// }
5893
59- // LoadResourceListItems links between Kustomize KRM input and Kubeconform input.
60- // TODO: Review the current approach to find out if there is a better solution.
61- func (kc * Kubeconform ) loadResourceListItems (rlItems []* yaml.RNode ) {
62- var tmpWriter * io.PipeWriter
63- kc .Input , tmpWriter = io .Pipe ()
64- go func () {
65- defer tmpWriter .Close ()
66- err := (& kio.ByteWriter {Writer : tmpWriter }).Write (rlItems )
67- if err != nil {
68- log .Fatal ("failed to load ResourceList items: " , err )
69- }
70- }()
71- }
72-
73- func (kcv * KubeconformValidator ) configure () (config.Config , string ) {
74- cfg , out , err := config .FromFlags (os .Args [0 ], kcv .Spec .Args )
75- if err != nil {
76- log .Fatal ("failed to parse args: " , err )
77- }
78-
79- // Override config using KubeconformValidator spec.config.
80- // That means "spec.config" has a priority over "spec.args".
81- if err := mergo .Merge (& cfg , kcv .Spec .Config , mergo .WithOverride ); err != nil {
82- log .Fatal ("failed to merge config: " , err )
83- }
84-
85- return cfg , out
86- }
87-
8894func (kcv * KubeconformValidator ) Filter (rlItems []* yaml.RNode ) ([]* yaml.RNode , error ) {
89- kc := & Kubeconform {}
95+ kc := & Kubeconform {IO : & bytes. Buffer {} }
9096 kc .loadResourceListItems (rlItems )
91-
92- // Configure Kubeconform.
93- cfg , out := kcv .configure ()
94- cfg .Stream = & config.Stream {
95- Input : kc .Input ,
96- Output : & kc .Output ,
97- }
97+ cfg , out := kc .configure (& kcv .Spec )
9898
9999 // Run Kubeconform validate.
100- if exitCode := kubeconform .Validate (cfg , out ); exitCode != 0 {
101- return nil , errors .Wrap (errors .Errorf ("Kubeconform validation output: %s" , kc .Output . String () ))
100+ if err := kubeconform .Validate (cfg , out ); err != nil {
101+ return nil , errors .Wrap (errors .Errorf ("Kubeconform validation output: %s" , kc .IO ))
102102 }
103103
104104 return rlItems , nil
@@ -108,7 +108,7 @@ func main() {
108108 rlSource := & kio.ByteReadWriter {}
109109 processor := & framework.VersionedAPIProcessor {FilterProvider : framework.GVKFilterMap {
110110 "KubeconformValidator" : {
111- "kubeconformvalidator .aabouzaid.com/v1alpha1" : & KubeconformValidator {},
111+ "validators.kustomize .aabouzaid.com/v1alpha1" : & KubeconformValidator {},
112112 }}}
113113
114114 if err := framework .Execute (processor , rlSource ); err != nil {
0 commit comments