Skip to content

Commit

Permalink
iter05: tidy up the structure
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Apr 12, 2023
1 parent aeb1fc9 commit 8f9abb8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ go build -o 'dist/kubeconformvalidator' .

## Example
```yaml
apiVersion: validators.kustomize.aabouzaid.com/v1alpha1
kind: KubeconformValidator
apiVersion: kubeconformvalidator.aabouzaid.com/v1alpha1
metadata:
name: validate
annotations:
Expand Down
2 changes: 1 addition & 1 deletion example/krm-function-input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: krm-function-input
functionConfig:
kind: KubeconformValidator
apiVersion: kubeconformvalidator.aabouzaid.com/v1alpha1
apiVersion: validators.kustomize.aabouzaid.com/v1alpha1
metadata:
name: validate
spec:
Expand Down
2 changes: 1 addition & 1 deletion example/kubeconformvalidator.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: validators.kustomize.aabouzaid.com/v1alpha1
kind: KubeconformValidator
apiVersion: kubeconformvalidator.aabouzaid.com/v1alpha1
metadata:
name: validate
annotations:
Expand Down
90 changes: 45 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,46 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

//
// Kubeconform.

type Kubeconform struct {
Input *io.PipeReader
Output bytes.Buffer
IO io.ReadWriter
Config *config.Config
}

// LoadResourceListItems links between Kustomize KRM input and Kubeconform input.
func (kc *Kubeconform) loadResourceListItems(rlItems []*yaml.RNode) {
err := (&kio.ByteWriter{Writer: kc.IO}).Write(rlItems)
if err != nil {
log.Fatal("failed to load ResourceList items: ", err)
}
}

func (kc *Kubeconform) configure(kcvSpec *kubeconformValidatorSpec) (config.Config, string) {
cfg, out, err := config.FromFlags(os.Args[0], kcvSpec.Args)
if err != nil {
log.Fatal("failed to parse args: ", err)
}

// Override config using KubeconformValidator spec.config.
// That means "spec.config" has a priority over "spec.args".
if err := mergo.Merge(&cfg, kcvSpec.Config, mergo.WithOverride); err != nil {
log.Fatal("failed to merge config: ", err)
}

cfg.LoadNGConfig()

// Configure Kubeconform IO.
cfg.Stream.Input = kc.IO
cfg.Stream.Output = kc.IO

return cfg, out
}

//
// KubeconformValidator.

type KubeconformValidator struct {
Kind string `yaml:"kind" json:"kind"`
Metadata struct {
Expand All @@ -32,16 +67,16 @@ type KubeconformValidator struct {
}

type kubeconformValidatorSpec struct {
Args []string `yaml:"args" json:"args"`
Config config.Config `yaml:"config" json:"config"`
Args []string `yaml:"args" json:"args"`
Config *config.Config `yaml:"config" json:"config"`
}

//go:embed plugin-schema.yaml
var kubeconformValidatorDefinition string

func (kcv *KubeconformValidator) Schema() (*spec.Schema, error) {
schema, err := framework.SchemaFromFunctionDefinition(
resid.NewGvk("kubeconformvalidator.aabouzaid.com", "v1alpha1", "KubeconformValidator"),
resid.NewGvk("validators.kustomize.aabouzaid.com", "v1alpha1", "KubeconformValidator"),
kubeconformValidatorDefinition)
return schema, errors.WrapPrefixf(err, "failed to parse KubeconformValidator schema")
}
Expand All @@ -56,49 +91,14 @@ func (kcv *KubeconformValidator) Validate() error {
// return nil
// }

// LoadResourceListItems links between Kustomize KRM input and Kubeconform input.
// TODO: Review the current approach to find out if there is a better solution.
func (kc *Kubeconform) loadResourceListItems(rlItems []*yaml.RNode) {
var tmpWriter *io.PipeWriter
kc.Input, tmpWriter = io.Pipe()
go func() {
defer tmpWriter.Close()
err := (&kio.ByteWriter{Writer: tmpWriter}).Write(rlItems)
if err != nil {
log.Fatal("failed to load ResourceList items: ", err)
}
}()
}

func (kcv *KubeconformValidator) configure() (config.Config, string) {
cfg, out, err := config.FromFlags(os.Args[0], kcv.Spec.Args)
if err != nil {
log.Fatal("failed to parse args: ", err)
}

// Override config using KubeconformValidator spec.config.
// That means "spec.config" has a priority over "spec.args".
if err := mergo.Merge(&cfg, kcv.Spec.Config, mergo.WithOverride); err != nil {
log.Fatal("failed to merge config: ", err)
}

return cfg, out
}

func (kcv *KubeconformValidator) Filter(rlItems []*yaml.RNode) ([]*yaml.RNode, error) {
kc := &Kubeconform{}
kc := &Kubeconform{IO: &bytes.Buffer{}}
kc.loadResourceListItems(rlItems)

// Configure Kubeconform.
cfg, out := kcv.configure()
cfg.Stream = &config.Stream{
Input: kc.Input,
Output: &kc.Output,
}
cfg, out := kc.configure(&kcv.Spec)

// Run Kubeconform validate.
if exitCode := kubeconform.Validate(cfg, out); exitCode != 0 {
return nil, errors.Wrap(errors.Errorf("Kubeconform validation output: %s", kc.Output.String()))
if err := kubeconform.Validate(cfg, out); err != nil {
return nil, errors.Wrap(errors.Errorf("Kubeconform validation output: %s", kc.IO))
}

return rlItems, nil
Expand All @@ -108,7 +108,7 @@ func main() {
rlSource := &kio.ByteReadWriter{}
processor := &framework.VersionedAPIProcessor{FilterProvider: framework.GVKFilterMap{
"KubeconformValidator": {
"kubeconformvalidator.aabouzaid.com/v1alpha1": &KubeconformValidator{},
"validators.kustomize.aabouzaid.com/v1alpha1": &KubeconformValidator{},
}}}

if err := framework.Execute(processor, rlSource); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugin-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: KRMFunctionDefinition
metadata:
name: kubeconformvalidator
spec:
group: kubeconformvalidator.aabouzaid.com
group: validators.kustomize.aabouzaid.com
names:
kind: KubeconformValidator
versions:
Expand Down

0 comments on commit 8f9abb8

Please sign in to comment.