Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper image merger to merge yaml files #428

Merged
merged 8 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ all: generate build-all-images test-unit test-lint ## Default: generate all, bui

APPS = gateway k8s-engine hub-js argo-runner helm-runner cloudsql-runner populator terraform-runner argo-actions
TESTS = e2e
INFRA = json-go-gen graphql-schema-linter jinja2
INFRA = json-go-gen graphql-schema-linter jinja2 merger

build-all-tools: ## Builds the standalone binaries for all tools
goreleaser build --rm-dist --skip-post-hooks --snapshot --single-target
Expand Down
2 changes: 1 addition & 1 deletion hack/ci/setup-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ fi
cat <<EOT >>"$GITHUB_ENV"
APPS=name=matrix::{"include":[{"APP":"gateway"},{"APP":"k8s-engine"},{"APP":"hub-js"},{"APP":"argo-runner"},{"APP":"helm-runner"},{"APP":"populator"},{"APP":"terraform-runner"},{"APP":"argo-actions"}]}
TESTS=name=matrix::{"include":[{"TEST":"e2e"}]}
INFRAS=name=matrix::{"include":[{"INFRA":"json-go-gen"},{"INFRA":"graphql-schema-linter"},{"INFRA":"jinja2"}]}
INFRAS=name=matrix::{"include":[{"INFRA":"json-go-gen"},{"INFRA":"graphql-schema-linter"},{"INFRA":"jinja2"},{"INFRA":"merger"}]}
EOT
3 changes: 2 additions & 1 deletion hack/images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ The structure of the folder looks as follows:
```
├── jinja2 # Image containing a containerized jinja CLI tool assisting with rendering templatized OCF content
├── json-go-gen # The image that contains quicktype to generates Go struct from JSON Schemas
└── graphql-schema-linter # The image that contains graphql-schema-linter to lint GraphQL files
├── graphql-schema-linter # The image that contains graphql-schema-linter to lint GraphQL files
└── merger # The image that contains YAML merger helper
```
10 changes: 10 additions & 0 deletions hack/images/merger/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mikefarah/yq:4.11.2

WORKDIR /yamls
USER root

RUN apk add --no-cache "bash=>5.1.0"

COPY merger.sh /

ENTRYPOINT ["/merger.sh"]
25 changes: 25 additions & 0 deletions hack/images/merger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# merger

## Overview

This folder contains the Docker image which merges multiple input YAML files into a single one.

The Docker image contains the `merger.sh` helper script. The script is an entrypoint of the image, and it is used to prefix and merge all YAML files found in `$SRC` directory.
Each file is prefixed with a file name without extension.

## Installation

To build the Docker image, run this command:

```bash
docker build -t merger .
```

## Configuration

You can configure the merger script by passing the following environment variables:

| Variable | Default | Description |
| ------------------------- | ------------ | ------------------------------------------------ |
| SRC | /yamls | Path to the directory with YAML files. |
| OUT | /merged.yaml | Output file with prefixed and merged YAML files. |
15 changes: 15 additions & 0 deletions hack/images/merger/merger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

SRC=${SRC:-"/yamls"}
OUT=${OUT:-"/merged.yaml"}

# prefix each file with its filename
for filename in "${SRC}"/*; do
filename=$(basename -- "$filename")
prefix="${filename%.*}"
yq e -i "{\"${prefix}\": . }" "${SRC}"/"${filename}"
done

# merge all yaml files into one
# shellcheck disable=SC2016
yq ea '. as $item ireduce ({}; . * $item )' "${SRC}"/*.yaml >"${OUT}"