Skip to content

Commit

Permalink
fix: exclude empty documents from helmreleases, add hook support #88, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis authored Jan 22, 2024
1 parent 707caae commit 122cdff
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 125 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ docker pull ghcr.io/doodlescheduling/flux-build:v0
| `--api-versions` | `API_VERSIONS` | `` | Kubernetes api versions used for Capabilities.APIVersions (See helm help) |
| `--kube-version` | `KUBE_VERSION` | `1.27.0` | Kubernetes version (Some helm charts validate manifests against a specific kubernetes version) |
| `--output` | `OUTPUT` | `/dev/stdout` | Path to output file |
| `--include-helm-hooks` | `INCLUDE_HELM_HOOKS` | `false` | Include helm hooks in the output |


## Github Action
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ require (
github.com/onsi/gomega v1.29.0
github.com/opencontainers/go-digest v1.0.0
github.com/otiai10/copy v1.14.0
github.com/sethvargo/go-envconfig v1.0.0
github.com/sigstore/cosign v1.13.2
github.com/sigstore/sigstore v1.5.2
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
go.uber.org/zap v1.26.0
golang.org/x/sync v0.4.0
helm.sh/helm/v3 v3.11.3
Expand Down Expand Up @@ -230,6 +230,7 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tent/canonical-json-go v0.0.0-20130607151641-96e4ba3a7613 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sethvargo/go-envconfig v1.0.0 h1:1C66wzy4QrROf5ew4KdVw942CQDa55qmlYmw9FZxZdU=
github.com/sethvargo/go-envconfig v1.0.0/go.mod h1:Lzc75ghUn5ucmcRGIdGQ33DKJrcjk4kihFYgSTBmjIc=
github.com/shibumi/go-pathspec v1.3.0 h1:QUyMZhFo0Md5B8zV8x2tesohbb5kfbpTi9rBnKh5dkI=
github.com/shibumi/go-pathspec v1.3.0/go.mod h1:Xutfslp817l2I1cZvgcfeMQJG5QnU2lh5tVaaMCl3jE=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
48 changes: 26 additions & 22 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (
)

type Action struct {
Output io.Writer
AllowFailure bool
FailFast bool
Workers int
CacheDir string
Paths []string
APIVersions []string
KubeVersion *chartutil.KubeVersion
Logger logr.Logger
Output io.Writer
AllowFailure bool
FailFast bool
Workers int
CacheDir string
Paths []string
APIVersions []string
IncludeHelmHooks bool
KubeVersion *chartutil.KubeVersion
Logger logr.Logger
}

func (a *Action) Run(ctx context.Context) error {
Expand Down Expand Up @@ -58,23 +59,30 @@ func (a *Action) Run(ctx context.Context) error {
})

resources := make(chan resmap.ResMap, len(a.Paths))
manifests := make(chan []byte, a.Workers)
manifests := make(chan resmap.ResMap, a.Workers)
helmBuilder := build.NewHelmBuilder(build.HelmOpts{
APIVersions: a.APIVersions,
KubeVersion: a.KubeVersion,
APIVersions: a.APIVersions,
KubeVersion: a.KubeVersion,
IncludeHelmHooks: a.IncludeHelmHooks,
})

helmResultPool.Push(worker.Task(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case manifest, ok := <-manifests:
case index, ok := <-manifests:
if !ok {
return nil
}

_, err := a.Output.Write(append([]byte("---\n"), manifest...))
y, err := index.AsYaml()
if err != nil {
a.Logger.Error(err, "failed to encode as yaml")
return abort(err)
}

_, err = a.Output.Write(append([]byte("---\n"), y...))
if err != nil {

a.Logger.Error(err, "failed to write helm manifests to output")
Expand All @@ -89,15 +97,11 @@ func (a *Action) Run(ctx context.Context) error {
a.Logger.Info("build kustomize path", "path", p)

kustomizePool.Push(worker.Task(func(ctx context.Context) error {
k := build.NewKustomizeBuilder(build.KustomizeOpts{
Path: p,
})

if index, b, err := k.Build(ctx); err != nil {
if index, err := build.Kustomize(ctx, p); err != nil {
a.Logger.Error(err, "failed build kustomization", "path", p)
return abort(err)
} else {
manifests <- b
manifests <- index
resources <- index
}

Expand Down Expand Up @@ -140,7 +144,7 @@ func (a *Action) Run(ctx context.Context) error {
helmPool.Push(worker.Task(func(ctx context.Context) error {
a.Logger.Info("build helm release", "namespace", res.GetNamespace(), "name", res.GetName())

manifest, err := helmBuilder.Build(ctx, res, index)
index, err := helmBuilder.Build(ctx, res, index)
if err != nil {
a.Logger.Error(err, "failed build helmrelease", "namespace", res.GetNamespace(), "name", res.GetName())
return abort(err)
Expand All @@ -150,7 +154,7 @@ func (a *Action) Run(ctx context.Context) error {
return nil
}

manifests <- manifest
manifests <- index
return nil
}))
}
Expand Down
37 changes: 29 additions & 8 deletions internal/build/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/helm/pkg/strvals"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/resid"
)

type HelmOpts struct {
APIVersions []string
FailFast bool
CacheDir string
KubeVersion *chartutil.KubeVersion
Getters helmgetter.Providers
Decoder runtime.Decoder
APIVersions []string
FailFast bool
CacheDir string
KubeVersion *chartutil.KubeVersion
Getters helmgetter.Providers
Decoder runtime.Decoder
IncludeHelmHooks bool
}

type Helm struct {
Expand Down Expand Up @@ -90,7 +92,7 @@ func NewHelmBuilder(opts HelmOpts) *Helm {
}
}

func (h *Helm) Build(ctx context.Context, r *resource.Resource, db map[ref]*resource.Resource) ([]byte, error) {
func (h *Helm) Build(ctx context.Context, r *resource.Resource, db map[ref]*resource.Resource) (resmap.ResMap, error) {
r.SetGvk(resid.Gvk{
Group: helmv1.GroupVersion.Group,
Version: helmv1.GroupVersion.Version,
Expand Down Expand Up @@ -156,7 +158,26 @@ func (h *Helm) Build(ctx context.Context, r *resource.Resource, db map[ref]*reso
return nil, err
}

return []byte(release.Manifest), nil
ksDir, err := os.MkdirTemp("", "helmrelease")
if err != nil {
return nil, err
}

err = os.WriteFile(filepath.Join(ksDir, "manifest.yaml"), []byte(release.Manifest), 0644)
if err != nil {
return nil, err
}

if h.opts.IncludeHelmHooks {
for i, hook := range release.Hooks {
err := os.WriteFile(filepath.Join(ksDir, fmt.Sprintf("hook_%d.yaml", i)), []byte(hook.Manifest), 0644)
if err != nil {
return nil, err
}
}
}

return Kustomize(ctx, ksDir)
}

func (h *Helm) getRepository(repository *resource.Resource) (runtime.Object, error) {
Expand Down
6 changes: 6 additions & 0 deletions internal/build/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ func (r ResourceIndex) Push(resources []*resource.Resource) error {

return nil
}

type ref struct {
schema.GroupKind
Name string
Namespace string
}
45 changes: 5 additions & 40 deletions internal/build/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"sync"

"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/provider"
Expand All @@ -20,35 +19,7 @@ import (

var kustomizeBuildMutex sync.Mutex

type KustomizeOpts struct {
Path string
}

type Kustomize struct {
opts KustomizeOpts
}

func NewKustomizeBuilder(opts KustomizeOpts) *Kustomize {
return &Kustomize{
opts: opts,
}
}

func (k *Kustomize) Build(ctx context.Context) (resmap.ResMap, []byte, error) {
resourcesMap, err := k.buildKustomization(k.opts.Path)
if err != nil {
return nil, nil, fmt.Errorf("failed build kustomization: %w", err)
}

kustomizeBuild, err := resourcesMap.AsYaml()
if err != nil {
return nil, nil, fmt.Errorf("failed marshal resources as yaml: %w", err)
}

return resourcesMap, kustomizeBuild, err
}

func (k *Kustomize) buildKustomization(path string) (resmap.ResMap, error) {
func Kustomize(ctx context.Context, path string) (resmap.ResMap, error) {
kfile := filepath.Join(path, konfig.DefaultKustomizationFileName())
fs := filesys.MakeFsOnDisk()

Expand Down Expand Up @@ -86,7 +57,7 @@ func (k *Kustomize) buildKustomization(path string) (resmap.ResMap, error) {
}()

pvd := provider.NewDefaultDepProvider()
err = k.createKustomization(path, fs, pvd.GetResourceFactory())
err = createKustomization(path, fs, pvd.GetResourceFactory())
if err != nil {
return nil, fmt.Errorf("failed create kustomization: %w", err)
}
Expand All @@ -105,7 +76,7 @@ func (k *Kustomize) buildKustomization(path string) (resmap.ResMap, error) {
return kustomizer.Run(fs, path)
}

func (k *Kustomize) createKustomization(path string, fSys filesys.FileSystem, rf *resource.Factory) error {
func createKustomization(path string, fSys filesys.FileSystem, rf *resource.Factory) error {
kfile := filepath.Join(path, konfig.DefaultKustomizationFileName())
kus := kustypes.Kustomization{
TypeMeta: kustypes.TypeMeta{
Expand All @@ -114,7 +85,7 @@ func (k *Kustomize) createKustomization(path string, fSys filesys.FileSystem, rf
},
}

detected, err := k.detectResources(fSys, rf, path, true)
detected, err := detectResources(fSys, rf, path, true)
if err != nil {
return err
}
Expand All @@ -129,7 +100,7 @@ func (k *Kustomize) createKustomization(path string, fSys filesys.FileSystem, rf
return os.WriteFile(kfile, kd, os.ModePerm)
}

func (k *Kustomize) detectResources(fSys filesys.FileSystem, rf *resource.Factory, base string, recursive bool) ([]string, error) {
func detectResources(fSys filesys.FileSystem, rf *resource.Factory, base string, recursive bool) ([]string, error) {
var paths []string

err := fSys.Walk(base, func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -173,9 +144,3 @@ func (k *Kustomize) detectResources(fSys filesys.FileSystem, rf *resource.Factor

return paths, err
}

type ref struct {
schema.GroupKind
Name string
Namespace string
}
Loading

0 comments on commit 122cdff

Please sign in to comment.