Skip to content

Commit

Permalink
Rework cli (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiola authored Jul 24, 2024
1 parent 22f62ee commit 754a318
Show file tree
Hide file tree
Showing 173 changed files with 6,958 additions and 7,307 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "Go",
"image": "golang:1.22.4",
"containerUser": "root",
"containerUser": "golang",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {},
"ghcr.io/devcontainers/features/common-utils:2": {"username": "golang"},
"ghcr.io/devcontainers/features/go:1": {"version": "none", "golangciLintVersion": "1.59.1"}
},
"runArgs": [
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,15 @@ jobs:
run: make ci-lint GOLANGCI_LINT_MODE="github-actions"

test:
name: Testing on ${{ matrix.os }} and kubernetes ${{ matrix.k8s_version}}
name: Testing on ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
k8s_version:
- "1.27"
- "1.28"
- "1.29"
- "1.30"
runs-on: ${{ matrix.os }}
env:
ENVTEST_K8S_VERSION: ${{matrix.k8s_version}}
GOEXPERIMENT: nocoverageredesign
steps:
- name: Checkout Repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
Expand Down
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif

# Set here the name of the package you want to build
CMDNAME:= mlp
BUILD_PATH:= ./cmd/$(CMDNAME)
BUILD_PATH:= .
CONFORMANCE_TEST_PATH:= $(PROJECT_DIR)/tests/e2e
IS_LIBRARY:=

Expand All @@ -63,7 +63,7 @@ GOARM:= $(shell go env GOARM)
GIT_REV:= $(shell git rev-parse --short HEAD 2>/dev/null)
VERSION:= $(shell git describe --tags --exact-match 2>/dev/null || (echo $(GIT_REV) | cut -c1-12))
# insert here the go module where to add the version metadata
VERSION_MODULE_NAME:= github.com/mia-platform/mlp/internal/cli
VERSION_MODULE_NAME:= github.com/mia-platform/mlp/pkg/cmd

# supported platforms for container creation, these are a subset of the supported
# platforms of the base image.
Expand Down Expand Up @@ -102,7 +102,14 @@ include tools/make/release.mk

# Uncomment the correct test suite to run during CI
.PHONY: ci
# ci: test-coverage
ci: test-integration-coverage
ci: test-coverage
# ci: test-integration-coverage

### Put your custom import, define or goals under here ###

generate-deps: $(TOOLS_BIN)/deepcopy-gen
$(TOOLS_BIN)/deepcopy-gen: $(TOOLS_DIR)/DEEPCOPY_GEN_VERSION
$(eval DEEPCOPY_GEN_VERSION:= $(shell cat $<))
mkdir -p $(TOOLS_BIN)
$(info Installing deepcopy-gen $(DEEPCOPY_GEN_VERSION) bin in $(TOOLS_BIN))
GOBIN=$(TOOLS_BIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(DEEPCOPY_GEN_VERSION)
95 changes: 30 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<div align="center">

# mlp

[![Go Report Card](https://goreportcard.com/badge/github.com/mia-platform/mlp)](https://goreportcard.com/report/github.com/mia-platform/mlp)
[![Test Build Docker image and Release](https://github.com/mia-platform/mlp/actions/workflows/test-build-docker-release.yml/badge.svg)](https://github.com/mia-platform/mlp/actions/workflows/test-build-docker-release.yml)
[![codecov](https://codecov.io/gh/mia-platform/mlp/branch/main/graph/badge.svg)](https://codecov.io/gh/mia-platform/mlp)
<center>

</div>
[![Build Status][github-actions-svg]][github-actions]
[![Go Report Card][go-report-card]][go-report-card-link]
[![GoDoc][godoc-svg]][godoc-link]

</center>

`mlp` is a command line tool responsible for creating, updating and deleting kubernetes resources based on files
generated by Mia-Platform Console.
Expand All @@ -20,80 +19,44 @@ The main subcommands that the tool has are:
- `hydrate`: helper to fill kustomization.yml with resources and patches
- `completion`: generate the autocompletion

## Install
## To Start Using `mlp`

### Using Brew
Read the documentation [here](./docs/10_overview.md).

```
brew install mia-platform/tap/mlp
```
## To Start Developing `mlp`

## Using Docker
To start developing the CLI you must have this requirements:

```
docker run -it --rm ghcr.io/mia-platform/mlp
```
- golang 1.22
- make

### Using Go
Once you have pulled the code locally, you can build the code with make:

```
go get -u github.com/mia-platform/mlp
```sh
make build
```

## Docs

If you want to read detailed documentation on all the available subcommands please refer to the [doc](./docs)

## Gitlab-CI usage

In order to use `mlp` in a pipeline these are the steps to follow:

- In the configuration files use `{{VAR}}` to reference the environment variable `VAR`
- Prepare a `mlp.yaml` file with all the secrets and configmaps to generate and apply inside the cluster (see file structure in [generate](./docs/40_generate.md)).
- run `mlp generate` with the flags:
- `--config-file`: the configuration files containing secrets and configmaps structure.
- `--env-prefix`: the prefixes to use while performing the environment variables lookup inside the configuration files.
- `--out`: output directory in which generated files will be placed
- run `mlp interpolate` with:
- `--filename`: the files/folders containing files to interpolate. The command does not look into sub-dirs.
- `--env-prefix`: the prefixes to use while performing the environment variables lookup.
- `--out`: output directory in which generated files will be placed
- run `mlp deploy` with:
- `--filename`: the files/folders containing files to deploy into the Kubernetes cluster.
- `--server`: Kubernetes URL
- `--certificate-authority`: Kubernetes CA
- `--token`: Kubernetes token
- `--namespace`: the namespace in which the resources will be deployed
- `--deploy-type`: the deploy type used
- `--force-deploy-when-no-semver`: flag used to force deploy of services that are not following semantic versioning.
- `--ensure-namespace` (default to `true`): set if the namespace existence should be ensured. By default it is set to true so that the namespace existence is checked and, if it not exists, created. If set to false, it throws if namespace not already exists.

Example:

The `script` section of the CI file should look like this:

```yaml
script:
- mkdir OUTPUT_DIR
- mlp generate -c config-file.yaml -e FIRST_PREFIX -e SECOND_PREFIX -o OUTPUT_DIR
- mlp interpolate -f SOURCE_PATH -e FIRST_PREFIX -e SECOND_PREFIX -o OUTPUT_DIR
- mlp deploy --server KUBERNETES_URL --certificate-authority /path/to/kubernetes/ca.pem --token KUBERNETES_TOKEN -f OUTPUT_DIR -n KUBERNETES_NAMESPACE --deploy-type DEPLOY_TYPE --force-deploy-when-no-semver=FORCE_DEPLOY_WHEN_NO_SEMVER
```
`make` will download all the dependencies needed and will build the binary for your current system that you can find
in the `bin` folder.

To build the docker image locally run:

Note that `mlp` suppose that the output directory already exists so it needs to be created before using the command.
```sh
make docker-build
```

## Testing
## Testing `mlp`

To run the tests use the command:

```sh
make test
```

There are also some integration test, which you can run with
Or add the `DEBUG_TEST` flag to run the test with debug mode enabled:

```sh
make test-integration
make test DEBUG_TEST=1
```

Before sending a PR be sure that all the linter pass with success:
Expand All @@ -102,7 +65,9 @@ Before sending a PR be sure that all the linter pass with success:
make lint
```

## Contributing

Participation to the project is governed by the [Code of Conduct](./CODE_OF_CONDUCT.md) and you can read
how you can improve the project in the [Contributing](./CONTRIBUTING.md) guidelines.
[github-actions]: https://github.com/mia-platform/mlp/actions
[github-actions-svg]: https://github.com/mia-platform/mlp/workflows/Continuous%20Integration%20Pipeline/badge.svg
[godoc-svg]: https://godoc.org/github.com/mia-platform/mlp?status.svg
[godoc-link]: https://godoc.org/github.com/mia-platform/mlp
[go-report-card]: https://goreportcard.com/badge/github.com/mia-platform/mlp
[go-report-card-link]: https://goreportcard.com/report/github.com/mia-platform/mlp
88 changes: 0 additions & 88 deletions cmd/mlp/main.go

This file was deleted.

49 changes: 0 additions & 49 deletions cmd/mlp/main_test.go

This file was deleted.

47 changes: 38 additions & 9 deletions docs/10_overview.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
# Overview
# mlp CLI

[`mlp`](https://github.com/mia-platform/mlp) is a comman line tool responsible for creating, updating and deleting kubernetes resources based on files generated by Mia-Platform Console.
`mlp`is a Command Line Interface that is responsible for creating, updating and deleting Kubernetes resources
based on files generated by Mia-Platform Console.
It can be viewed as a superpowered `kubectl apply` command that introduce additional functionalities to simply sending
patches to the api-server.

The main subcommands that the tool has are:
`mlp` can prune resources that are not present anymore between different deploys because it keeps an inventory
of all the resources that has applyed the last time.
It can force new deployment rollout even if there are no differeces between deploys, running Jobs immediately from
CronJob definitions, and it will add annotations to workload resources about their Secrets and ConfigMaps depedencies.
The cli will also automatically watch the progression of the applied resources and it will report what and how many
resources failed to reach a ready or successfull state.

- [`interpolate`](./30_interpolate.md): fill placeholders in kubernetes files with the values of ENV variables
- [`generate`](./40_generate.md): create files for kubernetes `ConfigMap` and `Secret` based on files and/or ENV values
- [`deploy`](./50_deploy.md): create and/or update resources in a kubernetes namespace with the interpolated/generated files
- `kustomize`: run kustomize build
- [`hydrate`](./70_hydrate.md): helper to fill kustomization.yml with resources and patches
- `completion`: generate the autocompletion
In addition `mlp` can also generate ConfigMaps or Secrets via a dedicate configuration file using a combination of
environment variabiles, literal values and files, giving the user the ability to not commiting sensitive data and giving
the ability to use different configuration for different runtime environments.

## Functionalities

- `deploy`: the main command, is used for creating, updating and pruning resources in a kubernetes
environment using the resouce files created by the Mia-Platform Console
- `generate`: create kubernetes `ConfigMap` and `Secret` based on a configuration file
- `hydrate`: is an helper function for configuring correctly the kustomization files inside the target folder
with all the files and patches found
- `interpolate`: will run through all the files passed and run through a templating function for render the final
manifests
- `kustomize`: is the same command of `kustomize build` and can be used if you project is using the kustomize structure
to render the resources to pass to the `interpolate` command

For more information about the various options available to the various commands you can always run
`mlp <command> --help` to see the helpers.

## Guides

Below, you can find additional documentation for `mlp`:

- [Setup](./20_setup.md)
- [Generation Configuration](./30_generate.md)
- [Hydration Logic](./40_hydrate.md)
- [Interpolatation Template](./50_interpolate.md)
Loading

0 comments on commit 754a318

Please sign in to comment.