Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pcjun97 committed May 31, 2021
0 parents commit 1ead639
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 0 deletions.
Binary file added HashAnnotator
Binary file not shown.
99 changes: 99 additions & 0 deletions HashAnnotator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strings"

"sigs.k8s.io/kustomize/api/filters/annotations"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/provider"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"
)

type hashannotator struct {
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
Resource *types.Selector `json:"resource,omitempty" yaml:"resource,omitempty"`
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
hasher ifc.KustHasher
}

func main() {
manifest, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read in manifest: %q", err)
os.Exit(1)
}

depProvider := provider.NewDefaultDepProvider()
rf := depProvider.GetResourceFactory()
rmf := resmap.NewFactory(rf)

m, err := rmf.NewResMapFromBytes(manifest)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to parse manifest: %q", err)
os.Exit(1)
}

var p hashannotator

defaultFieldSpecs := types.FieldSpec{Path: "metadata/annotations", CreateIfNotPresent: true}
p.FieldSpecs = []types.FieldSpec{defaultFieldSpecs}
p.hasher = rmf.RF().Hasher()

config, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read in config: %q", err)
os.Exit(1)
}

err = yaml.Unmarshal(config, &p)
if err != nil {
fmt.Fprintf(os.Stderr, "error unmarshalling config content: %q \n%s\n", err, config)
os.Exit(1)
}

targets, err := m.Select(*p.Target)
if err != nil {
fmt.Fprintf(os.Stderr, "error selecting target: %q", err)
os.Exit(1)
}

resources, err := m.Select(*p.Resource)
if err != nil {
fmt.Fprintf(os.Stderr, "error selecting resource: %q", err)
os.Exit(1)
}

an := make(map[string]string)
for _, res := range resources {
h, err := res.Hash(p.hasher)
if err != nil {
fmt.Fprintf(os.Stderr, "error hashing resource: %q", err)
os.Exit(1)
}
key := fmt.Sprintf("kustomize.config.k8s.io/hash-%s-%s-%s", res.GetNamespace(), strings.ToLower(res.GetKind()), res.GetName())
an[key] = h
}

for _, target := range targets {
err = target.ApplyFilter(annotations.Filter{
Annotations: an,
FsSlice: p.FieldSpecs,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error applying annotations filter: %q", err)
os.Exit(1)
}
}

output, err := m.AsYaml()
if err != nil {
fmt.Fprintf(os.Stderr, "error converting resource to yaml: %q", err)
os.Exit(1)
}
fmt.Fprintln(os.Stdout, string(output))
}
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PLUGIN_NAME="HashAnnotator"

default: install

.PHONY: install
install: go-install clean build install-plugin

.PHONY: install-plugin
install-plugin:
./install.sh

.PHONY: build
build:
go build -o $(PLUGIN_NAME)

.PHONY: clean
clean:
rm $(PLUGIN_NAME) || true
rm -rf $(XDG_CONFIG_HOME)/kustomize/plugin/pcjun97/v1/hashannotator || true

.PHONY: go-install
go-install:
go install
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module pcjun97/kustomize-hash-annotations

go 1.16

require (
sigs.k8s.io/kustomize/api v0.8.10
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit 1ead639

Please sign in to comment.