From 525829f5310213bd481669cdab7869a3f2fca5c9 Mon Sep 17 00:00:00 2001 From: nxcc <> Date: Wed, 23 Aug 2023 16:36:34 +0200 Subject: [PATCH] add YQ_PRETTYPRINT --- README.md | 43 +++++++++++++++++++++------------------ docker/Dockerfile-cmp | 4 +++- internal/cuegen/cuegen.go | 21 ++++++++++++++++++- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2a7e138..df533c3 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,8 @@ Some environment variables can help working with cuegen: CUEGEN_HTTP_USERNAME username for git authentication SOPS_AGE_KEY age key for decryption SOPS_AGE_KEY_FILE age key file for decryption + YQ_PRETTYPRINT !="": run yaml output thru `yq -P` + starting with `/`: use as path to yq ## Components Components can be @@ -179,32 +181,33 @@ Load all files from directory `scripts` as key/values into `configMap.scripts.da ## Changelog - * `v0.1.0` - Initial release - * `v0.1.1` - Improved attribute lookup - * `v0.2.0` - Added checks when reading `cuegen.yaml` - * `v0.2.1` - Improved error messages - * `v0.3.0` - Added ability to read subpaths from git repos - * `v0.3.1` - No code changes, trigger cmp build - * `v0.3.2` - No code changes, bump go version to 1.20 - * `v0.4.0` - switch default config to `cuegen.cue`, use cue v0.5.0-beta5 - * `v0.4.1` - Make components & checkPaths optional - * `v0.4.2` - downgrade cue to v0.5.0-beta.2 ([performance regression][gh2243]) - * `v0.4.3` - fix running as kustomize plugin - * `v0.4.4` - improve handling of git urls - * `v0.5.0` - add cuegen default config - * `v0.6.0` - add dumpOverlays option - * `v0.7.0` - upgrade cue to v0.5.0 (many fixes, rare performance regression still present) - * `v0.7.1` - fix secret handling of @readfile - * `v0.7.2` - internal cleanup - * `v0.8.0` - allow remote cuegen directories, rm kustomize plugin support - * `v0.9.0` - upgrade cue to v0.6.0 + * `v0.1.0` - Initial release + * `v0.1.1` - Improved attribute lookup + * `v0.2.0` - Added checks when reading `cuegen.yaml` + * `v0.2.1` - Improved error messages + * `v0.3.0` - Added ability to read subpaths from git repos + * `v0.3.1` - No code changes, trigger cmp build + * `v0.3.2` - No code changes, bump go version to 1.20 + * `v0.4.0` - switch default config to `cuegen.cue`, use cue v0.5.0-beta5 + * `v0.4.1` - Make components & checkPaths optional + * `v0.4.2` - downgrade cue to v0.5.0-beta.2 ([performance regression][gh2243]) + * `v0.4.3` - fix running as kustomize plugin + * `v0.4.4` - improve handling of git urls + * `v0.5.0` - add cuegen default config + * `v0.6.0` - add dumpOverlays option + * `v0.7.0` - upgrade cue to v0.5.0 (many fixes, rare performance regression still present) + * `v0.7.1` - fix secret handling of @readfile + * `v0.7.2` - internal cleanup + * `v0.8.0` - allow remote cuegen directories, rm kustomize plugin support + * `v0.9.0` - upgrade cue to v0.6.0 + * `v0.10.0` - add YQ_PRETTYPRINT to filter output thru `yq -P`` [CUE]: https://cuelang.org [SOPS]: https://github.com/mozilla/sops [k8stut]: https://cuelang.org/docs/tutorials/ [eg]: examples/ [rel]: https://github.com/noris-network/cuegen/releases/latest -[cmp]: https://argo-cd.readthedocs.io/en/stable/user-guide/config-management-plugins/#option-2-configure-plugin-via-sidecar +[cmp]: https://argo-cd.readthedocs.io/en/stable/operator-manual/config-management-plugins/#sidecar-plugin [cuegen-cmp]: https://hub.docker.com/r/nxcc/cuegen-cmp [expenv]: https://pkg.go.dev/os#ExpandEnv [cfgschema]: internal/app/schema.cue diff --git a/docker/Dockerfile-cmp b/docker/Dockerfile-cmp index 83421fb..c1607f4 100644 --- a/docker/Dockerfile-cmp +++ b/docker/Dockerfile-cmp @@ -1,10 +1,12 @@ -FROM golang:1.20 as builder +FROM golang:1.21 as builder WORKDIR /go/src/app ARG VERSION COPY . ./ RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X 'main.build=$VERSION'" -o cuegen . RUN sed "s/VERSION/$VERSION/g" docker/plugin.yaml > plugin.yaml +RUN curl -sfL https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64 -o /tmp/yq && chmod 755 /tmp/yq FROM gcr.io/distroless/static-debian11 +COPY --from=builder /tmp/yq /bin/yq COPY --from=builder /go/src/app/cuegen /bin/cuegen COPY --from=builder /go/src/app/plugin.yaml /home/argocd/cmp-server/config/plugin.yaml diff --git a/internal/cuegen/cuegen.go b/internal/cuegen/cuegen.go index ebdd3cf..591147f 100644 --- a/internal/cuegen/cuegen.go +++ b/internal/cuegen/cuegen.go @@ -21,6 +21,7 @@ import ( "io/fs" "log" "os" + "os/exec" "path/filepath" "strings" @@ -175,7 +176,25 @@ func (cg Cuegen) Exec() error { return fmt.Errorf("Exec: marshal stream: %v", err) } - fmt.Print(noBinaryPrefix(yamlString)) + fixedYamlString := noBinaryPrefix(yamlString) + + // filter output thru yq -P + yqPp := os.Getenv("YQ_PRETTYPRINT") + if yqPp != "" { + yqBin := "yq" + if strings.HasPrefix(yqPp, "/") { + yqBin = yqPp + } + cmd := exec.Command(yqBin, "-P") + cmd.Stdin = strings.NewReader(fixedYamlString) + cmd.Stdout = os.Stdout + err := cmd.Run() + if err != nil { + return err + } + } else { + fmt.Print(fixedYamlString) + } return nil }