diff --git a/.gitignore b/.gitignore index f604376..6bf1379 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.venv dist/* .cache/* chart/* diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b7efcb4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "test/bats"] + path = test/bats + url = https://github.com/bats-core/bats-core.git +[submodule "test/test_helper/bats-support"] + path = test/test_helper/bats-support + url = https://github.com/bats-core/bats-support.git +[submodule "test/test_helper/bats-assert"] + path = test/test_helper/bats-assert + url = https://github.com/bats-core/bats-assert.git diff --git a/Makefile b/Makefile index 5b9903f..f507674 100644 --- a/Makefile +++ b/Makefile @@ -4,27 +4,51 @@ VERSION=$(shell git describe --exact-match --tags $(CUR_SHA) 2>/dev/null || echo CTN:=$(shell which podman 2>&1 1>/dev/null && echo "podman" || echo "docker") PREFIX=~/.local +GOVERSION=1.21 GO=container OUT=katenary -BLD_CMD=go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary/*.go +BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary GOOS=linux GOARCH=amd64 +SIGNER=metal3d@gmail.com -BUILD_IMAGE=docker.io/golang:1.18-alpine +BUILD_IMAGE=docker.io/golang:$(GOVERSION)-alpine +# SHELL=/bin/bash -.PHONY: help clean build +# List of source files +SOURCES=$(wildcard ./*.go ./*/*.go ./*/*/*.go) +# List of binaries to build and sign +BINARIES=dist/katenary-linux-amd64 dist/katenary-linux-arm64 dist/katenary.exe dist/katenary-darwin-amd64 dist/katenary-freebsd-amd64 dist/katenary-freebsd-arm64 +# List of signatures to build +ASC_BINARIES=$(patsubst %,%.asc,$(BINARIES)) +# defaults +SHELL := bash +# strict mode +.SHELLFLAGS := -eu -o pipefail -c +# One session per target .ONESHELL: +.DELETE_ON_ERROR: +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules +.PHONY: help clean build install + +all: build + help: - @cat < Build on host using go" +else + @echo "=> Build in container using" $(CTN) +endif + echo $(BLD_CMD) +ifeq ($(GO),local) + $(BLD_CMD) +else ifeq ($(CTN),podman) + @podman run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \ + --rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it $(BUILD_IMAGE) $(BLD_CMD) +else + @docker run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \ + --rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --user $(shell id -u):$(shell id -g) -e HOME=/tmp -it $(BUILD_IMAGE) $(BLD_CMD) +endif + echo "=> Stripping if possible" + strip $(OUT) 2>/dev/null || echo "=> No strip available" + + +## Release build +dist: prepare $(BINARIES) $(ASC_BINARIES) + +prepare: pull mkdir -p dist dist/katenary-linux-amd64: @@ -69,7 +112,6 @@ dist/katenary-linux-amd64: @echo -e "\033[1;32mBuilding katenary $(VERSION) for linux-amd64...\033[0m" $(MAKE) katenary GOOS=linux GOARCH=amd64 OUT=$@ - dist/katenary-linux-arm64: @echo @echo -e "\033[1;32mBuilding katenary $(VERSION) for linux-arm...\033[0m" @@ -94,30 +136,16 @@ dist/katenary-freebsd-arm64: @echo @echo -e "\033[1;32mBuilding katenary $(VERSION) for freebsd-arm64...\033[0m" $(MAKE) katenary GOOS=freebsd GOARCH=arm64 OUT=$@ - -katenary: $(wildcard */*.go Makefile go.mod go.sum) -ifeq ($(GO),local) - @echo "=> Build in host using go" -else - @echo "=> Build in container using" $(CTN) -endif - echo $(BLD_CMD) -ifeq ($(GO),local) - $(BLD_CMD) -else ifeq ($(CTN),podman) - @podman run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \ - --rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it $(BUILD_IMAGE) $(BLD_CMD) -else - @docker run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \ - --rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --user $(shell id -u):$(shell id -g) -e HOME=/tmp -it $(BUILD_IMAGE) $(BLD_CMD) -endif - echo "=> Stripping if possible" - strip $(OUT) 2>/dev/null || echo "=> No strip available" +gpg-sign: + rm -f dist/*.asc + $(MAKE) $(ASC_BINARIES) +dist/%.asc: dist/% + gpg --armor --detach-sign --default-key $(SIGNER) $< &>/dev/null || exit 1 install: build - cp katenary $(PREFIX)/bin/katenary + install -Dm755 katenary $(PREFIX)/bin/katenary uninstall: rm -f $(PREFIX)/bin/katenary @@ -131,8 +159,6 @@ test: @echo -e "\033[1;33mTesting katenary $(VERSION)...\033[0m" go test -v ./... - -.ONESHELL: push-release: build-all @rm -f release.id # read personal access token from .git-credentials @@ -154,3 +180,37 @@ push-release: build-all https://uploads.github.com/repos/metal3d/katenary/releases/$$(cat release.id)/assets?name=$$(basename $$i) done @rm -f release.id + + +__label_doc: + @echo "=> Generating labels doc..." + # short label doc + go run ./cmd/katenary help-labels -m | \ + sed -i ' + /START_LABEL_DOC/,/STOP_LABEL_DOC/{/ +| Label name | Description | Type | +| ----------------------------- | ------------------------------------------------------ | --------------------- | +| `katenary.v3/configmap-files` | Add files to the configmap. | list of strings | +| `katenary.v3/cronjob` | Create a cronjob from the service. | object | +| `katenary.v3/dependencies` | Add Helm dependencies to the service. | list of objects | +| `katenary.v3/description` | Description of the service | string | +| `katenary.v3/env-from` | Add environment variables from antoher service. | list of strings | +| `katenary.v3/health-check` | Health check to be added to the deployment. | object | +| `katenary.v3/ignore` | Ignore the service | bool | +| `katenary.v3/ingress` | Ingress rules to be added to the service. | object | +| `katenary.v3/main-app` | Mark the service as the main app. | bool | +| `katenary.v3/map-env` | Map env vars from the service to the deployment. | object | +| `katenary.v3/ports` | Ports to be added to the service. | list of uint32 | +| `katenary.v3/same-pod` | Move the same-pod deployment to the target deployment. | string | +| `katenary.v3/secrets` | Env vars to be set as secrets. | list of string | +| `katenary.v3/values` | Environment variables to be added to the values.yaml | list of string or map | -HealthCheck label defines how to make LivenessProbe on Kubernetes. + + +## Detailed description + + +### katenary.v3/configmap-files + +Add files to the configmap. + +**Type**: `list of strings` + +It makes a file or directory to be converted to one or more ConfigMaps +and mounted in the pod. The file or directory is relative to the +service directory. + +If it is a directory, all files inside it are added to the ConfigMap. + +If the directory as subdirectories, so one configmap per subpath are created. !!! Warning - This overrides the compose file healthcheck + It is not intended to be used to store an entire project in configmaps. + It is intended to be used to store configuration files that are not managed + by the application, like nginx configuration files. Keep in mind that your + project sources should be stored in an application image or in a storage. + +**Example:** + +```yaml +volumes + - ./conf.d:/etc/nginx/conf.d +labels: + katenary.v3/configmap-files: |- + - ./conf.d +``` + +### katenary.v3/cronjob + +Create a cronjob from the service. + +**Type**: `object` + +This adds a cronjob to the chart. + +The label value is a YAML object with the following attributes: +- command: the command to be executed +- schedule: the cron schedule (cron format or @every where "every" is a + duration like 1h30m, daily, hourly...) +- rbac: false (optionnal), if true, it will create a role, a rolebinding and + a serviceaccount to make your cronjob able to connect the Kubernetes API + +**Example:** + +```yaml +labels: + katenary.v3/cronjob: |- + command: echo "hello world" + schedule: "* */1 * * *" # or @hourly for example +``` + +### katenary.v3/dependencies + +Add Helm dependencies to the service. + +**Type**: `list of objects` + +Set the service to be, actually, a Helm dependency. This means that the +service will not be exported as template. The dependencies are added to +the Chart.yaml file and the values are added to the values.yaml file. + +It's a list of objects with the following attributes: + +- name: the name of the dependency +- repository: the repository of the dependency +- alias: the name of the dependency in values.yaml (optional) +- values: the values to be set in values.yaml (optional) !!! Info - The hostname is set to "localhost" by convention, but Katenary will ignore the hostname in tcp and http tests because it will create a LivenessProbe. + Katenary doesn't update the helm depenedencies by default. + + Use `--helm-update` (or `-u`) flag to update the dependencies. + + example: katenary convert -u -Some example of usage: +By setting an alias, it is possible to change the name of the dependency +in values.yaml. + +**Example:** ```yaml -services: - mariadb: - image: mariadb - labels: - katenary.io/healthcheck: tcp://localhost:3306 +labels: + katenary.v3/dependencies: |- + - name: mariadb + repository: oci://registry-1.docker.io/bitnamicharts - webapp: - image: nginx - labels: - katenary.io/healthcheck: http://localhost:80 + ## optional, it changes the name of the section in values.yaml + # alias: mydatabase - example: - image: yourimage - labels: - katenary.io/healthcheck: "test -f /opt/installed" + ## optional, it adds the values to values.yaml + values: + auth: + database: mydatabasename + username: myuser + password: the secret password ``` -## crontabs +### katenary.v3/description -Crontabs label proposes to create a complete CronTab object with needed RBAC to make it possible to run command inside the pod(s) with `kubectl`. Katenary will make the job for you. You only need to provide the command(s) to call. +Description of the service -It's a YAML array in multiline label. +**Type**: `string` + +This replaces the default comment in values.yaml file to the given description. +It is useful to document the service and configuration. + +The value can be set with a documentation in multiline format. + +**Example:** ```yaml -services: - mariadb: - image: mariadb - labels: - katenary.io/crontabs: | - - command: mysqldump -B myapp -uroot -p$${MYSQL_ROOT_PASSWORD} > dump.sql - schedule: "@every 1h" -``` -The object is: -``` -command: Command to run -schedule: the cron form schedule string -allPods: boolean (default false) to activate the cront on each pod -image: image name to use (default is bitnami/kubectl) - with corresponding tag to your kubernetes version +labels: + katenary.v3/description: |- + This is a description of the service. + It can be multiline. ``` -## empty-dirs +### katenary.v3/env-from -You sometime don't need to create a PersistentVolumeClaim. For example when a volume in your compose file is actually made to share the data between 2 or more containers. +Add environment variables from antoher service. -In this case, an "emptyDir" volume is appreciated. +**Type**: `list of strings` + +It adds environment variables from another service to the current service. + +**Example:** ```yaml -services: - webapp: - image: nginx - volumes: - - websource:/var/www/html - labels: - # sources is actually an empty directory on the node - katenary.io/empty-dirs: websource +service1: + image: nginx:1.19 + environment: + FOO: bar - php: - image: php:7-fpm - volumes: - - sources:/var/www/html - labels: - # in the same pod than webapp - katenary.io/same-pod: webapp - # see the corresponding section, get the volume - # fro webapp - katenary.io/volume-from: | - sources: - webapp: websource +service2: + image: php:7.4-fpm + labels: + # get the congigMap from service1 where FOO is + # defined inside this service too + katenary.v3/env-from: |- + - myservice1 ``` -## volume-from +### katenary.v3/health-check -We see this in the [empty-dir](#empty-dir) section, this label defines that the corresponding volume should be shared in this pod. +Health check to be added to the deployment. + +**Type**: `object` + +Health check to be added to the deployment. + +**Example:** ```yaml -services: - webapp: - image: nginx - volumes: - - datasource:/var/www/html - - app: - image: php - volumes: - - data:/opt/data - labels: - katenary.io/volume-from: | - # data in this container... - data: - # ... correspond to "datasource" in "webapp" container - webapp: datasource +labels: + katenary.v3/health-check: |- + httpGet: + path: /health + port: 8080 ``` -This implies that the declared volume in "webapp" will be mounted to "app" pods. +### katenary.v3/ignore + +Ignore the service + +**Type**: `bool` + +Ingoring a service to not be exported in helm chart. + +**Example:** + +```yaml +labels: + katenary.v3/ignore: "true" +``` + +### katenary.v3/ingress + +Ingress rules to be added to the service. + +**Type**: `object` + +Declare an ingress rule for the service. The port should be exposed or +declared with `katenary.v3/ports`. + +**Example:** + +```yaml +labels: + katenary.v3/ingress: |- + port: 80 + hostname: mywebsite.com (optional) +``` + +### katenary.v3/main-app + +Mark the service as the main app. + +**Type**: `bool` + +This makes the service to be the main application. Its image tag is +considered to be the + +Chart appVersion and to be the defaultvalue in Pod container +image attribute. !!! Warning - This is possible with Kubernetes volumes restrictions. So, it works in these cases: + This label cannot be repeated in others services. If this label is + set in more than one service as true, Katenary will return an error. - - if the volume class is Read Write Many - - or if you mount the volume in the same pod (so in the same node) - - and/or the volume is an emptyDir - - -## same-pod - -It's sometimes important and/or necessary to declare that 2 services are in the same pod. For example, using PHP-FPM and NGinx. In this case, you can declare that both services are in the same pod. - -You must declare this label only on "supplementary" services and always use the same master service for the entire pod declaration. +**Example:** ```yaml -services: - web: - image: nginx - - php: - image: php:8-fpm - labels: - katenary.io/same-pod: web +ghost: + image: ghost:1.25.5 + labels: + # The chart is now named ghost, and the appVersion is 1.25.5. + # In Deployment, the image attribute is set to ghost:1.25.5 if + # you don't change the "tag" attribute in values.yaml + katenary.v3/main-app: true ``` -The above example will create a `web` deployment, the PHP container is added in the `web` pod. +### katenary.v3/map-env -## configmap-volumes +Map env vars from the service to the deployment. -This label proposes to declare a file or directory where content is actually static and can be mounted as configMap volume. +**Type**: `object` -It's a comma separated label, you can declare several volumes. +Because you may need to change the variable for Kubernetes, this label +forces the value to another. It is also particullary helpful to use a template +value instead. For example, you could bind the value to a service name +with Helm attributes: +`{{ tpl .Release.Name . }}`. -For example, in `static/index.html`: +If you use `__APP__` in the value, it will be replaced by the Chart name. -```html - -Hello - -``` - -And a compose file (snippet): +**Example:** ```yaml -serivces: - web: - image: nginx - volumes: - - ./static:/usr/share/nginx/html:z - labels: - katenary.io/configmap-volumes: ./statics +env: + DB_HOST: database + RUNNING: docker + OTHER: value +labels: + katenary.v3/map-env: |- + RUNNING: kubernetes + DB_HOST: '{{ include "__APP__.fullname" . }}-database' ``` -What will make Katenary: +### katenary.v3/ports -- create a configmap containing the "index.html" file as data -- declare the volume in the `web` deployment file -- mount the configmap in `/usr/share/nginx/html` directory of the container +Ports to be added to the service. -## ingress +**Type**: `list of uint32` -Declare which port to use to create an ingress. The hostname will be declared in `values.yaml` file. +Only useful for services without exposed port. It is mandatory if the +service is a dependency of another service. + +**Example:** ```yaml -serivces: - web: - image: nginx - ports: - - 8080:80 - labels: - katenary.io/ingress: 80 +labels: + katenary.v3/ports: |- + - 8080 + - 8081 ``` -!!! Info - A port **must** be declared, in `ports` section or with `katenary.io/ports` label. This to force the creation of a `Service`. +### katenary.v3/same-pod -## ports and container-ports +Move the same-pod deployment to the target deployment. -It's sometimes not mandatory to declare a port in compose file, or maybe you want to avoid to expose them in the compose file. But Katenary will sometimes need to know the ports to create service, for example to allow `depends_on` directive. +**Type**: `string` -In this case, you can declare the ports in the corresponding label: +This will make the service to be included in another service pod. Some services +must work together in the same pod, like a sidecar or a proxy or nginx + php-fpm. + +Note that volume and VolumeMount are copied from the source to the target +deployment. + +**Example:** ```yaml -serivces: - web: - image: nginx - labels: - katenary.io/ports: 80,443 +web: + image: nginx:1.19 + +php: + image: php:7.4-fpm + labels: + katenary.v3/same-pod: web ``` -This will leave Katenary creating the service to open these ports to others pods. +### katenary.v3/secrets -Sometimes, you need to have `containerPort` in pods but **avoid the service declaration**, so you can use this label: +Env vars to be set as secrets. + +**Type**: `list of string` + +This label allows setting the environment variables as secrets. The variable +is removed from the environment and added to a secret object. + +The variable can be set to the `katenary.v3/values` too, +so the secret value can be configured in values.yaml + +**Example:** ```yaml -services: - php: - image: php:8-fpm - labels: - katenary.io/container-ports: 9000 +env: + PASSWORD: a very secret password + NOT_A_SECRET: a public value +labels: + katenary.v3/secrets: |- + - PASSWORD ``` -That will only declare the container port in the pod, but not in the service. +### katenary.v3/values -!!! Info - It's very useful when you need to declare ports in conjonction with `same-pod`. Katenary would create a service with all the pods ports inside. The `container-ports` label will make the ports to be ignored in the service creation. +Environment variables to be added to the values.yaml -## mapenv +**Type**: `list of string or map` -Environment variables are working great for your compose stack but you sometimes need to change them in Helm. This label allows you to remap the value for Helm. +By default, all environment variables in the "env" and environment +files are added to configmaps with the static values set. This label +allows to add environment variables to the values.yaml file. -For example, when you use an environment variable to point on another service. +Note that the value inside the configmap is `{{ tpl vaname . }}`, so +you can set the value to a template that will be rendered with the +values.yaml file. + +The value can be set with a documentation. This may help to understand +the purpose of the variable. + +**Example:** ```yaml -serivces: - php: - image: php - environment: - DB_HOST: database - - database: - image: mariadb - labels: - katenary.io/ports: 3306 +env: + FOO: bar + DB_NAME: mydb + TO_CONFIGURE: something that can be changed in values.yaml + A_COMPLEX_VALUE: example +labels: + katenary.v3/values: |- + # simple values, set as is in values.yaml + - TO_CONFIGURE + # complex values, set as a template in values.yaml with a documentation + - A_COMPLEX_VALUE: |- + This is the documentation for the variable to + configure in values.yaml. + It can be, of course, a multiline text. ``` -The above example will break when you'll start it in Kubernetes because the `database` service will not be named like this, it will be renamed to `{{ .Release.Name }}-database`. So, you can declare the rewrite: - -```yaml -services: - php: - image: php - environment: - DB_HOST: database - labels: - katenary.io/mapenv: | - DB_HOST: "{{ .Release.Name }}"-database - database: - image: mariadb - labels: - katenary.io/ports: 3306 - -``` - -It's also useful when you want to change a variable value to another when you deploy on Kubernetes. - -## secret-envfiles - -Katenary binds all "environemnt files" to config maps. But some of these files can be bound as sercrets. - -In this case, declare the files as is: - -```yaml -services: - app: - image: #... - env_file: - - ./env/whatever - - ./env/sensitives - labels: - katenary.io/secret-envfiles: ./env/sensitives -``` - -## secret-vars - -If you have some environemnt variables to declare as secret, you can list them in the `secret-vars` label. - -```yaml -services: - database: - image: mariadb - environemnt: - MYSQL_PASSWORD: foobar - MYSQL_ROOT_PASSWORD: longpasswordhere - MYSQL_USER: john - MYSQL_DATABASE: appdb - labels: - katenary.io/secret-vars: MYSQL_ROOT_PASSWORD,MYSQL_PASSWORD -``` - -## ignore - -Simply ignore the service to not be exported in the Helm Chart. - -```yaml -serivces: - - # this service is able to answer HTTP - # on port 5000 - webapp: - image: myapp - labels: - # declare the port - katenary.io/ports: 5000 - # the ingress controller is a web proxy, so... - katenary.io/ingress: 5000 - - - # with local Docker, I want to access my webapp - # with "myapp.locahost" so I use a nice proxy on - # port 80 - proxy: - image: quay.io/pathwae/proxy - ports: - - 80:80 - environemnt: - CONFIG: | - myapp.localhost: webapp:5000 - labels: - # I don't need it in Helm, it's only - # for local test! - katenary.io/ignore: true -``` + diff --git a/doc/docs/packages/cmd/katenary.md b/doc/docs/packages/cmd/katenary.md new file mode 100644 index 0000000..03a3bb2 --- /dev/null +++ b/doc/docs/packages/cmd/katenary.md @@ -0,0 +1,8 @@ + + +# katenary + +``` go +import "katenary/cmd/katenary" +``` + diff --git a/doc/docs/packages/generator.md b/doc/docs/packages/generator.md new file mode 100644 index 0000000..5fd4773 --- /dev/null +++ b/doc/docs/packages/generator.md @@ -0,0 +1,893 @@ + + +# generator + +``` go +import "katenary/generator" +``` + +The generator package generates kubernetes objects from a compose file +and transforms them into a helm chart. + +The generator package is the core of katenary. It is responsible for +generating kubernetes objects from a compose file and transforming them +into a helm chart. Convertion manipulates Yaml representation of +kubernetes object to add conditions, labels, annotations, etc. to the +objects. It also create the values to be set to the values.yaml file. + +The generate.Convert() create an HelmChart object and call “Generate()” +method to convert from a compose file to a helm chart. It saves the helm +chart in the given directory. + +If you want to change or override the write behavior, you can use the +HelmChart.Generate() function and implement your own write function. +This function returns the helm chart object containing all kubernetes +objects and helm chart ingormation. It does not write the helm chart to +the disk. + +TODO: Manage cronjob + rbac TODO: create note.txt TODO: manage emptyDirs + +## Constants + +``` go +const KATENARY_PREFIX = "katenary.v3/" +``` + +## Variables + +``` go +var ( + + // Standard annotationss + Annotations = map[string]string{ + KATENARY_PREFIX + "version": Version, + } +) +``` + +Version is the version of katenary. It is set at compile time. + +``` go +var Version = "master" // changed at compile time +``` + +## func Convert + +``` go +func Convert(config ConvertOptions, dockerComposeFile ...string) +``` + +Convert a compose (docker, podman…) project to a helm chart. It calls +Generate() to generate the chart and then write it to the disk. + +## func GetLabelHelp + +``` go +func GetLabelHelp(asMarkdown bool) string +``` + +Generate the help for the labels. + +## func GetLabelHelpFor + +``` go +func GetLabelHelpFor(labelname string, asMarkdown bool) string +``` + +GetLabelHelpFor returns the help for a specific label. + +## func GetLabelNames + +``` go +func GetLabelNames() []string +``` + +GetLabelNames returns a sorted list of all katenary label names. + +## func GetLabels + +``` go +func GetLabels(serviceName, appName string) map[string]string +``` + +## func GetMatchLabels + +``` go +func GetMatchLabels(serviceName, appName string) map[string]string +``` + +## func Helper + +``` go +func Helper(name string) string +``` + +Helper returns the \_helpers.tpl file for a chart. + +## func NewCronJob + +``` go +func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (*CronJob, *RBAC) +``` + +NewCronJob creates a new CronJob from a compose service. The appName is +the name of the application taken from the project name. + +## type ChartTemplate + +ChartTemplate is a template of a chart. It contains the content of the +template and the name of the service. This is used internally to +generate the templates. + +TODO: maybe we can set it private. + +``` go +type ChartTemplate struct { + Content []byte + Servicename string +} +``` + +## type ConfigMap + +ConfigMap is a kubernetes ConfigMap. Implements the DataMap interface. + +``` go +type ConfigMap struct { + *corev1.ConfigMap + // contains filtered or unexported fields +} +``` + +### func NewConfigMap + +``` go +func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap +``` + +NewConfigMap creates a new ConfigMap from a compose service. The appName +is the name of the application taken from the project name. The +ConfigMap is filled by environment variables and labels “map-env”. + +### func NewConfigMapFromFiles + +``` go +func NewConfigMapFromFiles(service types.ServiceConfig, appName string, path string) *ConfigMap +``` + +NewConfigMapFromFiles creates a new ConfigMap from a compose service. +This path is the path to the file or directory. If the path is a +directory, all files in the directory are added to the ConfigMap. Each +subdirectory are ignored. Note that the Generate() function will create +the subdirectories ConfigMaps. + +### func (*ConfigMap) AddData + +``` go +func (c *ConfigMap) AddData(key string, value string) +``` + +AddData adds a key value pair to the configmap. Append or overwrite the +value if the key already exists. + +### func (*ConfigMap) AppendDir + +``` go +func (c *ConfigMap) AppendDir(path string) +``` + +AddFile adds files from given path to the configmap. It is not +recursive, to add all files in a directory, you need to call this +function for each subdirectory. + +### func (*ConfigMap) Filename + +``` go +func (c *ConfigMap) Filename() string +``` + +Filename returns the filename of the configmap. If the configmap is used +for files, the filename contains the path. + +### func (*ConfigMap) SetData + +``` go +func (c *ConfigMap) SetData(data map[string]string) +``` + +SetData sets the data of the configmap. It replaces the entire data. + +### func (*ConfigMap) Yaml + +``` go +func (c *ConfigMap) Yaml() ([]byte, error) +``` + +Yaml returns the yaml representation of the configmap + +## type ConvertOptions + +ConvertOptions are the options to convert a compose project to a helm +chart. + +``` go +type ConvertOptions struct { + Force bool // Force the chart directory deletion if it already exists. + OutputDir string // The output directory of the chart. + Profiles []string // Profile to use for the conversion. + HelmUpdate bool // If true, the "helm dep update" command will be run after the chart generation. + AppVersion *string // Set the chart "appVersion" field. If nil, the version will be set to 0.1.0. + ChartVersion string // Set the chart "version" field. +} +``` + +## type CronJob + +CronJob is a kubernetes CronJob. + +``` go +type CronJob struct { + *batchv1.CronJob + // contains filtered or unexported fields +} +``` + +### func (*CronJob) Filename + +``` go +func (c *CronJob) Filename() string +``` + +Filename returns the filename of the cronjob. + +Implements the Yaml interface. + +### func (*CronJob) Yaml + +``` go +func (c *CronJob) Yaml() ([]byte, error) +``` + +Yaml returns the yaml representation of the cronjob. + +Implements the Yaml interface. + +## type CronJobValue + +CronJobValue is a cronjob configuration that will be saved in +values.yaml. + +``` go +type CronJobValue struct { + Repository *RepositoryValue `yaml:"repository,omitempty"` + Environment map[string]any `yaml:"environment,omitempty"` + ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"` + Schedule string `yaml:"schedule"` +} +``` + +## type DataMap + +DataMap is a kubernetes ConfigMap or Secret. It can be used to add data +to the ConfigMap or Secret. + +``` go +type DataMap interface { + SetData(map[string]string) + AddData(string, string) +} +``` + +### func NewFileMap + +``` go +func NewFileMap(service types.ServiceConfig, appName string, kind string) DataMap +``` + +NewFileMap creates a new DataMap from a compose service. The appName is +the name of the application taken from the project name. + +## type Dependency + +Dependency is a dependency of a chart to other charts. + +``` go +type Dependency struct { + Name string `yaml:"name"` + Version string `yaml:"version"` + Repository string `yaml:"repository"` + Alias string `yaml:"alias,omitempty"` + Values map[string]any `yaml:"-"` // do not export to Chart.yaml +} +``` + +## type Deployment + +Deployment is a kubernetes Deployment. + +``` go +type Deployment struct { + *appsv1.Deployment `yaml:",inline"` + // contains filtered or unexported fields +} +``` + +### func NewDeployment + +``` go +func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment +``` + +NewDeployment creates a new Deployment from a compose service. The +appName is the name of the application taken from the project name. It +also creates the Values map that will be used to create the values.yaml +file. + +### func (*Deployment) AddContainer + +``` go +func (d *Deployment) AddContainer(service types.ServiceConfig) +``` + +AddContainer adds a container to the deployment. + +### func (*Deployment) AddHealthCheck + +``` go +func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) +``` + +### func (*Deployment) AddIngress + +``` go +func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *Ingress +``` + +AddIngress adds an ingress to the deployment. It creates the ingress +object. + +### func (*Deployment) AddVolumes + +``` go +func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) +``` + +AddVolumes adds a volume to the deployment. It does not create the PVC, +it only adds the volumes to the deployment. If the volume is a bind +volume it will warn the user that it is not supported yet. + +### func (*Deployment) BindFrom + +``` go +func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) +``` + +### func (*Deployment) DependsOn + +``` go +func (d *Deployment) DependsOn(to *Deployment) error +``` + +DependsOn adds a initContainer to the deployment that will wait for the +service to be up. + +### func (*Deployment) Filename + +``` go +func (d *Deployment) Filename() string +``` + +### func (*Deployment) SetEnvFrom + +``` go +func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) +``` + +SetEnvFrom sets the environment variables to a configmap. The configmap +is created. + +### func (*Deployment) Yaml + +``` go +func (d *Deployment) Yaml() ([]byte, error) +``` + +Yaml returns the yaml representation of the deployment. + +## type FileMapUsage + +FileMapUsage is the usage of the filemap. + +``` go +type FileMapUsage uint8 +``` + +FileMapUsage constants. + +``` go +const ( + FileMapUsageConfigMap FileMapUsage = iota // pure configmap for key:values. + FileMapUsageFiles // files in a configmap. +) +``` + +## type HelmChart + +HelmChart is a Helm Chart representation. It contains all the tempaltes, +values, versions, helpers… + +``` go +type HelmChart struct { + Name string `yaml:"name"` + ApiVersion string `yaml:"apiVersion"` + Version string `yaml:"version"` + AppVersion string `yaml:"appVersion"` + Description string `yaml:"description"` + Dependencies []Dependency `yaml:"dependencies,omitempty"` + Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml + Helper string `yaml:"-"` // do not export to yaml + Values map[string]any `yaml:"-"` // do not export to yaml + VolumeMounts map[string]any `yaml:"-"` // do not export to yaml + // contains filtered or unexported fields +} +``` + +### func Generate + +``` go +func Generate(project *types.Project) (*HelmChart, error) +``` + +Generate a chart from a compose project. This does not write files to +disk, it only creates the HelmChart object. + +The Generate function will create the HelmChart object this way: + +1. Detect the service port name or leave the port number if not found. + +2. Create a deployment for each service that are not ingnore. + +3. Create a service and ingresses for each service that has ports + and/or declared ingresses. + +4. Create a PVC or Configmap volumes for each volume. + +5. Create init containers for each service which has dependencies to + other services. + +6. Create a chart dependencies. + +7. Create a configmap and secrets from the environment variables. + +8. Merge the same-pod services. + +### func NewChart + +``` go +func NewChart(name string) *HelmChart +``` + +NewChart creates a new empty chart with the given name. + +## type Help + +Help is the documentation of a label. + +``` go +type Help struct { + Short string `yaml:"short"` + Long string `yaml:"long"` + Example string `yaml:"example"` + Type string `yaml:"type"` +} +``` + +## type Ingress + +``` go +type Ingress struct { + *networkv1.Ingress + // contains filtered or unexported fields +} +``` + +### func NewIngress + +``` go +func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress +``` + +NewIngress creates a new Ingress from a compose service. + +### func (*Ingress) Filename + +``` go +func (ingress *Ingress) Filename() string +``` + +### func (*Ingress) Yaml + +``` go +func (ingress *Ingress) Yaml() ([]byte, error) +``` + +## type IngressValue + +IngressValue is a ingress configuration that will be saved in +values.yaml. + +``` go +type IngressValue struct { + Enabled bool `yaml:"enabled"` + Host string `yaml:"host"` + Path string `yaml:"path"` + Class string `yaml:"class"` + Annotations map[string]string `yaml:"annotations"` +} +``` + +## type Label + +Label is a katenary label to find in compose files. + +``` go +type Label = string +``` + +Known labels. + +``` go +const ( + LABEL_MAIN_APP Label = KATENARY_PREFIX + "main-app" + LABEL_VALUES Label = KATENARY_PREFIX + "values" + LABEL_SECRETS Label = KATENARY_PREFIX + "secrets" + LABEL_PORTS Label = KATENARY_PREFIX + "ports" + LABEL_INGRESS Label = KATENARY_PREFIX + "ingress" + LABEL_MAP_ENV Label = KATENARY_PREFIX + "map-env" + LABEL_HEALTHCHECK Label = KATENARY_PREFIX + "health-check" + LABEL_SAME_POD Label = KATENARY_PREFIX + "same-pod" + LABEL_DESCRIPTION Label = KATENARY_PREFIX + "description" + LABEL_IGNORE Label = KATENARY_PREFIX + "ignore" + LABEL_DEPENDENCIES Label = KATENARY_PREFIX + "dependencies" + LABEL_CM_FILES Label = KATENARY_PREFIX + "configmap-files" + LABEL_CRONJOB Label = KATENARY_PREFIX + "cronjob" + LABEL_ENV_FROM Label = KATENARY_PREFIX + "env-from" +) +``` + +## type LabelType + +LabelType identifies the type of label to generate in objects. TODO: is +this still needed? + +``` go +type LabelType uint8 +``` + +``` go +const ( + DeploymentLabel LabelType = iota + ServiceLabel +) +``` + +## type PersistenceValue + +PersistenceValue is a persistence configuration that will be saved in +values.yaml. + +``` go +type PersistenceValue struct { + Enabled bool `yaml:"enabled"` + StorageClass string `yaml:"storageClass"` + Size string `yaml:"size"` + AccessMode []string `yaml:"accessMode"` +} +``` + +## type RBAC + +RBAC is a kubernetes RBAC containing a role, a rolebinding and an +associated serviceaccount. + +``` go +type RBAC struct { + RoleBinding *RoleBinding + Role *Role + ServiceAccount *ServiceAccount +} +``` + +### func NewRBAC + +``` go +func NewRBAC(service types.ServiceConfig, appName string) *RBAC +``` + +NewRBAC creates a new RBAC from a compose service. The appName is the +name of the application taken from the project name. + +## type RepositoryValue + +RepositoryValue is a docker repository image and tag that will be saved +in values.yaml. + +``` go +type RepositoryValue struct { + Image string `yaml:"image"` + Tag string `yaml:"tag"` +} +``` + +## type Role + +Role is a kubernetes Role. + +``` go +type Role struct { + *rbacv1.Role + // contains filtered or unexported fields +} +``` + +### func (*Role) Filename + +``` go +func (r *Role) Filename() string +``` + +### func (*Role) Yaml + +``` go +func (r *Role) Yaml() ([]byte, error) +``` + +## type RoleBinding + +RoleBinding is a kubernetes RoleBinding. + +``` go +type RoleBinding struct { + *rbacv1.RoleBinding + // contains filtered or unexported fields +} +``` + +### func (*RoleBinding) Filename + +``` go +func (r *RoleBinding) Filename() string +``` + +### func (*RoleBinding) Yaml + +``` go +func (r *RoleBinding) Yaml() ([]byte, error) +``` + +## type Secret + +Secret is a kubernetes Secret. + +Implements the DataMap interface. + +``` go +type Secret struct { + *corev1.Secret + // contains filtered or unexported fields +} +``` + +### func NewSecret + +``` go +func NewSecret(service types.ServiceConfig, appName string) *Secret +``` + +NewSecret creates a new Secret from a compose service + +### func (*Secret) AddData + +``` go +func (s *Secret) AddData(key string, value string) +``` + +AddData adds a key value pair to the secret. + +### func (*Secret) Filename + +``` go +func (s *Secret) Filename() string +``` + +Filename returns the filename of the secret. + +### func (*Secret) SetData + +``` go +func (s *Secret) SetData(data map[string]string) +``` + +SetData sets the data of the secret. + +### func (*Secret) Yaml + +``` go +func (s *Secret) Yaml() ([]byte, error) +``` + +Yaml returns the yaml representation of the secret. + +## type Service + +Service is a kubernetes Service. + +``` go +type Service struct { + *v1.Service `yaml:",inline"` + // contains filtered or unexported fields +} +``` + +### func NewService + +``` go +func NewService(service types.ServiceConfig, appName string) *Service +``` + +NewService creates a new Service from a compose service. + +### func (*Service) AddPort + +``` go +func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) +``` + +AddPort adds a port to the service. + +### func (*Service) Filename + +``` go +func (s *Service) Filename() string +``` + +Filename returns the filename of the service. + +### func (*Service) Yaml + +``` go +func (s *Service) Yaml() ([]byte, error) +``` + +Yaml returns the yaml representation of the service. + +## type ServiceAccount + +ServiceAccount is a kubernetes ServiceAccount. + +``` go +type ServiceAccount struct { + *corev1.ServiceAccount + // contains filtered or unexported fields +} +``` + +### func (*ServiceAccount) Filename + +``` go +func (r *ServiceAccount) Filename() string +``` + +### func (*ServiceAccount) Yaml + +``` go +func (r *ServiceAccount) Yaml() ([]byte, error) +``` + +## type Value + +Value will be saved in values.yaml. It contains configuraiton for all +deployment and services. The content will be lile: + + name_of_component: + repository: + image: image_name + tag: image_tag + persistence: + enabled: true + storageClass: storage_class_name + ingress: + enabled: true + host: host_name + path: path_name + environment: + ENV_VAR_1: value_1 + ENV_VAR_2: value_2 + +``` go +type Value struct { + Repository *RepositoryValue `yaml:"repository,omitempty"` + Persistence map[string]*PersistenceValue `yaml:"persistence,omitempty"` + Ingress *IngressValue `yaml:"ingress,omitempty"` + ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"` + Environment map[string]any `yaml:"environment,omitempty"` + Replicas *uint32 `yaml:"replicas,omitempty"` + CronJob *CronJobValue `yaml:"cronjob,omitempty"` +} +``` + +### func NewValue + +``` go +func NewValue(service types.ServiceConfig, main ...bool) *Value +``` + +NewValue creates a new Value from a compose service. The value contains +the necessary information to deploy the service (image, tag, replicas, +etc.). + +If \`main\` is true, the tag will be empty because it will be set in the +helm chart appVersion. + +### func (*Value) AddIngress + +``` go +func (v *Value) AddIngress(host, path string) +``` + +### func (*Value) AddPersistence + +``` go +func (v *Value) AddPersistence(volumeName string) +``` + +AddPersistence adds persistence configuration to the Value. + +## type VolumeClaim + +VolumeClaim is a kubernetes VolumeClaim. This is a +PersistentVolumeClaim. + +``` go +type VolumeClaim struct { + *v1.PersistentVolumeClaim + // contains filtered or unexported fields +} +``` + +### func NewVolumeClaim + +``` go +func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim +``` + +NewVolumeClaim creates a new VolumeClaim from a compose service. + +### func (*VolumeClaim) Filename + +``` go +func (v *VolumeClaim) Filename() string +``` + +Filename returns the suggested filename for a VolumeClaim. + +### func (*VolumeClaim) Yaml + +``` go +func (v *VolumeClaim) Yaml() ([]byte, error) +``` + +Yaml marshals a VolumeClaim into yaml. + +## type Yaml + +Yaml is a kubernetes object that can be converted to yaml. + +``` go +type Yaml interface { + Yaml() ([]byte, error) + Filename() string +} +``` + +Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc) diff --git a/doc/docs/packages/generator/extrafiles.md b/doc/docs/packages/generator/extrafiles.md new file mode 100644 index 0000000..ca80dd0 --- /dev/null +++ b/doc/docs/packages/generator/extrafiles.md @@ -0,0 +1,28 @@ + + +# extrafiles + +``` go +import "katenary/generator/extrafiles" +``` + +extrafiles package provides function to generate the Chart files that +are not objects. Like README.md and notes.txt… + +## func NotesFile + +``` go +func NotesFile() string +``` + +NoteTXTFile returns the content of the note.txt file. + +## func ReadMeFile + +``` go +func ReadMeFile(charname, description string, values map[string]any) string +``` + +ReadMeFile returns the content of the README.md file. + +Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc) diff --git a/doc/docs/packages/parser.md b/doc/docs/packages/parser.md new file mode 100644 index 0000000..3283326 --- /dev/null +++ b/doc/docs/packages/parser.md @@ -0,0 +1,20 @@ + + +# parser + +``` go +import "katenary/parser" +``` + +Parser package is a wrapper around compose-go to parse compose files. + +## func Parse + +``` go +func Parse(profiles []string, dockerComposeFile ...string) (*types.Project, error) +``` + +Parse compose files and return a project. The project is parsed with +dotenv, osenv and profiles. + +Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc) diff --git a/doc/docs/packages/update.md b/doc/docs/packages/update.md new file mode 100644 index 0000000..c6fab01 --- /dev/null +++ b/doc/docs/packages/update.md @@ -0,0 +1,55 @@ + + +# update + +``` go +import "katenary/update" +``` + +Update package is used to check if a new version of katenary is +available. + +## Variables + +``` go +var Version = "master" // reset by cmd/main.go +``` + +## func DownloadFile + +``` go +func DownloadFile(url, exe string) error +``` + +DownloadFile will download a url to a local file. It also ensure that +the file is executable. + +## func DownloadLatestVersion + +``` go +func DownloadLatestVersion(assets []Asset) error +``` + +DownloadLatestVersion will download the latest version of katenary. + +## type Asset + +Asset is a github asset from release url. + +``` go +type Asset struct { + Name string `json:"name"` + URL string `json:"browser_download_url"` +} +``` + +### func CheckLatestVersion + +``` go +func CheckLatestVersion() (string, []Asset, error) +``` + +CheckLatestVersion check katenary latest version from release and +propose to download it + +Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc) diff --git a/doc/docs/packages/utils.md b/doc/docs/packages/utils.md new file mode 100644 index 0000000..b497f03 --- /dev/null +++ b/doc/docs/packages/utils.md @@ -0,0 +1,187 @@ + + +# utils + +``` go +import "katenary/utils" +``` + +Utils package provides some utility functions used in katenary. It +defines some constants and functions used in the whole project. + +## Constants + +Icons used in katenary. + +``` go +const ( + IconSuccess Icon = "✅" + IconFailure = "❌" + IconWarning = "⚠️'" + IconNote = "📝" + IconWorld = "🌐" + IconPlug = "🔌" + IconPackage = "📦" + IconCabinet = "🗄️" + IconInfo = "❕" + IconSecret = "🔒" + IconConfig = "🔧" + IconDependency = "🔗" +) +``` + +## func CountStartingSpaces + +``` go +func CountStartingSpaces(line string) int +``` + +CountStartingSpaces counts the number of spaces at the beginning of a +string. + +## func GetContainerByName + +``` go +func GetContainerByName(name string, containers []corev1.Container) (*corev1.Container, int) +``` + +GetContainerByName returns a container by name and its index in the +array. It returns nil, -1 if not found. + +## func GetKind + +``` go +func GetKind(path string) (kind string) +``` + +GetKind returns the kind of the resource from the file path. + +## func GetServiceNameByPort + +``` go +func GetServiceNameByPort(port int) string +``` + +GetServiceNameByPort returns the service name for a port. It the service +name is not found, it returns an empty string. + +## func GetValuesFromLabel + +``` go +func GetValuesFromLabel(service types.ServiceConfig, LabelValues string) map[string]*EnvConfig +``` + +GetValuesFromLabel returns a map of values from a label. + +## func HashComposefiles + +``` go +func HashComposefiles(files []string) (string, error) +``` + +HashComposefiles returns a hash of the compose files. + +## func Int32Ptr + +``` go +func Int32Ptr(i int32) *int32 +``` + +Int32Ptr returns a pointer to an int32. + +## func MapKeys + +``` go +func MapKeys(m map[string]interface{}) []string +``` + +## func PathToName + +``` go +func PathToName(path string) string +``` + +PathToName converts a path to a kubernetes complient name. + +## func StrPtr + +``` go +func StrPtr(s string) *string +``` + +StrPtr returns a pointer to a string. + +## func TplName + +``` go +func TplName(serviceName, appname string, suffix ...string) string +``` + +TplName returns the name of the kubernetes resource as a template +string. It is used in the templates and defined in \_helper.tpl file. + +## func TplValue + +``` go +func TplValue(serviceName, variable string, pipes ...string) string +``` + +GetContainerByName returns a container by name and its index in the +array. + +## func Warn + +``` go +func Warn(msg ...interface{}) +``` + +Warn prints a warning message + +## func WordWrap + +``` go +func WordWrap(text string, lineWidth int) string +``` + +WordWrap wraps a string to a given line width. Warning: it may break the +string. You need to check the result. + +## func Wrap + +``` go +func Wrap(src, above, below string) string +``` + +Wrap wraps a string with a string above and below. It will respect the +indentation of the src string. + +## func WrapBytes + +``` go +func WrapBytes(src, above, below []byte) []byte +``` + +WrapBytes wraps a byte array with a byte array above and below. It will +respect the indentation of the src string. + +## type EnvConfig + +EnvConfig is a struct to hold the description of an environment +variable. + +``` go +type EnvConfig struct { + Description string + Service types.ServiceConfig +} +``` + +## type Icon + +Icon is a unicode icon + +``` go +type Icon string +``` + +Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc) diff --git a/doc/docs/statics/main.css b/doc/docs/statics/main.css index 9c1b7b0..94fca4d 100644 --- a/doc/docs/statics/main.css +++ b/doc/docs/statics/main.css @@ -27,7 +27,7 @@ button.md-clipboard:hover::after { article a, article a:visited { - color: var(--md-code-hl-number-color); + color: var(--md-code-hl-number-color) !important; } .md-center { @@ -53,3 +53,15 @@ pre code.hljs { background-color: var(--code-bg-color); color: var(--code-fg-color); } + +table tbody code { + text-align: left; + white-space: nowrap; + font-size: 1em !important; + background-color: transparent !important; + color: var(--md-code-hl-special-color) !important; +} + +h3[id*="katenaryio"] { + color: var(--md-code-hl-special-color); +} diff --git a/doc/mkdocs.yml b/doc/mkdocs.yml index 3409b25..e45174d 100644 --- a/doc/mkdocs.yml +++ b/doc/mkdocs.yml @@ -18,8 +18,8 @@ markdown_extensions: - admonition - attr_list - pymdownx.emoji: - emoji_generator: !!python/name:materialx.emoji.to_svg - emoji_index: !!python/name:materialx.emoji.twemoji + emoji_index: !!python/name:material.extensions.emoji.twemoji + emoji_generator: !!python/name:material.extensions.emoji.to_svg - pymdownx.highlight: anchor_linenums: true use_pygments: false @@ -28,7 +28,7 @@ extra_css: - statics/main.css extra_javascript: - statics/addons.js -copyright: Copyright © 2021 - 2022 - Katenary authors +copyright: Copyright © 2021 - 2023 - Katenary authors extra: generator: false social: @@ -38,3 +38,12 @@ nav: - "Home": index.md - usage.md - labels.md + - Behind the scene: + - coding.md + - dependencies.md + - Go Packages: + - packages/generator.md + - packages/parser.md + - packages/update.md + - packages/utils.md + - packages/generator/extrafiles.md diff --git a/doc/requirements.txt b/doc/requirements.txt index 2ea3f00..234bde6 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,4 +1,4 @@ -mkdocs==1.3.0 +mkdocs>=1.3.0 Jinja2>=2.10.2 MarkupSafe>=2.0 pymdown-extensions>=9.5 diff --git a/examples/basic/README.md b/examples/basic/README.md deleted file mode 100644 index 1b3974a..0000000 --- a/examples/basic/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Basic example - -This is a basic example of what can do Katenary with standard docker-compose file. - -In this example: - -- `depends_on` yield a `initContainer` in the webapp ddeployment to wait for database -- so we need to declare the listened port inside `database` container as we don't use it with docker-compose- also, we needed to declare that `DB_HOST` is actually a service name using `mapenv` label - -Take a look on [chart/basic](chart/basic) directory to see what `katenary convert` command has generated. diff --git a/examples/basic/chart/basic/Chart.yaml b/examples/basic/chart/basic/Chart.yaml deleted file mode 100644 index 88573d5..0000000 --- a/examples/basic/chart/basic/Chart.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Create on 2022-02-17T10:27:30+01:00 -# Katenary command line: katenary convert -apiVersion: v2 -appVersion: 0.0.1 -description: A helm chart for basic -name: basic -type: application -version: 0.1.0 diff --git a/examples/basic/chart/basic/templates/NOTES.txt b/examples/basic/chart/basic/templates/NOTES.txt deleted file mode 100644 index f4ec230..0000000 --- a/examples/basic/chart/basic/templates/NOTES.txt +++ /dev/null @@ -1,8 +0,0 @@ - -Congratulations, - -Your application is now deployed. This may take a while to be up and responding. - -{{ if .Values.webapp.ingress.enabled -}} -- webapp is accessible on : http://{{ .Values.webapp.ingress.host }} -{{- end }} diff --git a/examples/basic/chart/basic/templates/database.deployment.yaml b/examples/basic/chart/basic/templates/database.deployment.yaml deleted file mode 100644 index 1f18276..0000000 --- a/examples/basic/chart/basic/templates/database.deployment.yaml +++ /dev/null @@ -1,39 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: '{{ .Release.Name }}-database' - labels: - katenary.io/component: database - katenary.io/project: basic - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: b9f12bb7d1e97901c1d7680394209525763f6640 - katenary.io/version: master-3619cc4 -spec: - replicas: 1 - selector: - matchLabels: - katenary.io/component: database - katenary.io/release: '{{ .Release.Name }}' - template: - metadata: - labels: - katenary.io/component: database - katenary.io/release: '{{ .Release.Name }}' - spec: - containers: - - name: database - image: '{{ .Values.database.image }}' - ports: - - name: database - containerPort: 3306 - env: - - name: MARIADB_PASSWORD - value: foo - - name: MARIADB_DATABASE - value: myapp - - name: MARIADB_ROOT_PASSWORD - value: foobar - - name: MARIADB_USER - value: foo - diff --git a/examples/basic/chart/basic/templates/database.service.yaml b/examples/basic/chart/basic/templates/database.service.yaml deleted file mode 100644 index ffde282..0000000 --- a/examples/basic/chart/basic/templates/database.service.yaml +++ /dev/null @@ -1,19 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: '{{ .Release.Name }}-database' - labels: - katenary.io/component: database - katenary.io/project: basic - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: b9f12bb7d1e97901c1d7680394209525763f6640 - katenary.io/version: master-3619cc4 -spec: - selector: - katenary.io/component: database - katenary.io/release: '{{ .Release.Name }}' - ports: - - protocol: TCP - port: 3306 - targetPort: 3306 diff --git a/examples/basic/chart/basic/templates/webapp.deployment.yaml b/examples/basic/chart/basic/templates/webapp.deployment.yaml deleted file mode 100644 index d53d242..0000000 --- a/examples/basic/chart/basic/templates/webapp.deployment.yaml +++ /dev/null @@ -1,48 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: '{{ .Release.Name }}-webapp' - labels: - katenary.io/component: webapp - katenary.io/project: basic - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: b9f12bb7d1e97901c1d7680394209525763f6640 - katenary.io/version: master-3619cc4 -spec: - replicas: 1 - selector: - matchLabels: - katenary.io/component: webapp - katenary.io/release: '{{ .Release.Name }}' - template: - metadata: - labels: - katenary.io/component: webapp - katenary.io/release: '{{ .Release.Name }}' - spec: - initContainers: - - name: check-database - image: busybox - command: - - sh - - -c - - |- - OK=0 - echo "Checking database port" - while [ $OK != 1 ]; do - echo -n "." - nc -z {{ .Release.Name }}-database 3306 2>&1 >/dev/null && OK=1 || sleep 1 - done - echo - echo "Done" - containers: - - name: webapp - image: '{{ .Values.webapp.image }}' - ports: - - name: webapp - containerPort: 80 - env: - - name: DB_HOST - value: '{{ .Release.Name }}-database' - diff --git a/examples/basic/chart/basic/templates/webapp.ingress.yaml b/examples/basic/chart/basic/templates/webapp.ingress.yaml deleted file mode 100644 index 6bb2544..0000000 --- a/examples/basic/chart/basic/templates/webapp.ingress.yaml +++ /dev/null @@ -1,34 +0,0 @@ -{{- if .Values.webapp.ingress.enabled -}} -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: '{{ .Release.Name }}-webapp' - labels: - katenary.io/component: webapp - katenary.io/project: basic - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: b9f12bb7d1e97901c1d7680394209525763f6640 - katenary.io/version: master-3619cc4 -spec: - {{- if and .Values.webapp.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: '{{ .Values.webapp.ingress.class }}' - {{- end }} - rules: - - host: '{{ .Values.webapp.ingress.host }}' - http: - paths: - - path: / - pathType: Prefix - backend: - {{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }} - service: - name: '{{ .Release.Name }}-webapp' - port: - number: 80 - {{- else }} - serviceName: '{{ .Release.Name }}-webapp' - servicePort: 80 - {{- end }} - -{{- end -}} diff --git a/examples/basic/chart/basic/templates/webapp.service.yaml b/examples/basic/chart/basic/templates/webapp.service.yaml deleted file mode 100644 index 60b5080..0000000 --- a/examples/basic/chart/basic/templates/webapp.service.yaml +++ /dev/null @@ -1,19 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: '{{ .Release.Name }}-webapp' - labels: - katenary.io/component: webapp - katenary.io/project: basic - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: b9f12bb7d1e97901c1d7680394209525763f6640 - katenary.io/version: master-3619cc4 -spec: - selector: - katenary.io/component: webapp - katenary.io/release: '{{ .Release.Name }}' - ports: - - protocol: TCP - port: 80 - targetPort: 80 diff --git a/examples/basic/chart/basic/values.yaml b/examples/basic/chart/basic/values.yaml deleted file mode 100644 index 36216b1..0000000 --- a/examples/basic/chart/basic/values.yaml +++ /dev/null @@ -1,8 +0,0 @@ -database: - image: mariadb:10 -webapp: - image: php:7-apache - ingress: - class: nginx - enabled: false - host: webapp.basic.tld diff --git a/examples/basic/docker-compose.yaml b/examples/basic/docker-compose.yaml deleted file mode 100644 index dd24083..0000000 --- a/examples/basic/docker-compose.yaml +++ /dev/null @@ -1,31 +0,0 @@ -version: "3" - -# this example is absolutely not working, it's an example to see how it is converted -# by Katenary -services: - webapp: - image: php:7-apache - environment: - DB_HOST: database - ports: - - "8080:80" - labels: - # expose an ingress - katenary.io/ingress: 80 - # DB_HOST is actually a service name - katenary.io/mapenv: | - DB_HOST: "{{ .Release.Name }}-database" - depends_on: - - database - - database: - image: mariadb:10 - environment: - MARIADB_ROOT_PASSWORD: foobar - MARIADB_USER: foo - MARIADB_PASSWORD: foo - MARIADB_DATABASE: myapp - labels: - # because we don't provide "ports" or "expose", alert katenary - # to use the mysql port for service declaration - katenary.io/ports: 3306 diff --git a/examples/cronjobs/chart/README.md b/examples/cronjobs/chart/README.md new file mode 100644 index 0000000..7da7cf1 --- /dev/null +++ b/examples/cronjobs/chart/README.md @@ -0,0 +1,49 @@ +# cronjobs + +A Helm chart for cronjobs + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```bash +# Standard Helm install +$ helm install my-release cronjobs + +# To use a custom namespace and force the creation of the namespace +$ helm install my-release --namespace my-namespace --create-namespace cronjobs + +# To use a custom values file +$ helm install my-release -f my-values.yaml cronjobs +``` + +See the [Helm documentation](https://helm.sh/docs/intro/using_helm/) for more information on installing and managing the chart. + +## Configuration + +The following table lists the configurable parameters of the cronjobs chart and their default values. + +| Parameter | Default | +| ----------------------------------- | -------------- | +| `app.imagePullPolicy` | `IfNotPresent` | +| `app.replicas` | `1` | +| `app.repository.image` | `nginx` | +| `app.repository.tag` | `` | +| `backup.cronjob.imagePullPolicy` | `IfNotPresent` | +| `backup.cronjob.repository.image` | `alpine` | +| `backup.cronjob.repository.tag` | `1` | +| `backup.cronjob.schedule` | `@hourly` | +| `backup.imagePullPolicy` | `IfNotPresent` | +| `backup.replicas` | `1` | +| `backup.repository.image` | `alpine` | +| `backup.repository.tag` | `1` | +| `withrbac.cronjob.imagePullPolicy` | `IfNotPresent` | +| `withrbac.cronjob.repository.image` | `busybox` | +| `withrbac.cronjob.repository.tag` | `` | +| `withrbac.cronjob.schedule` | `@daily` | +| `withrbac.imagePullPolicy` | `IfNotPresent` | +| `withrbac.replicas` | `1` | +| `withrbac.repository.image` | `busybox` | +| `withrbac.repository.tag` | `` | + + diff --git a/examples/cronjobs/chart/templates/NOTES.txt b/examples/cronjobs/chart/templates/NOTES.txt new file mode 100644 index 0000000..3121a00 --- /dev/null +++ b/examples/cronjobs/chart/templates/NOTES.txt @@ -0,0 +1,27 @@ +Your release is named {{ .Release.Name }}. + +To learn more about the release, try: + + $ helm -n {{ .Release.Namespace }} status {{ .Release.Name }} + $ helm -n {{ .Release.Namespace }} get all {{ .Release.Name }} + +To delete the release, run: + + $ helm -n {{ .Release.Namespace }} delete {{ .Release.Name }} + +You can see this notes again by running: + + $ helm -n {{ .Release.Namespace }} get notes {{ .Release.Name }} + +{{- $count := 0 -}} +{{- range $s, $v := .Values -}} +{{- if and $v $v.ingress -}} +{{- $count = add $count 1 -}} +{{- if eq $count 1 }} + +The ingress list is: +{{ end }} + - {{ $s }}: http://{{ $v.ingress.host }}{{ $v.ingress.path }} +{{- end -}} +{{ end -}} + diff --git a/examples/cronjobs/chart/templates/_helpers.tpl b/examples/cronjobs/chart/templates/_helpers.tpl new file mode 100644 index 0000000..2bba0e2 --- /dev/null +++ b/examples/cronjobs/chart/templates/_helpers.tpl @@ -0,0 +1,36 @@ +{{- define "cronjobs.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "cronjobs.name" -}} +{{- if .Values.nameOverride -}} +{{- .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} + +{{- define "cronjobs.labels" -}} +{{ include "cronjobs.selectorLabels" .}} +{{ if .Chart.Version -}} +{{ printf "katenary.v3/chart-version: %s" .Chart.Version }} +{{- end }} +{{ if .Chart.AppVersion -}} +{{ printf "katenary.v3/app-version: %s" .Chart.AppVersion }} +{{- end }} +{{- end -}} + +{{- define "cronjobs.selectorLabels" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{ printf "katenary.v3/name: %s" $name }} +{{ printf "katenary.v3/instance: %s" .Release.Name }} +{{- end -}} diff --git a/examples/ghost/README.md b/examples/ghost/README.md deleted file mode 100644 index 58135d4..0000000 --- a/examples/ghost/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Example with Ghost - -[Ghost](https://ghost.org/) is a simple but powerfull blog engine. It is very nice to test some behaviors with Docker or Podman. - -The given `docker-compose.yaml` file here declares a stand-alone blog service. To help using it, we use [Patwae](https://pathwae.net) reverse-proxy to listend http://ghost.example.localhost - -The problem to solve is that the `url` environment variable correspond to the Ingress host when we will convert it to Helm Chart. So, we use the `mapenv` label to declare that `url` is actually `{{ .Values.blog.ingress.host }}` value. - -Note that we also `ignore` pathwae because we don't need it in our Helm Chart. diff --git a/examples/ghost/chart/ghost/Chart.yaml b/examples/ghost/chart/ghost/Chart.yaml deleted file mode 100644 index f4732b0..0000000 --- a/examples/ghost/chart/ghost/Chart.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Create on 2022-05-05T14:16:27+02:00 -# Katenary command line: /tmp/go-build669507924/b001/exe/main convert -apiVersion: v2 -appVersion: 0.0.1 -description: A helm chart for ghost -name: ghost -type: application -version: 0.1.0 diff --git a/examples/ghost/chart/ghost/templates/NOTES.txt b/examples/ghost/chart/ghost/templates/NOTES.txt deleted file mode 100644 index 10ce5b3..0000000 --- a/examples/ghost/chart/ghost/templates/NOTES.txt +++ /dev/null @@ -1,8 +0,0 @@ - -Congratulations, - -Your application is now deployed. This may take a while to be up and responding. - -{{ if .Values.blog.ingress.enabled -}} -- blog is accessible on : http://{{ .Values.blog.ingress.host }} -{{- end }} diff --git a/examples/ghost/chart/ghost/templates/blog.deployment.yaml b/examples/ghost/chart/ghost/templates/blog.deployment.yaml deleted file mode 100644 index 6378e0d..0000000 --- a/examples/ghost/chart/ghost/templates/blog.deployment.yaml +++ /dev/null @@ -1,33 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: '{{ .Release.Name }}-blog' - labels: - katenary.io/component: blog - katenary.io/project: ghost - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 0c2bbf548ff569c3dc5d77dc158e98bbe86fb5d4 - katenary.io/version: master -spec: - replicas: 1 - selector: - matchLabels: - katenary.io/component: blog - katenary.io/release: '{{ .Release.Name }}' - template: - metadata: - labels: - katenary.io/component: blog - katenary.io/release: '{{ .Release.Name }}' - spec: - containers: - - name: blog - image: '{{ .Values.blog.image }}' - ports: - - name: blog - containerPort: 2368 - env: - - name: url - value: http://{{ .Values.blog.ingress.host }} - diff --git a/examples/ghost/chart/ghost/templates/blog.ingress.yaml b/examples/ghost/chart/ghost/templates/blog.ingress.yaml deleted file mode 100644 index 43c804d..0000000 --- a/examples/ghost/chart/ghost/templates/blog.ingress.yaml +++ /dev/null @@ -1,42 +0,0 @@ -{{- if .Values.blog.ingress.enabled -}} -{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1 -{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1beta1 -{{- else -}} -apiVersion: extensions/v1beta1 -{{- end }} -kind: Ingress -metadata: - name: '{{ .Release.Name }}-blog' - labels: - katenary.io/component: blog - katenary.io/project: ghost - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 0c2bbf548ff569c3dc5d77dc158e98bbe86fb5d4 - katenary.io/version: master -spec: - {{- if and .Values.blog.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: '{{ .Values.blog.ingress.class }}' - {{- end }} - rules: - - host: '{{ .Values.blog.ingress.host }}' - http: - paths: - - path: / - {{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }} - pathType: Prefix - {{- end }} - backend: - {{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }} - service: - name: '{{ .Release.Name }}-blog' - port: - number: 2368 - {{- else }} - serviceName: '{{ .Release.Name }}-blog' - servicePort: 2368 - {{- end }} - -{{- end -}} diff --git a/examples/ghost/chart/ghost/templates/blog.service.yaml b/examples/ghost/chart/ghost/templates/blog.service.yaml deleted file mode 100644 index 5c54299..0000000 --- a/examples/ghost/chart/ghost/templates/blog.service.yaml +++ /dev/null @@ -1,19 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: '{{ .Release.Name }}-blog' - labels: - katenary.io/component: blog - katenary.io/project: ghost - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 0c2bbf548ff569c3dc5d77dc158e98bbe86fb5d4 - katenary.io/version: master -spec: - selector: - katenary.io/component: blog - katenary.io/release: '{{ .Release.Name }}' - ports: - - protocol: TCP - port: 2368 - targetPort: 2368 diff --git a/examples/ghost/chart/ghost/values.yaml b/examples/ghost/chart/ghost/values.yaml deleted file mode 100644 index 6ef57af..0000000 --- a/examples/ghost/chart/ghost/values.yaml +++ /dev/null @@ -1,6 +0,0 @@ -blog: - image: ghost - ingress: - class: nginx - enabled: false - host: blog.ghost.tld diff --git a/examples/ghost/docker-compose.yaml b/examples/ghost/docker-compose.yaml deleted file mode 100644 index 67472f7..0000000 --- a/examples/ghost/docker-compose.yaml +++ /dev/null @@ -1,30 +0,0 @@ -version: "3" - -services: - blog: - image: ghost - environment: - # this is OK for local test, but not with Helm - # because the URL depends on Ingress - url: http://ghost.example.localhost - labels: - katenary.io/ports: 2368 - katenary.io/ingress: 2368 - # ... so we declare that "url" is actually - # the ingress host - katenary.io/mapenv: | - url: http://{{ .Values.blog.ingress.host }} - - proxy: - # A simple proxy for localhost - image: quay.io/pathwae/proxy - environment: - CONFIG: | - ghost.example.localhost: - to: http://blog:2368 - ports: - - 80:80 - labels: - # we don't want this in Helm because we will use - # an ingress - katenary.io/ignore: true diff --git a/examples/multidir/chart/README.md b/examples/multidir/chart/README.md new file mode 100644 index 0000000..93ed0ac --- /dev/null +++ b/examples/multidir/chart/README.md @@ -0,0 +1,37 @@ +# multidir + +A Helm chart for multidir + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```bash +# Standard Helm install +$ helm install my-release multidir + +# To use a custom namespace and force the creation of the namespace +$ helm install my-release --namespace my-namespace --create-namespace multidir + +# To use a custom values file +$ helm install my-release -f my-values.yaml multidir +``` + +See the [Helm documentation](https://helm.sh/docs/intro/using_helm/) for more information on installing and managing the chart. + +## Configuration + +The following table lists the configurable parameters of the multidir chart and their default values. + +| Parameter | Default | +| ---------------------- | -------------- | +| `bar.imagePullPolicy` | `IfNotPresent` | +| `bar.replicas` | `1` | +| `bar.repository.image` | `alpine` | +| `bar.repository.tag` | `` | +| `foo.imagePullPolicy` | `IfNotPresent` | +| `foo.replicas` | `1` | +| `foo.repository.image` | `alpine` | +| `foo.repository.tag` | `` | + + diff --git a/examples/multidir/chart/templates/NOTES.txt b/examples/multidir/chart/templates/NOTES.txt new file mode 100644 index 0000000..3121a00 --- /dev/null +++ b/examples/multidir/chart/templates/NOTES.txt @@ -0,0 +1,27 @@ +Your release is named {{ .Release.Name }}. + +To learn more about the release, try: + + $ helm -n {{ .Release.Namespace }} status {{ .Release.Name }} + $ helm -n {{ .Release.Namespace }} get all {{ .Release.Name }} + +To delete the release, run: + + $ helm -n {{ .Release.Namespace }} delete {{ .Release.Name }} + +You can see this notes again by running: + + $ helm -n {{ .Release.Namespace }} get notes {{ .Release.Name }} + +{{- $count := 0 -}} +{{- range $s, $v := .Values -}} +{{- if and $v $v.ingress -}} +{{- $count = add $count 1 -}} +{{- if eq $count 1 }} + +The ingress list is: +{{ end }} + - {{ $s }}: http://{{ $v.ingress.host }}{{ $v.ingress.path }} +{{- end -}} +{{ end -}} + diff --git a/examples/multidir/chart/templates/_helpers.tpl b/examples/multidir/chart/templates/_helpers.tpl new file mode 100644 index 0000000..a0db3ab --- /dev/null +++ b/examples/multidir/chart/templates/_helpers.tpl @@ -0,0 +1,36 @@ +{{- define "multidir.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "multidir.name" -}} +{{- if .Values.nameOverride -}} +{{- .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} + +{{- define "multidir.labels" -}} +{{ include "multidir.selectorLabels" .}} +{{ if .Chart.Version -}} +{{ printf "katenary.v3/chart-version: %s" .Chart.Version }} +{{- end }} +{{ if .Chart.AppVersion -}} +{{ printf "katenary.v3/app-version: %s" .Chart.AppVersion }} +{{- end }} +{{- end -}} + +{{- define "multidir.selectorLabels" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{ printf "katenary.v3/name: %s" $name }} +{{ printf "katenary.v3/instance: %s" .Release.Name }} +{{- end -}} diff --git a/examples/multidir/conf/example1.conf b/examples/multidir/conf/example1.conf new file mode 100644 index 0000000..1ce1e1f --- /dev/null +++ b/examples/multidir/conf/example1.conf @@ -0,0 +1 @@ +A file containing configuration here diff --git a/examples/multidir/conf/otherdir/example.conf b/examples/multidir/conf/otherdir/example.conf new file mode 100644 index 0000000..a34c637 --- /dev/null +++ b/examples/multidir/conf/otherdir/example.conf @@ -0,0 +1,2 @@ +variable: foo +example: bar diff --git a/examples/same-pod/README.md b/examples/same-pod/README.md deleted file mode 100644 index e066d7d..0000000 --- a/examples/same-pod/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Make it possible to bind several containers in one pod - -In this example, we need to make nginx and php-fpm to run inside the same "pod". The reason is that we configured FPM to listen an unix socket instead of the 9000 port. - -Because NGinx will need to connect to the unix socket wich is a file, both containers should share the same node and work together. - -So, in the docker-compose file, we need to declare: -- `katenary.io/empty-dirs: socket` where `socket` is the "volume name", this will avoid the creation of a PVC -- `katenary.io/same-pod: http` in `php` container to declare that this will be added in the `containers` section of the `http` deployment - -You can note that we also use `configmap-volumes` to declare our configuration as `configMap`. - -Take a look on [chart/same-pod](chart/same-pod) directory to see the result of the `katenary convert` command. diff --git a/examples/same-pod/chart/same-pod/Chart.yaml b/examples/same-pod/chart/same-pod/Chart.yaml deleted file mode 100644 index 146c029..0000000 --- a/examples/same-pod/chart/same-pod/Chart.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Create on 2022-02-17T11:36:02+01:00 -# Katenary command line: katenary convert --force -apiVersion: v2 -appVersion: 0.0.1 -description: A helm chart for same-pod -name: same-pod -type: application -version: 0.1.0 diff --git a/examples/same-pod/chart/same-pod/templates/NOTES.txt b/examples/same-pod/chart/same-pod/templates/NOTES.txt deleted file mode 100644 index dffd887..0000000 --- a/examples/same-pod/chart/same-pod/templates/NOTES.txt +++ /dev/null @@ -1,8 +0,0 @@ - -Congratulations, - -Your application is now deployed. This may take a while to be up and responding. - -{{ if .Values.http.ingress.enabled -}} -- http is accessible on : http://{{ .Values.http.ingress.host }} -{{- end }} diff --git a/examples/same-pod/chart/same-pod/templates/http.config-nginx-http.configmap.yaml b/examples/same-pod/chart/same-pod/templates/http.config-nginx-http.configmap.yaml deleted file mode 100644 index e090e01..0000000 --- a/examples/same-pod/chart/same-pod/templates/http.config-nginx-http.configmap.yaml +++ /dev/null @@ -1,23 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: '{{ .Release.Name }}-config-nginx-http' - labels: - katenary.io/component: "" - katenary.io/project: same-pod - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 74e67695bfdbb829f15531321e158808018280e0 - katenary.io/version: master-bf44d44 -data: - default.conf: | - upstream _php { - server unix:/sock/fpm.sock; - } - server { - listen 80; - location ~ ^/index\.php(/|$) { - fastcgi_pass _php; - include fastcgi_params; - } - } diff --git a/examples/same-pod/chart/same-pod/templates/http.config-php-php.configmap.yaml b/examples/same-pod/chart/same-pod/templates/http.config-php-php.configmap.yaml deleted file mode 100644 index 99093be..0000000 --- a/examples/same-pod/chart/same-pod/templates/http.config-php-php.configmap.yaml +++ /dev/null @@ -1,30 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: '{{ .Release.Name }}-config-php-php' - labels: - katenary.io/component: "" - katenary.io/project: same-pod - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 74e67695bfdbb829f15531321e158808018280e0 - katenary.io/version: master-bf44d44 -data: - www.conf: | - [www] - user = www-data - group = www-data - - listen = /sock/fpm.sock - - pm = dynamic - pm.max_children = 5 - pm.start_servers = 2 - pm.min_spare_servers = 1 - pm.max_spare_servers = 3 - - access.log = /proc/self/fd/2 - log_limit = 8192 - clear_env = no - catch_workers_output = yes - decorate_workers_output = no diff --git a/examples/same-pod/chart/same-pod/templates/http.deployment.yaml b/examples/same-pod/chart/same-pod/templates/http.deployment.yaml deleted file mode 100644 index f1d86c0..0000000 --- a/examples/same-pod/chart/same-pod/templates/http.deployment.yaml +++ /dev/null @@ -1,52 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: '{{ .Release.Name }}-http' - labels: - katenary.io/component: http - katenary.io/project: same-pod - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 74e67695bfdbb829f15531321e158808018280e0 - katenary.io/version: master-bf44d44 -spec: - replicas: 1 - selector: - matchLabels: - katenary.io/component: http - katenary.io/release: '{{ .Release.Name }}' - template: - metadata: - labels: - katenary.io/component: http - katenary.io/release: '{{ .Release.Name }}' - spec: - containers: - - name: http - image: '{{ .Values.http.image }}' - ports: - - name: http - containerPort: 80 - volumeMounts: - - mountPath: /sock - name: sock - - mountPath: /etc/nginx/conf.d - name: config-nginx - - name: php - image: '{{ .Values.php.image }}' - volumeMounts: - - mountPath: /sock - name: sock - - mountPath: /usr/local/etc/php-fpm.d/www.conf - name: config-php - subPath: www.conf - volumes: - - emptyDir: {} - name: sock - - configMap: - name: '{{ .Release.Name }}-config-nginx-http' - name: config-nginx - - configMap: - name: '{{ .Release.Name }}-config-php-php' - name: config-php - diff --git a/examples/same-pod/chart/same-pod/templates/http.ingress.yaml b/examples/same-pod/chart/same-pod/templates/http.ingress.yaml deleted file mode 100644 index 220838f..0000000 --- a/examples/same-pod/chart/same-pod/templates/http.ingress.yaml +++ /dev/null @@ -1,34 +0,0 @@ -{{- if .Values.http.ingress.enabled -}} -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: '{{ .Release.Name }}-http' - labels: - katenary.io/component: http - katenary.io/project: same-pod - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 74e67695bfdbb829f15531321e158808018280e0 - katenary.io/version: master-bf44d44 -spec: - {{- if and .Values.http.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: '{{ .Values.http.ingress.class }}' - {{- end }} - rules: - - host: '{{ .Values.http.ingress.host }}' - http: - paths: - - path: / - pathType: Prefix - backend: - {{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }} - service: - name: '{{ .Release.Name }}-http' - port: - number: 80 - {{- else }} - serviceName: '{{ .Release.Name }}-http' - servicePort: 80 - {{- end }} - -{{- end -}} diff --git a/examples/same-pod/chart/same-pod/templates/http.service.yaml b/examples/same-pod/chart/same-pod/templates/http.service.yaml deleted file mode 100644 index 88157dd..0000000 --- a/examples/same-pod/chart/same-pod/templates/http.service.yaml +++ /dev/null @@ -1,19 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: '{{ .Release.Name }}-http' - labels: - katenary.io/component: http - katenary.io/project: same-pod - katenary.io/release: '{{ .Release.Name }}' - annotations: - katenary.io/docker-compose-sha1: 74e67695bfdbb829f15531321e158808018280e0 - katenary.io/version: master-bf44d44 -spec: - selector: - katenary.io/component: http - katenary.io/release: '{{ .Release.Name }}' - ports: - - protocol: TCP - port: 80 - targetPort: 80 diff --git a/examples/same-pod/chart/same-pod/values.yaml b/examples/same-pod/chart/same-pod/values.yaml deleted file mode 100644 index 68e0b65..0000000 --- a/examples/same-pod/chart/same-pod/values.yaml +++ /dev/null @@ -1,8 +0,0 @@ -http: - image: nginx:alpine - ingress: - class: nginx - enabled: false - host: http.same-pod.tld -php: - image: php:fpm diff --git a/examples/same-pod/config/nginx/default.conf b/examples/same-pod/config/nginx/default.conf deleted file mode 100644 index f0dbd67..0000000 --- a/examples/same-pod/config/nginx/default.conf +++ /dev/null @@ -1,10 +0,0 @@ -upstream _php { - server unix:/sock/fpm.sock; -} -server { - listen 80; - location ~ ^/index\.php(/|$) { - fastcgi_pass _php; - include fastcgi_params; - } -} diff --git a/examples/same-pod/config/php/www.conf b/examples/same-pod/config/php/www.conf deleted file mode 100644 index 3640421..0000000 --- a/examples/same-pod/config/php/www.conf +++ /dev/null @@ -1,17 +0,0 @@ -[www] -user = www-data -group = www-data - -listen = /sock/fpm.sock - -pm = dynamic -pm.max_children = 5 -pm.start_servers = 2 -pm.min_spare_servers = 1 -pm.max_spare_servers = 3 - -access.log = /proc/self/fd/2 -log_limit = 8192 -clear_env = no -catch_workers_output = yes -decorate_workers_output = no diff --git a/examples/same-pod/docker-compose.yaml b/examples/same-pod/docker-compose.yaml deleted file mode 100644 index a215527..0000000 --- a/examples/same-pod/docker-compose.yaml +++ /dev/null @@ -1,38 +0,0 @@ -version: "3" - -services: - - http: - image: nginx:alpine - ports: - - "8080:80" - volumes: - - "sock:/sock" - - "./config/nginx:/etc/nginx/conf.d:z" - - labels: - # the "sock" volume will need to be shared to the same pod, so let's - # declare that this is not a PVC - katenary.io/empty-dirs: sock - - # use ./config/nginx as a configMap - katenary.io/configmap-volumes: ./config/nginx - - # declare an ingress - katenary.io/ingress: 80 - - php: - image: php:fpm - volumes: - - "sock:/sock" - - "./config/php/www.conf:/usr/local/etc/php-fpm.d/www.conf:z" - labels: - # fpm will need to use a unix socket shared - # with nginx (http service above), so we want here - # make a single pod containing nginx and php - katenary.io/same-pod: http - # use the ./config/php files as a configMap - katenary.io/configmap-volumes: ./config/php/www.conf - -volumes: - sock: diff --git a/examples/shareenv/chart/README.md b/examples/shareenv/chart/README.md new file mode 100644 index 0000000..db87974 --- /dev/null +++ b/examples/shareenv/chart/README.md @@ -0,0 +1,37 @@ +# shareenv + +A Helm chart for shareenv + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```bash +# Standard Helm install +$ helm install my-release shareenv + +# To use a custom namespace and force the creation of the namespace +$ helm install my-release --namespace my-namespace --create-namespace shareenv + +# To use a custom values file +$ helm install my-release -f my-values.yaml shareenv +``` + +See the [Helm documentation](https://helm.sh/docs/intro/using_helm/) for more information on installing and managing the chart. + +## Configuration + +The following table lists the configurable parameters of the shareenv chart and their default values. + +| Parameter | Default | +| ----------------------- | -------------- | +| `app1.imagePullPolicy` | `IfNotPresent` | +| `app1.replicas` | `1` | +| `app1.repository.image` | `nginx` | +| `app1.repository.tag` | `1` | +| `app2.imagePullPolicy` | `IfNotPresent` | +| `app2.replicas` | `1` | +| `app2.repository.image` | `nginx` | +| `app2.repository.tag` | `1` | + + diff --git a/examples/shareenv/chart/templates/NOTES.txt b/examples/shareenv/chart/templates/NOTES.txt new file mode 100644 index 0000000..3121a00 --- /dev/null +++ b/examples/shareenv/chart/templates/NOTES.txt @@ -0,0 +1,27 @@ +Your release is named {{ .Release.Name }}. + +To learn more about the release, try: + + $ helm -n {{ .Release.Namespace }} status {{ .Release.Name }} + $ helm -n {{ .Release.Namespace }} get all {{ .Release.Name }} + +To delete the release, run: + + $ helm -n {{ .Release.Namespace }} delete {{ .Release.Name }} + +You can see this notes again by running: + + $ helm -n {{ .Release.Namespace }} get notes {{ .Release.Name }} + +{{- $count := 0 -}} +{{- range $s, $v := .Values -}} +{{- if and $v $v.ingress -}} +{{- $count = add $count 1 -}} +{{- if eq $count 1 }} + +The ingress list is: +{{ end }} + - {{ $s }}: http://{{ $v.ingress.host }}{{ $v.ingress.path }} +{{- end -}} +{{ end -}} + diff --git a/examples/shareenv/chart/templates/_helpers.tpl b/examples/shareenv/chart/templates/_helpers.tpl new file mode 100644 index 0000000..e51ea07 --- /dev/null +++ b/examples/shareenv/chart/templates/_helpers.tpl @@ -0,0 +1,36 @@ +{{- define "shareenv.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "shareenv.name" -}} +{{- if .Values.nameOverride -}} +{{- .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} + +{{- define "shareenv.labels" -}} +{{ include "shareenv.selectorLabels" .}} +{{ if .Chart.Version -}} +{{ printf "katenary.v3/chart-version: %s" .Chart.Version }} +{{- end }} +{{ if .Chart.AppVersion -}} +{{ printf "katenary.v3/app-version: %s" .Chart.AppVersion }} +{{- end }} +{{- end -}} + +{{- define "shareenv.selectorLabels" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{ printf "katenary.v3/name: %s" $name }} +{{ printf "katenary.v3/instance: %s" .Release.Name }} +{{- end -}} diff --git a/examples/somevolumes/chart/README.md b/examples/somevolumes/chart/README.md new file mode 100644 index 0000000..edf4198 --- /dev/null +++ b/examples/somevolumes/chart/README.md @@ -0,0 +1,37 @@ +# somevolumes + +A Helm chart for somevolumes + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```bash +# Standard Helm install +$ helm install my-release somevolumes + +# To use a custom namespace and force the creation of the namespace +$ helm install my-release --namespace my-namespace --create-namespace somevolumes + +# To use a custom values file +$ helm install my-release -f my-values.yaml somevolumes +``` + +See the [Helm documentation](https://helm.sh/docs/intro/using_helm/) for more information on installing and managing the chart. + +## Configuration + +The following table lists the configurable parameters of the somevolumes chart and their default values. + +| Parameter | Default | +| ----------------------------------------------- | ----------------- | +| `site1.imagePullPolicy` | `IfNotPresent` | +| `site1.persistence.statics.accessMode[0].value` | `ReadWriteOnce` | +| `site1.persistence.statics.enabled` | `true` | +| `site1.persistence.statics.size` | `1Gi` | +| `site1.persistence.statics.storageClass` | `-` | +| `site1.replicas` | `1` | +| `site1.repository.image` | `docker.io/nginx` | +| `site1.repository.tag` | `1` | + + diff --git a/examples/somevolumes/chart/templates/NOTES.txt b/examples/somevolumes/chart/templates/NOTES.txt new file mode 100644 index 0000000..3121a00 --- /dev/null +++ b/examples/somevolumes/chart/templates/NOTES.txt @@ -0,0 +1,27 @@ +Your release is named {{ .Release.Name }}. + +To learn more about the release, try: + + $ helm -n {{ .Release.Namespace }} status {{ .Release.Name }} + $ helm -n {{ .Release.Namespace }} get all {{ .Release.Name }} + +To delete the release, run: + + $ helm -n {{ .Release.Namespace }} delete {{ .Release.Name }} + +You can see this notes again by running: + + $ helm -n {{ .Release.Namespace }} get notes {{ .Release.Name }} + +{{- $count := 0 -}} +{{- range $s, $v := .Values -}} +{{- if and $v $v.ingress -}} +{{- $count = add $count 1 -}} +{{- if eq $count 1 }} + +The ingress list is: +{{ end }} + - {{ $s }}: http://{{ $v.ingress.host }}{{ $v.ingress.path }} +{{- end -}} +{{ end -}} + diff --git a/examples/somevolumes/chart/templates/_helpers.tpl b/examples/somevolumes/chart/templates/_helpers.tpl new file mode 100644 index 0000000..978ffc8 --- /dev/null +++ b/examples/somevolumes/chart/templates/_helpers.tpl @@ -0,0 +1,36 @@ +{{- define "somevolumes.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "somevolumes.name" -}} +{{- if .Values.nameOverride -}} +{{- .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} + +{{- define "somevolumes.labels" -}} +{{ include "somevolumes.selectorLabels" .}} +{{ if .Chart.Version -}} +{{ printf "katenary.v3/chart-version: %s" .Chart.Version }} +{{- end }} +{{ if .Chart.AppVersion -}} +{{ printf "katenary.v3/app-version: %s" .Chart.AppVersion }} +{{- end }} +{{- end -}} + +{{- define "somevolumes.selectorLabels" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{ printf "katenary.v3/name: %s" $name }} +{{ printf "katenary.v3/instance: %s" .Release.Name }} +{{- end -}} diff --git a/generator/chart.go b/generator/chart.go new file mode 100644 index 0000000..215a0c7 --- /dev/null +++ b/generator/chart.go @@ -0,0 +1,60 @@ +package generator + +// Dependency is a dependency of a chart to other charts. +type Dependency struct { + Name string `yaml:"name"` + Version string `yaml:"version"` + Repository string `yaml:"repository"` + Alias string `yaml:"alias,omitempty"` + Values map[string]any `yaml:"-"` // do not export to Chart.yaml +} + +// ChartTemplate is a template of a chart. It contains the content of the template and the name of the service. +// This is used internally to generate the templates. +// +// TODO: maybe we can set it private. +type ChartTemplate struct { + Content []byte + Servicename string +} + +// HelmChart is a Helm Chart representation. It contains all the +// tempaltes, values, versions, helpers... +type HelmChart struct { + Name string `yaml:"name"` + ApiVersion string `yaml:"apiVersion"` + Version string `yaml:"version"` + AppVersion string `yaml:"appVersion"` + Description string `yaml:"description"` + Dependencies []Dependency `yaml:"dependencies,omitempty"` + Templates map[string]*ChartTemplate `yaml:"-"` // do not export to yaml + Helper string `yaml:"-"` // do not export to yaml + Values map[string]any `yaml:"-"` // do not export to yaml + VolumeMounts map[string]any `yaml:"-"` // do not export to yaml + composeHash *string `yaml:"-"` // do not export to yaml +} + +// NewChart creates a new empty chart with the given name. +func NewChart(name string) *HelmChart { + return &HelmChart{ + Name: name, + Templates: make(map[string]*ChartTemplate, 0), + Description: "A Helm chart for " + name, + ApiVersion: "v2", + Version: "", + AppVersion: "", // set to 0.1.0 by default if no "main-app" label is found + Values: map[string]any{ + "pullSecrets": []string{}, + }, + } +} + +// ConvertOptions are the options to convert a compose project to a helm chart. +type ConvertOptions struct { + Force bool // Force the chart directory deletion if it already exists. + OutputDir string // The output directory of the chart. + Profiles []string // Profile to use for the conversion. + HelmUpdate bool // If true, the "helm dep update" command will be run after the chart generation. + AppVersion *string // Set the chart "appVersion" field. If nil, the version will be set to 0.1.0. + ChartVersion string // Set the chart "version" field. +} diff --git a/generator/configMap.go b/generator/configMap.go new file mode 100644 index 0000000..4ab4625 --- /dev/null +++ b/generator/configMap.go @@ -0,0 +1,224 @@ +package generator + +import ( + "katenary/utils" + "log" + "os" + "path/filepath" + "regexp" + "strings" + + "github.com/compose-spec/compose-go/types" + goyaml "gopkg.in/yaml.v3" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +// only used to check interface implementation +var ( + _ DataMap = (*ConfigMap)(nil) + _ Yaml = (*ConfigMap)(nil) +) + +// NewFileMap creates a new DataMap from a compose service. The appName is the name of the application taken from the project name. +func NewFileMap(service types.ServiceConfig, appName string, kind string) DataMap { + switch kind { + case "configmap": + return NewConfigMap(service, appName) + default: + log.Fatalf("Unknown filemap kind: %s", kind) + } + return nil +} + +// FileMapUsage is the usage of the filemap. +type FileMapUsage uint8 + +// FileMapUsage constants. +const ( + FileMapUsageConfigMap FileMapUsage = iota // pure configmap for key:values. + FileMapUsageFiles // files in a configmap. +) + +// ConfigMap is a kubernetes ConfigMap. +// Implements the DataMap interface. +type ConfigMap struct { + *corev1.ConfigMap + service *types.ServiceConfig + usage FileMapUsage + path string +} + +// NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. +// The ConfigMap is filled by environment variables and labels "map-env". +func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap { + + done := map[string]bool{} + drop := map[string]bool{} + secrets := []string{} + labelValues := []string{} + + cm := &ConfigMap{ + service: &service, + ConfigMap: &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Data: make(map[string]string), + }, + } + + // get the secrets from the labels + if v, ok := service.Labels[LABEL_SECRETS]; ok { + err := yaml.Unmarshal([]byte(v), &secrets) + if err != nil { + log.Fatal(err) + } + // drop the secrets from the environment + for _, secret := range secrets { + drop[secret] = true + } + } + // get the label values from the labels + varDescriptons := utils.GetValuesFromLabel(service, LABEL_VALUES) + for value := range varDescriptons { + labelValues = append(labelValues, value) + } + + // change the environment variables to the values defined in the values.yaml + for _, value := range labelValues { + if _, ok := service.Environment[value]; !ok { + done[value] = true + continue + } + //val := `{{ tpl .Values.` + service.Name + `.environment.` + value + ` $ }}` + val := utils.TplValue(service.Name, "environment."+value) + service.Environment[value] = &val + } + + // remove the variables that are already defined in the environment + if l, ok := service.Labels[LABEL_MAP_ENV]; ok { + envmap := make(map[string]string) + if err := goyaml.Unmarshal([]byte(l), &envmap); err != nil { + log.Fatal("Error parsing map-env", err) + } + for key, value := range envmap { + cm.AddData(key, strings.ReplaceAll(value, "__APP__", appName)) + done[key] = true + } + } + for key, env := range service.Environment { + if _, ok := done[key]; ok { + continue + } + if _, ok := drop[key]; ok { + continue + } + cm.AddData(key, *env) + } + + return cm +} + +// NewConfigMapFromFiles creates a new ConfigMap from a compose service. This path is the path to the +// file or directory. If the path is a directory, all files in the directory are added to the ConfigMap. +// Each subdirectory are ignored. Note that the Generate() function will create the subdirectories ConfigMaps. +func NewConfigMapFromFiles(service types.ServiceConfig, appName string, path string) *ConfigMap { + normalized := path + normalized = strings.TrimLeft(normalized, ".") + normalized = strings.TrimLeft(normalized, "/") + normalized = regexp.MustCompile(`[^a-zA-Z0-9-]+`).ReplaceAllString(normalized, "-") + + cm := &ConfigMap{ + path: path, + service: &service, + usage: FileMapUsageFiles, + ConfigMap: &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName) + "-" + normalized, + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Data: make(map[string]string), + }, + } + // cumulate the path to the WorkingDir + path = filepath.Join(service.WorkingDir, path) + path = filepath.Clean(path) + cm.AppendDir(path) + return cm +} + +// SetData sets the data of the configmap. It replaces the entire data. +func (c *ConfigMap) SetData(data map[string]string) { + c.Data = data +} + +// AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists. +func (c *ConfigMap) AddData(key string, value string) { + c.Data[key] = value +} + +// AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory, +// you need to call this function for each subdirectory. +func (c *ConfigMap) AppendDir(path string) { + // read all files in the path and add them to the configmap + stat, err := os.Stat(path) + if err != nil { + log.Fatalf("Path %s does not exist\n", path) + + } + // recursively read all files in the path and add them to the configmap + if stat.IsDir() { + files, err := os.ReadDir(path) + if err != nil { + log.Fatal(err) + } + for _, file := range files { + if file.IsDir() { + continue + } + path := filepath.Join(path, file.Name()) + content, err := os.ReadFile(path) + if err != nil { + log.Fatal(err) + } + // remove the path from the file + filename := filepath.Base(path) + c.AddData(filename, string(content)) + } + } else { + // add the file to the configmap + content, err := os.ReadFile(path) + if err != nil { + log.Fatal(err) + } + c.AddData(filepath.Base(path), string(content)) + } +} + +// Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path. +func (c *ConfigMap) Filename() string { + switch c.usage { + case FileMapUsageFiles: + return filepath.Join(c.service.Name, "statics", c.path, "configmap.yaml") + default: + return c.service.Name + ".configmap.yaml" + } +} + +// Yaml returns the yaml representation of the configmap +func (c *ConfigMap) Yaml() ([]byte, error) { + return yaml.Marshal(c) +} diff --git a/generator/container.go b/generator/container.go deleted file mode 100644 index ef15b6f..0000000 --- a/generator/container.go +++ /dev/null @@ -1,200 +0,0 @@ -package generator - -import ( - "fmt" - "katenary/helm" - "katenary/logger" - "log" - "os" - "strconv" - "strings" - - "github.com/compose-spec/compose-go/types" -) - -// Generate a container in deployment with all needed objects (volumes, secrets, env, ...). -// The deployName shoud be the name of the deployment, we cannot get it from Metadata as this is a variable name. -func newContainerForDeployment( - deployName, containerName string, - deployment *helm.Deployment, - s *types.ServiceConfig, - fileGeneratorChan HelmFileGenerator) *helm.Container { - - buildCrontab(deployName, deployment, s, fileGeneratorChan) - - container := helm.NewContainer(containerName, s.Image, s.Environment, s.Labels) - - applyEnvMapLabel(s, container) - if secretFile := setSecretVar(containerName, s, container); secretFile != nil { - fileGeneratorChan <- secretFile - container.EnvFrom = append(container.EnvFrom, map[string]map[string]string{ - "secretRef": { - "name": secretFile.Metadata().Name, - }, - }) - } - setEnvToValues(containerName, s, container) - prepareContainer(container, s, containerName) - prepareEnvFromFiles(deployName, s, container, fileGeneratorChan) - - // add the container in deployment - if deployment.Spec.Template.Spec.Containers == nil { - deployment.Spec.Template.Spec.Containers = make([]*helm.Container, 0) - } - deployment.Spec.Template.Spec.Containers = append( - deployment.Spec.Template.Spec.Containers, - container, - ) - - // add the volumes - if deployment.Spec.Template.Spec.Volumes == nil { - deployment.Spec.Template.Spec.Volumes = make([]map[string]interface{}, 0) - } - // manage LABEL_VOLUMEFROM - addVolumeFrom(deployment, container, s) - // and then we can add other volumes - deployment.Spec.Template.Spec.Volumes = append( - deployment.Spec.Template.Spec.Volumes, - prepareVolumes(deployName, containerName, s, container, fileGeneratorChan)..., - ) - - // add init containers - if deployment.Spec.Template.Spec.InitContainers == nil { - deployment.Spec.Template.Spec.InitContainers = make([]*helm.Container, 0) - } - deployment.Spec.Template.Spec.InitContainers = append( - deployment.Spec.Template.Spec.InitContainers, - prepareInitContainers(containerName, s, container)..., - ) - - // check if there is containerPort assigned in label, add it, and do - // not create service for this. - if ports, ok := s.Labels[helm.LABEL_CONTAINER_PORT]; ok { - for _, port := range strings.Split(ports, ",") { - func(port string, container *helm.Container, s *types.ServiceConfig) { - port = strings.TrimSpace(port) - if port == "" { - return - } - portNumber, err := strconv.Atoi(port) - if err != nil { - return - } - // avoid already declared ports - for _, p := range s.Ports { - if int(p.Target) == portNumber { - return - } - } - container.Ports = append(container.Ports, &helm.ContainerPort{ - Name: deployName + "-" + port, - ContainerPort: portNumber, - }) - }(port, container, s) - } - } - - return container -} - -// prepareContainer assigns image, command, env, and labels to a container. -func prepareContainer(container *helm.Container, service *types.ServiceConfig, servicename string) { - // if there is no image name, this should fail! - if service.Image == "" { - log.Fatal(ICON_PACKAGE+" No image name for service ", servicename) - } - - // Get the image tag - imageParts := strings.Split(service.Image, ":") - tag := "" - if len(imageParts) == 2 { - container.Image = imageParts[0] - tag = imageParts[1] - } - - vtag := ".Values." + servicename + ".repository.tag" - container.Image = `{{ .Values.` + servicename + `.repository.image }}` + - `{{ if ne ` + vtag + ` "" }}:{{ ` + vtag + ` }}{{ end }}` - container.Command = service.Command - AddValues(servicename, map[string]EnvVal{ - "repository": map[string]EnvVal{ - "image": imageParts[0], - "tag": tag, - }, - }) - prepareProbes(servicename, service, container) - generateContainerPorts(service, servicename, container) -} - -// generateContainerPorts add the container ports of a service. -func generateContainerPorts(s *types.ServiceConfig, name string, container *helm.Container) { - - exists := make(map[int]string) - for _, port := range s.Ports { - portName := name - for _, n := range exists { - if name == n { - portName = fmt.Sprintf("%s-%d", name, port.Target) - } - } - container.Ports = append(container.Ports, &helm.ContainerPort{ - Name: portName, - ContainerPort: int(port.Target), - }) - exists[int(port.Target)] = name - } - - // manage the "expose" section to be a NodePort in Kubernetes - for _, expose := range s.Expose { - - port, _ := strconv.Atoi(expose) - - if _, exist := exists[port]; exist { - continue - } - container.Ports = append(container.Ports, &helm.ContainerPort{ - Name: name, - ContainerPort: port, - }) - } -} - -// prepareInitContainers add the init containers of a service. -func prepareInitContainers(name string, s *types.ServiceConfig, container *helm.Container) []*helm.Container { - - // We need to detect others services, but we probably not have parsed them yet, so - // we will wait for them for a while. - initContainers := make([]*helm.Container, 0) - for dp := range s.DependsOn { - c := helm.NewContainer("check-"+dp, "busybox", nil, s.Labels) - command := strings.ReplaceAll(strings.TrimSpace(dependScript), "__service__", dp) - - foundPort := -1 - locker.Lock() - if defaultPort, ok := servicesMap[dp]; !ok { - logger.Redf("Error while getting port for service %s\n", dp) - os.Exit(1) - } else { - foundPort = defaultPort - } - locker.Unlock() - if foundPort == -1 { - log.Fatalf( - "ERROR, the %s service is waiting for %s port number, "+ - "but it is never discovered. You must declare at least one port in "+ - "the \"ports\" section of the service in the docker-compose file", - name, - dp, - ) - } - command = strings.ReplaceAll(command, "__port__", strconv.Itoa(foundPort)) - - c.Command = []string{ - "sh", - "-c", - command, - } - initContainers = append(initContainers, c) - } - return initContainers -} diff --git a/generator/converter.go b/generator/converter.go new file mode 100644 index 0000000..9af18ff --- /dev/null +++ b/generator/converter.go @@ -0,0 +1,638 @@ +package generator + +import ( + "bytes" + "errors" + "fmt" + "katenary/generator/extrafiles" + "katenary/parser" + "katenary/utils" + "log" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/compose-spec/compose-go/types" + goyaml "gopkg.in/yaml.v3" +) + +const headerHelp = `# This file is autogenerated by katenary +# +# DO NOT EDIT IT BY HAND UNLESS YOU KNOW WHAT YOU ARE DOING +# +# If you want to change the content of this file, you should edit the +# compose file and run katenary again. +# If you need to override some values, you can do it in a override file +# and use the -f flag to specify it when running the helm command. + + +` + +// Convert a compose (docker, podman...) project to a helm chart. +// It calls Generate() to generate the chart and then write it to the disk. +func Convert(config ConvertOptions, dockerComposeFile ...string) { + + var ( + templateDir = filepath.Join(config.OutputDir, "templates") + helpersPath = filepath.Join(config.OutputDir, "templates", "_helpers.tpl") + chartPath = filepath.Join(config.OutputDir, "Chart.yaml") + valuesPath = filepath.Join(config.OutputDir, "values.yaml") + readmePath = filepath.Join(config.OutputDir, "README.md") + notesPath = filepath.Join(templateDir, "NOTES.txt") + ) + + // the current working directory is the directory + currentDir, _ := os.Getwd() + // go to the root of the project + if err := os.Chdir(filepath.Dir(dockerComposeFile[0])); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + defer os.Chdir(currentDir) // after the generation, go back to the original directory + + // repove the directory part of the docker-compose files + for i, f := range dockerComposeFile { + dockerComposeFile[i] = filepath.Base(f) + } + + // parse the compose files + project, err := parser.Parse(config.Profiles, dockerComposeFile...) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // check older version of labels + if err := checkOldLabels(project); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + + if !config.Force { + // check if the chart directory exists + // if yes, prevent the user from overwriting it and ask for confirmation + if _, err := os.Stat(config.OutputDir); err == nil { + fmt.Print(utils.IconWarning, " The chart directory "+config.OutputDir+" already exists, do you want to overwrite it? [y/N] ") + var answer string + fmt.Scanln(&answer) + if strings.ToLower(answer) != "y" { + fmt.Println("Aborting") + os.Exit(126) // 126 is the exit code for "Command invoked cannot execute" + } + } + fmt.Println() // clean line + } + + // Build the objects ! + chart, err := Generate(project) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // if the app version is set from the command line, use it + if config.AppVersion != nil { + chart.AppVersion = *config.AppVersion + } + chart.Version = config.ChartVersion + + // remove the chart directory if it exists + os.RemoveAll(config.OutputDir) + + // create the chart directory + if err := os.MkdirAll(templateDir, 0755); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + + for name, template := range chart.Templates { + t := template.Content + t = removeNewlinesInsideBrackets(t) + t = removeUnwantedLines(t) + t = addModeline(t) + + kind := utils.GetKind(name) + var icon utils.Icon + switch kind { + case "deployment": + icon = utils.IconPackage + case "service": + icon = utils.IconPlug + case "ingress": + icon = utils.IconWorld + case "volumeclaim": + icon = utils.IconCabinet + case "configmap": + icon = utils.IconConfig + case "secret": + icon = utils.IconSecret + default: + icon = utils.IconInfo + } + + servicename := template.Servicename + if err := os.MkdirAll(filepath.Join(templateDir, servicename), 0755); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + fmt.Println(icon, "Creating", kind, servicename) + // if the name is a path, create the directory + if strings.Contains(name, string(filepath.Separator)) { + name = filepath.Join(templateDir, name) + err := os.MkdirAll(filepath.Dir(name), 0755) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + } else { + // remove the serivce name from the template name + name = strings.Replace(name, servicename+".", "", 1) + name = filepath.Join(templateDir, servicename, name) + } + f, err := os.Create(name) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + + f.Write(t) + f.Close() + } + + // calculate the sha1 hash of the services + + buf := bytes.NewBuffer(nil) + encoder := goyaml.NewEncoder(buf) + encoder.SetIndent(2) + if err := encoder.Encode(chart); err != nil { + fmt.Println(err) + os.Exit(1) + } + + yamlChart := buf.Bytes() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + // concat chart adding a comment with hash of services on top + yamlChart = append([]byte(fmt.Sprintf("# compose hash (sha1): %s\n", *chart.composeHash)), yamlChart...) + // add the list of compose files + files := []string{} + for _, file := range project.ComposeFiles { + base := filepath.Base(file) + files = append(files, base) + } + yamlChart = append([]byte(fmt.Sprintf("# compose files: %s\n", strings.Join(files, ", "))), yamlChart...) + // add generated date + yamlChart = append([]byte(fmt.Sprintf("# generated at: %s\n", time.Now().Format(time.RFC3339))), yamlChart...) + + // document Chart.yaml file + yamlChart = addChartDoc(yamlChart, project) + + f, err := os.Create(chartPath) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + f.Write(yamlChart) + f.Close() + + buf.Reset() + encoder = goyaml.NewEncoder(buf) + encoder.SetIndent(2) + if err = encoder.Encode(&chart.Values); err != nil { + fmt.Println(err) + os.Exit(1) + } + values := buf.Bytes() + values = addDescriptions(values, *project) + values = addDependencyDescription(values, chart.Dependencies) + values = addCommentsToValues(values) + values = addStorageClassHelp(values) + values = addImagePullSecretsHelp(values) + values = addImagePullPolicyHelp(values) + values = addVariablesDoc(values, project) + values = addMainTagAppDoc(values, project) + values = append([]byte(headerHelp), values...) + + f, err = os.Create(valuesPath) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + f.Write(values) + f.Close() + + f, err = os.Create(helpersPath) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + f.Write([]byte(chart.Helper)) + f.Close() + + readme := extrafiles.ReadMeFile(chart.Name, chart.Description, chart.Values) + f, err = os.Create(readmePath) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + f.Write([]byte(readme)) + f.Close() + + notes := extrafiles.NotesFile() + f, err = os.Create(notesPath) + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + f.Write([]byte(notes)) + f.Close() + + if config.HelmUpdate { + if err := helmUpdate(config); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } else if err := helmLint(config); err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } else { + fmt.Println(utils.IconSuccess, "Helm chart created successfully") + } + } +} + +const ingressClassHelp = `# Default value for ingress.class annotation +# class: "-" +# If the value is "-", controller will not set ingressClassName +# If the value is "", Ingress will be set to an empty string, so +# controller will use the default value for ingressClass +# If the value is specified, controller will set the named class e.g. "nginx" +# More info: https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource +` + +func addCommentsToValues(values []byte) []byte { + lines := strings.Split(string(values), "\n") + for i, line := range lines { + if strings.Contains(line, "ingress:") { + spaces := utils.CountStartingSpaces(line) + spacesString := strings.Repeat(" ", spaces) + // indent ingressClassHelper comment + ingressClassHelp := strings.ReplaceAll(ingressClassHelp, "\n", "\n"+spacesString) + ingressClassHelp = strings.TrimRight(ingressClassHelp, " ") + ingressClassHelp = spacesString + ingressClassHelp + lines[i] = ingressClassHelp + line + } + } + return []byte(strings.Join(lines, "\n")) +} + +const storageClassHelp = `# Storage class to use for PVCs +# storageClass: "-" means use default +# storageClass: "" means do not specify +# storageClass: "foo" means use that storageClass +# More info: https://kubernetes.io/docs/concepts/storage/storage-classes/ +` + +// addStorageClassHelp adds a comment to the values.yaml file to explain how to +// use the storageClass option. +func addStorageClassHelp(values []byte) []byte { + lines := strings.Split(string(values), "\n") + for i, line := range lines { + if strings.Contains(line, "storageClass:") { + spaces := utils.CountStartingSpaces(line) + spacesString := strings.Repeat(" ", spaces) + // indent ingressClassHelper comment + storageClassHelp := strings.ReplaceAll(storageClassHelp, "\n", "\n"+spacesString) + storageClassHelp = strings.TrimRight(storageClassHelp, " ") + storageClassHelp = spacesString + storageClassHelp + lines[i] = storageClassHelp + line + } + } + return []byte(strings.Join(lines, "\n")) +} + +// addModeline adds a modeline to the values.yaml file to make sure that vim +// will use the correct syntax highlighting. +func addModeline(values []byte) []byte { + modeline := "# vi" + "m: ft=gotmpl.yaml" + + // if the values ends by `{{- end }}` we need to add the modeline before + lines := strings.Split(string(values), "\n") + + if lines[len(lines)-1] == "{{- end }}" || lines[len(lines)-1] == "{{- end -}}" { + lines = lines[:len(lines)-1] + lines = append(lines, modeline, "{{- end }}") + return []byte(strings.Join(lines, "\n")) + } + + return append(values, []byte(modeline)...) +} + +// addDescriptions adds the description from the label to the values.yaml file on top +// of the service definition. +func addDescriptions(values []byte, project types.Project) []byte { + for _, service := range project.Services { + if description, ok := service.Labels[LABEL_DESCRIPTION]; ok { + // set it as comment + description = "\n# " + strings.ReplaceAll(description, "\n", "\n# ") + + values = regexp.MustCompile( + `(?m)^`+service.Name+`:$`, + ).ReplaceAll(values, []byte(description+"\n"+service.Name+":")) + } else { + // set it as comment + description = "\n# " + service.Name + " configuration" + + values = regexp.MustCompile( + `(?m)^`+service.Name+`:$`, + ).ReplaceAll( + values, + []byte(description+"\n"+service.Name+":"), + ) + } + } + return values +} + +func addDependencyDescription(values []byte, dependencies []Dependency) []byte { + for _, d := range dependencies { + name := d.Name + if d.Alias != "" { + name = d.Alias + } + + values = regexp.MustCompile( + `(?m)^`+name+`:$`, + ).ReplaceAll( + values, + []byte("\n# "+d.Name+" helm dependency configuration\n"+name+":"), + ) + } + return values +} + +const imagePullSecretHelp = ` +# imagePullSecrets allows you to specify a name of an image pull secret. +# You must provide a list of object with the name field set to the name of the +# e.g. +# pullSecrets: +# - name: regcred +# You are, for now, repsonsible for creating the secret. +# More info: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ +` + +func addImagePullSecretsHelp(values []byte) []byte { + // add imagePullSecrets help + lines := strings.Split(string(values), "\n") + for i, line := range lines { + if strings.Contains(line, "pullSecrets:") { + spaces := utils.CountStartingSpaces(line) + spacesString := strings.Repeat(" ", spaces) + // indent imagePullSecretHelp comment + imagePullSecretHelp := strings.ReplaceAll(imagePullSecretHelp, "\n", "\n"+spacesString) + imagePullSecretHelp = strings.TrimRight(imagePullSecretHelp, " ") + imagePullSecretHelp = spacesString + imagePullSecretHelp + lines[i] = imagePullSecretHelp + line + } + } + return []byte(strings.Join(lines, "\n")) +} + +func addChartDoc(values []byte, project *types.Project) []byte { + chartDoc := fmt.Sprintf(`# This is the main values.yaml file for the %s chart. +# More information can be found in the chart's README.md file. +# +`, project.Name) + + lines := strings.Split(string(values), "\n") + for i, line := range lines { + if regexp.MustCompile(`(?m)^name:`).MatchString(line) { + doc := fmt.Sprintf("\n# Name of the chart (required), basically the name of the project.\n") + lines[i] = doc + line + } else if regexp.MustCompile(`(?m)^version:`).MatchString(line) { + doc := fmt.Sprintf("\n# Version of the chart (required)\n") + lines[i] = doc + line + } else if strings.Contains(line, "appVersion:") { + spaces := utils.CountStartingSpaces(line) + doc := fmt.Sprintf( + "\n%s# Version of the application (required).\n%s# This should be the main application version.\n", + strings.Repeat(" ", spaces), + strings.Repeat(" ", spaces), + ) + lines[i] = doc + line + } else if strings.Contains(line, "dependencies:") { + spaces := utils.CountStartingSpaces(line) + doc := fmt.Sprintf("\n"+ + "%s# Dependencies are external charts that this chart will depend on.\n"+ + "%s# More information can be found in the chart's README.md file.\n", + strings.Repeat(" ", spaces), + strings.Repeat(" ", spaces), + ) + lines[i] = doc + line + } + } + return []byte(chartDoc + strings.Join(lines, "\n")) + +} + +const imagePullPolicyHelp = `# imagePullPolicy allows you to specify a policy to cache or always pull an image. +# You must provide a string value with one of the following values: +# - Always -> will always pull the image +# - Never -> will never pull the image, the image should be present on the node +# - IfNotPresent -> will pull the image only if it is not present on the node +# More info: https://kubernetes.io/docs/concepts/containers/images/#updating-images +` + +func addImagePullPolicyHelp(values []byte) []byte { + // add imagePullPolicy help + lines := strings.Split(string(values), "\n") + for i, line := range lines { + if strings.Contains(line, "imagePullPolicy:") { + spaces := utils.CountStartingSpaces(line) + spacesString := strings.Repeat(" ", spaces) + // indent imagePullPolicyHelp comment + imagePullPolicyHelp := strings.ReplaceAll(imagePullPolicyHelp, "\n", "\n"+spacesString) + imagePullPolicyHelp = strings.TrimRight(imagePullPolicyHelp, " ") + imagePullPolicyHelp = spacesString + imagePullPolicyHelp + lines[i] = imagePullPolicyHelp + line + } + } + return []byte(strings.Join(lines, "\n")) +} + +func addVariablesDoc(values []byte, project *types.Project) []byte { + + lines := strings.Split(string(values), "\n") + + currentService := "" + for _, service := range project.Services { + variables := utils.GetValuesFromLabel(service, LABEL_VALUES) + for i, line := range lines { + if regexp.MustCompile(`(?m)^` + service.Name + `:`).MatchString(line) { + currentService = service.Name + } + for varname, variable := range variables { + if variable == nil { + continue + } + spaces := utils.CountStartingSpaces(line) + if regexp.MustCompile(`(?m)\s*`+varname+`:`).MatchString(line) && currentService == service.Name { + + // add # to the beginning of the Description + doc := strings.ReplaceAll("\n"+variable.Description, "\n", "\n"+strings.Repeat(" ", spaces)+"# ") + doc = strings.TrimRight(doc, " ") + doc += "\n" + line + + lines[i] = doc + } + } + } + } + return []byte(strings.Join(lines, "\n")) +} + +const mainTagAppDoc = `This is the version of the main application. +Leave it to blank to use the Chart "AppVersion" value.` + +func addMainTagAppDoc(values []byte, project *types.Project) []byte { + lines := strings.Split(string(values), "\n") + + for _, service := range project.Services { + inService := false + inRegistry := false + // read the label LabelMainApp + if v, ok := service.Labels[LABEL_MAIN_APP]; !ok { + continue + } else if v == "false" || v == "no" || v == "0" { + continue + } else { + fmt.Printf("%s Adding main tag app doc %s\n", utils.IconConfig, service.Name) + } + + for i, line := range lines { + if regexp.MustCompile(`^` + service.Name + `:`).MatchString(line) { + inService = true + } + if inService && regexp.MustCompile(`^\s*repository:.*`).MatchString(line) { + inRegistry = true + } + if inService && inRegistry { + if regexp.MustCompile(`^\s*tag: .*`).MatchString(line) { + spaces := utils.CountStartingSpaces(line) + doc := strings.ReplaceAll(mainTagAppDoc, "\n", "\n"+strings.Repeat(" ", spaces)+"# ") + doc = strings.Repeat(" ", spaces) + "# " + doc + + lines[i] = doc + "\n" + line + "\n" + break + } + } + } + } + + return []byte(strings.Join(lines, "\n")) +} + +func removeNewlinesInsideBrackets(values []byte) []byte { + re, err := regexp.Compile(`(?s)\{\{(.*?)\}\}`) + if err != nil { + log.Fatal(err) + } + return re.ReplaceAllFunc(values, func(b []byte) []byte { + // get the first match + matches := re.FindSubmatch(b) + replacement := bytes.ReplaceAll(matches[1], []byte("\n"), []byte(" ")) + // remove repeated spaces + replacement = regexp.MustCompile(`\s+`).ReplaceAll(replacement, []byte(" ")) + // remove newlines inside brackets + return bytes.ReplaceAll(b, matches[1], replacement) + + }) +} + +var unwantedLines = []string{ + "creationTimestamp:", + "status:", +} + +func removeUnwantedLines(values []byte) []byte { + lines := strings.Split(string(values), "\n") + output := []string{} + for _, line := range lines { + next := false + for _, unwanted := range unwantedLines { + if strings.Contains(line, unwanted) { + next = true + } + } + if !next { + output = append(output, line) + } + } + return []byte(strings.Join(output, "\n")) +} + +// check if the project makes use of older labels (kanetary.[^v3]) +func checkOldLabels(project *types.Project) error { + badServices := make([]string, 0) + for _, service := range project.Services { + for label := range service.Labels { + if strings.Contains(label, "katenary.") && !strings.Contains(label, KATENARY_PREFIX) { + badServices = append(badServices, fmt.Sprintf("- %s: %s", service.Name, label)) + } + } + } + if len(badServices) > 0 { + message := fmt.Sprintf(` Old labels detected in project "%s". + + The current version of katenary uses labels with the prefix "%s" which are not compatible with previous versions. + Your project is not compatible with this version. + + Please upgrade your labels to follow the current version + + Services to upgrade: +%s`, + project.Name, + KATENARY_PREFIX[0:len(KATENARY_PREFIX)-1], + strings.Join(badServices, "\n"), + ) + + return errors.New(utils.WordWrap(message, 80)) + + } + return nil +} + +func helmUpdate(config ConvertOptions) error { + + // lookup for "helm" binary + fmt.Println(utils.IconInfo, "Updating helm dependencies...") + helm, err := exec.LookPath("helm") + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + // run "helm dependency update" + cmd := exec.Command(helm, "dependency", "update", config.OutputDir) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func helmLint(config ConvertOptions) error { + + fmt.Println(utils.IconInfo, "Linting...") + helm, err := exec.LookPath("helm") + if err != nil { + fmt.Println(utils.IconFailure, err) + os.Exit(1) + } + cmd := exec.Command(helm, "lint", config.OutputDir) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() + +} diff --git a/generator/cronJob.go b/generator/cronJob.go new file mode 100644 index 0000000..91e409c --- /dev/null +++ b/generator/cronJob.go @@ -0,0 +1,133 @@ +package generator + +import ( + "katenary/utils" + "log" + "strings" + + "github.com/compose-spec/compose-go/types" + goyaml "gopkg.in/yaml.v3" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +// only used to check interface implementation +var ( + _ Yaml = (*CronJob)(nil) +) + +// CronJob is a kubernetes CronJob. +type CronJob struct { + *batchv1.CronJob + service *types.ServiceConfig +} + +// NewCronJob creates a new CronJob from a compose service. The appName is the name of the application taken from the project name. +func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (*CronJob, *RBAC) { + var labels, ok = service.Labels[LABEL_CRONJOB] + if !ok { + return nil, nil + } + mapping := struct { + Image string `yaml:"image,omitempty"` + Command string `yaml:"command"` + Schedule string `yaml:"schedule"` + Rbac bool `yaml:"rbac"` + }{ + Image: "", + Command: "", + Schedule: "", + Rbac: false, + } + if err := goyaml.Unmarshal([]byte(labels), &mapping); err != nil { + log.Fatalf("Error parsing cronjob labels: %s", err) + return nil, nil + } + + if _, ok := chart.Values[service.Name]; !ok { + chart.Values[service.Name] = NewValue(service, false) + } + if chart.Values[service.Name].(*Value).CronJob == nil { + chart.Values[service.Name].(*Value).CronJob = &CronJobValue{} + } + chart.Values[service.Name].(*Value).CronJob.Schedule = mapping.Schedule + chart.Values[service.Name].(*Value).CronJob.ImagePullPolicy = "IfNotPresent" + chart.Values[service.Name].(*Value).CronJob.Environment = map[string]any{} + + image, tag := mapping.Image, "" + if image == "" { // if image is not set, use the image from the service + image = service.Image + } + + if strings.Contains(image, ":") { + image = strings.Split(service.Image, ":")[0] + tag = strings.Split(service.Image, ":")[1] + } + + chart.Values[service.Name].(*Value).CronJob.Repository = &RepositoryValue{ + Image: image, + Tag: tag, + } + + cronjob := &CronJob{ + CronJob: &batchv1.CronJob{ + TypeMeta: metav1.TypeMeta{ + Kind: "CronJob", + APIVersion: "batch/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Spec: batchv1.CronJobSpec{ + Schedule: "{{ .Values." + service.Name + ".cronjob.schedule }}", + JobTemplate: batchv1.JobTemplateSpec{ + Spec: batchv1.JobSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "cronjob", + Image: "{{ .Values." + service.Name + ".cronjob.repository.image }}:{{ default .Values." + service.Name + ".cronjob.repository.tag \"latest\" }}", + Command: []string{ + "sh", + "-c", + mapping.Command, + }, + }, + }, + }, + }, + }, + }, + }, + }, + service: &service, + } + + var rbac *RBAC + if mapping.Rbac { + rbac = NewRBAC(service, appName) + // add the service account to the cronjob + cronjob.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName = utils.TplName(service.Name, appName) + } + + return cronjob, rbac +} + +// Filename returns the filename of the cronjob. +// +// Implements the Yaml interface. +func (c *CronJob) Filename() string { + return c.service.Name + ".cronjob.yaml" +} + +// Yaml returns the yaml representation of the cronjob. +// +// Implements the Yaml interface. +func (c *CronJob) Yaml() ([]byte, error) { + return yaml.Marshal(c) +} diff --git a/generator/crontabs.go b/generator/crontabs.go deleted file mode 100644 index efacb79..0000000 --- a/generator/crontabs.go +++ /dev/null @@ -1,110 +0,0 @@ -package generator - -import ( - "fmt" - "katenary/helm" - "katenary/logger" - "log" - - "github.com/alessio/shellescape" - "github.com/compose-spec/compose-go/types" - "gopkg.in/yaml.v3" -) - -const ( - cronMulti = `pods=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[*].metadata.name}')` - cronMultiCmd = ` -for pod in $pods; do - kubectl exec -i $pod -c %s -- sh -c %s -done` - cronSingle = `pod=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[0].metadata.name}')` - cronCmd = ` -kubectl exec -i $pod -c %s -- sh -c %s` -) - -type CronDef struct { - Command string `yaml:"command"` - Schedule string `yaml:"schedule"` - Image string `yaml:"image"` - Multi bool `yaml:"allPods,omitempty"` -} - -func buildCrontab(deployName string, deployment *helm.Deployment, s *types.ServiceConfig, fileGeneratorChan HelmFileGenerator) { - // get the cron label from the service - var crondef string - var ok bool - if crondef, ok = s.Labels[helm.LABEL_CRON]; !ok { - return - } - - // parse yaml - crons := []CronDef{} - err := yaml.Unmarshal([]byte(crondef), &crons) - if err != nil { - log.Fatalf("error: %v", err) - } - - if len(crons) == 0 { - return - } - - // create a serviceAccount - sa := helm.NewServiceAccount(deployName) - // create a role - role := helm.NewCronRole(deployName) - - // create a roleBinding - roleBinding := helm.NewRoleBinding(deployName, sa, role) - - // make generation - logger.Magenta(ICON_RBAC, "Generating ServiceAccount, Role and RoleBinding for cron jobs", deployName) - fileGeneratorChan <- sa - fileGeneratorChan <- role - fileGeneratorChan <- roleBinding - - numcron := len(crons) - 1 - index := 1 - - // create crontabs - for _, cron := range crons { - escaped := shellescape.Quote(cron.Command) - var cmd, podget string - if cron.Multi { - podget = cronMulti - cmd = cronMultiCmd - } else { - podget = cronSingle - cmd = cronCmd - } - podget = fmt.Sprintf(podget, helm.K, deployName, helm.K) - cmd = fmt.Sprintf(cmd, s.Name, escaped) - cmd = podget + cmd - - if cron.Image == "" { - cron.Image = `bitnami/kubectl:{{ printf "%s.%s" .Capabilities.KubeVersion.Major .Capabilities.KubeVersion.Minor }}` - } - - name := deployName - if numcron > 0 { - name = fmt.Sprintf("%s-%d", deployName, index) - } - - // add crontab - suffix := "" - if numcron > 0 { - suffix = fmt.Sprintf("%d", index) - } - cronTab := helm.NewCrontab( - name, - cron.Image, - cmd, - cron.Schedule, - sa, - ) - logger.Magenta(ICON_CRON, "Generating crontab", deployName, suffix) - fileGeneratorChan <- cronTab - index++ - } - - return -} diff --git a/generator/deployment.go b/generator/deployment.go index 0cbb727..627bfd3 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -1,70 +1,569 @@ package generator import ( - "katenary/helm" - "katenary/logger" + "fmt" + "katenary/utils" + "log" + "os" + "path/filepath" + "regexp" + "strings" + "time" "github.com/compose-spec/compose-go/types" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" ) -// This function will try to yied deployment and services based on a service from the compose file structure. -func buildDeployment(name string, s *types.ServiceConfig, linked map[string]types.ServiceConfig, fileGeneratorChan HelmFileGenerator) { +var _ Yaml = (*Deployment)(nil) - logger.Magenta(ICON_PACKAGE+" Generating deployment for ", name) - deployment := helm.NewDeployment(name) +// Deployment is a kubernetes Deployment. +type Deployment struct { + *appsv1.Deployment `yaml:",inline"` + chart *HelmChart `yaml:"-"` + configMaps map[string]bool `yaml:"-"` + service *types.ServiceConfig `yaml:"-"` + defaultTag string `yaml:"-"` + isMainApp bool `yaml:"-"` +} - newContainerForDeployment(name, name, deployment, s, fileGeneratorChan) +// NewDeployment creates a new Deployment from a compose service. The appName is the name of the application taken from the project name. +// It also creates the Values map that will be used to create the values.yaml file. +func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment { - // Add selectors - selectors := buildSelector(name, s) - selectors[helm.K+"/resource"] = "deployment" - deployment.Spec.Selector = map[string]interface{}{ - "matchLabels": selectors, + ports := []corev1.ContainerPort{} + for _, port := range service.Ports { + ports = append(ports, corev1.ContainerPort{ + ContainerPort: int32(port.Target), + }) } - deployment.Spec.Template.Metadata.Labels = selectors - // Now, the linked services (same pod) - for lname, link := range linked { - newContainerForDeployment(name, lname, deployment, &link, fileGeneratorChan) - // append ports and expose ports to the deployment, - // to be able to generate them in the Service file - if len(link.Ports) > 0 || len(link.Expose) > 0 { - s.Ports = append(s.Ports, link.Ports...) - s.Expose = append(s.Expose, link.Expose...) + isMainApp := false + if mainLabel, ok := service.Labels[LABEL_MAIN_APP]; ok { + main := strings.ToLower(mainLabel) + isMainApp = main == "true" || main == "yes" || main == "1" + } + + defaultTag := `default "latest"` + if isMainApp { + defaultTag = `default .Chart.AppVersion "latest"` + } + + chart.Values[service.Name] = NewValue(service, isMainApp) + appName := chart.Name + + dep := &Deployment{ + isMainApp: isMainApp, + defaultTag: defaultTag, + service: &service, + chart: chart, + Deployment: &appsv1.Deployment{ + TypeMeta: metav1.TypeMeta{ + Kind: "Deployment", + APIVersion: "apps/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Spec: appsv1.DeploymentSpec{ + Replicas: utils.Int32Ptr(1), + Selector: &metav1.LabelSelector{ + MatchLabels: GetMatchLabels(service.Name, appName), + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: GetMatchLabels(service.Name, appName), + }, + }, + }, + }, + configMaps: map[string]bool{}, + } + + // add containers + dep.AddContainer(service) + + // add volumes + dep.AddVolumes(service, appName) + + if service.Environment != nil { + dep.SetEnvFrom(service, appName) + } + + return dep +} + +// DependsOn adds a initContainer to the deployment that will wait for the service to be up. +func (d *Deployment) DependsOn(to *Deployment) error { + // Add a initContainer with busybox:latest using netcat to check if the service is up + // it will wait until the service responds to all ports + for _, container := range to.Spec.Template.Spec.Containers { + commands := []string{} + for _, port := range container.Ports { + command := fmt.Sprintf("until nc -z %s %d; do\n sleep 1;\ndone", to.Name, port.ContainerPort) + commands = append(commands, command) + } + + command := []string{"/bin/sh", "-c", strings.Join(commands, "\n")} + d.Spec.Template.Spec.InitContainers = append(d.Spec.Template.Spec.InitContainers, corev1.Container{ + Name: "wait-for-" + to.service.Name, + Image: "busybox:latest", + Command: command, + }) + } + + return nil +} + +// AddContainer adds a container to the deployment. +func (d *Deployment) AddContainer(service types.ServiceConfig) { + ports := []corev1.ContainerPort{} + + for _, port := range service.Ports { + name := utils.GetServiceNameByPort(int(port.Target)) + if name == "" { + utils.Warn("Port name not found for port ", port.Target, " in service ", service.Name, ". Using port number instead") + } + ports = append(ports, corev1.ContainerPort{ + ContainerPort: int32(port.Target), + Name: name, + }) + } + + container := corev1.Container{ + Image: utils.TplValue(service.Name, "repository.image") + ":" + + utils.TplValue(service.Name, "repository.tag", d.defaultTag), + Ports: ports, + Name: service.Name, + ImagePullPolicy: corev1.PullIfNotPresent, + } + if _, ok := d.chart.Values[service.Name]; !ok { + d.chart.Values[service.Name] = NewValue(service, d.isMainApp) + } + d.chart.Values[service.Name].(*Value).ImagePullPolicy = string(corev1.PullIfNotPresent) + + // add an imagePullSecret, it actually does not work because the secret is not + // created but it add the reference in the YAML file. We'll change it in Yaml() + // method. + d.Spec.Template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{{ + Name: `{{ .Values.pullSecrets | toYaml | indent __indent__ }}`, + }} + + d.AddHealthCheck(service, &container) + + d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, container) +} + +// AddIngress adds an ingress to the deployment. It creates the ingress object. +func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *Ingress { + return NewIngress(service, d.chart) +} + +// AddVolumes adds a volume to the deployment. It does not create the PVC, it only adds the volumes to the deployment. +// If the volume is a bind volume it will warn the user that it is not supported yet. +func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) { + + tobind := map[string]bool{} + if v, ok := service.Labels[LABEL_CM_FILES]; ok { + binds := []string{} + if err := yaml.Unmarshal([]byte(v), &binds); err != nil { + log.Fatal(err) + } + for _, bind := range binds { + tobind[bind] = true } } - // Remove duplicates in volumes - volumes := make([]map[string]interface{}, 0) - done := make(map[string]bool) - for _, vol := range deployment.Spec.Template.Spec.Volumes { - name := vol["name"].(string) - if _, ok := done[name]; ok { + isSamePod := false + if v, ok := service.Labels[LABEL_SAME_POD]; !ok { + isSamePod = false + } else { + isSamePod = v != "" + } + + for _, volume := range service.Volumes { + // not declared as a bind volume, skip + if _, ok := tobind[volume.Source]; !isSamePod && volume.Type == "bind" && !ok { + utils.Warn( + "Bind volumes are not supported yet, " + + "excepting for those declared as " + + LABEL_CM_FILES + + ", skipping volume " + volume.Source + + " from service " + service.Name, + ) continue - } else { - done[name] = true - volumes = append(volumes, vol) } - } - deployment.Spec.Template.Spec.Volumes = volumes - // Then, create Services and possible Ingresses for ingress labels, "ports" and "expose" section - if len(s.Ports) > 0 || len(s.Expose) > 0 { - for _, s := range generateServicesAndIngresses(name, s) { - if s != nil { - fileGeneratorChan <- s + container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + if container == nil { + utils.Warn("Container not found for volume", volume.Source) + continue + } + + // ensure that the volume is not already present in the container + for _, vm := range container.VolumeMounts { + if vm.Name == volume.Source { + continue } } + + switch volume.Type { + case "volume": + // Add volume to container + container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ + Name: volume.Source, + MountPath: volume.Target, + }) + // Add volume to values.yaml only if it the service is not in the same pod that another service. + // If it is in the same pod, the volume will be added to the other service later + if _, ok := service.Labels[LABEL_SAME_POD]; !ok { + d.chart.Values[service.Name].(*Value).AddPersistence(volume.Source) + } + // Add volume to deployment + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: volume.Source, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: utils.TplName(service.Name, appName, volume.Source), + }, + }, + }) + case "bind": + // Add volume to container + cm := NewConfigMapFromFiles(service, appName, volume.Source) + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: utils.PathToName(volume.Source), + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: cm.ObjectMeta.Name, + }, + }, + }, + }) + // add the mount path to the container + container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ + Name: utils.PathToName(volume.Source), + MountPath: volume.Target, + }) + + d.configMaps[utils.PathToName(volume.Source)] = true + // add all subdirectories to the list of directories + stat, err := os.Stat(volume.Source) + if err != nil { + log.Fatal(err) + } + if stat.IsDir() { + files, err := os.ReadDir(volume.Source) + if err != nil { + log.Fatal(err) + } + for _, file := range files { + if file.IsDir() { + cm := NewConfigMapFromFiles(service, appName, filepath.Join(volume.Source, file.Name())) + name := utils.PathToName(volume.Source) + "-" + file.Name() + d.configMaps[name] = true + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, corev1.Volume{ + Name: utils.PathToName(volume.Source) + "-" + file.Name(), + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: cm.ObjectMeta.Name, + }, + }, + }, + }) + // add the mount path to the container + container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ + Name: name, + MountPath: filepath.Join(volume.Target, file.Name()), + }) + } + } + } + } + + d.Spec.Template.Spec.Containers[index] = *container } - - // add the volumes in Values - if len(VolumeValues[name]) > 0 { - AddValues(name, map[string]EnvVal{"persistence": VolumeValues[name]}) - } - - // the deployment is ready, give it - fileGeneratorChan <- deployment - - // and then, we can say that it's the end - fileGeneratorChan <- nil +} + +func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) { + log.Printf("In %s deployment, add volumes for service %s from binded deployment %s", d.Name, service.Name, binded.Name) + // find the volume in the binded deployment + for _, bindedVolume := range binded.Spec.Template.Spec.Volumes { + log.Println("bindedVolume.Name found", bindedVolume.Name) + skip := false + for _, targetVol := range d.Spec.Template.Spec.Volumes { + if targetVol.Name == bindedVolume.Name { + log.Println("Volume", bindedVolume.Name, "already exists in deployment", d.Name) + skip = true + break + } + } + if !skip { + // add the volume to the current deployment + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, bindedVolume) + log.Println("d.Spec.Template.Spec.Volumes", d.Spec.Template.Spec.Volumes) + // get the container + + } + // add volume mount to the container + targetContainer, ti := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + sourceContainer, _ := utils.GetContainerByName(service.Name, binded.Spec.Template.Spec.Containers) + for _, bindedMount := range sourceContainer.VolumeMounts { + if bindedMount.Name == bindedVolume.Name { + log.Println("bindedMount.Name found", bindedMount.Name) + targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount) + } + } + d.Spec.Template.Spec.Containers[ti] = *targetContainer + } +} + +// SetEnvFrom sets the environment variables to a configmap. The configmap is created. +func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string) { + + if len(service.Environment) == 0 { + return + } + + drop := []string{} + secrets := []string{} + + // secrets from label + labelSecrets := []string{} + if v, ok := service.Labels[LABEL_SECRETS]; ok { + err := yaml.Unmarshal([]byte(v), &labelSecrets) + if err != nil { + log.Fatal(err) + } + } + + // values from label + varDescriptons := utils.GetValuesFromLabel(service, LABEL_VALUES) + labelValues := []string{} + for v := range varDescriptons { + labelValues = append(labelValues, v) + } + + for _, secret := range labelSecrets { + // get the secret name + _, ok := service.Environment[secret] + if !ok { + drop = append(drop, secret) + utils.Warn("Secret " + secret + " not found in service " + service.Name + " - skpped") + continue + } + secrets = append(secrets, secret) + } + + // for each values from label "values", add it to Values map and change the envFrom + // value to {{ .Values.. }} + for _, value := range labelValues { + // get the environment variable name + val, ok := service.Environment[value] + if !ok { + drop = append(drop, value) + utils.Warn("Environment variable " + value + " not found in service " + service.Name + " - skpped") + continue + } + if d.chart.Values[service.Name].(*Value).Environment == nil { + d.chart.Values[service.Name].(*Value).Environment = make(map[string]any) + } + d.chart.Values[service.Name].(*Value).Environment[value] = *val + // set the environment variable to bind to the values.yaml file + v := utils.TplValue(service.Name, "environment."+value) + service.Environment[value] = &v + } + + for _, value := range drop { + delete(service.Environment, value) + } + + fromSources := []corev1.EnvFromSource{} + + if len(service.Environment) > 0 { + fromSources = append(fromSources, corev1.EnvFromSource{ + ConfigMapRef: &corev1.ConfigMapEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: utils.TplName(service.Name, appName), + }, + }, + }) + } + + if len(secrets) > 0 { + fromSources = append(fromSources, corev1.EnvFromSource{ + SecretRef: &corev1.SecretEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: utils.TplName(service.Name, appName), + }, + }, + }) + } + + container, index := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers) + if container == nil { + utils.Warn("Container not found for service " + service.Name) + return + } + + container.EnvFrom = append(container.EnvFrom, fromSources...) + + if container.Env == nil { + container.Env = []corev1.EnvVar{} + } + + d.Spec.Template.Spec.Containers[index] = *container +} + +func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container) { + + // get the label for healthcheck + if v, ok := service.Labels[LABEL_HEALTHCHECK]; ok { + probes := struct { + LivenessProbe *corev1.Probe `yaml:"livenessProbe"` + ReadinessProbe *corev1.Probe `yaml:"readinessProbe"` + }{} + err := yaml.Unmarshal([]byte(v), &probes) + if err != nil { + log.Fatal(err) + } + container.LivenessProbe = probes.LivenessProbe + container.ReadinessProbe = probes.ReadinessProbe + return + } + + if service.HealthCheck != nil { + period := 30.0 + if service.HealthCheck.Interval != nil { + period = time.Duration(*service.HealthCheck.Interval).Seconds() + } + container.LivenessProbe = &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + Exec: &corev1.ExecAction{ + Command: service.HealthCheck.Test[1:], + }, + }, + PeriodSeconds: int32(period), + } + } +} + +// Yaml returns the yaml representation of the deployment. +func (d *Deployment) Yaml() ([]byte, error) { + serviceName := d.service.Name + y, err := yaml.Marshal(d) + if err != nil { + return nil, err + } + + // for each volume mount, add a condition "if values has persistence" + changing := false + content := strings.Split(string(y), "\n") + spaces := "" + volumeName := "" + + // this loop add condition for each volume mount + for line, volume := range content { + // find the volume name + for i := line; i < len(content); i++ { + if strings.Contains(content[i], "name: ") { + volumeName = strings.TrimSpace(strings.Replace(content[i], "name: ", "", 1)) + break + } + + } + if volumeName == "" { + continue + } + + if _, ok := d.configMaps[volumeName]; ok { + continue + } + + if strings.Contains(volume, "- mountPath: ") { + spaces = strings.Repeat(" ", utils.CountStartingSpaces(volume)) + content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + volumeName + `.enabled }}` + "\n" + volume + changing = true + } + if strings.Contains(volume, "name: ") && changing { + content[line] = volume + "\n" + spaces + "{{- end }}" + changing = false + } + } + + changing = false + inVolumes := false + volumeName = "" + // this loop changes imagePullPolicy to {{ .Values..imagePullPolicy }} + // and the volume definition adding the condition "if values has persistence" + for i, line := range content { + + if strings.Contains(line, "imagePullPolicy:") { + spaces = strings.Repeat(" ", utils.CountStartingSpaces(line)) + content[i] = spaces + "imagePullPolicy: {{ .Values." + serviceName + ".imagePullPolicy }}" + } + + // find the volume name + for i := i; i < len(content); i++ { + if strings.Contains(content[i], "- name: ") { + volumeName = strings.TrimSpace(strings.Replace(content[i], "- name: ", "", 1)) + break + } + } + if strings.Contains(line, "volumes:") { + inVolumes = true + } + + if volumeName == "" { + continue + } + + if _, ok := d.configMaps[volumeName]; ok { + continue + } + + if strings.Contains(line, "- name: ") && inVolumes { + spaces = strings.Repeat(" ", utils.CountStartingSpaces(line)) + content[i] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + volumeName + `.enabled }}` + "\n" + line + changing = true + } + if strings.Contains(line, "claimName: ") && changing { + content[i] = line + "\n" + spaces + "{{- end }}" + changing = false + } + } + + // for impagePullSecrets, replace the name with the value from values.yaml + inpullsecrets := false + for i, line := range content { + if strings.Contains(line, "imagePullSecrets:") { + inpullsecrets = true + } + if inpullsecrets && strings.Contains(line, "- name: ") && inpullsecrets { + line = strings.Replace(line, "- name: ", "", 1) + line = strings.ReplaceAll(line, "'", "") + content[i] = line + inpullsecrets = false + } + } + + // Find the replicas line and replace it with the value from values.yaml + for i, line := range content { + if strings.Contains(line, "replicas:") { + line = regexp.MustCompile("replicas: .*$").ReplaceAllString(line, "replicas: {{ .Values."+serviceName+".replicas }}") + content[i] = line + } + } + + return []byte(strings.Join(content, "\n")), nil +} + +func (d *Deployment) Filename() string { + return d.service.Name + ".deployment.yaml" } diff --git a/generator/doc.go b/generator/doc.go new file mode 100644 index 0000000..095c2e7 --- /dev/null +++ b/generator/doc.go @@ -0,0 +1,18 @@ +/* +The generator package generates kubernetes objects from a compose file and transforms them into a helm chart. + +The generator package is the core of katenary. It is responsible for generating kubernetes objects from a compose file and transforming them into a helm chart. +Convertion manipulates Yaml representation of kubernetes object to add conditions, labels, annotations, etc. to the objects. It also create the values to be set to +the values.yaml file. + +The generate.Convert() create an HelmChart object and call "Generate()" method to convert from a compose file to a helm chart. +It saves the helm chart in the given directory. + +If you want to change or override the write behavior, you can use the HelmChart.Generate() function and implement your own write function. This function returns +the helm chart object containing all kubernetes objects and helm chart ingormation. It does not write the helm chart to the disk. + +TODO: Manage cronjob + rbac +TODO: create note.txt +TODO: manage emptyDirs +*/ +package generator diff --git a/generator/env.go b/generator/env.go deleted file mode 100644 index d12f605..0000000 --- a/generator/env.go +++ /dev/null @@ -1,154 +0,0 @@ -package generator - -import ( - "fmt" - "io/ioutil" - "katenary/compose" - "katenary/helm" - "katenary/logger" - "katenary/tools" - "os" - "path/filepath" - "strings" - - "github.com/compose-spec/compose-go/types" - "gopkg.in/yaml.v3" -) - -// applyEnvMapLabel will get all LABEL_MAP_ENV to rebuild the env map with tpl. -func applyEnvMapLabel(s *types.ServiceConfig, c *helm.Container) { - - locker.Lock() - defer locker.Unlock() - mapenv, ok := s.Labels[helm.LABEL_MAP_ENV] - if !ok { - return - } - - // the mapenv is a YAML string - var envmap map[string]EnvVal - err := yaml.Unmarshal([]byte(mapenv), &envmap) - if err != nil { - logger.ActivateColors = true - logger.Red(err.Error()) - logger.ActivateColors = false - return - } - - // add in envmap - for k, v := range envmap { - vstring := fmt.Sprintf("%v", v) - s.Environment[k] = &vstring - touched := false - if c.Env != nil { - c.Env = make([]*helm.Value, 0) - } - for _, env := range c.Env { - if env.Name == k { - env.Value = v - touched = true - } - } - if !touched { - c.Env = append(c.Env, &helm.Value{Name: k, Value: v}) - } - } -} - -// readEnvFile read environment file and add to the values.yaml map. -func readEnvFile(envfilename string) map[string]EnvVal { - env := make(map[string]EnvVal) - content, err := ioutil.ReadFile(envfilename) - if err != nil { - logger.ActivateColors = true - logger.Red(err.Error()) - logger.ActivateColors = false - os.Exit(2) - } - // each value is on a separate line with KEY=value - lines := strings.Split(string(content), "\n") - for _, line := range lines { - if strings.Contains(line, "=") { - kv := strings.SplitN(line, "=", 2) - env[kv[0]] = kv[1] - } - } - return env -} - -// prepareEnvFromFiles generate configMap or secrets from environment files. -func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Container, fileGeneratorChan HelmFileGenerator) { - - // prepare secrets - secretsFiles := make([]string, 0) - if v, ok := s.Labels[helm.LABEL_ENV_SECRET]; ok { - secretsFiles = strings.Split(v, ",") - } - - var secretVars []string - if v, ok := s.Labels[helm.LABEL_SECRETVARS]; ok { - secretVars = strings.Split(v, ",") - } - - for i, s := range secretVars { - secretVars[i] = strings.TrimSpace(s) - } - - // manage environment files (env_file in compose) - for _, envfile := range s.EnvFile { - f := tools.PathToName(envfile) - f = strings.ReplaceAll(f, ".env", "") - isSecret := false - for _, s := range secretsFiles { - s = strings.TrimSpace(s) - if s == envfile { - isSecret = true - } - } - var store helm.InlineConfig - if !isSecret { - logger.Bluef(ICON_CONF+" Generating configMap from %s\n", envfile) - store = helm.NewConfigMap(name, envfile) - } else { - logger.Bluef(ICON_SECRET+" Generating secret from %s\n", envfile) - store = helm.NewSecret(name, envfile) - } - - envfile = filepath.Join(compose.GetCurrentDir(), envfile) - if err := store.AddEnvFile(envfile, secretVars); err != nil { - logger.ActivateColors = true - logger.Red(err.Error()) - logger.ActivateColors = false - os.Exit(2) - } - - section := "configMapRef" - if isSecret { - section = "secretRef" - } - - container.EnvFrom = append(container.EnvFrom, map[string]map[string]string{ - section: { - "name": store.Metadata().Name, - }, - }) - - // read the envfile and remove them from the container environment or secret - envs := readEnvFile(envfile) - for varname := range envs { - if !isSecret { - // remove varname from container - for i, s := range container.Env { - if s.Name == varname { - container.Env = append(container.Env[:i], container.Env[i+1:]...) - i-- - } - } - } - } - - if store != nil { - fileGeneratorChan <- store.(HelmFile) - } - } -} diff --git a/generator/extrafiles/doc.go b/generator/extrafiles/doc.go new file mode 100644 index 0000000..5033525 --- /dev/null +++ b/generator/extrafiles/doc.go @@ -0,0 +1,2 @@ +/* extrafiles package provides function to generate the Chart files that are not objects. Like README.md and notes.txt... */ +package extrafiles diff --git a/generator/extrafiles/notes.go b/generator/extrafiles/notes.go new file mode 100644 index 0000000..373e7ee --- /dev/null +++ b/generator/extrafiles/notes.go @@ -0,0 +1,11 @@ +package extrafiles + +import _ "embed" + +//go:embed notes.tpl +var notesTemplate string + +// NoteTXTFile returns the content of the note.txt file. +func NotesFile() string { + return notesTemplate +} diff --git a/generator/extrafiles/notes.tpl b/generator/extrafiles/notes.tpl new file mode 100644 index 0000000..3121a00 --- /dev/null +++ b/generator/extrafiles/notes.tpl @@ -0,0 +1,27 @@ +Your release is named {{ .Release.Name }}. + +To learn more about the release, try: + + $ helm -n {{ .Release.Namespace }} status {{ .Release.Name }} + $ helm -n {{ .Release.Namespace }} get all {{ .Release.Name }} + +To delete the release, run: + + $ helm -n {{ .Release.Namespace }} delete {{ .Release.Name }} + +You can see this notes again by running: + + $ helm -n {{ .Release.Namespace }} get notes {{ .Release.Name }} + +{{- $count := 0 -}} +{{- range $s, $v := .Values -}} +{{- if and $v $v.ingress -}} +{{- $count = add $count 1 -}} +{{- if eq $count 1 }} + +The ingress list is: +{{ end }} + - {{ $s }}: http://{{ $v.ingress.host }}{{ $v.ingress.path }} +{{- end -}} +{{ end -}} + diff --git a/generator/extrafiles/readme.go b/generator/extrafiles/readme.go new file mode 100644 index 0000000..865d203 --- /dev/null +++ b/generator/extrafiles/readme.go @@ -0,0 +1,99 @@ +package extrafiles + +import ( + "bytes" + "fmt" + "sort" + "strings" + "text/template" + + _ "embed" + + "gopkg.in/yaml.v3" +) + +type chart struct { + Name string + Description string + Values []string +} + +//go:embed readme.tpl +var readmeTemplate string + +// ReadMeFile returns the content of the README.md file. +func ReadMeFile(charname, description string, values map[string]any) string { + + // values is a yaml structure with keys and structured values... + // we want to make list of dot separated keys and their values + + vv := map[string]any{} + out, _ := yaml.Marshal(values) + yaml.Unmarshal(out, &vv) + + result := make(map[string]string) + parseValues("", vv, result) + + funcMap := template.FuncMap{ + "repeat": func(s string, count int) string { + return strings.Repeat(s, count) + }, + } + tpl, err := template.New("readme").Funcs(funcMap).Parse(readmeTemplate) + if err != nil { + panic(err) + } + + valuesLines := []string{} + maxParamLen := 0 + maxDefaultLen := 0 + for key, value := range result { + if len(key) > maxParamLen { + maxParamLen = len(key) + } + if len(value) > maxDefaultLen { + maxDefaultLen = len(value) + } + } + for key, value := range result { + valuesLines = append(valuesLines, fmt.Sprintf("| %-*s | %-*s |", maxParamLen, key, maxDefaultLen, value)) + } + sort.Strings(valuesLines) + + buf := &bytes.Buffer{} + err = tpl.Execute(buf, map[string]any{ + "DescrptionPadding": maxParamLen, + "DefaultPadding": maxDefaultLen, + "Chart": chart{ + Name: charname, + Description: description, + Values: valuesLines, + }, + }) + if err != nil { + panic(err) + } + + return buf.String() +} + +func parseValues(prefix string, values map[string]interface{}, result map[string]string) { + for key, value := range values { + path := key + if prefix != "" { + path = prefix + "." + key + } + + switch v := value.(type) { + case []interface{}: + for i, u := range v { + parseValues(fmt.Sprintf("%s[%d]", path, i), map[string]interface{}{"value": u}, result) + } + case map[string]interface{}: + parseValues(path, v, result) + default: + strValue := fmt.Sprintf("`%v`", value) + result["`"+path+"`"] = strValue + } + } +} diff --git a/generator/extrafiles/readme.tpl b/generator/extrafiles/readme.tpl new file mode 100644 index 0000000..5ca4116 --- /dev/null +++ b/generator/extrafiles/readme.tpl @@ -0,0 +1,32 @@ +# {{ .Chart.Name }} + +{{ .Chart.Description }} + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```bash +# Standard Helm install +$ helm install my-release {{ .Chart.Name }} + +# To use a custom namespace and force the creation of the namespace +$ helm install my-release --namespace my-namespace --create-namespace {{ .Chart.Name }} + +# To use a custom values file +$ helm install my-release -f my-values.yaml {{ .Chart.Name }} +``` + +See the [Helm documentation](https://helm.sh/docs/intro/using_helm/) for more information on installing and managing the chart. + +## Configuration + +The following table lists the configurable parameters of the {{ .Chart.Name }} chart and their default values. + +| {{ printf "%-*s" .DescrptionPadding "Parameter" }} | {{ printf "%-*s" .DefaultPadding "Default" }} | +| {{ repeat "-" .DescrptionPadding }} | {{ repeat "-" .DefaultPadding }} | +{{- range .Chart.Values }} +{{ . }} +{{- end }} + + diff --git a/generator/generator.go b/generator/generator.go new file mode 100644 index 0000000..192ef74 --- /dev/null +++ b/generator/generator.go @@ -0,0 +1,658 @@ +package generator + +// TODO: configmap from files 20% + +import ( + "bytes" + "fmt" + "katenary/utils" + "log" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + + "github.com/compose-spec/compose-go/types" + goyaml "gopkg.in/yaml.v3" + corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/yaml" +) + +// Generate a chart from a compose project. +// This does not write files to disk, it only creates the HelmChart object. +// +// The Generate function will create the HelmChart object this way: +// +// 1. Detect the service port name or leave the port number if not found. +// +// 2. Create a deployment for each service that are not ingnore. +// +// 3. Create a service and ingresses for each service that has ports and/or declared ingresses. +// +// 4. Create a PVC or Configmap volumes for each volume. +// +// 5. Create init containers for each service which has dependencies to other services. +// +// 6. Create a chart dependencies. +// +// 7. Create a configmap and secrets from the environment variables. +// +// 8. Merge the same-pod services. +func Generate(project *types.Project) (*HelmChart, error) { + + var ( + appName = project.Name + deployments = make(map[string]*Deployment, len(project.Services)) + services = make(map[string]*Service) + podToMerge = make(map[string]*Deployment) + ) + chart := NewChart(appName) + + // Add the compose files hash to the chart annotations + hash, err := utils.HashComposefiles(project.ComposeFiles) + if err != nil { + return nil, err + } + Annotations[KATENARY_PREFIX+"compose-hash"] = hash + chart.composeHash = &hash + + // find the "main-app" label, and set chart.AppVersion to the tag if exists + mainCount := 0 + for _, service := range project.Services { + if serviceIsMain(service) { + log.Printf("Found main app %s", service.Name) + mainCount++ + if mainCount > 1 { + return nil, fmt.Errorf("found more than one main app") + } + setChartVersion(chart, service) + } + } + if mainCount == 0 { + chart.AppVersion = "0.1.0" + } + + // first pass, create all deployments whatewer they are. + for _, service := range project.Services { + // check the "ports" label from container and add it to the service + if err := fixPorts(&service); err != nil { + return nil, err + } + + // isgnored service + if isIgnored(service) { + fmt.Printf("%s Ignoring service %s\n", utils.IconInfo, service.Name) + continue + } + + // helm dependency + if isHelmDependency, err := setDependencies(chart, service); err != nil { + return nil, err + } else if isHelmDependency { + continue + } + + // create all deployments + d := NewDeployment(service, chart) + deployments[service.Name] = d + + // generate the cronjob if needed + setCronJob(service, chart, appName) + + // get the same-pod label if exists, add it to the list. + // We later will copy some parts to the target deployment and remove this one. + if samePod, ok := service.Labels[LABEL_SAME_POD]; ok && samePod != "" { + podToMerge[samePod] = d + } + + // create the needed service for the container port + if len(service.Ports) > 0 { + s := NewService(service, appName) + services[service.Name] = s + } + + // create all ingresses + if ingress := d.AddIngress(service, appName); ingress != nil { + y, _ := ingress.Yaml() + chart.Templates[ingress.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + } + } + + // now we have all deployments, we can create PVC if needed (it's separated from + // the above loop because we need all deployments to not duplicate PVC for "same-pod" services) + for _, service := range project.Services { + if err := buildVolumes(service, chart, deployments); err != nil { + return nil, err + } + } + // drop all "same-pod" deployments because the containers and volumes are already + // in the target deployment + for _, service := range project.Services { + if samepod, ok := service.Labels[LABEL_SAME_POD]; ok && samepod != "" { + // move this deployment volumes to the target deployment + if target, ok := deployments[samepod]; ok { + target.AddContainer(service) + target.BindFrom(service, deployments[service.Name]) + delete(deployments, service.Name) + } else { + log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, LABEL_SAME_POD) + } + } + } + + // create init containers for all DependsOn + for _, s := range project.Services { + for _, d := range s.GetDependencies() { + if dep, ok := deployments[d]; ok { + deployments[s.Name].DependsOn(dep) + } else { + log.Printf("service %[1]s depends on %[2]s, but %[2]s is not defined", s.Name, d) + } + } + } + + // generate configmaps with environment variables + generateConfigMapsAndSecrets(project, chart) + + // if the env-from label is set, we need to add the env vars from the configmap + // to the environment of the service + for _, s := range project.Services { + setSharedConf(s, chart, deployments) + } + + // generate yaml files + for _, d := range deployments { + y, _ := d.Yaml() + chart.Templates[d.Filename()] = &ChartTemplate{ + Content: y, + Servicename: d.service.Name, + } + } + + // generate all services + for _, s := range services { + y, _ := s.Yaml() + chart.Templates[s.Filename()] = &ChartTemplate{ + Content: y, + Servicename: s.service.Name, + } + } + + // compute all needed resplacements in YAML templates + for n, v := range chart.Templates { + v.Content = removeReplaceString(v.Content) + v.Content = computeNIndent(v.Content) + chart.Templates[n].Content = v.Content + } + + // generate helper + chart.Helper = Helper(appName) + + return chart, nil +} + +// computeNIndentm replace all __indent__ labels with the number of spaces before the label. +func computeNIndent(b []byte) []byte { + lines := bytes.Split(b, []byte("\n")) + for i, line := range lines { + if !bytes.Contains(line, []byte("__indent__")) { + continue + } + startSpaces := "" + spaces := regexp.MustCompile(`^\s+`).FindAllString(string(line), -1) + if len(spaces) > 0 { + startSpaces = spaces[0] + } + line = []byte(startSpaces + strings.TrimLeft(string(line), " ")) + line = bytes.ReplaceAll(line, []byte("__indent__"), []byte(fmt.Sprintf("%d", len(startSpaces)))) + lines[i] = line + } + return bytes.Join(lines, []byte("\n")) +} + +// removeReplaceString replace all __replace_ labels with the value of the +// capture group and remove all new lines and repeated spaces. +// +// we created: +// +// __replace_bar: '{{ include "foo.labels" . +// }}' +// +// note the new line and spaces... +// +// we now want to replace it with {{ include "foo.labels" . }}, without the label name. +func removeReplaceString(b []byte) []byte { + + // replace all matches with the value of the capture group + // and remove all new lines and repeated spaces + b = replaceLabelRegexp.ReplaceAllFunc(b, func(b []byte) []byte { + inc := replaceLabelRegexp.FindSubmatch(b)[1] + inc = bytes.ReplaceAll(inc, []byte("\n"), []byte("")) + inc = bytes.ReplaceAll(inc, []byte("\r"), []byte("")) + inc = regexp.MustCompile(`\s+`).ReplaceAll(inc, []byte(" ")) + return inc + }) + return b +} + +func serviceIsMain(service types.ServiceConfig) bool { + if main, ok := service.Labels[LABEL_MAIN_APP]; ok { + return main == "true" || main == "yes" || main == "1" + } + return false +} + +func setChartVersion(chart *HelmChart, service types.ServiceConfig) { + if chart.Version == "" { + image := service.Image + parts := strings.Split(image, ":") + if len(parts) > 1 { + chart.AppVersion = parts[1] + } else { + chart.AppVersion = "0.1.0" + } + } +} + +func fixPorts(service *types.ServiceConfig) error { + // check the "ports" label from container and add it to the service + if portsLabel, ok := service.Labels[LABEL_PORTS]; ok { + ports := []uint32{} + if err := goyaml.Unmarshal([]byte(portsLabel), &ports); err != nil { + // maybe it's a string, comma separated + parts := strings.Split(portsLabel, ",") + for _, part := range parts { + part = strings.TrimSpace(part) + if part == "" { + continue + } + port, err := strconv.ParseUint(part, 10, 32) + if err != nil { + return err + } + ports = append(ports, uint32(port)) + } + } + for _, port := range ports { + service.Ports = append(service.Ports, types.ServicePortConfig{ + Target: port, + }) + } + } + return nil +} + +func setCronJob(service types.ServiceConfig, chart *HelmChart, appName string) *CronJob { + if _, ok := service.Labels[LABEL_CRONJOB]; !ok { + return nil + } + cronjob, rbac := NewCronJob(service, chart, appName) + y, _ := cronjob.Yaml() + chart.Templates[cronjob.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + + if rbac != nil { + y, _ := rbac.RoleBinding.Yaml() + chart.Templates[rbac.RoleBinding.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + y, _ = rbac.Role.Yaml() + chart.Templates[rbac.Role.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + y, _ = rbac.ServiceAccount.Yaml() + chart.Templates[rbac.ServiceAccount.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + } + + return cronjob +} + +func setDependencies(chart *HelmChart, service types.ServiceConfig) (bool, error) { + // helm dependency + if v, ok := service.Labels[LABEL_DEPENDENCIES]; ok { + d := Dependency{} + if err := yaml.Unmarshal([]byte(v), &d); err != nil { + return false, err + } + fmt.Printf("%s Adding dependency to %s\n", utils.IconDependency, d.Name) + chart.Dependencies = append(chart.Dependencies, d) + + name := d.Name + if d.Alias != "" { + name = d.Alias + } + // add the dependency env vars to the values.yaml + chart.Values[name] = d.Values + return true, nil + } + return false, nil +} + +func isIgnored(service types.ServiceConfig) bool { + if v, ok := service.Labels[LABEL_IGNORE]; ok { + return v == "true" || v == "yes" || v == "1" + } + return false +} + +func buildVolumes(service types.ServiceConfig, chart *HelmChart, deployments map[string]*Deployment) error { + appName := chart.Name + for _, v := range service.Volumes { + // Do not add volumes if the pod is injected in a deployments + // via "same-pod" and the volume in destination deployment exists + if samePodVolume(service, v, deployments) { + continue + } + switch v.Type { + case "volume": + pvc := NewVolumeClaim(service, v.Source, appName) + + // if the service is integrated in another deployment, we need to add the volume + // to the target deployment + if override, ok := service.Labels[LABEL_SAME_POD]; ok { + pvc.nameOverride = override + pvc.PersistentVolumeClaim.Spec.StorageClassName = utils.StrPtr(`{{ .Values.` + override + `.persistence.` + v.Source + `.storageClass }}`) + chart.Values[override].(*Value).AddPersistence(v.Source) + } + y, _ := pvc.Yaml() + chart.Templates[pvc.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, //TODO, use name + } + + case "bind": + // ensure the path is in labels + bindPath := map[string]string{} + if _, ok := service.Labels[LABEL_CM_FILES]; ok { + files := []string{} + if err := yaml.Unmarshal([]byte(service.Labels[LABEL_CM_FILES]), &files); err != nil { + return err + } + for _, f := range files { + bindPath[f] = f + } + } + if _, ok := bindPath[v.Source]; !ok { + continue + } + + cm := NewConfigMapFromFiles(service, appName, v.Source) + var err error + var y []byte + if y, err = cm.Yaml(); err != nil { + log.Fatal(err) + } + chart.Templates[cm.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + + // continue with subdirectories + stat, err := os.Stat(v.Source) + if err != nil { + return err + } + if stat.IsDir() { + files, err := filepath.Glob(filepath.Join(v.Source, "*")) + if err != nil { + return err + } + for _, f := range files { + if f == v.Source { + continue + } + if stat, err := os.Stat(f); err != nil || !stat.IsDir() { + continue + } + cm := NewConfigMapFromFiles(service, appName, f) + var err error + var y []byte + if y, err = cm.Yaml(); err != nil { + log.Fatal(err) + } + log.Printf("Adding configmap %s %s", cm.Filename(), f) + chart.Templates[cm.Filename()] = &ChartTemplate{ + Content: y, + Servicename: service.Name, + } + } + } + + } + } + return nil +} + +func generateConfigMapsAndSecrets(project *types.Project, chart *HelmChart) error { + appName := chart.Name + for _, s := range project.Services { + if s.Environment == nil || len(s.Environment) == 0 { + continue + } + + originalEnv := types.MappingWithEquals{} + secretsVar := types.MappingWithEquals{} + + // copy env to originalEnv + for k, v := range s.Environment { + originalEnv[k] = v + } + + if v, ok := s.Labels[LABEL_SECRETS]; ok { + list := []string{} + if err := yaml.Unmarshal([]byte(v), &list); err != nil { + log.Fatal("error unmarshaling secrets label:", err) + } + for _, secret := range list { + if secret == "" { + continue + } + if _, ok := s.Environment[secret]; !ok { + fmt.Printf("%s secret %s not found in environment", utils.IconWarning, secret) + continue + } + secretsVar[secret] = s.Environment[secret] + } + } + + if len(secretsVar) > 0 { + s.Environment = secretsVar + sec := NewSecret(s, appName) + y, _ := sec.Yaml() + name := sec.service.Name + chart.Templates[name+".secret.yaml"] = &ChartTemplate{ + Content: y, + Servicename: s.Name, + } + } + + // remove secrets from env + s.Environment = originalEnv // back to original + for k := range secretsVar { + delete(s.Environment, k) + } + if len(s.Environment) > 0 { + cm := NewConfigMap(s, appName) + y, _ := cm.Yaml() + name := cm.service.Name + chart.Templates[name+".configmap.yaml"] = &ChartTemplate{ + Content: y, + Servicename: s.Name, + } + } + } + return nil +} + +func mergePods(target, from *Deployment, services map[string]*Service, chart *HelmChart) { + + targetName := target.service.Name + fromName := from.service.Name + + // copy the volumes from the source deployment + for _, v := range from.Spec.Template.Spec.Volumes { + // ensure that the volume is not already present + found := false + for _, tv := range target.Spec.Template.Spec.Volumes { + if tv.Name == v.Name { + found = true + break + } + } + if found { + continue + } + target.Spec.Template.Spec.Volumes = append(target.Spec.Template.Spec.Volumes, v) + } + // copy the containers from the source deployment + for _, c := range from.Spec.Template.Spec.Containers { + target.Spec.Template.Spec.Containers = append(target.Spec.Template.Spec.Containers, c) + } + // copy the init containers from the source deployment + for _, c := range from.Spec.Template.Spec.InitContainers { + target.Spec.Template.Spec.InitContainers = append(target.Spec.Template.Spec.InitContainers, c) + } + // drop the deployment from the chart + delete(chart.Templates, fromName+".deployment.yaml") + + // rewite the target deployment + y, err := target.Yaml() + if err != nil { + log.Fatal("error rewriting deployment:", err) + } + chart.Templates[target.Filename()] = &ChartTemplate{ + Content: y, + Servicename: targetName, + } + + // now, if the source deployment has a service, we need to merge it with the target service + if _, ok := chart.Templates[targetName+".service.yaml"]; ok { + container, _ := utils.GetContainerByName(fromName, target.Spec.Template.Spec.Containers) + if container.Ports == nil || len(container.Ports) == 0 { + return + } + targetService := services[targetName] + for _, port := range container.Ports { + targetService.AddPort(types.ServicePortConfig{ + Target: uint32(port.ContainerPort), + Protocol: "TCP", + }, port.Name) + } + // rewrite the tartget service + y, _ := targetService.Yaml() + chart.Templates[targetName+".service.yaml"] = &ChartTemplate{ + Content: y, + Servicename: target.service.Name, + } + + // and remove the source service from the chart + delete(chart.Templates, fromName+".service.yaml") + + // In Valuses, remove the "replicas" key from the source service + if v, ok := chart.Values[fromName]; ok { + // if v is a Value + if v, ok := v.(*Value); ok { + v.Replicas = nil + } + } + } +} + +func samePodVolume(service types.ServiceConfig, v types.ServiceVolumeConfig, deployments map[string]*Deployment) bool { + // if the service has volumes, and it has "same-pod" label + // - get the target deployment + // - check if it has the same volume + // if not, return false + + if v.Source == "" { + return false + } + + if service.Volumes == nil || len(service.Volumes) == 0 { + return false + } + + targetDeployment := "" + if targetName, ok := service.Labels[LABEL_SAME_POD]; !ok { + return false + } else { + targetDeployment = targetName + } + + // get the target deployment + var target *Deployment + for _, d := range deployments { + if d.service.Name == targetDeployment { + target = d + break + } + } + if target == nil { + return false + } + + // check if it has the same volume + for _, tv := range target.Spec.Template.Spec.Volumes { + if tv.Name == v.Source { + log.Printf("found same pod volume %s in deployment %s and %s", tv.Name, service.Name, targetDeployment) + return true + } + } + return false +} + +func setSharedConf(service types.ServiceConfig, chart *HelmChart, deployments map[string]*Deployment) { + // if the service has the "shared-conf" label, we need to add the configmap + // to the chart and add the env vars to the service + if _, ok := service.Labels[LABEL_ENV_FROM]; !ok { + return + } + fromservices := []string{} + if err := yaml.Unmarshal([]byte(service.Labels[LABEL_ENV_FROM]), &fromservices); err != nil { + log.Fatal("error unmarshaling env-from label:", err) + } + // find the configmap in the chart templates + for _, fromservice := range fromservices { + if _, ok := chart.Templates[fromservice+".configmap.yaml"]; !ok { + log.Printf("configmap %s not found in chart templates", fromservice) + continue + } + // find the corresponding target deployment + var target *Deployment + for _, d := range deployments { + if d.service.Name == service.Name { + target = d + break + } + } + if target == nil { + continue + } + // add the configmap to the service + for i, c := range target.Spec.Template.Spec.Containers { + if c.Name != service.Name { + continue + } + c.EnvFrom = append(c.EnvFrom, corev1.EnvFromSource{ + ConfigMapRef: &corev1.ConfigMapEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: utils.TplName(fromservice, chart.Name), + }, + }, + }) + target.Spec.Template.Spec.Containers[i] = c + } + } + +} diff --git a/generator/globals.go b/generator/globals.go new file mode 100644 index 0000000..edab2a4 --- /dev/null +++ b/generator/globals.go @@ -0,0 +1,19 @@ +package generator + +import "regexp" + +var ( + // regexp to all tpl strings + tplValueRegexp = regexp.MustCompile(`\{\{.*\}\}-`) + + // find all labels starting by __replace_ and ending with ":" + // and get the value between the quotes + // ?s => multiline + // (?P.+?) => named capture group to "inc" variable (so we could use $inc in the replace) + replaceLabelRegexp = regexp.MustCompile(`(?s)__replace_.+?: '(?P.+?)'`) + + // Standard annotationss + Annotations = map[string]string{ + KATENARY_PREFIX + "version": Version, + } +) diff --git a/generator/helmHelper.tpl b/generator/helmHelper.tpl new file mode 100644 index 0000000..8d40010 --- /dev/null +++ b/generator/helmHelper.tpl @@ -0,0 +1,36 @@ +{{- define "__APP__.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "__APP__.name" -}} +{{- if .Values.nameOverride -}} +{{- .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} + +{{- define "__APP__.labels" -}} +{{ include "__APP__.selectorLabels" .}} +{{ if .Chart.Version -}} +{{ printf "__PREFIX__chart-version: %s" .Chart.Version }} +{{- end }} +{{ if .Chart.AppVersion -}} +{{ printf "__PREFIX__app-version: %s" .Chart.AppVersion }} +{{- end }} +{{- end -}} + +{{- define "__APP__.selectorLabels" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{ printf "__PREFIX__name: %s" $name }} +{{ printf "__PREFIX__instance: %s" .Release.Name }} +{{- end -}} diff --git a/generator/helper.go b/generator/helper.go new file mode 100644 index 0000000..51487af --- /dev/null +++ b/generator/helper.go @@ -0,0 +1,19 @@ +package generator + +import ( + _ "embed" + "strings" +) + +// helmHelper is a template for the _helpers.tpl file in the chart templates directory. +// +//go:embed helmHelper.tpl +var helmHelper string + +// Helper returns the _helpers.tpl file for a chart. +func Helper(name string) string { + helmHelper := strings.ReplaceAll(helmHelper, "__APP__", name) + helmHelper = strings.ReplaceAll(helmHelper, "__PREFIX__", KATENARY_PREFIX) + helmHelper = strings.ReplaceAll(helmHelper, "__VERSION__", "0.1.0") + return helmHelper +} diff --git a/generator/ingress.go b/generator/ingress.go new file mode 100644 index 0000000..569dc93 --- /dev/null +++ b/generator/ingress.go @@ -0,0 +1,175 @@ +package generator + +import ( + "katenary/utils" + "log" + "strings" + + "github.com/compose-spec/compose-go/types" + goyaml "gopkg.in/yaml.v3" + networkv1 "k8s.io/api/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +var _ Yaml = (*Ingress)(nil) + +type Ingress struct { + *networkv1.Ingress + service *types.ServiceConfig `yaml:"-"` +} + +// NewIngress creates a new Ingress from a compose service. +func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { + + appName := Chart.Name + + // parse the KATENARY_PREFIX/ingress label from the service + if service.Labels == nil { + service.Labels = make(map[string]string) + } + var label string + var ok bool + if label, ok = service.Labels[LABEL_INGRESS]; !ok { + return nil + } + + mapping := map[string]interface{}{ + "enabled": false, + "host": service.Name + ".tld", + "path": "/", + "class": "-", + } + if err := goyaml.Unmarshal([]byte(label), &mapping); err != nil { + log.Fatalf("Failed to parse ingress label: %s\n", err) + } + + // create the ingress + pathType := networkv1.PathTypeImplementationSpecific + serviceName := `{{ include "` + appName + `.fullname" . }}-` + service.Name + if v, ok := mapping["port"]; ok { + if port, ok := v.(int); ok { + mapping["port"] = int32(port) + } + } else { + log.Fatalf("No port provided for ingress target in service %s\n", service.Name) + } + + // Add the ingress host to the values.yaml + if Chart.Values[service.Name] == nil { + Chart.Values[service.Name] = &Value{} + } + Chart.Values[service.Name].(*Value).Ingress = &IngressValue{ + Enabled: mapping["enabled"].(bool), + Path: mapping["path"].(string), + Host: mapping["host"].(string), + Class: mapping["class"].(string), + Annotations: map[string]string{}, + } + + //ingressClassName := `{{ .Values.` + service.Name + `.ingress.class }}` + ingressClassName := utils.TplValue(service.Name, "ingress.class") + + servicePortName := utils.GetServiceNameByPort(int(mapping["port"].(int32))) + ingressService := &networkv1.IngressServiceBackend{ + Name: serviceName, + Port: networkv1.ServiceBackendPort{}, + } + if servicePortName != "" { + ingressService.Port.Name = servicePortName + } else { + ingressService.Port.Number = mapping["port"].(int32) + } + + ing := &Ingress{ + service: &service, + Ingress: &networkv1.Ingress{ + TypeMeta: metav1.TypeMeta{ + Kind: "Ingress", + APIVersion: "networking.k8s.io/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Spec: networkv1.IngressSpec{ + IngressClassName: &ingressClassName, + Rules: []networkv1.IngressRule{ + { + Host: utils.TplValue(service.Name, "ingress.host"), + IngressRuleValue: networkv1.IngressRuleValue{ + HTTP: &networkv1.HTTPIngressRuleValue{ + Paths: []networkv1.HTTPIngressPath{ + { + Path: utils.TplValue(service.Name, "ingress.path"), + PathType: &pathType, + Backend: networkv1.IngressBackend{ + Service: ingressService, + }, + }, + }, + }, + }, + }, + }, + TLS: []networkv1.IngressTLS{ + { + Hosts: []string{ + `{{ tpl .Values.` + service.Name + `.ingress.host . }}`, + }, + SecretName: `{{ include "` + appName + `.fullname" . }}-` + service.Name + `-tls`, + }, + }, + }, + }, + } + + return ing +} + +func (ingress *Ingress) Yaml() ([]byte, error) { + serviceName := ingress.service.Name + ret, err := yaml.Marshal(ingress) + if err != nil { + return nil, err + } + + lines := strings.Split(string(ret), "\n") + out := []string{ + `{{- if .Values.` + serviceName + `.ingress.enabled -}}`, + } + for _, line := range lines { + if strings.Contains(line, "loadBalancer: ") { + continue + } + + if strings.Contains(line, "labels:") { + // add annotations above labels from values.yaml + content := `` + + ` {{- if .Values.` + serviceName + `.ingress.annotations -}}` + "\n" + + ` {{- toYaml .Values.` + serviceName + `.ingress.annotations | nindent 4 }}` + "\n" + + ` {{- end }}` + "\n" + + line + + out = append(out, content) + } else if strings.Contains(line, "ingressClassName: ") { + content := utils.Wrap( + line, + `{{- if ne .Values.`+serviceName+`.ingress.class "-" }}`, + `{{- end }}`, + ) + out = append(out, content) + } else { + out = append(out, line) + } + } + out = append(out, `{{- end -}}`) + ret = []byte(strings.Join(out, "\n")) + return ret, nil + +} + +func (ingress *Ingress) Filename() string { + return ingress.service.Name + ".ingress.yaml" +} diff --git a/generator/katenaryLabels.go b/generator/katenaryLabels.go new file mode 100644 index 0000000..dbd41c0 --- /dev/null +++ b/generator/katenaryLabels.go @@ -0,0 +1,229 @@ +package generator + +import ( + "bytes" + _ "embed" + "fmt" + "katenary/utils" + "regexp" + "sort" + "strings" + "text/tabwriter" + "text/template" + + "sigs.k8s.io/yaml" +) + +var ( + // Set the documentation of labels here + // + //go:embed katenaryLabelsDoc.yaml + labelFullHelpYAML []byte + + // parsed yaml + labelFullHelp map[string]Help +) + +// Label is a katenary label to find in compose files. +type Label = string + +// Help is the documentation of a label. +type Help struct { + Short string `yaml:"short"` + Long string `yaml:"long"` + Example string `yaml:"example"` + Type string `yaml:"type"` +} + +const KATENARY_PREFIX = "katenary.v3/" + +// Known labels. +const ( + LABEL_MAIN_APP Label = KATENARY_PREFIX + "main-app" + LABEL_VALUES Label = KATENARY_PREFIX + "values" + LABEL_SECRETS Label = KATENARY_PREFIX + "secrets" + LABEL_PORTS Label = KATENARY_PREFIX + "ports" + LABEL_INGRESS Label = KATENARY_PREFIX + "ingress" + LABEL_MAP_ENV Label = KATENARY_PREFIX + "map-env" + LABEL_HEALTHCHECK Label = KATENARY_PREFIX + "health-check" + LABEL_SAME_POD Label = KATENARY_PREFIX + "same-pod" + LABEL_DESCRIPTION Label = KATENARY_PREFIX + "description" + LABEL_IGNORE Label = KATENARY_PREFIX + "ignore" + LABEL_DEPENDENCIES Label = KATENARY_PREFIX + "dependencies" + LABEL_CM_FILES Label = KATENARY_PREFIX + "configmap-files" + LABEL_CRONJOB Label = KATENARY_PREFIX + "cronjob" + LABEL_ENV_FROM Label = KATENARY_PREFIX + "env-from" +) + +func init() { + if err := yaml.Unmarshal(labelFullHelpYAML, &labelFullHelp); err != nil { + panic(err) + } +} + +// Generate the help for the labels. +func GetLabelHelp(asMarkdown bool) string { + names := GetLabelNames() // sorted + if !asMarkdown { + return generatePlainHelp(names) + } + return generateMarkdownHelp(names) +} + +func generatePlainHelp(names []string) string { + var builder strings.Builder + for _, name := range names { + help := labelFullHelp[name] + fmt.Fprintf(&builder, "%s%s:\t%s\t%s\n", KATENARY_PREFIX, name, help.Type, help.Short) + } + + // use tabwriter to align the help text + buf := new(strings.Builder) + w := tabwriter.NewWriter(buf, 0, 8, 0, '\t', tabwriter.AlignRight) + fmt.Fprintln(w, builder.String()) + w.Flush() + + head := "To get more information about a label, use `katenary help-label \ne.g. katenary help-label dependencies\n\n" + return head + buf.String() +} + +func generateMarkdownHelp(names []string) string { + var builder strings.Builder + var maxNameLength, maxDescriptionLength, maxTypeLength int + + max := func(a, b int) int { + if a > b { + return a + } + return b + } + for _, name := range names { + help := labelFullHelp[name] + maxNameLength = max(maxNameLength, len(name)+2+len(KATENARY_PREFIX)) + maxDescriptionLength = max(maxDescriptionLength, len(help.Short)) + maxTypeLength = max(maxTypeLength, len(help.Type)) + } + + fmt.Fprintf(&builder, "%s\n", generateTableHeader(maxNameLength, maxDescriptionLength, maxTypeLength)) + fmt.Fprintf(&builder, "%s\n", generateTableHeaderSeparator(maxNameLength, maxDescriptionLength, maxTypeLength)) + + for _, name := range names { + help := labelFullHelp[name] + fmt.Fprintf(&builder, "| %-*s | %-*s | %-*s |\n", + maxNameLength, "`"+KATENARY_PREFIX+name+"`", // enclose in backticks + maxDescriptionLength, help.Short, + maxTypeLength, help.Type, + ) + } + + return builder.String() +} + +func generateTableHeader(maxNameLength, maxDescriptionLength, maxTypeLength int) string { + return fmt.Sprintf( + "| %-*s | %-*s | %-*s |", + maxNameLength, "Label name", + maxDescriptionLength, "Description", + maxTypeLength, "Type", + ) +} + +func generateTableHeaderSeparator(maxNameLength, maxDescriptionLength, maxTypeLength int) string { + return fmt.Sprintf( + "| %s | %s | %s |", + strings.Repeat("-", maxNameLength), + strings.Repeat("-", maxDescriptionLength), + strings.Repeat("-", maxTypeLength), + ) +} + +// GetLabelHelpFor returns the help for a specific label. +func GetLabelHelpFor(labelname string, asMarkdown bool) string { + + help, ok := labelFullHelp[labelname] + if !ok { + return "No help available for " + labelname + "." + } + + help.Long = strings.TrimPrefix(help.Long, "\n") + help.Example = strings.TrimPrefix(help.Example, "\n") + help.Short = strings.TrimPrefix(help.Short, "\n") + + // get help template + helpTemplate := getHelpTemplate(asMarkdown) + + if asMarkdown { + // enclose templates in backticks + help.Long = regexp.MustCompile(`\{\{(.*?)\}\}`).ReplaceAllString(help.Long, "`{{$1}}`") + help.Long = strings.ReplaceAll(help.Long, "__APP__", "`__APP__`") + } else { + help.Long = strings.ReplaceAll(help.Long, " \n", "\n") + help.Long = strings.ReplaceAll(help.Long, "`", "") + help.Long = strings.ReplaceAll(help.Long, "", "") + help.Long = strings.ReplaceAll(help.Long, "", "") + help.Long = utils.WordWrap(help.Long, 80) + } + + var buf bytes.Buffer + template.Must(template.New("shorthelp").Parse(help.Long)).Execute(&buf, struct { + KATENARY_PREFIX string + }{ + KATENARY_PREFIX: KATENARY_PREFIX, + }) + help.Long = buf.String() + buf.Reset() + + template.Must(template.New("example").Parse(help.Example)).Execute(&buf, struct { + KATENARY_PREFIX string + }{ + KATENARY_PREFIX: KATENARY_PREFIX, + }) + help.Example = buf.String() + buf.Reset() + + template.Must(template.New("complete").Parse(helpTemplate)).Execute(&buf, struct { + Name string + Help Help + KATENARY_PREFIX string + }{ + Name: labelname, + Help: help, + KATENARY_PREFIX: KATENARY_PREFIX, + }) + + return buf.String() +} + +// GetLabelNames returns a sorted list of all katenary label names. +func GetLabelNames() []string { + var names []string + for name := range labelFullHelp { + names = append(names, name) + } + sort.Strings(names) + return names +} + +func getHelpTemplate(asMarkdown bool) string { + if asMarkdown { + return `## {{ .KATENARY_PREFIX }}{{ .Name }} + +{{ .Help.Short }} + +**Type**: ` + "`" + `{{ .Help.Type }}` + "`" + ` + +{{ .Help.Long }} + +**Example:**` + "\n\n```yaml\n" + `{{ .Help.Example }}` + "\n```\n" + } + + return `{{ .KATENARY_PREFIX }}{{ .Name }}: {{ .Help.Short }} +Type: {{ .Help.Type }} + +{{ .Help.Long }} + +Example: +{{ .Help.Example }} +` + +} diff --git a/generator/labels.go b/generator/labels.go new file mode 100644 index 0000000..bbf06e5 --- /dev/null +++ b/generator/labels.go @@ -0,0 +1,36 @@ +package generator + +import ( + "fmt" +) + +// LabelType identifies the type of label to generate in objects. +// TODO: is this still needed? +type LabelType uint8 + +const ( + DeploymentLabel LabelType = iota + ServiceLabel +) + +func GetLabels(serviceName, appName string) map[string]string { + labels := map[string]string{ + KATENARY_PREFIX + "component": serviceName, + } + + key := `{{- include "%s.labels" . | nindent __indent__ }}` + labels[`__replace_`+serviceName] = fmt.Sprintf(key, appName) + + return labels +} + +func GetMatchLabels(serviceName, appName string) map[string]string { + labels := map[string]string{ + KATENARY_PREFIX + "component": serviceName, + } + + key := `{{- include "%s.selectorLabels" . | nindent __indent__ }}` + labels[`__replace_`+serviceName] = fmt.Sprintf(key, appName) + + return labels +} diff --git a/generator/main.go b/generator/main.go deleted file mode 100644 index dd09adc..0000000 --- a/generator/main.go +++ /dev/null @@ -1,304 +0,0 @@ -package generator - -import ( - "fmt" - "io/ioutil" - "katenary/helm" - "katenary/logger" - "katenary/tools" - "log" - "net/url" - "os" - "path/filepath" - "runtime" - "strconv" - "strings" - "sync" - - "github.com/compose-spec/compose-go/types" -) - -type EnvVal = helm.EnvValue - -const ( - ICON_PACKAGE = "📦" - ICON_SERVICE = "🔌" - ICON_SECRET = "🔏" - ICON_CONF = "📝" - ICON_STORE = "⚡" - ICON_INGRESS = "🌐" - ICON_RBAC = "🔑" - ICON_CRON = "🕒" -) - -var ( - EmptyDirs = []string{} - servicesMap = make(map[string]int) - locker = &sync.Mutex{} - - dependScript = ` -OK=0 -echo "Checking __service__ port" -while [ $OK != 1 ]; do - echo -n "." - nc -z ` + helm.ReleaseNameTpl + `-__service__ __port__ 2>&1 >/dev/null && OK=1 || sleep 1 -done -echo -echo "Done" -` - - madeDeployments = make(map[string]helm.Deployment, 0) -) - -// Create a Deployment for a given compose.Service. It returns a list chan -// of HelmFileGenerator which will be used to generate the files (deployment, secrets, configMap...). -func CreateReplicaObject(name string, s types.ServiceConfig, linked map[string]types.ServiceConfig) HelmFileGenerator { - ret := make(chan HelmFile, runtime.NumCPU()) - // there is a bug woth typs.ServiceConfig if we use the pointer. So we need to dereference it. - go buildDeployment(name, &s, linked, ret) - return ret -} - -// Create a service (k8s). -func generateServicesAndIngresses(name string, s *types.ServiceConfig) []HelmFile { - - ret := make([]HelmFile, 0) // can handle helm.Service or helm.Ingress - logger.Magenta(ICON_SERVICE+" Generating service for ", name) - ks := helm.NewService(name) - - for _, p := range s.Ports { - target := int(p.Target) - ks.Spec.Ports = append(ks.Spec.Ports, helm.NewServicePort(target, target)) - } - ks.Spec.Selector = buildSelector(name, s) - - ret = append(ret, ks) - if v, ok := s.Labels[helm.LABEL_INGRESS]; ok { - port, err := strconv.Atoi(v) - if err != nil { - log.Fatalf("The given port \"%v\" as ingress port in \"%s\" service is not an integer\n", v, name) - } - logger.Cyanf(ICON_INGRESS+" Create an ingress for port %d on %s service\n", port, name) - ing := createIngress(name, port, s) - ret = append(ret, ing) - } - - if len(s.Expose) > 0 { - logger.Magenta(ICON_SERVICE+" Generating service for ", name+"-external") - ks := helm.NewService(name + "-external") - ks.Spec.Type = "NodePort" - for _, expose := range s.Expose { - - p, _ := strconv.Atoi(expose) - ks.Spec.Ports = append(ks.Spec.Ports, helm.NewServicePort(p, p)) - } - ks.Spec.Selector = buildSelector(name, s) - ret = append(ret, ks) - } - - return ret -} - -// Create an ingress. -func createIngress(name string, port int, s *types.ServiceConfig) *helm.Ingress { - ingress := helm.NewIngress(name) - - annotations := map[string]string{} - ingressVal := map[string]interface{}{ - "class": "nginx", - "host": name + "." + helm.Appname + ".tld", - "enabled": false, - "annotations": annotations, - } - - // add Annotations in values - AddValues(name, map[string]EnvVal{"ingress": ingressVal}) - - ingress.Spec.Rules = []helm.IngressRule{ - { - Host: fmt.Sprintf("{{ .Values.%s.ingress.host }}", name), - Http: helm.IngressHttp{ - Paths: []helm.IngressPath{{ - Path: "/", - PathType: "Prefix", - Backend: &helm.IngressBackend{ - Service: helm.IngressService{ - Name: helm.ReleaseNameTpl + "-" + name, - Port: map[string]interface{}{ - "number": port, - }, - }, - }, - }}, - }, - }, - } - ingress.SetIngressClass(name) - - return ingress -} - -// Build the selector for the service. -func buildSelector(name string, s *types.ServiceConfig) map[string]string { - return map[string]string{ - "katenary.io/component": name, - "katenary.io/release": helm.ReleaseNameTpl, - } -} - -// buildConfigMapFromPath generates a ConfigMap from a path. -func buildConfigMapFromPath(name, path string) *helm.ConfigMap { - stat, err := os.Stat(path) - if err != nil { - return nil - } - - files := make(map[string]string, 0) - if stat.IsDir() { - found, _ := filepath.Glob(path + "/*") - for _, f := range found { - if s, err := os.Stat(f); err != nil || s.IsDir() { - if err != nil { - fmt.Fprintf(os.Stderr, "An error occured reading volume path %s\n", err.Error()) - } else { - logger.ActivateColors = true - logger.Yellowf("Warning, %s is a directory, at this time we only "+ - "can create configmap for first level file list\n", f) - logger.ActivateColors = false - } - continue - } - _, filename := filepath.Split(f) - c, _ := ioutil.ReadFile(f) - files[filename] = string(c) - } - } else { - c, _ := ioutil.ReadFile(path) - _, filename := filepath.Split(path) - files[filename] = string(c) - } - - cm := helm.NewConfigMap(name, tools.GetRelPath(path)) - cm.Data = files - return cm -} - -// prepareProbes generate http/tcp/command probes for a service. -func prepareProbes(name string, s *types.ServiceConfig, container *helm.Container) { - // first, check if there a label for the probe - if check, ok := s.Labels[helm.LABEL_HEALTHCHECK]; ok { - check = strings.TrimSpace(check) - p := helm.NewProbeFromService(s) - // get the port of the "url" check - if checkurl, err := url.Parse(check); err == nil { - if err == nil { - container.LivenessProbe = buildProtoProbe(p, checkurl) - } - } else { - // it's a command - container.LivenessProbe = p - container.LivenessProbe.Exec = &helm.Exec{ - Command: []string{ - "sh", - "-c", - check, - }, - } - } - return // label overrides everything - } - - // if not, we will use the default one - if s.HealthCheck != nil { - container.LivenessProbe = buildCommandProbe(s) - } -} - -// buildProtoProbe builds a probe from a url that can be http or tcp. -func buildProtoProbe(probe *helm.Probe, u *url.URL) *helm.Probe { - port, err := strconv.Atoi(u.Port()) - if err != nil { - port = 80 - } - - path := "/" - if u.Path != "" { - path = u.Path - } - - switch u.Scheme { - case "http", "https": - probe.HttpGet = &helm.HttpGet{ - Path: path, - Port: port, - } - case "tcp": - probe.TCP = &helm.TCP{ - Port: port, - } - default: - logger.Redf("Error while parsing healthcheck url %s\n", u.String()) - os.Exit(1) - } - return probe -} - -func buildCommandProbe(s *types.ServiceConfig) *helm.Probe { - - // Get the first element of the command from ServiceConfig - first := s.HealthCheck.Test[0] - - p := helm.NewProbeFromService(s) - switch first { - case "CMD", "CMD-SHELL": - // CMD or CMD-SHELL - p.Exec = &helm.Exec{ - Command: s.HealthCheck.Test[1:], - } - return p - default: - // badly made but it should work... - p.Exec = &helm.Exec{ - Command: []string(s.HealthCheck.Test), - } - return p - } -} - -func setSecretVar(name string, s *types.ServiceConfig, c *helm.Container) *helm.Secret { - // get the list of secret vars - secretvars, ok := s.Labels[helm.LABEL_SECRETVARS] - if !ok { - return nil - } - - store := helm.NewSecret(name, "") - for _, secretvar := range strings.Split(secretvars, ",") { - secretvar = strings.TrimSpace(secretvar) - // get the value from env - _, ok := s.Environment[secretvar] - if !ok { - continue - } - // add the secret - store.AddEnv(secretvar, ".Values."+name+".environment."+secretvar) - AddEnvironment(name, secretvar, *s.Environment[secretvar]) - - // Finally remove the secret var from the environment on the service - // and the helm container definition. - defer func(secretvar string) { // defered because AddEnvironment locks the memory - locker.Lock() - defer locker.Unlock() - - for i, env := range c.Env { - if env.Name == secretvar { - c.Env = append(c.Env[:i], c.Env[i+1:]...) - i-- - } - } - - delete(s.Environment, secretvar) - }(secretvar) - } - return store -} diff --git a/generator/main_test.go b/generator/main_test.go deleted file mode 100644 index c90ca39..0000000 --- a/generator/main_test.go +++ /dev/null @@ -1,397 +0,0 @@ -package generator - -import ( - "io/ioutil" - "katenary/compose" - "katenary/helm" - "katenary/logger" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/compose-spec/compose-go/cli" -) - -const DOCKER_COMPOSE_YML = `version: '3' -services: - # first service, very simple - http: - image: nginx - ports: - - "80:80" - - # second service, with environment variables - http2: - image: nginx - environment: - SOME_ENV_VAR: some_value - ANOTHER_ENV_VAR: another_value - - # third service with ingress label - web: - image: nginx - ports: - - "80:80" - labels: - katenary.io/ingress: 80 - - web2: - image: nginx - command: ["/bin/sh", "-c", "while true; do echo hello; sleep 1; done"] - - # fourth service is a php service depending on database - php: - image: php:7.2-apache - depends_on: - - database - environment: - SOME_ENV_VAR: some_value - ANOTHER_ENV_VAR: another_value - DB_HOST: database - labels: - katenary.io/mapenv: | - DB_HOST: {{ .Release.Name }}-database - - database: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: database - MYSQL_USER: user - MYSQL_PASSWORD: password - volumes: - - data:/var/lib/mysql - labels: - katenary.io/ports: 3306 - - - # try to deploy 2 services but one is in the same pod than the other - http3: - image: nginx - - http4: - image: nginx - labels: - katenary.io/same-pod: http3 - - # unmapped volumes - novol: - image: nginx - volumes: - - /tmp/data - labels: - katenary.io/ports: 80 - - # use = sign for environment variables - eqenv: - image: nginx - environment: - - SOME_ENV_VAR=some_value - - ANOTHER_ENV_VAR=another_value - - # use environment file - useenvfile: - image: nginx - env_file: - - config/env - -volumes: - data: -` - -var defaultCliFiles = cli.DefaultFileNames -var TMP_DIR = "" -var TMPWORK_DIR = "" - -func init() { - logger.NOLOG = len(os.Getenv("NOLOG")) < 1 -} - -func setUp(t *testing.T) (string, *compose.Parser) { - - // cleanup "made" files - helm.ResetMadePVC() - - cli.DefaultFileNames = defaultCliFiles - - // create a temporary directory - tmp, err := os.MkdirTemp(os.TempDir(), "katenary-test-") - if err != nil { - t.Fatal(err) - } - - tmpwork, err := os.MkdirTemp(os.TempDir(), "katenary-test-work-") - if err != nil { - t.Fatal(err) - } - - composefile := filepath.Join(tmpwork, "docker-compose.yaml") - p := compose.NewParser([]string{composefile}, DOCKER_COMPOSE_YML) - - // create envfile for "useenvfile" service - err = os.Mkdir(filepath.Join(tmpwork, "config"), 0777) - if err != nil { - t.Fatal(err) - } - envfile := filepath.Join(tmpwork, "config", "env") - fp, err := os.Create(envfile) - if err != nil { - t.Fatal("MKFILE", err) - } - fp.WriteString("FILEENV1=some_value\n") - fp.WriteString("FILEENV2=another_value\n") - fp.Close() - - TMP_DIR = tmp - TMPWORK_DIR = tmpwork - - p.Parse("testapp") - - Generate(p, "test-0", "testapp", "1.2.3", "4.5.6", DOCKER_COMPOSE_YML, tmp) - - return tmp, p -} - -func tearDown() { - if len(TMP_DIR) > 0 { - os.RemoveAll(TMP_DIR) - } - if len(TMPWORK_DIR) > 0 { - os.RemoveAll(TMPWORK_DIR) - } -} - -// Check if the web2 service has got a command. -func TestCommand(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - if name == "web2" { - // Ensure that the command is correctly set - // The command should be a string array - path := filepath.Join(tmp, "templates", name+".deployment.yaml") - path = filepath.Join(tmp, "templates", name+".deployment.yaml") - fp, _ := os.Open(path) - defer fp.Close() - lines, _ := ioutil.ReadAll(fp) - next := false - commands := make([]string, 0) - for _, line := range strings.Split(string(lines), "\n") { - if strings.Contains(line, "command") { - next = true - continue - } - if next { - commands = append(commands, line) - } - } - ok := 0 - for _, command := range commands { - if strings.Contains(command, "- /bin/sh") { - ok++ - } - if strings.Contains(command, "- -c") { - ok++ - } - if strings.Contains(command, "while true; do") { - ok++ - } - } - if ok != 3 { - t.Error("Command is not correctly set") - } - } - } -} - -// Check if environment is correctly set. -func TestEnvs(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - - if name == "php" { - // the "DB_HOST" environment variable inside the template must be set to '{{ .Release.Name }}-database' - path := filepath.Join(tmp, "templates", name+".deployment.yaml") - // read the file and find the DB_HOST variable - matched := false - fp, _ := os.Open(path) - defer fp.Close() - lines, _ := ioutil.ReadAll(fp) - next := false - for _, line := range strings.Split(string(lines), "\n") { - if !next && strings.Contains(line, "name: DB_HOST") { - next = true - continue - } else if next && strings.Contains(line, "value:") { - matched = true - if !strings.Contains(line, "{{ tpl .Values.php.environment.DB_HOST . }}") { - t.Error("DB_HOST variable should be set to {{ tpl .Values.php.environment.DB_HOST . }}", line, string(lines)) - } - break - } - } - if !matched { - t.Error("DB_HOST variable not found in ", path) - t.Log(string(lines)) - } - } - } -} - -// Check if the same pod is not deployed twice. -func TestSamePod(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - path := filepath.Join(tmp, "templates", name+".deployment.yaml") - - if _, found := service.Labels[helm.LABEL_SAMEPOD]; found { - // fail if the service has a deployment - if _, err := os.Stat(path); err == nil { - t.Error("Service ", name, " should not have a deployment") - } - continue - } - - // others should have a deployment file - t.Log("Checking ", name, " deployment file") - _, err := os.Stat(path) - if err != nil { - t.Fatal(err) - } - } -} - -// Check if the ports are correctly set. -func TestPorts(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - path := "" - - // if the service has a port found in helm.LABEL_PORT or ports, so the service file should exist - hasPort := false - if _, found := service.Labels[helm.LABEL_PORT]; found { - hasPort = true - } - if service.Ports != nil { - hasPort = true - } - if hasPort { - path = filepath.Join(tmp, "templates", name+".service.yaml") - t.Log("Checking ", name, " service file") - _, err := os.Stat(path) - if err != nil { - t.Error(err) - } - } - } -} - -// Check if the volumes are correctly set. -func TestPVC(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - path := filepath.Join(tmp, "templates", name+"-data.pvc.yaml") - - // the "database" service should have a pvc file in templates (name-data.pvc.yaml) - if name == "database" { - path = filepath.Join(tmp, "templates", name+"-data.pvc.yaml") - t.Log("Checking ", name, " pvc file") - _, err := os.Stat(path) - if err != nil { - list, _ := filepath.Glob(tmp + "/templates/*") - t.Log(list) - t.Fatal(err) - } - } - } -} - -//Check if web service has got a ingress. -func TestIngress(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - path := filepath.Join(tmp, "templates", name+".ingress.yaml") - - // the "web" service should have a ingress file in templates (name.ingress.yaml) - if name == "web" { - path = filepath.Join(tmp, "templates", name+".ingress.yaml") - t.Log("Checking ", name, " ingress file") - _, err := os.Stat(path) - if err != nil { - t.Fatal(err) - } - } - } -} - -// Check unmapped volumes -func TestUnmappedVolumes(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - for _, service := range p.Data.Services { - name := service.Name - if name == "novol" { - path := filepath.Join(tmp, "templates", name+".deployment.yaml") - fp, _ := os.Open(path) - defer fp.Close() - lines, _ := ioutil.ReadAll(fp) - for _, line := range strings.Split(string(lines), "\n") { - if strings.Contains(line, "novol-data") { - t.Error("novol service should not have a volume") - } - } - } - } -} - -// Check if service using equal sign for environment works -func TestEqualSignOnEnv(t *testing.T) { - tmp, p := setUp(t) - defer tearDown() - - // if the name is eqenv, the service should habe environment - for _, service := range p.Data.Services { - name := service.Name - if name == "eqenv" { - path := filepath.Join(tmp, "templates", name+".deployment.yaml") - fp, _ := os.Open(path) - defer fp.Close() - lines, _ := ioutil.ReadAll(fp) - match := 0 - for _, line := range strings.Split(string(lines), "\n") { - // we must find the line with the environment variable name - if strings.Contains(line, "SOME_ENV_VAR") { - // we must find the line with the environment variable value - match++ - } - if strings.Contains(line, "ANOTHER_ENV_VAR") { - // we must find the line with the environment variable value - match++ - } - } - if match != 4 { // because the value points on .Values... - t.Error("eqenv service should have 2 environment variables") - t.Log(string(lines)) - } - } - } -} diff --git a/generator/rbac.go b/generator/rbac.go new file mode 100644 index 0000000..8d0df76 --- /dev/null +++ b/generator/rbac.go @@ -0,0 +1,139 @@ +package generator + +import ( + "katenary/utils" + + "github.com/compose-spec/compose-go/types" + corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +var ( + _ Yaml = (*RoleBinding)(nil) + _ Yaml = (*Role)(nil) + _ Yaml = (*ServiceAccount)(nil) +) + +// RBAC is a kubernetes RBAC containing a role, a rolebinding and an associated serviceaccount. +type RBAC struct { + RoleBinding *RoleBinding + Role *Role + ServiceAccount *ServiceAccount +} + +// NewRBAC creates a new RBAC from a compose service. The appName is the name of the application taken from the project name. +func NewRBAC(service types.ServiceConfig, appName string) *RBAC { + role := &Role{ + Role: &rbacv1.Role{ + TypeMeta: metav1.TypeMeta{ + Kind: "Role", + APIVersion: "rbac.authorization.k8s.io/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Rules: []rbacv1.PolicyRule{ + { + APIGroups: []string{"", "extensions", "apps"}, + Resources: []string{"*"}, + Verbs: []string{"*"}, + }, + }, + }, + service: &service, + } + + rolebinding := &RoleBinding{ + RoleBinding: &rbacv1.RoleBinding{ + TypeMeta: metav1.TypeMeta{ + Kind: "RoleBinding", + APIVersion: "rbac.authorization.k8s.io/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Subjects: []rbacv1.Subject{ + { + Kind: "ServiceAccount", + Name: utils.TplName(service.Name, appName), + Namespace: "{{ .Release.Namespace }}", + }, + }, + RoleRef: rbacv1.RoleRef{ + Kind: "Role", + Name: utils.TplName(service.Name, appName), + APIGroup: "rbac.authorization.k8s.io", + }, + }, + service: &service, + } + + serviceaccount := &ServiceAccount{ + ServiceAccount: &corev1.ServiceAccount{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceAccount", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + }, + service: &service, + } + + return &RBAC{ + RoleBinding: rolebinding, + Role: role, + ServiceAccount: serviceaccount, + } +} + +// RoleBinding is a kubernetes RoleBinding. +type RoleBinding struct { + *rbacv1.RoleBinding + service *types.ServiceConfig +} + +func (r *RoleBinding) Yaml() ([]byte, error) { + return yaml.Marshal(r) +} + +func (r *RoleBinding) Filename() string { + return r.service.Name + ".rolebinding.yaml" +} + +// Role is a kubernetes Role. +type Role struct { + *rbacv1.Role + service *types.ServiceConfig +} + +func (r *Role) Yaml() ([]byte, error) { + return yaml.Marshal(r) +} + +func (r *Role) Filename() string { + return r.service.Name + ".role.yaml" +} + +// ServiceAccount is a kubernetes ServiceAccount. +type ServiceAccount struct { + *corev1.ServiceAccount + service *types.ServiceConfig +} + +func (r *ServiceAccount) Yaml() ([]byte, error) { + return yaml.Marshal(r) +} + +func (r *ServiceAccount) Filename() string { + return r.service.Name + ".serviceaccount.yaml" +} diff --git a/generator/secret.go b/generator/secret.go new file mode 100644 index 0000000..be98fed --- /dev/null +++ b/generator/secret.go @@ -0,0 +1,111 @@ +package generator + +import ( + "encoding/base64" + "fmt" + "katenary/utils" + "strings" + + "github.com/compose-spec/compose-go/types" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +var _ DataMap = (*Secret)(nil) +var _ Yaml = (*Secret)(nil) + +// Secret is a kubernetes Secret. +// +// Implements the DataMap interface. +type Secret struct { + *corev1.Secret + service types.ServiceConfig `yaml:"-"` +} + +// NewSecret creates a new Secret from a compose service +func NewSecret(service types.ServiceConfig, appName string) *Secret { + secret := &Secret{ + service: service, + Secret: &corev1.Secret{ + TypeMeta: metav1.TypeMeta{ + Kind: "Secret", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Data: make(map[string][]byte), + }, + } + + // check if the value should be in values.yaml + valueList := []string{} + varDescriptons := utils.GetValuesFromLabel(service, LABEL_VALUES) + for value := range varDescriptons { + valueList = append(valueList, value) + } + + // wrap values with quotes + for _, value := range service.Environment { + if value == nil { + continue + } + *value = fmt.Sprintf(`"%s"`, *value) + } + + for _, value := range valueList { + if val, ok := service.Environment[value]; ok { + value = strings.TrimPrefix(value, `"`) + *val = `.Values.` + service.Name + `.environment.` + value + } + } + + for key, value := range service.Environment { + if value == nil { + continue + } + secret.AddData(key, *value) + } + + return secret +} + +// SetData sets the data of the secret. +func (s *Secret) SetData(data map[string]string) { + for key, value := range data { + s.AddData(key, fmt.Sprintf("%s", value)) + } + +} + +// AddData adds a key value pair to the secret. +func (s *Secret) AddData(key string, value string) { + if value == "" { + return + } + s.Data[key] = []byte(`{{ tpl ` + value + ` $ | quote | b64enc }}`) +} + +// Yaml returns the yaml representation of the secret. +func (s *Secret) Yaml() ([]byte, error) { + y, err := yaml.Marshal(s) + if err != nil { + return nil, err + } + + // replace the b64 value by the real value + for _, value := range s.Data { + encoded := base64.StdEncoding.EncodeToString([]byte(value)) + y = []byte(strings.ReplaceAll(string(y), encoded, string(value))) + } + + return y, nil +} + +// Filename returns the filename of the secret. +func (s *Secret) Filename() string { + return s.service.Name + ".secret.yaml" +} diff --git a/generator/service.go b/generator/service.go new file mode 100644 index 0000000..90c1422 --- /dev/null +++ b/generator/service.go @@ -0,0 +1,95 @@ +package generator + +import ( + "katenary/utils" + "regexp" + "strings" + + "github.com/compose-spec/compose-go/types" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + "sigs.k8s.io/yaml" +) + +var _ Yaml = (*Service)(nil) + +// Service is a kubernetes Service. +type Service struct { + *v1.Service `yaml:",inline"` + service *types.ServiceConfig `yaml:"-"` +} + +// NewService creates a new Service from a compose service. +func NewService(service types.ServiceConfig, appName string) *Service { + + ports := []v1.ServicePort{} + + s := &Service{ + service: &service, + Service: &v1.Service{ + TypeMeta: metav1.TypeMeta{ + Kind: "Service", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName), + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Spec: v1.ServiceSpec{ + Selector: GetMatchLabels(service.Name, appName), + Ports: ports, + }, + }, + } + for _, port := range service.Ports { + s.AddPort(port) + } + + return s +} + +// AddPort adds a port to the service. +func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) { + name := s.service.Name + if len(serviceName) > 0 { + name = serviceName[0] + } + + var finalport intstr.IntOrString + + if targetPort := utils.GetServiceNameByPort(int(port.Target)); targetPort == "" { + finalport = intstr.FromInt(int(port.Target)) + } else { + finalport = intstr.FromString(targetPort) + name = targetPort + } + + s.Spec.Ports = append(s.Spec.Ports, v1.ServicePort{ + Protocol: v1.ProtocolTCP, + Port: int32(port.Target), + TargetPort: finalport, + Name: name, + }) +} + +// Yaml returns the yaml representation of the service. +func (s *Service) Yaml() ([]byte, error) { + y, err := yaml.Marshal(s) + lines := []string{} + for _, line := range strings.Split(string(y), "\n") { + if regexp.MustCompile(`^\s*loadBalancer:\s*`).MatchString(line) { + continue + } + lines = append(lines, line) + } + y = []byte(strings.Join(lines, "\n")) + + return y, err +} + +// Filename returns the filename of the service. +func (s *Service) Filename() string { + return s.service.Name + ".service.yaml" +} diff --git a/generator/types.go b/generator/types.go new file mode 100644 index 0000000..1699242 --- /dev/null +++ b/generator/types.go @@ -0,0 +1,13 @@ +package generator + +// DataMap is a kubernetes ConfigMap or Secret. It can be used to add data to the ConfigMap or Secret. +type DataMap interface { + SetData(map[string]string) + AddData(string, string) +} + +// Yaml is a kubernetes object that can be converted to yaml. +type Yaml interface { + Yaml() ([]byte, error) + Filename() string +} diff --git a/generator/values.go b/generator/values.go index 02b2861..055a6b3 100644 --- a/generator/values.go +++ b/generator/values.go @@ -1,77 +1,121 @@ package generator import ( - "katenary/helm" "strings" "github.com/compose-spec/compose-go/types" ) -var ( - // Values is kept in memory to create a values.yaml file. - Values = make(map[string]map[string]interface{}) -) +// Values is a map of all values for all services. Written to values.yaml. +// var Values = map[string]any{} -// AddValues adds values to the values.yaml map. -func AddValues(servicename string, values map[string]EnvVal) { - locker.Lock() - defer locker.Unlock() - - if _, ok := Values[servicename]; !ok { - Values[servicename] = make(map[string]interface{}) - } - - for k, v := range values { - Values[servicename][k] = v - } +// RepositoryValue is a docker repository image and tag that will be saved in values.yaml. +type RepositoryValue struct { + Image string `yaml:"image"` + Tag string `yaml:"tag"` } -func AddEnvironment(servicename string, key string, val EnvVal) { - locker.Lock() - defer locker.Unlock() - - if _, ok := Values[servicename]; !ok { - Values[servicename] = make(map[string]interface{}) - } - - if _, ok := Values[servicename]["environment"]; !ok { - Values[servicename]["environment"] = make(map[string]EnvVal) - } - Values[servicename]["environment"].(map[string]EnvVal)[key] = val - +// PersistenceValue is a persistence configuration that will be saved in values.yaml. +type PersistenceValue struct { + Enabled bool `yaml:"enabled"` + StorageClass string `yaml:"storageClass"` + Size string `yaml:"size"` + AccessMode []string `yaml:"accessMode"` } -// setEnvToValues will set the environment variables to the values.yaml map. -func setEnvToValues(name string, s *types.ServiceConfig, c *helm.Container) { - // crete the "environment" key +// IngressValue is a ingress configuration that will be saved in values.yaml. +type IngressValue struct { + Enabled bool `yaml:"enabled"` + Host string `yaml:"host"` + Path string `yaml:"path"` + Class string `yaml:"class"` + Annotations map[string]string `yaml:"annotations"` +} - env := make(map[string]EnvVal) - for k, v := range s.Environment { - env[k] = v - } - if len(env) == 0 { - return +// Value will be saved in values.yaml. It contains configuraiton for all deployment and services. +// The content will be lile: +// +// name_of_component: +// repository: +// image: image_name +// tag: image_tag +// persistence: +// enabled: true +// storageClass: storage_class_name +// ingress: +// enabled: true +// host: host_name +// path: path_name +// environment: +// ENV_VAR_1: value_1 +// ENV_VAR_2: value_2 +type Value struct { + Repository *RepositoryValue `yaml:"repository,omitempty"` + Persistence map[string]*PersistenceValue `yaml:"persistence,omitempty"` + Ingress *IngressValue `yaml:"ingress,omitempty"` + ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"` + Environment map[string]any `yaml:"environment,omitempty"` + Replicas *uint32 `yaml:"replicas,omitempty"` + CronJob *CronJobValue `yaml:"cronjob,omitempty"` +} + +// CronJobValue is a cronjob configuration that will be saved in values.yaml. +type CronJobValue struct { + Repository *RepositoryValue `yaml:"repository,omitempty"` + Environment map[string]any `yaml:"environment,omitempty"` + ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"` + Schedule string `yaml:"schedule"` +} + +// NewValue creates a new Value from a compose service. +// The value contains the necessary information to deploy the service (image, tag, replicas, etc.). +// +// If `main` is true, the tag will be empty because +// it will be set in the helm chart appVersion. +func NewValue(service types.ServiceConfig, main ...bool) *Value { + replicas := uint32(1) + v := &Value{ + Replicas: &replicas, } - for k, v := range env { - k = strings.ReplaceAll(k, ".", "_") - AddEnvironment(name, k, v) + // find the image tag + tag := "" + split := strings.Split(service.Image, ":") + v.Repository = &RepositoryValue{ + Image: split[0], } - //AddValues(name, map[string]EnvVal{"environment": valuesEnv}) - for k := range env { - fixedK := strings.ReplaceAll(k, ".", "_") - v := "{{ tpl .Values." + name + ".environment." + fixedK + " . }}" - s.Environment[k] = &v - touched := false - for _, c := range c.Env { - if c.Name == k { - c.Value = v - touched = true - } - } - if !touched { - c.Env = append(c.Env, &helm.Value{Name: k, Value: v}) + // for main service, the tag should the appVersion. So here we set it to empty. + if len(main) > 0 && !main[0] { + if len(split) > 1 { + tag = split[1] } + v.Repository.Tag = tag + } else { + v.Repository.Tag = "" + } + + return v +} + +// AddPersistence adds persistence configuration to the Value. +func (v *Value) AddPersistence(volumeName string) { + if v.Persistence == nil { + v.Persistence = make(map[string]*PersistenceValue, 0) + } + v.Persistence[volumeName] = &PersistenceValue{ + Enabled: true, + StorageClass: "-", + Size: "1Gi", + AccessMode: []string{"ReadWriteOnce"}, + } +} + +func (v *Value) AddIngress(host, path string) { + v.Ingress = &IngressValue{ + Enabled: true, + Host: host, + Path: path, + Class: "-", } } diff --git a/generator/version.go b/generator/version.go new file mode 100644 index 0000000..9602118 --- /dev/null +++ b/generator/version.go @@ -0,0 +1,4 @@ +package generator + +// Version is the version of katenary. It is set at compile time. +var Version = "master" // changed at compile time diff --git a/generator/volume.go b/generator/volume.go new file mode 100644 index 0000000..8269b2c --- /dev/null +++ b/generator/volume.go @@ -0,0 +1,119 @@ +package generator + +import ( + "katenary/utils" + "strings" + + "github.com/compose-spec/compose-go/types" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +var _ Yaml = (*VolumeClaim)(nil) + +// VolumeClaim is a kubernetes VolumeClaim. This is a PersistentVolumeClaim. +type VolumeClaim struct { + *v1.PersistentVolumeClaim + service *types.ServiceConfig `yaml:"-"` + volumeName string + nameOverride string +} + +// NewVolumeClaim creates a new VolumeClaim from a compose service. +func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim { + return &VolumeClaim{ + volumeName: volumeName, + service: &service, + PersistentVolumeClaim: &v1.PersistentVolumeClaim{ + TypeMeta: metav1.TypeMeta{ + Kind: "PersistentVolumeClaim", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: utils.TplName(service.Name, appName) + "-" + volumeName, + Labels: GetLabels(service.Name, appName), + Annotations: Annotations, + }, + Spec: v1.PersistentVolumeClaimSpec{ + AccessModes: []v1.PersistentVolumeAccessMode{ + v1.ReadWriteOnce, + }, + StorageClassName: utils.StrPtr(`{{ .Values.` + service.Name + `.persistence.` + volumeName + `.storageClass }}`), + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + v1.ResourceStorage: resource.MustParse("1Gi"), + }, + }, + }, + }, + } +} + +// Yaml marshals a VolumeClaim into yaml. +func (v *VolumeClaim) Yaml() ([]byte, error) { + serviceName := v.service.Name + if v.nameOverride != "" { + serviceName = v.nameOverride + } + volumeName := v.volumeName + out, err := yaml.Marshal(v) + + if err != nil { + return nil, err + } + + // replace 1Gi to {{ .Values.serviceName.volume.size }} + out = []byte( + strings.Replace( + string(out), + "1Gi", + utils.TplValue(serviceName, "persistence."+volumeName+".size"), + 1, + ), + ) + + out = []byte( + strings.Replace( + string(out), + "- ReadWriteOnce", + "{{- .Values."+ + serviceName+ + ".persistence."+ + volumeName+ + ".accessMode | toYaml | nindent __indent__ }}", + 1, + ), + ) + + lines := strings.Split(string(out), "\n") + for i, line := range lines { + if strings.Contains(line, "storageClass") { + lines[i] = utils.Wrap( + line, + "{{- if ne .Values."+serviceName+".persistence."+volumeName+".storageClass \"-\" }}", + "{{- end }}", + ) + } + } + out = []byte(strings.Join(lines, "\n")) + + // add condition + out = []byte( + "{{- if .Values." + + serviceName + + ".persistence." + + volumeName + + ".enabled }}\n" + + string(out) + + "\n{{- end }}", + ) + + return out, nil +} + +// Filename returns the suggested filename for a VolumeClaim. +func (v *VolumeClaim) Filename() string { + return v.service.Name + "." + v.volumeName + ".volumeclaim.yaml" +} diff --git a/generator/volumes.go b/generator/volumes.go deleted file mode 100644 index 28975ce..0000000 --- a/generator/volumes.go +++ /dev/null @@ -1,236 +0,0 @@ -package generator - -import ( - "katenary/helm" - "katenary/logger" - "katenary/tools" - "os" - "path/filepath" - "strings" - - "github.com/compose-spec/compose-go/types" - "gopkg.in/yaml.v3" -) - -var ( - // VolumeValues is the map of volumes for each deployment - // containing volume configuration - VolumeValues = make(map[string]map[string]map[string]EnvVal) -) - -// AddVolumeValues add a volume to the values.yaml map for the given deployment name. -func AddVolumeValues(deployment string, volname string, values map[string]EnvVal) { - locker.Lock() - defer locker.Unlock() - - if _, ok := VolumeValues[deployment]; !ok { - VolumeValues[deployment] = make(map[string]map[string]EnvVal) - } - VolumeValues[deployment][volname] = values -} - -// addVolumeFrom takes the LABEL_VOLUMEFROM to get volumes from another container. This can only work with -// container that has got LABEL_SAMEPOD as we need to get the volumes from another container in the same deployment. -func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *types.ServiceConfig) { - labelfrom, ok := s.Labels[helm.LABEL_VOLUMEFROM] - if !ok { - return - } - - // decode Yaml from the label - var volumesFrom map[string]map[string]string - err := yaml.Unmarshal([]byte(labelfrom), &volumesFrom) - if err != nil { - logger.ActivateColors = true - logger.Red(err.Error()) - logger.ActivateColors = false - return - } - - // for each declared volume "from", we will find it from the deployment volumes and add it to the container. - // Then, to avoid duplicates, we will remove it from the ServiceConfig object. - for name, volumes := range volumesFrom { - for volumeName := range volumes { - initianame := volumeName - volumeName = tools.PathToName(volumeName) - // get the volume from the deployment container "name" - var ctn *helm.Container - for _, c := range deployment.Spec.Template.Spec.Containers { - if c.Name == name { - ctn = c - break - } - } - if ctn == nil { - logger.ActivateColors = true - logger.Redf("VolumeFrom: container %s not found", name) - logger.ActivateColors = false - continue - } - // get the volume from the container - for _, v := range ctn.VolumeMounts { - switch v := v.(type) { - case map[string]interface{}: - if v["name"] == volumeName { - if container.VolumeMounts == nil { - container.VolumeMounts = make([]interface{}, 0) - } - // make a copy of the volume mount and then add it to the VolumeMounts - var mountpoint = make(map[string]interface{}) - for k, v := range v { - mountpoint[k] = v - } - container.VolumeMounts = append(container.VolumeMounts, mountpoint) - - // remove the volume from the ServiceConfig - for i, vol := range s.Volumes { - if vol.Source == initianame { - s.Volumes = append(s.Volumes[:i], s.Volumes[i+1:]...) - i-- - break - } - } - } - } - } - } - } -} - -// prepareVolumes add the volumes of a service. -func prepareVolumes( - deployment, name string, - s *types.ServiceConfig, - container *helm.Container, - fileGeneratorChan HelmFileGenerator) []map[string]interface{} { - - volumes := make([]map[string]interface{}, 0) - mountPoints := make([]interface{}, 0) - configMapsVolumes := make([]string, 0) - if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok { - configMapsVolumes = strings.Split(v, ",") - for i, cm := range configMapsVolumes { - configMapsVolumes[i] = strings.TrimSpace(cm) - } - } - - for _, vol := range s.Volumes { - - volname := vol.Source - volepath := vol.Target - - if volname == "" { - logger.ActivateColors = true - logger.Yellowf("Warning, volume source to %s is empty for %s -- skipping\n", volepath, name) - logger.ActivateColors = false - continue - } - - isConfigMap := false - for _, cmVol := range configMapsVolumes { - if tools.GetRelPath(volname) == cmVol { - isConfigMap = true - break - } - } - - // local volume cannt be mounted - if !isConfigMap && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) { - logger.ActivateColors = true - logger.Redf("You cannot, at this time, have local volume in %s deployment\n", name) - logger.ActivateColors = false - continue - } - if isConfigMap { - // check if the volname path points on a file, if so, we need to add subvolume to the interface - stat, err := os.Stat(volname) - if err != nil { - logger.ActivateColors = true - logger.Redf("An error occured reading volume path %s\n", err.Error()) - logger.ActivateColors = false - continue - } - pointToFile := "" - if !stat.IsDir() { - pointToFile = filepath.Base(volname) - } - - // the volume is a path and it's explicitally asked to be a configmap in labels - cm := buildConfigMapFromPath(name, volname) - cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + tools.PathToName(volname) - - // build a configmapRef for this volume - volname := tools.PathToName(volname) - volumes = append(volumes, map[string]interface{}{ - "name": volname, - "configMap": map[string]string{ - "name": cm.K8sBase.Metadata.Name, - }, - }) - if len(pointToFile) > 0 { - mountPoints = append(mountPoints, map[string]interface{}{ - "name": volname, - "mountPath": volepath, - "subPath": pointToFile, - }) - } else { - mountPoints = append(mountPoints, map[string]interface{}{ - "name": volname, - "mountPath": volepath, - }) - } - if cm != nil { - fileGeneratorChan <- cm - } - } else { - // It's a Volume. Mount this from PVC to declare. - - volname = strings.ReplaceAll(volname, "-", "") - - isEmptyDir := false - for _, v := range EmptyDirs { - v = strings.ReplaceAll(v, "-", "") - if v == volname { - volumes = append(volumes, map[string]interface{}{ - "name": volname, - "emptyDir": map[string]string{}, - }) - mountPoints = append(mountPoints, map[string]interface{}{ - "name": volname, - "mountPath": volepath, - }) - container.VolumeMounts = append(container.VolumeMounts, mountPoints...) - isEmptyDir = true - break - } - } - if isEmptyDir { - continue - } - - volumes = append(volumes, map[string]interface{}{ - "name": volname, - "persistentVolumeClaim": map[string]string{ - "claimName": helm.ReleaseNameTpl + "-" + volname, - }, - }) - mountPoints = append(mountPoints, map[string]interface{}{ - "name": volname, - "mountPath": volepath, - }) - - logger.Yellow(ICON_STORE+" Generate volume values", volname, "for container named", name, "in deployment", deployment) - AddVolumeValues(deployment, volname, map[string]EnvVal{ - "enabled": false, - "capacity": "1Gi", - }) - - if pvc := helm.NewPVC(deployment, volname); pvc != nil { - fileGeneratorChan <- pvc - } - } - } - // add the volume in the container and return the volume definition to add in Deployment - container.VolumeMounts = append(container.VolumeMounts, mountPoints...) - return volumes -} diff --git a/generator/writer.go b/generator/writer.go deleted file mode 100644 index d7de1ce..0000000 --- a/generator/writer.go +++ /dev/null @@ -1,236 +0,0 @@ -package generator - -import ( - "katenary/compose" - "katenary/generator/writers" - "katenary/helm" - "katenary/tools" - "log" - "os" - "path/filepath" - "regexp" - "strconv" - "strings" - "time" - - "github.com/compose-spec/compose-go/types" - "gopkg.in/yaml.v3" -) - -// HelmFile represents a helm file from helm package that has got some necessary methods -// to generate a helm file. -type HelmFile interface { - GetType() string - GetPathRessource() string -} - -// HelmFileGenerator is a chanel of HelmFile. -type HelmFileGenerator chan HelmFile - -var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`) - -func portExists(port int, ports []types.ServicePortConfig) bool { - for _, p := range ports { - if p.Target == uint32(port) { - log.Println("portExists:", port, p.Target) - return true - } - } - return false -} - -// Generate get a parsed compose file, and generate the helm files. -func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVersion, composeFile, dirName string) { - - // make the appname global (yes... ugly but easy) - helm.Appname = appName - helm.Version = katernayVersion - templatesDir := filepath.Join(dirName, "templates") - - // try to create the directory - err := os.MkdirAll(templatesDir, 0755) - if err != nil { - log.Fatal(err) - } - - generators := make(map[string]HelmFileGenerator) - - // remove skipped services from the parsed data - for i, service := range p.Data.Services { - if v, ok := service.Labels[helm.LABEL_IGNORE]; !ok || v != "true" { - continue - } - p.Data.Services = append(p.Data.Services[:i], p.Data.Services[i+1:]...) - i-- - - // find this service in others as "depends_on" and remove it - for _, service2 := range p.Data.Services { - delete(service2.DependsOn, service.Name) - } - } - - for i, service := range p.Data.Services { - n := service.Name - - // if the service port is declared in labels, add it to the service. - if ports, ok := service.Labels[helm.LABEL_PORT]; ok { - if service.Ports == nil { - service.Ports = make([]types.ServicePortConfig, 0) - } - for _, port := range strings.Split(ports, ",") { - port = strings.TrimSpace(port) - target, err := strconv.Atoi(port) - if err != nil { - log.Fatal(err) - } - if portExists(target, service.Ports) { - continue - } - service.Ports = append(service.Ports, types.ServicePortConfig{ - Target: uint32(target), - }) - } - } - // find port and store it in servicesMap - for _, port := range service.Ports { - target := int(port.Target) - if target != 0 { - servicesMap[n] = target - break - } - } - - // manage emptyDir volumes - if empty, ok := service.Labels[helm.LABEL_EMPTYDIRS]; ok { - //split empty list by coma - emptyDirs := strings.Split(empty, ",") - for i, emptyDir := range emptyDirs { - emptyDirs[i] = strings.TrimSpace(emptyDir) - } - //append them in EmptyDirs - EmptyDirs = append(EmptyDirs, emptyDirs...) - } - p.Data.Services[i] = service - - } - - // for all services in linked map, and not in samePods map, generate the service - for _, s := range p.Data.Services { - name := s.Name - - // do not make a deployment for services declared to be in the same pod than another - if _, ok := s.Labels[helm.LABEL_SAMEPOD]; ok { - continue - } - - // find services that is in the same pod - linked := make(map[string]types.ServiceConfig, 0) - for _, service := range p.Data.Services { - n := service.Name - if linkname, ok := service.Labels[helm.LABEL_SAMEPOD]; ok && linkname == name { - linked[n] = service - delete(s.DependsOn, n) - } - } - - generators[name] = CreateReplicaObject(name, s, linked) - } - - // to generate notes, we need to keep an Ingresses list - ingresses := make(map[string]*helm.Ingress) - - for n, generator := range generators { // generators is a map : name -> generator - for helmFile := range generator { // generator is a chan - if helmFile == nil { // generator finished - break - } - kind := helmFile.(helm.Kinded).Get() - kind = strings.ToLower(kind) - - // Add a SHA inside the generated file, it's only - // to make it easy to check it the compose file corresponds to the - // generated helm chart - helmFile.(helm.Signable).BuildSHA(composeFile) - - // Some types need special fixes in yaml generation - switch c := helmFile.(type) { - case *helm.Storage: - // For storage, we need to add a "condition" to activate it - writers.BuildStorage(c, n, templatesDir) - - case *helm.Deployment: - // for the deployment, we need to fix persitence volumes - // to be activated only when the storage is "enabled", - // either we use an "emptyDir" - writers.BuildDeployment(c, n, templatesDir) - - case *helm.Service: - // Change the type for service if it's an "exposed" port - writers.BuildService(c, n, templatesDir) - - case *helm.Ingress: - // we need to make ingresses "activable" from values - ingresses[n] = c // keep it to generate notes - writers.BuildIngress(c, n, templatesDir) - - case *helm.ConfigMap, *helm.Secret: - // there could be several files, so let's force the filename - name := c.(helm.Named).Name() + "." + c.GetType() - suffix := c.GetPathRessource() - suffix = tools.PathToName(suffix) - name += suffix - name = PrefixRE.ReplaceAllString(name, "") - writers.BuildConfigMap(c, kind, n, name, templatesDir) - - default: - name := c.(helm.Named).Name() + "." + c.GetType() - name = PrefixRE.ReplaceAllString(name, "") - fname := filepath.Join(templatesDir, name+".yaml") - fp, err := os.Create(fname) - if err != nil { - log.Fatal(err) - } - defer fp.Close() - enc := yaml.NewEncoder(fp) - enc.SetIndent(writers.IndentSize) - enc.Encode(c) - } - } - } - // Create the values.yaml file - valueFile, err := os.Create(filepath.Join(dirName, "values.yaml")) - if err != nil { - log.Fatal(err) - } - defer valueFile.Close() - enc := yaml.NewEncoder(valueFile) - enc.SetIndent(writers.IndentSize) - enc.Encode(Values) - - // Create tht Chart.yaml file - chartFile, err := os.Create(filepath.Join(dirName, "Chart.yaml")) - if err != nil { - log.Fatal(err) - } - defer chartFile.Close() - chartFile.WriteString(`# Create on ` + time.Now().Format(time.RFC3339) + "\n") - chartFile.WriteString(`# Katenary command line: ` + strings.Join(os.Args, " ") + "\n") - enc = yaml.NewEncoder(chartFile) - enc.SetIndent(writers.IndentSize) - enc.Encode(map[string]interface{}{ - "apiVersion": "v2", - "name": appName, - "description": "A helm chart for " + appName, - "type": "application", - "version": chartVersion, - "appVersion": appVersion, - }) - - // And finally, create a NOTE.txt file - noteFile, err := os.Create(filepath.Join(templatesDir, "NOTES.txt")) - if err != nil { - log.Fatal(err) - } - defer noteFile.Close() - noteFile.WriteString(helm.GenerateNotesFile(ingresses)) -} diff --git a/generator/writers/configmap.go b/generator/writers/configmap.go deleted file mode 100644 index d045f1d..0000000 --- a/generator/writers/configmap.go +++ /dev/null @@ -1,18 +0,0 @@ -package writers - -import ( - "os" - "path/filepath" - - "gopkg.in/yaml.v3" -) - -// BuildConfigMap writes the configMap. -func BuildConfigMap(c interface{}, kind, servicename, name, templatesDir string) { - fname := filepath.Join(templatesDir, name+"."+kind+".yaml") - fp, _ := os.Create(fname) - enc := yaml.NewEncoder(fp) - enc.SetIndent(IndentSize) - enc.Encode(c) - fp.Close() -} diff --git a/generator/writers/deployment.go b/generator/writers/deployment.go deleted file mode 100644 index 7f594ca..0000000 --- a/generator/writers/deployment.go +++ /dev/null @@ -1,44 +0,0 @@ -package writers - -import ( - "bytes" - "katenary/helm" - "os" - "path/filepath" - "strings" - - "gopkg.in/yaml.v3" -) - -// BuildDeployment builds a deployment. -func BuildDeployment(deployment *helm.Deployment, name, templatesDir string) { - kind := "deployment" - fname := filepath.Join(templatesDir, name+"."+kind+".yaml") - fp, _ := os.Create(fname) - buffer := bytes.NewBuffer(nil) - enc := yaml.NewEncoder(buffer) - enc.SetIndent(IndentSize) - enc.Encode(deployment) - _content := string(buffer.Bytes()) - content := strings.Split(string(_content), "\n") - dataname := "" - component := deployment.Spec.Selector["matchLabels"].(map[string]string)[helm.K+"/component"] - n := 0 // will be count of lines only on "persistentVolumeClaim" line, to indent "else" and "end" at the right place - for _, line := range content { - if strings.Contains(line, "name:") { - dataname = strings.Split(line, ":")[1] - dataname = strings.TrimSpace(dataname) - } else if strings.Contains(line, "persistentVolumeClaim") { - n = CountSpaces(line) - line = strings.Repeat(" ", n) + "{{- if .Values." + component + ".persistence." + dataname + ".enabled }}\n" + line - } else if strings.Contains(line, "claimName") { - spaces := strings.Repeat(" ", n) - line += "\n" + spaces + "{{ else }}" - line += "\n" + spaces + "emptyDir: {}" - line += "\n" + spaces + "{{- end }}" - } - fp.WriteString(line + "\n") - } - fp.Close() - -} diff --git a/generator/writers/ingress.go b/generator/writers/ingress.go deleted file mode 100644 index fbfdc60..0000000 --- a/generator/writers/ingress.go +++ /dev/null @@ -1,101 +0,0 @@ -package writers - -import ( - "bytes" - "katenary/helm" - "os" - "path/filepath" - "strings" - - "gopkg.in/yaml.v3" -) - -const ( - classAndVersionCondition = `{{- if and .Values.__name__.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}` + "\n" - versionCondition118 = `{{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }}` + "\n" - versionCondition119 = `{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }}` + "\n" - apiVersion = `{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1 -{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1beta1 -{{- else -}} -apiVersion: extensions/v1beta1 -{{- end }}` -) - -// BuildIngress generates the ingress yaml file with conditions. -func BuildIngress(ingress *helm.Ingress, name, templatesDir string) { - // Set the backend for 1.18 - for _, b := range ingress.Spec.Rules { - for _, p := range b.Http.Paths { - p.Backend.ServiceName = p.Backend.Service.Name - if n, ok := p.Backend.Service.Port["number"]; ok { - p.Backend.ServicePort = n - } - } - } - kind := "ingress" - buffer := bytes.NewBuffer(nil) - fname := filepath.Join(templatesDir, name+"."+kind+".yaml") - enc := yaml.NewEncoder(buffer) - enc.SetIndent(IndentSize) - buffer.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n") - enc.Encode(ingress) - buffer.WriteString("{{- end -}}") - - fp, err := os.Create(fname) - if err != nil { - panic(err) - } - defer fp.Close() - - content := string(buffer.Bytes()) - lines := strings.Split(content, "\n") - - backendHit := false - for _, l := range lines { - // apiVersion is a pain... - if strings.Contains(l, "apiVersion:") { - l = apiVersion - } - - // add annotations linked to the Values - if strings.Contains(l, "annotations:") { - n := CountSpaces(l) + IndentSize - l += "\n" + strings.Repeat(" ", n) + "{{- range $k, $v := .Values.__name__.ingress.annotations }}\n" - l += strings.Repeat(" ", n) + "{{ $k }}: {{ $v }}\n" - l += strings.Repeat(" ", n) + "{{- end }}" - l = strings.ReplaceAll(l, "__name__", name) - } - - // pathTyype is ony for 1.19+ - if strings.Contains(l, "pathType:") { - n := CountSpaces(l) - l = strings.Repeat(" ", n) + versionCondition118 + - l + "\n" + - strings.Repeat(" ", n) + "{{- end }}" - } - - if strings.Contains(l, "ingressClassName") { - // should be set only if the version of Kubernetes is 1.18-0 or higher - cond := strings.ReplaceAll(classAndVersionCondition, "__name__", name) - l = ` ` + cond + l + "\n" + ` {{- end }}` - } - - // manage the backend format following the Kubernetes 1.19-0 version or higher - if strings.Contains(l, "service:") { - n := CountSpaces(l) - l = strings.Repeat(" ", n) + versionCondition119 + l - } - if strings.Contains(l, "serviceName:") || strings.Contains(l, "servicePort:") { - n := CountSpaces(l) - if !backendHit { - l = strings.Repeat(" ", n) + "{{- else }}\n" + l - } else { - l = l + "\n" + strings.Repeat(" ", n) + "{{- end }}\n" - } - backendHit = true - } - fp.WriteString(l + "\n") - } -} diff --git a/generator/writers/service.go b/generator/writers/service.go deleted file mode 100644 index c898e27..0000000 --- a/generator/writers/service.go +++ /dev/null @@ -1,24 +0,0 @@ -package writers - -import ( - "katenary/helm" - "os" - "path/filepath" - - "gopkg.in/yaml.v3" -) - -// BuildService writes the service (external or not). -func BuildService(service *helm.Service, name, templatesDir string) { - kind := "service" - suffix := "" - if service.Spec.Type == "NodePort" { - suffix = "-external" - } - fname := filepath.Join(templatesDir, name+suffix+"."+kind+".yaml") - fp, _ := os.Create(fname) - enc := yaml.NewEncoder(fp) - enc.SetIndent(IndentSize) - enc.Encode(service) - fp.Close() -} diff --git a/generator/writers/storage.go b/generator/writers/storage.go deleted file mode 100644 index 2201c01..0000000 --- a/generator/writers/storage.go +++ /dev/null @@ -1,32 +0,0 @@ -package writers - -import ( - "katenary/helm" - "log" - "os" - "path/filepath" - - "gopkg.in/yaml.v3" -) - -// BuildStorage writes the persistentVolumeClaim. -func BuildStorage(storage *helm.Storage, name, templatesDir string) { - kind := "pvc" - name = storage.Metadata.Labels[helm.K+"/component"] - pvcname := storage.Metadata.Labels[helm.K+"/pvc-name"] - fname := filepath.Join(templatesDir, name+"-"+pvcname+"."+kind+".yaml") - fp, err := os.Create(fname) - if err != nil { - log.Fatal(err) - } - defer fp.Close() - volname := storage.K8sBase.Metadata.Labels[helm.K+"/pvc-name"] - - fp.WriteString("{{ if .Values." + name + ".persistence." + volname + ".enabled }}\n") - enc := yaml.NewEncoder(fp) - enc.SetIndent(IndentSize) - if err := enc.Encode(storage); err != nil { - log.Fatal(err) - } - fp.WriteString("{{- end -}}") -} diff --git a/generator/writers/utils.go b/generator/writers/utils.go deleted file mode 100644 index 5bfa607..0000000 --- a/generator/writers/utils.go +++ /dev/null @@ -1,17 +0,0 @@ -package writers - -// IndentSize set the indentation size for yaml output. Could ba changed by command line argument. -var IndentSize = 2 - -// CountSpaces returns the number of spaces from the begining of the line. -func CountSpaces(line string) int { - var spaces int - for _, char := range line { - if char == ' ' { - spaces++ - } else { - break - } - } - return spaces -} diff --git a/go.mod b/go.mod index 326ba90..4af0223 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,48 @@ -module katenary +module katenary // github.com/metal3d/katenary -go 1.16 +go 1.20 require ( - github.com/alessio/shellescape v1.4.1 - github.com/compose-spec/compose-go v1.2.8 - github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 // indirect - github.com/kr/pretty v0.2.0 // indirect - github.com/spf13/cobra v1.5.0 - github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect - golang.org/x/mod v0.5.1 - golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + github.com/compose-spec/compose-go v1.14.0 + github.com/mitchellh/go-wordwrap v1.0.1 + github.com/spf13/cobra v1.7.0 + github.com/thediveo/netdb v1.0.2 + golang.org/x/mod v0.8.0 gopkg.in/yaml.v3 v3.0.1 + k8s.io/api v0.27.2 + k8s.io/apimachinery v0.27.2 + sigs.k8s.io/yaml v1.3.0 +) + +require ( + github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/go-logr/logr v1.2.3 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/imdario/mergo v0.3.15 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/mattn/go-shellwords v1.0.12 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect + github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect + github.com/xeipuuv/gojsonschema v1.2.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sync v0.2.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/text v0.9.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.90.1 // indirect + k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect ) diff --git a/go.sum b/go.sum index ae9b41e..86f3b95 100644 --- a/go.sum +++ b/go.sum @@ -1,191 +1,137 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= -github.com/Azure/azure-sdk-for-go v56.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= -github.com/Azure/go-autorest v10.8.1+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= -github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= -github.com/Azure/go-autorest/autorest v0.11.24/go.mod h1:G6kyRlFnTuSbEYkQGawPfsCswgme4iYf6rfSKUDzbCc= -github.com/Azure/go-autorest/autorest/adal v0.9.18/go.mod h1:XVVeme+LZwABT8K5Lc3hA4nAe8LDBVle26gTrguhhPQ= -github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74= -github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k= -github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcPR4o9jEImooCeWJcYV/zLE= -github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8= -github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= -github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= -github.com/aws/aws-sdk-go v1.43.16/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= -github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= -github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= -github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= -github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8= -github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50= -github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE= -github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/compose-spec/compose-go v1.2.8 h1:ImPy82xn+rJKL5xmgEyesZEfqJmrzJ1WuZSHEhxMEFI= -github.com/compose-spec/compose-go v1.2.8/go.mod h1:813WrDd7NtOl9ZVqswlJ5iCQy3lxI3KYxKkY8EeHQ7w= -github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= -github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= -github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/compose-spec/compose-go v1.14.0 h1:/+tQxBEPIrfsi87Qh7/VjMzcJN3BRNER/RO71ku+u6E= +github.com/compose-spec/compose-go v1.14.0/go.mod h1:m0o4G6MQDHjjz9rY7No9FpnNi+9sKic262rzrwuCqic= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0= -github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e/go.mod h1:xpWTC2KnJMiDLkoawhsPQcXjvwATEBcbq0xevG2YR9M= -github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 h1:KfVB1Z5fm10trO24Rn5Zzocd8sTm5k/gS24ijxQ1aJU= -github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414/go.mod h1:2oyLKljQFnsI1tzJxjUg4GI+HEpDfzFP3LrGM04rKg0= -github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= +github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa h1:L9Ay/slwQ4ERSPaurC+TVkZrM0K98GNrEEo1En3e8as= +github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa/go.mod h1:WHNsWjnIn2V1LYOrME7e8KxSeKunYHsxEm4am0BUtcI= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= -github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= -github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw= -github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= -github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE= -github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= -github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= -github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= -github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= -github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= -github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= -github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= +github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= +github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM= +github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= -github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= -github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= -github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= +github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU= +github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= +github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8AyFNU9d0= +github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo= +github.com/onsi/ginkgo/v2 v2.5.0/go.mod h1:Luc4sArBICYCS8THh8v3i3i5CuSZO+RaQRaJoeNwomw= +github.com/onsi/ginkgo/v2 v2.7.0/go.mod h1:yjiuMwPokqY1XauOgju45q3sJt6VzQ/Fict1LFVcsAo= +github.com/onsi/ginkgo/v2 v2.8.1/go.mod h1:N1/NbDngAFcSLdyZ+/aYTYGSlq9qMCS/cNKGJjy+csc= +github.com/onsi/ginkgo/v2 v2.9.0/go.mod h1:4xkjoL/tZv4SMWeww56BU5kAt19mVB47gTWxmrTcxyk= +github.com/onsi/ginkgo/v2 v2.9.1 h1:zie5Ly042PD3bsCvsSOPvRnFwyo3rKe64TJlD6nu0mk= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= +github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo= +github.com/onsi/gomega v1.21.1/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc= +github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM= +github.com/onsi/gomega v1.24.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg= +github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM= +github.com/onsi/gomega v1.26.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM= +github.com/onsi/gomega v1.27.1/go.mod h1:aHX5xOykVYzWOV4WqQy0sy8BQptgukenXpCXfadcIAw= +github.com/onsi/gomega v1.27.3/go.mod h1:5vG284IBtfDAmDyrK+eGyZmUgUlmi+Wngqo557cZ6Gw= +github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= -github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= -github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= -github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g= -github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= -github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= -github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= -github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= -github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= -github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= -github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= -github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= -github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= -github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/thediveo/netdb v1.0.2 h1:icuZWO8btuubgjFFFhxWmXALATlQO6bqEer7DPxRPco= +github.com/thediveo/netdb v1.0.2/go.mod h1:Mz/McdR84D8xUX7rWk0cRgNLrLvqfDPzTAQKUeCR0OY= +github.com/xanzy/go-gitlab v0.81.0/go.mod h1:VMbY3JIWdZ/ckvHbQqkyd3iYk2aViKrNIQ23IbFMQDo= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= @@ -193,111 +139,155 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= -github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs= -github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA= -github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg= -go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= -go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= -go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/cloud v0.0.0-20151119220103-975617b05ea8/go.mod h1:0H1ncTHf11KCFhTc/+EFRbzSCOZx+VUbRMk55Yv5MYk= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.29.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20141024133853-64131543e789/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= -gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo= -gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +k8s.io/api v0.27.2 h1:+H17AJpUMvl+clT+BPnKf0E3ksMAzoBBg7CntpSuADo= +k8s.io/api v0.27.2/go.mod h1:ENmbocXfBT2ADujUXcBhHV55RIT31IIEvkntP6vZKS4= +k8s.io/apimachinery v0.27.2 h1:vBjGaKKieaIreI+oQwELalVG4d8f3YAMNpWLzDXkxeg= +k8s.io/apimachinery v0.27.2/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E= +k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw= +k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= +k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 h1:kmDqav+P+/5e1i9tFfHq1qcF3sOrDp+YEkVDAHu7Jwk= +k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/helm/configAndSecretMap.go b/helm/configAndSecretMap.go deleted file mode 100644 index daba9ef..0000000 --- a/helm/configAndSecretMap.go +++ /dev/null @@ -1,155 +0,0 @@ -package helm - -import ( - "errors" - "fmt" - "io/ioutil" - "katenary/tools" - "strings" -) - -// InlineConfig is made to represent a configMap or a secret -type InlineConfig interface { - AddEnvFile(filename string, filter []string) error - AddEnv(key, val string) error - Metadata() *Metadata -} - -var _ InlineConfig = (*ConfigMap)(nil) -var _ InlineConfig = (*Secret)(nil) - -// ConfigMap is made to represent a configMap with data. -type ConfigMap struct { - *K8sBase `yaml:",inline"` - Data map[string]string `yaml:"data"` -} - -// NewConfigMap returns a new initialzed ConfigMap. -func NewConfigMap(name, path string) *ConfigMap { - base := NewBase() - base.ApiVersion = "v1" - base.Kind = "ConfigMap" - base.Metadata.Name = ReleaseNameTpl + "-" + name - base.Metadata.Labels[K+"/component"] = name - if path != "" { - base.Metadata.Labels[K+"/path"] = tools.PathToName(path) - } - return &ConfigMap{ - K8sBase: base, - Data: make(map[string]string), - } -} - -// Metadata returns the metadata of the configMap. -func (c *ConfigMap) Metadata() *Metadata { - return c.K8sBase.Metadata -} - -// AddEnvFile adds an environment file to the configMap. -func (c *ConfigMap) AddEnvFile(file string, filter []string) error { - content, err := ioutil.ReadFile(file) - if err != nil { - return err - } - - lines := strings.Split(string(content), "\n") - for _, l := range lines { - //Check if the line is a comment - l = strings.TrimSpace(l) - isComment := strings.HasPrefix(l, "#") - if len(l) == 0 || isComment { - continue - } - parts := strings.SplitN(l, "=", 2) - if len(parts) < 2 { - return errors.New("The environment file " + file + " is not valid") - } - - var skip bool - for _, filterEnv := range filter { - if parts[0] == filterEnv { - skip = true - } - } - if !skip { - // c.Data[parts[0]] = parts[1] - name := strings.ReplaceAll(c.Name(), ReleaseNameTpl+"-", "") - c.Data[parts[0]] = fmt.Sprintf("{{ tpl .Values.%s.environment.%s .}}", name, parts[0]) - } - } - return nil -} - -func (c *ConfigMap) AddEnv(key, val string) error { - c.Data[key] = val - return nil -} - -// Secret is made to represent a secret with data. -type Secret struct { - *K8sBase `yaml:",inline"` - Data map[string]string `yaml:"data"` -} - -// NewSecret returns a new initialzed Secret. -func NewSecret(name, path string) *Secret { - base := NewBase() - base.ApiVersion = "v1" - base.Kind = "Secret" - base.Metadata.Name = ReleaseNameTpl + "-" + name - base.Metadata.Labels[K+"/component"] = name - if path != "" { - base.Metadata.Labels[K+"/path"] = tools.PathToName(path) - } - return &Secret{ - K8sBase: base, - Data: make(map[string]string), - } -} - -// AddEnvFile adds an environment file to the secret. -func (s *Secret) AddEnvFile(file string, filter []string) error { - content, err := ioutil.ReadFile(file) - if err != nil { - return err - } - - lines := strings.Split(string(content), "\n") - for _, l := range lines { - l = strings.TrimSpace(l) - isComment := strings.HasPrefix(l, "#") - if len(l) == 0 || isComment { - continue - } - parts := strings.SplitN(l, "=", 2) - if len(parts) < 2 { - return errors.New("The environment file " + file + " is not valid") - } - - var skip bool - for _, filterEnv := range filter { - if parts[0] == filterEnv { - skip = true - } - } - if !skip { - //s.Data[parts[0]] = fmt.Sprintf(`{{ "%s" | b64enc }}`, parts[1]) - name := strings.ReplaceAll(s.Name(), ReleaseNameTpl+"-", "") - s.Data[parts[0]] = fmt.Sprintf("{{ tpl .Values.%s.environment.%s . | b64enc }}", name, parts[0]) - } - } - - return nil - -} - -// Metadata returns the metadata of the secret. -func (s *Secret) Metadata() *Metadata { - return s.K8sBase.Metadata -} - -// AddEnv adds an environment variable to the secret. -func (s *Secret) AddEnv(key, val string) error { - s.Data[key] = fmt.Sprintf(`{{ %s | b64enc }}`, val) - return nil -} diff --git a/helm/container.go b/helm/container.go deleted file mode 100644 index 05441fa..0000000 --- a/helm/container.go +++ /dev/null @@ -1,65 +0,0 @@ -package helm - -import ( - "katenary/logger" - "strings" - - "github.com/compose-spec/compose-go/types" -) - -type EnvValue interface{} - -// ContainerPort represent a port mapping. -type ContainerPort struct { - Name string - ContainerPort int `yaml:"containerPort"` -} - -// Value represent a environment variable with name and value. -type Value struct { - Name string `yaml:"name"` - Value EnvValue `yaml:"value"` -} - -// Container represent a container with name, image, and environment variables. It is used in Deployment. -type Container struct { - Name string `yaml:"name,omitempty"` - Image string `yaml:"image"` - Ports []*ContainerPort `yaml:"ports,omitempty"` - Env []*Value `yaml:"env,omitempty"` - EnvFrom []map[string]map[string]string `yaml:"envFrom,omitempty"` - Command []string `yaml:"command,omitempty"` - VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"` - LivenessProbe *Probe `yaml:"livenessProbe,omitempty"` -} - -// NewContainer creates a new container with name, image, labels and environment variables. -func NewContainer(name, image string, environment types.MappingWithEquals, labels map[string]string) *Container { - container := &Container{ - Image: image, - Name: name, - EnvFrom: make([]map[string]map[string]string, 0), - } - - // find bound environment variable to a service - toServices := make([]string, 0) - if bound, ok := labels[LABEL_ENV_SERVICE]; ok { - toServices = strings.Split(bound, ",") - } - if len(toServices) > 0 { - // warn, it's deprecated now - logger.ActivateColors = true - logger.Yellowf( - "[deprecated] in \"%s\" service: label %s is deprecated and **ignored**, please use %s instead\n"+ - "e.g.\n"+ - " labels:\n"+ - " FOO: {{ .Release.Name }}-fooservice\n", - name, - LABEL_ENV_SERVICE, - LABEL_MAP_ENV, - ) - logger.ActivateColors = false - } - - return container -} diff --git a/helm/cronTab.go b/helm/cronTab.go deleted file mode 100644 index ff1e454..0000000 --- a/helm/cronTab.go +++ /dev/null @@ -1,70 +0,0 @@ -package helm - -type CronTab struct { - *K8sBase `yaml:",inline"` - Spec CronSpec `yaml:"spec"` -} -type CronSpec struct { - Schedule string `yaml:"schedule"` - JobTemplate JobTemplate `yaml:"jobTemplate"` - SuccessfulJobsHistoryLimit int `yaml:"successfulJobsHistoryLimit"` - FailedJobsHistoryLimit int `yaml:"failedJobsHistoryLimit"` - ConcurrencyPolicy string `yaml:"concurrencyPolicy"` -} -type JobTemplate struct { - Spec JobSpecDescription `yaml:"spec"` -} - -type JobSpecDescription struct { - Template JobSpecTemplate `yaml:"template"` -} - -type JobSpecTemplate struct { - Metadata Metadata `yaml:"metadata"` - Spec Job `yaml:"spec"` -} - -type Job struct { - ServiceAccount string `yaml:"serviceAccount,omitempty"` - ServiceAccountName string `yaml:"serviceAccountName,omitempty"` - Containers []Container `yaml:"containers"` - RestartPolicy string `yaml:"restartPolicy,omitempty"` -} - -func NewCrontab(name, image, command, schedule string, serviceAccount *ServiceAccount) *CronTab { - cron := &CronTab{ - K8sBase: NewBase(), - } - cron.K8sBase.ApiVersion = "batch/v1" - cron.K8sBase.Kind = "CronJob" - - cron.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name - cron.K8sBase.Metadata.Labels[K+"/component"] = name - cron.Spec.Schedule = schedule - cron.Spec.SuccessfulJobsHistoryLimit = 3 - cron.Spec.FailedJobsHistoryLimit = 3 - cron.Spec.ConcurrencyPolicy = "Forbid" - cron.Spec.JobTemplate.Spec.Template.Metadata = Metadata{ - Labels: cron.K8sBase.Metadata.Labels, - } - cron.Spec.JobTemplate.Spec.Template.Spec = Job{ - ServiceAccount: serviceAccount.Name(), - ServiceAccountName: serviceAccount.Name(), - RestartPolicy: "OnFailure", - } - if command != "" { - cron.AddCommand(command, image, name) - } - - return cron -} - -// AddCommand adds a command to the cron job -func (c *CronTab) AddCommand(command, image, name string) { - container := Container{ - Name: name, - Image: image, - Command: []string{"sh", "-c", command}, - } - c.Spec.JobTemplate.Spec.Template.Spec.Containers = append(c.Spec.JobTemplate.Spec.Template.Spec.Containers, container) -} diff --git a/helm/deployment.go b/helm/deployment.go deleted file mode 100644 index 649d1db..0000000 --- a/helm/deployment.go +++ /dev/null @@ -1,47 +0,0 @@ -package helm - -// Deployment is a k8s deployment. -type Deployment struct { - *K8sBase `yaml:",inline"` - Spec *DepSpec `yaml:"spec"` -} - -func NewDeployment(name string) *Deployment { - d := &Deployment{K8sBase: NewBase(), Spec: NewDepSpec()} - d.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name - d.K8sBase.ApiVersion = "apps/v1" - d.K8sBase.Kind = "Deployment" - d.K8sBase.Metadata.Labels[K+"/component"] = name - d.K8sBase.Metadata.Labels[K+"/resource"] = "deployment" - return d -} - -type DepSpec struct { - Replicas int `yaml:"replicas"` - Selector map[string]interface{} `yaml:"selector"` - Template PodTemplate `yaml:"template"` -} - -func NewDepSpec() *DepSpec { - return &DepSpec{ - Replicas: 1, - Template: PodTemplate{ - Metadata: Metadata{ - Labels: map[string]string{ - K + "/resource": "deployment", - }, - }, - }, - } -} - -type PodSpec struct { - InitContainers []*Container `yaml:"initContainers,omitempty"` - Containers []*Container `yaml:"containers"` - Volumes []map[string]interface{} `yaml:"volumes,omitempty"` -} - -type PodTemplate struct { - Metadata Metadata `yaml:"metadata"` - Spec PodSpec `yaml:"spec"` -} diff --git a/helm/ingress.go b/helm/ingress.go deleted file mode 100644 index f2ad8e8..0000000 --- a/helm/ingress.go +++ /dev/null @@ -1,54 +0,0 @@ -package helm - -// Ingress is the kubernetes ingress object. -type Ingress struct { - *K8sBase `yaml:",inline"` - Spec IngressSpec -} - -func NewIngress(name string) *Ingress { - i := &Ingress{} - i.K8sBase = NewBase() - i.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name - i.K8sBase.Kind = "Ingress" - i.ApiVersion = "networking.k8s.io/v1" - i.K8sBase.Metadata.Labels[K+"/component"] = name - - return i -} - -func (i *Ingress) SetIngressClass(name string) { - class := "{{ .Values." + name + ".ingress.class }}" - i.Spec.IngressClassName = class -} - -type IngressSpec struct { - IngressClassName string `yaml:"ingressClassName,omitempty"` - Rules []IngressRule -} - -type IngressRule struct { - Host string - Http IngressHttp -} - -type IngressHttp struct { - Paths []IngressPath -} - -type IngressPath struct { - Path string - PathType string `yaml:"pathType"` - Backend *IngressBackend -} - -type IngressBackend struct { - Service IngressService - ServiceName string `yaml:"serviceName"` // for kubernetes version < 1.18 - ServicePort interface{} `yaml:"servicePort"` // for kubernetes version < 1.18 -} - -type IngressService struct { - Name string `yaml:"name"` - Port map[string]interface{} `yaml:"port"` -} diff --git a/helm/k8sbase.go b/helm/k8sbase.go deleted file mode 100644 index df95877..0000000 --- a/helm/k8sbase.go +++ /dev/null @@ -1,73 +0,0 @@ -package helm - -import ( - "crypto/sha1" - "fmt" - "io/ioutil" - "strings" -) - -// Metadata is the metadata for a kubernetes object. -type Metadata struct { - Name string `yaml:"name,omitempty"` - Labels map[string]string `yaml:"labels"` - Annotations map[string]string `yaml:"annotations,omitempty"` -} - -func NewMetadata() *Metadata { - return &Metadata{ - Name: "", - Labels: make(map[string]string), - Annotations: make(map[string]string), - } -} - -// K8sBase is the base for all kubernetes objects. -type K8sBase struct { - ApiVersion string `yaml:"apiVersion"` - Kind string `yaml:"kind"` - Metadata *Metadata `yaml:"metadata"` -} - -// NewBase is a factory for creating a new base object with metadata, labels and annotations set to the default. -func NewBase() *K8sBase { - b := &K8sBase{ - Metadata: NewMetadata(), - } - // add some information of the build - b.Metadata.Labels[K+"/project"] = "{{ .Chart.Name }}" - b.Metadata.Labels[K+"/release"] = ReleaseNameTpl - b.Metadata.Annotations[K+"/version"] = Version - return b -} - -func (k *K8sBase) BuildSHA(filename string) { - c, _ := ioutil.ReadFile(filename) - //sum := sha256.Sum256(c) - sum := sha1.Sum(c) - k.Metadata.Annotations[K+"/docker-compose-sha1"] = fmt.Sprintf("%x", string(sum[:])) -} - -// Get returns the Kind. -func (k *K8sBase) Get() string { - return k.Kind -} - -// Name returns the name of the object from Metadata. -func (k *K8sBase) Name() string { - return k.Metadata.Name -} - -func (k *K8sBase) GetType() string { - if n, ok := k.Metadata.Labels[K+"/type"]; ok { - return n - } - return strings.ToLower(k.Kind) -} - -func (k *K8sBase) GetPathRessource() string { - if p, ok := k.Metadata.Labels[K+"/path"]; ok { - return p - } - return "" -} diff --git a/helm/labels.go b/helm/labels.go deleted file mode 100644 index 62db879..0000000 --- a/helm/labels.go +++ /dev/null @@ -1,77 +0,0 @@ -package helm - -import ( - "bytes" - "html/template" -) - -const ReleaseNameTpl = "{{ .Release.Name }}" -const ( - LABEL_MAP_ENV = K + "/mapenv" - LABEL_ENV_SECRET = K + "/secret-envfiles" - LABEL_PORT = K + "/ports" - LABEL_CONTAINER_PORT = K + "/container-ports" - LABEL_INGRESS = K + "/ingress" - LABEL_VOL_CM = K + "/configmap-volumes" - LABEL_HEALTHCHECK = K + "/healthcheck" - LABEL_SAMEPOD = K + "/same-pod" - LABEL_VOLUMEFROM = K + "/volume-from" - LABEL_EMPTYDIRS = K + "/empty-dirs" - LABEL_IGNORE = K + "/ignore" - LABEL_SECRETVARS = K + "/secret-vars" - LABEL_CRON = K + "/crontabs" - - //deprecated: use LABEL_MAP_ENV instead - LABEL_ENV_SERVICE = K + "/env-to-service" -) - -// GetLabelsDocumentation returns the documentation for the labels. -func GetLabelsDocumentation() string { - t, err := template.New("labels").Parse(`# Labels -{{.LABEL_IGNORE | printf "%-33s"}}: ignore the container, it will not yied any object in the helm chart (bool) -{{.LABEL_SECRETVARS | printf "%-33s"}}: secret variables to push on a secret file (coma separated) -{{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap (coma separated) -{{.LABEL_MAP_ENV | printf "%-33s"}}: map environment variable to a template string (yaml style, object) -{{.LABEL_PORT | printf "%-33s"}}: set the ports to assign on the container in pod + expose as a service (coma separated) -{{.LABEL_CONTAINER_PORT | printf "%-33s"}}: set the ports to assign on the contaienr in pod but avoid service (coma separated) -{{.LABEL_INGRESS | printf "%-33s"}}: set the port to expose in an ingress (coma separated) -{{.LABEL_VOL_CM | printf "%-33s"}}: specifies that the volumes points on a configmap (coma separated) -{{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the -{{ printf "%-34s" ""}} given service name (string) -{{.LABEL_VOLUMEFROM | printf "%-33s"}}: specifies that the volumes to be mounted from the given service (yaml style) -{{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of -{{ printf "%-34s" ""}} persistentVolumeClaim (coma separated) -{{.LABEL_CRON | printf "%-33s"}}: specifies a cronjobs to create (yaml style, array) - this will create a -{{ printf "%-34s" ""}} cronjob, a service account, a role and a rolebinding to start the command with "kubectl" -{{ printf "%-34s" ""}} The form is the following: -{{ printf "%-34s" ""}} - command: the command to run -{{ printf "%-34s" ""}} schedule: the schedule to run the command (e.g. "@daily" or "*/1 * * * *") -{{ printf "%-34s" ""}} image: the image to use for the command (default to "bitnami/kubectl") -{{ printf "%-34s" ""}} allPods: true if you want to run the command on all pods (default to false) -{{.LABEL_HEALTHCHECK | printf "%-33s"}}: specifies that the container should be monitored by a healthcheck, -{{ printf "%-34s" ""}} **it overrides the docker-compose healthcheck**. -{{ printf "%-34s" ""}} You can use these form of label values: -{{ printf "%-35s" ""}} -> http://[ignored][:port][/path] to specify an http healthcheck -{{ printf "%-35s" ""}} -> tcp://[ignored]:port to specify a tcp healthcheck -{{ printf "%-35s" ""}} -> other string is condidered as a "command" healthcheck`) - if err != nil { - panic(err) - } - buff := bytes.NewBuffer(nil) - t.Execute(buff, map[string]string{ - "LABEL_ENV_SECRET": LABEL_ENV_SECRET, - "LABEL_PORT": LABEL_PORT, - "LABEL_CONTAINER_PORT": LABEL_CONTAINER_PORT, - "LABEL_INGRESS": LABEL_INGRESS, - "LABEL_VOL_CM": LABEL_VOL_CM, - "LABEL_HEALTHCHECK": LABEL_HEALTHCHECK, - "LABEL_SAMEPOD": LABEL_SAMEPOD, - "LABEL_VOLUMEFROM": LABEL_VOLUMEFROM, - "LABEL_EMPTYDIRS": LABEL_EMPTYDIRS, - "LABEL_IGNORE": LABEL_IGNORE, - "LABEL_MAP_ENV": LABEL_MAP_ENV, - "LABEL_SECRETVARS": LABEL_SECRETVARS, - "LABEL_CRON": LABEL_CRON, - }) - return buff.String() -} diff --git a/helm/notes.go b/helm/notes.go deleted file mode 100644 index 54a33ec..0000000 --- a/helm/notes.go +++ /dev/null @@ -1,25 +0,0 @@ -package helm - -import "strings" - -var NOTES = ` -Congratulations, - -Your application is now deployed. This may take a while to be up and responding. - -__list__ -` - -// GenerateNotesFile generates the notes file for the helm chart. -func GenerateNotesFile(ingressess map[string]*Ingress) string { - - list := make([]string, 0) - - for name, ing := range ingressess { - for _, r := range ing.Spec.Rules { - list = append(list, "{{ if .Values."+name+".ingress.enabled -}}\n- "+name+" is accessible on : http://"+r.Host+"\n{{- end }}") - } - } - - return strings.ReplaceAll(NOTES, "__list__", strings.Join(list, "\n")) -} diff --git a/helm/probe.go b/helm/probe.go deleted file mode 100644 index 38608a6..0000000 --- a/helm/probe.go +++ /dev/null @@ -1,104 +0,0 @@ -package helm - -import ( - "time" - - "github.com/compose-spec/compose-go/types" -) - -// Probe is a struct that can be used to create a Liveness or Readiness probe. -type Probe struct { - HttpGet *HttpGet `yaml:"httpGet,omitempty"` - Exec *Exec `yaml:"exec,omitempty"` - TCP *TCP `yaml:"tcpSocket,omitempty"` - Period float64 `yaml:"periodSeconds"` - InitialDelay float64 `yaml:"initialDelaySeconds"` - Success uint64 `yaml:"successThreshold"` - Failure uint64 `yaml:"failureThreshold"` -} - -// Create a new Probe object that can be apply to HttpProbe or TCPProbe. -func NewProbe(period, initialDelaySeconds float64, success, failure uint64) *Probe { - probe := &Probe{ - Period: period, - Success: success, - Failure: failure, - InitialDelay: initialDelaySeconds, - } - - // fix default values from - // https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ - if period == 0 { - probe.Period = 10 - } - if success == 0 { - probe.Success = 1 - } - if failure == 0 { - probe.Failure = 3 - } - return probe -} - -// NewProbeWithDuration creates a new Probe object with the given duration from types. -func NewProbeWithDuration(period, initialDelaySeconds *types.Duration, success, failure *uint64) *Probe { - - if period == nil { - d := types.Duration(0 * time.Second) - period = &d - } - - if initialDelaySeconds == nil { - d := types.Duration(0 * time.Second) - initialDelaySeconds = &d - } - - if success == nil { - s := uint64(0) - success = &s - } - - if failure == nil { - f := uint64(0) - failure = &f - } - - p, err := time.ParseDuration(period.String()) - if err != nil { - p = time.Second * 10 - } - - i, err := time.ParseDuration(initialDelaySeconds.String()) - if err != nil { - i = time.Second * 0 - } - - return NewProbe(p.Seconds(), i.Seconds(), *success, *failure) - -} - -// NewProbeFromService creates a new Probe object from a ServiceConfig. -func NewProbeFromService(s *types.ServiceConfig) *Probe { - if s == nil || s.HealthCheck == nil { - return NewProbe(0, 0, 0, 0) - } - - return NewProbeWithDuration(s.HealthCheck.Interval, s.HealthCheck.StartPeriod, nil, s.HealthCheck.Retries) - -} - -// HttpGet is a Probe configuration to check http health. -type HttpGet struct { - Path string `yaml:"path"` - Port int `yaml:"port"` -} - -// Execis a Probe configuration to check exec health. -type Exec struct { - Command []string `yaml:"command"` -} - -// TCP is a Probe configuration to check tcp health. -type TCP struct { - Port int `yaml:"port"` -} diff --git a/helm/role.go b/helm/role.go deleted file mode 100644 index 111058a..0000000 --- a/helm/role.go +++ /dev/null @@ -1,38 +0,0 @@ -package helm - -type Rule struct { - ApiGroup []string `yaml:"apiGroups,omitempty"` - Resources []string `yaml:"resources,omitempty"` - Verbs []string `yaml:"verbs,omitempty"` -} - -type Role struct { - *K8sBase `yaml:",inline"` - Rules []Rule `yaml:"rules,omitempty"` -} - -func NewCronRole(name string) *Role { - role := &Role{ - K8sBase: NewBase(), - } - - role.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name + "-cron-executor" - role.K8sBase.Kind = "Role" - role.K8sBase.ApiVersion = "rbac.authorization.k8s.io/v1" - role.K8sBase.Metadata.Labels[K+"/component"] = name - - role.Rules = []Rule{ - { - ApiGroup: []string{""}, - Resources: []string{"pods", "pods/log"}, - Verbs: []string{"get", "list", "watch", "create", "update", "patch", "delete"}, - }, - { - ApiGroup: []string{""}, - Resources: []string{"pods/exec"}, - Verbs: []string{"create"}, - }, - } - - return role -} diff --git a/helm/roleBinding.go b/helm/roleBinding.go deleted file mode 100644 index a99d8ef..0000000 --- a/helm/roleBinding.go +++ /dev/null @@ -1,44 +0,0 @@ -package helm - -type RoleRef struct { - Kind string `yaml:"kind"` - Name string `yaml:"name"` - APIGroup string `yaml:"apiGroup"` -} - -type Subject struct { - Kind string `yaml:"kind"` - Name string `yaml:"name"` - Namespace string `yaml:"namespace"` -} - -type RoleBinding struct { - *K8sBase `yaml:",inline"` - RoleRef RoleRef `yaml:"roleRef,omitempty"` - Subjects []Subject `yaml:"subjects,omitempty"` -} - -func NewRoleBinding(name string, user *ServiceAccount, role *Role) *RoleBinding { - rb := &RoleBinding{ - K8sBase: NewBase(), - } - - rb.K8sBase.Kind = "RoleBinding" - rb.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name + "-cron-allow" - rb.K8sBase.ApiVersion = "rbac.authorization.k8s.io/v1" - rb.K8sBase.Metadata.Labels[K+"/component"] = name - - rb.RoleRef.Kind = "Role" - rb.RoleRef.Name = role.Metadata.Name - rb.RoleRef.APIGroup = "rbac.authorization.k8s.io" - - rb.Subjects = []Subject{ - { - Kind: "ServiceAccount", - Name: user.Metadata.Name, - Namespace: "{{ .Release.Namespace }}", - }, - } - - return rb -} diff --git a/helm/service.go b/helm/service.go deleted file mode 100644 index 78a0f78..0000000 --- a/helm/service.go +++ /dev/null @@ -1,55 +0,0 @@ -package helm - -import "strconv" - -// Service is a Kubernetes service. -type Service struct { - *K8sBase `yaml:",inline"` - Spec *ServiceSpec `yaml:"spec"` -} - -// NewService creates a new initialized service. -func NewService(name string) *Service { - s := &Service{ - K8sBase: NewBase(), - Spec: NewServiceSpec(), - } - s.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name - s.K8sBase.Kind = "Service" - s.K8sBase.ApiVersion = "v1" - s.K8sBase.Metadata.Labels[K+"/component"] = name - return s -} - -// ServicePort is a port on a service. -type ServicePort struct { - Protocol string `yaml:"protocol"` - Port int `yaml:"port"` - TargetPort int `yaml:"targetPort"` - Name string `yaml:"name"` -} - -// NewServicePort creates a new initialized service port. -func NewServicePort(port, target int) *ServicePort { - return &ServicePort{ - Protocol: "TCP", - Port: port, - TargetPort: port, - Name: "port-" + strconv.Itoa(target), - } -} - -// ServiceSpec is the spec for a service. -type ServiceSpec struct { - Selector map[string]string - Ports []*ServicePort - Type string `yaml:"type,omitempty"` -} - -// NewServiceSpec creates a new initialized service spec. -func NewServiceSpec() *ServiceSpec { - return &ServiceSpec{ - Selector: make(map[string]string), - Ports: make([]*ServicePort, 0), - } -} diff --git a/helm/serviceAccount.go b/helm/serviceAccount.go deleted file mode 100644 index e7b44c5..0000000 --- a/helm/serviceAccount.go +++ /dev/null @@ -1,18 +0,0 @@ -package helm - -// ServiceAccount defines a service account -type ServiceAccount struct { - *K8sBase `yaml:",inline"` -} - -// NewServiceAccount creates a new service account with a given name. -func NewServiceAccount(name string) *ServiceAccount { - sa := &ServiceAccount{ - K8sBase: NewBase(), - } - sa.K8sBase.Kind = "ServiceAccount" - sa.K8sBase.ApiVersion = "v1" - sa.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + name + "-cron-user" - sa.K8sBase.Metadata.Labels[K+"/component"] = name - return sa -} diff --git a/helm/storage.go b/helm/storage.go deleted file mode 100644 index e09e82f..0000000 --- a/helm/storage.go +++ /dev/null @@ -1,54 +0,0 @@ -package helm - -import "sync" - -var ( - made = make(map[string]bool) - locker = sync.Mutex{} -) - -// ResetMadePVC resets the cache of made PVCs. -// Useful in tests only. -func ResetMadePVC() { - locker.Lock() - defer locker.Unlock() - made = make(map[string]bool) -} - -// Storage is a struct for a PersistentVolumeClaim. -type Storage struct { - *K8sBase `yaml:",inline"` - Spec *PVCSpec -} - -// NewPVC creates a new PersistentVolumeClaim object. -func NewPVC(name, storageName string) *Storage { - locker.Lock() - defer locker.Unlock() - if _, ok := made[name+storageName]; ok { - return nil - } - made[name+storageName] = true - pvc := &Storage{} - pvc.K8sBase = NewBase() - pvc.K8sBase.Kind = "PersistentVolumeClaim" - pvc.K8sBase.Metadata.Labels[K+"/pvc-name"] = storageName - pvc.K8sBase.ApiVersion = "v1" - pvc.K8sBase.Metadata.Name = ReleaseNameTpl + "-" + storageName - pvc.K8sBase.Metadata.Labels[K+"/component"] = name - pvc.Spec = &PVCSpec{ - Resouces: map[string]interface{}{ - "requests": map[string]string{ - "storage": "{{ .Values." + name + ".persistence." + storageName + ".capacity }}", - }, - }, - AccessModes: []string{"ReadWriteOnce"}, - } - return pvc -} - -// PVCSpec is a struct for a PersistentVolumeClaim spec. -type PVCSpec struct { - Resouces map[string]interface{} `yaml:"resources"` - AccessModes []string `yaml:"accessModes"` -} diff --git a/helm/types.go b/helm/types.go deleted file mode 100644 index 9fcd3d7..0000000 --- a/helm/types.go +++ /dev/null @@ -1,41 +0,0 @@ -package helm - -import ( - "os" - "strings" -) - -const K = "katenary.io" - -var ( - Appname = "" // set at runtime - Version = "1.0" // should be set from main.Version -) - -// Kinded represent an object with a kind. -type Kinded interface { - // Get must resturn the kind name. - Get() string -} - -// Signable represents an object with a signature. -type Signable interface { - // BuildSHA must return the signature. - BuildSHA(filename string) -} - -// Named represents an object with a name. -type Named interface { - // Name must return the name of the object (from metadata). - Name() string -} - -// GetProjectName returns the name of the project. -func GetProjectName() string { - if len(Appname) > 0 { - return Appname - } - p, _ := os.Getwd() - path := strings.Split(p, string(os.PathSeparator)) - return path[len(path)-1] -} diff --git a/logger/color_test.go b/logger/color_test.go deleted file mode 100644 index 6f0dea0..0000000 --- a/logger/color_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package logger - -import "testing" - -func TestColor(t *testing.T) { - NOLOG = false - Red("Red text") - Grey("Grey text") -} diff --git a/logger/utils.go b/logger/utils.go deleted file mode 100644 index 18438e2..0000000 --- a/logger/utils.go +++ /dev/null @@ -1,96 +0,0 @@ -package logger - -import ( - "fmt" - "os" - "sync" -) - -type Color int - -var ActivateColors = false -var NOLOG = false - -const ( - GREY Color = 30 + iota - RED - GREEN - YELLOW - BLUE - MAGENTA - CYAN -) - -var waiter = sync.Mutex{} - -func color(c Color, args ...interface{}) { - if NOLOG { - return - } - if !ActivateColors { - fmt.Println(args...) - return - } - waiter.Lock() - fmt.Fprintf(os.Stdout, "\x1b[%dm", c) - fmt.Fprint(os.Stdout, args...) - fmt.Fprintf(os.Stdout, "\x1b[0m\n") - waiter.Unlock() -} - -func colorf(c Color, format string, args ...interface{}) { - if NOLOG { - return - } - if !ActivateColors { - fmt.Printf(format, args...) - return - } - waiter.Lock() - fmt.Fprintf(os.Stdout, "\x1b[%dm", c) - fmt.Fprintf(os.Stdout, format, args...) - fmt.Fprintf(os.Stdout, "\x1b[0m") - waiter.Unlock() -} - -func Grey(args ...interface{}) { - color(GREY, args...) -} - -func Red(args ...interface{}) { - color(RED, args...) -} -func Green(args ...interface{}) { - color(GREEN, args...) -} -func Yellow(args ...interface{}) { - color(YELLOW, args...) -} -func Blue(args ...interface{}) { - color(BLUE, args...) -} -func Magenta(args ...interface{}) { - color(MAGENTA, args...) -} -func Greyf(format string, args ...interface{}) { - colorf(GREY, format, args...) -} - -func Redf(format string, args ...interface{}) { - colorf(RED, format, args...) -} -func Greenf(format string, args ...interface{}) { - colorf(GREEN, format, args...) -} -func Yellowf(format string, args ...interface{}) { - colorf(YELLOW, format, args...) -} -func Bluef(format string, args ...interface{}) { - colorf(BLUE, format, args...) -} -func Magentaf(format string, args ...interface{}) { - colorf(MAGENTA, format, args...) -} -func Cyanf(format string, args ...interface{}) { - colorf(CYAN, format, args...) -} diff --git a/parser/main.go b/parser/main.go new file mode 100644 index 0000000..39cf582 --- /dev/null +++ b/parser/main.go @@ -0,0 +1,29 @@ +// Parser package is a wrapper around compose-go to parse compose files. +package parser + +import ( + "github.com/compose-spec/compose-go/cli" + "github.com/compose-spec/compose-go/types" +) + +// Parse compose files and return a project. The project is parsed with dotenv, osenv and profiles. +func Parse(profiles []string, dockerComposeFile ...string) (*types.Project, error) { + + if len(dockerComposeFile) > 0 { + cli.DefaultFileNames = dockerComposeFile + } + + options, err := cli.NewProjectOptions(nil, + cli.WithProfiles(profiles), + cli.WithDefaultConfigPath, + cli.WithOsEnv, + cli.WithDotEnv, + cli.WithNormalization(true), + cli.WithInterpolation(true), + //cli.WithResolvedPaths(true), + ) + if err != nil { + return nil, err + } + return cli.ProjectFromOptions(options) +} diff --git a/test/bats b/test/bats new file mode 160000 index 0000000..e9fd17a --- /dev/null +++ b/test/bats @@ -0,0 +1 @@ +Subproject commit e9fd17a70721e447313691f239d297cecea6dfb7 diff --git a/test/examples.bats b/test/examples.bats new file mode 100644 index 0000000..4151f15 --- /dev/null +++ b/test/examples.bats @@ -0,0 +1,9 @@ +@test "generating and linting examples" { + for d in $(find ../examples/ -maxdepth 1 -mindepth 1 -type d); do + pushd $d + rm -rf chart + go run ../../cmd/katenary convert -f + helm lint chart + popd + done +} diff --git a/test/test_helper/bats-assert b/test/test_helper/bats-assert new file mode 160000 index 0000000..e2d855b --- /dev/null +++ b/test/test_helper/bats-assert @@ -0,0 +1 @@ +Subproject commit e2d855bc78619ee15b0c702b5c30fb074101159f diff --git a/test/test_helper/bats-support b/test/test_helper/bats-support new file mode 160000 index 0000000..9bf10e8 --- /dev/null +++ b/test/test_helper/bats-support @@ -0,0 +1 @@ +Subproject commit 9bf10e876dd6b624fe44423f0b35e064225f7556 diff --git a/tools/path.go b/tools/path.go deleted file mode 100644 index 560704b..0000000 --- a/tools/path.go +++ /dev/null @@ -1,25 +0,0 @@ -package tools - -import ( - "katenary/compose" - "regexp" - "strings" -) - -// replaceChars replaces some chars in a string. -const replaceChars = `[^a-zA-Z0-9_]+` - -// GetRelPath return the relative path from the root of the project. -func GetRelPath(path string) string { - return strings.Replace(path, compose.GetCurrentDir(), ".", 1) -} - -// PathToName transform a path to a yaml name. -func PathToName(path string) string { - path = strings.TrimPrefix(GetRelPath(path), "./") - path = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-") - if path[0] == '-' { - path = path[1:] - } - return path -} diff --git a/tools/path_test.go b/tools/path_test.go deleted file mode 100644 index cbda344..0000000 --- a/tools/path_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package tools - -import ( - "katenary/compose" - "testing" -) - -func Test_PathToName(t *testing.T) { - path := compose.GetCurrentDir() + "/envéfoo.file" - name := PathToName(path) - if name != "env-foo-file" { - t.Error("Expected env-foo-file, got ", name) - } -} diff --git a/update/main.go b/update/main.go index bce0820..c9db8c9 100644 --- a/update/main.go +++ b/update/main.go @@ -1,3 +1,4 @@ +/* Update package is used to check if a new version of katenary is available.*/ package update import ( @@ -76,6 +77,13 @@ func CheckLatestVersion() (string, []Asset, error) { // DownloadLatestVersion will download the latest version of katenary. func DownloadLatestVersion(assets []Asset) error { + + defer func() { + if r := recover(); r != nil { + os.Rename(exe+".old", exe) + } + }() + // Download the latest version fmt.Println("Downloading the latest version...") diff --git a/utils/doc.go b/utils/doc.go new file mode 100644 index 0000000..b3896e7 --- /dev/null +++ b/utils/doc.go @@ -0,0 +1,2 @@ +// Utils package provides some utility functions used in katenary. It defines some constants and functions used in the whole project. +package utils diff --git a/utils/hash.go b/utils/hash.go new file mode 100644 index 0000000..c6f7746 --- /dev/null +++ b/utils/hash.go @@ -0,0 +1,26 @@ +package utils + +import ( + "crypto/sha1" + "encoding/hex" + "io" + "os" + "sort" +) + +// HashComposefiles returns a hash of the compose files. +func HashComposefiles(files []string) (string, error) { + sort.Strings(files) // ensure the order is always the same + sha := sha1.New() + for _, file := range files { + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + if _, err := io.Copy(sha, f); err != nil { + return "", err + } + } + return hex.EncodeToString(sha.Sum(nil)), nil +} diff --git a/utils/icons.go b/utils/icons.go new file mode 100644 index 0000000..3767db5 --- /dev/null +++ b/utils/icons.go @@ -0,0 +1,31 @@ +package utils + +import "fmt" + +// Icon is a unicode icon +type Icon string + +// Icons used in katenary. +const ( + IconSuccess Icon = "✅" + IconFailure = "❌" + IconWarning = "⚠️'" + IconNote = "📝" + IconWorld = "🌐" + IconPlug = "🔌" + IconPackage = "📦" + IconCabinet = "🗄️" + IconInfo = "❕" + IconSecret = "🔒" + IconConfig = "🔧" + IconDependency = "🔗" +) + +// Warn prints a warning message +func Warn(msg ...interface{}) { + orange := "\033[38;5;214m" + reset := "\033[0m" + fmt.Print(IconWarning, orange, " ") + fmt.Print(msg...) + fmt.Println(reset) +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..ecccc66 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,163 @@ +package utils + +import ( + "log" + "path/filepath" + "strings" + + "github.com/compose-spec/compose-go/types" + "github.com/mitchellh/go-wordwrap" + "github.com/thediveo/netdb" + "gopkg.in/yaml.v3" + corev1 "k8s.io/api/core/v1" +) + +// TplName returns the name of the kubernetes resource as a template string. +// It is used in the templates and defined in _helper.tpl file. +func TplName(serviceName, appname string, suffix ...string) string { + if len(suffix) > 0 { + suffix[0] = "-" + suffix[0] + } + return `{{ include "` + appname + `.fullname" . }}-` + serviceName + strings.Join(suffix, "-") +} + +// Int32Ptr returns a pointer to an int32. +func Int32Ptr(i int32) *int32 { return &i } + +// StrPtr returns a pointer to a string. +func StrPtr(s string) *string { return &s } + +// CountStartingSpaces counts the number of spaces at the beginning of a string. +func CountStartingSpaces(line string) int { + count := 0 + for _, char := range line { + if char == ' ' { + count++ + } else { + break + } + } + return count +} + +// GetKind returns the kind of the resource from the file path. +func GetKind(path string) (kind string) { + defer func() { + if r := recover(); r != nil { + kind = "" + } + }() + filename := filepath.Base(path) + parts := strings.Split(filename, ".") + if len(parts) == 2 { + kind = parts[0] + } else { + kind = strings.Split(path, ".")[1] + } + return +} + +// Wrap wraps a string with a string above and below. It will respect the indentation of the src string. +func Wrap(src, above, below string) string { + spaces := strings.Repeat(" ", CountStartingSpaces(src)) + return spaces + above + "\n" + src + "\n" + spaces + below +} + +// WrapBytes wraps a byte array with a byte array above and below. It will respect the indentation of the src string. +func WrapBytes(src, above, below []byte) []byte { + return []byte(Wrap(string(src), string(above), string(below))) +} + +// GetServiceNameByPort returns the service name for a port. It the service name is not found, it returns an empty string. +func GetServiceNameByPort(port int) string { + name := "" + info := netdb.ServiceByPort(port, "tcp") + if info != nil { + name = info.Name + } + return name +} + +// GetContainerByName returns a container by name and its index in the array. It returns nil, -1 if not found. +func GetContainerByName(name string, containers []corev1.Container) (*corev1.Container, int) { + for index, c := range containers { + if c.Name == name { + return &c, index + } + } + return nil, -1 +} + +// GetContainerByName returns a container by name and its index in the array. +func TplValue(serviceName, variable string, pipes ...string) string { + + if len(pipes) == 0 { + return `{{ tpl .Values.` + serviceName + `.` + variable + ` $ }}` + } else { + return `{{ tpl .Values.` + serviceName + `.` + variable + ` $ | ` + strings.Join(pipes, " | ") + ` }}` + } +} + +// PathToName converts a path to a kubernetes complient name. +func PathToName(path string) string { + if len(path) == 0 { + return "" + } + + path = filepath.Clean(path) + if path[0] == '/' || path[0] == '.' { + path = path[1:] + } + path = strings.Replace(path, "/", "_", -1) + path = strings.Replace(path, ".", "_", -1) + return path +} + +// EnvConfig is a struct to hold the description of an environment variable. +type EnvConfig struct { + Description string + Service types.ServiceConfig +} + +// GetValuesFromLabel returns a map of values from a label. +func GetValuesFromLabel(service types.ServiceConfig, LabelValues string) map[string]*EnvConfig { + descriptions := make(map[string]*EnvConfig) + if v, ok := service.Labels[LabelValues]; ok { + labelContent := []any{} + err := yaml.Unmarshal([]byte(v), &labelContent) + if err != nil { + log.Printf("Error parsing label %s: %s", v, err) + log.Fatal(err) + } + for _, value := range labelContent { + switch value.(type) { + case string: + descriptions[value.(string)] = nil + case map[string]interface{}: + for k, v := range value.(map[string]interface{}) { + descriptions[k] = &EnvConfig{Service: service, Description: v.(string)} + } + case map[interface{}]interface{}: + for k, v := range value.(map[interface{}]interface{}) { + descriptions[k.(string)] = &EnvConfig{Service: service, Description: v.(string)} + } + default: + log.Fatalf("Unknown type in label: %s %T", LabelValues, value) + } + } + } + return descriptions +} + +// WordWrap wraps a string to a given line width. Warning: it may break the string. You need to check the result. +func WordWrap(text string, lineWidth int) string { + return wordwrap.WrapString(text, uint(lineWidth)) +} + +func MapKeys(m map[string]interface{}) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + return keys +}