Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/http-cache-semanti…
Browse files Browse the repository at this point in the history
…cs-4.1.1
  • Loading branch information
skifahrer authored Jul 21, 2023
2 parents 8931d8d + 7262572 commit 6abc333
Show file tree
Hide file tree
Showing 22 changed files with 963 additions and 49 deletions.
170 changes: 162 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Installing dependencies
yarn global add bratislava/bratiska-cli
```
#### Note
This utility needs `linux` based operating system to run properly. If you are using `Windows`, you can use `WSL` to run
it.
### Versions
We provide three versions of `bratiska-cli`:
Expand Down Expand Up @@ -47,7 +52,8 @@ yarn global add bratislava/bratiska-cli#stable
To be able to work with this utility, you need to have a few things configured. Different commands require different
accesses.
How to install required tools and how to configure them is described bellow this list.
How to install required tools and how to configure them is described bellow this list. This utility runs only on `linux`
based operating systems.
### `tag` command
Expand Down Expand Up @@ -89,6 +95,14 @@ The most complex command, requires much more things to be installed and configur
- access rights to github repository
- access rights to kubernetes cluster
### `label` command
If you need to add label to kubernetes resources, you can use this command. It requires:
- installed `git`
- installed `kubectl`
- access rights to kubernetes cluster
#### Accesses:
1. Docker Harbor access - can be
Expand Down Expand Up @@ -321,6 +335,14 @@ If you want to specify image registry, use `--registry` flag.
bratiska-cli build_image --registry harbor.example.com
```
#### Specify namespace
If you want to specify for harbor, use `--namespace` flag.
```bash
bratiska-cli build_image --namespace wordpress
```
#### No image registry check
CLI will tell you that image you are building is already in registry. To skip this check use `--no_image_repo_check`
Expand All @@ -346,6 +368,14 @@ If you want to force rebuild an image, use `--force_rebuild` flag. It is needed
bratiska-cli build_image --force_rebuild
```
##### Specify the tag value
If you want to specify tag for the image, use `--tag` flag.
```bash
bratiska-cli build_image --tag 6.2-wordpress-php-fpm8.2
```
#### Debugging
If you have some problem, there is always option to use --debug flag to print more info.
Expand All @@ -360,6 +390,7 @@ To use beta functionality, you can use `--beta` flag.
bratiska-cli build_image --force_rebuild --beta
```
### command `build_kustomize`
If you want to just build kustomize from the source `/kubernetes` folder you can use `build_kustomize` command.
Expand Down Expand Up @@ -409,7 +440,7 @@ bratiska-cli build_kustomize --no_image_repo_check
#### Specify namespace
If you want to specify kubernetes namespace, use `--namespace` flag.
If you want to specify kubernetes namespace together with harbor namespace (we use it together), use `--namespace` flag.
```bash
bratiska-cli build_kustomize --namespace standalone
Expand Down Expand Up @@ -592,6 +623,122 @@ Dry run with custom image and specified folder to kustomize.
bratiska-cli deploy --dry_run --image harbor.bratislava.sk/standalone/nest-prisma-template:bratiska-cli-3f3ce4fd14c76138a081596b2987a81f18a3c747-master-untracked --kustomize ./kubernetes/base
```
### command `label`
This command allow you to add lables to kubernetes resources. You can specify resources by name or by label selector. As
secrets don't have app name labels, bratiska-cli tries to use some predefined most used secrets to try to hit correct
secret for that app.
Kubernetes resource with new label looks like this:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nest-forms-backend-app
namespace: standalone
labels:
app: nest-forms-backend
source: nest-forms-backend
labelkey: labelvalue
```

#### Simple add label to kubernetes app

If you want to add label to almost all resources which has a deployment like `nest-prisma-template`, you can use this
command:

```bash
bratiska-cli label key=value
```

Please note that you need to be in folder where is `project.json` file from which we determine the name of the app.
In normal run you don`t need to specify resource, because label command is taking all these resources into account:
```bash
pods
deployments
statefulsets
persistentvolumeclaims
services
endpoints
ingresses
configmaps
sealedsecrets
secrets
```
#### Add label on staging or production
Just as in other cases you can use `--staging` or `--production` flag to confirm environment. You also need to have
kubectl context set to correct cluster.
```bash
bratiska-cli label key=value --staging
```
or
```bash
bratiska-cli label key=value --production
```
#### Specify resource which needs to be tagged
If you want to tag only one resource, you can use this command:
```bash
bratiska-cli label key=value --resources=deployments
```
#### Specify secret which needs to be tagged
If your app has some secret which was not taged from general list of secrets, you can specify it like this:
```bash
bratiska-cli label key=value --secret=database-secret
```
Usually secret has a name `nest-prisma-template-database-secret`, but you have to specify it without app name.
Here is the list of secrets which are trying to be tagged by default:
```bash
tls
database-secret
general-secret
mailgun-secret
azure-secret
internals-secret
meilisearch-secret
plugin-preview-secret
cognito-secret
magproxy-secret
scanner-secret
forms-secret
app-secret
mapbox-secret
msal-secret
ginis-secret
```
If secret does not exist in the namespace `label` command will just skip it and says that secret does not exist.
```bash
Error from server (NotFound): secrets "nest-forms-backend-mapbox-secret" not found
secret mapbox-secret does not exist. Skipping...
```
#### Add label to subset of resources
This feature is not yet fully implemented in the version `3.0.0` but if you need to add label to sub resources you can
use:
```bash
bratiska-cli label key=value --recursive
```
Please not that this will restart the current running pods.
## Automatisation
### Bratiska-cli args in config.json
Expand Down Expand Up @@ -642,16 +789,16 @@ in `config.json`
### Environment variables for docker next build
Sometimes you need to have different environment variables for other clusters during docker next build. You can
Sometimes you need to have different environment variables for other clusters during docker next build with bratiska.
You can
achieve it by creating files:
`.env.dev`
`.env.staging`
`.env.prod`
`.env.bratiska-cli-build.dev`
`.env.bratiska-cli-build.staging`
`.env.bratiska-cli-build.prod`
Which is then loaded by bratiska-cli, and all its content is copied to file `.env.production.local`, which is then
loaded
to docker and processed by next during the build. More info regarding the next envs can be found
loaded to docker and processed by next during the build. More info regarding the next envs can be found
here: https://nextjs.org/docs/basic-features/environment-variables
### Environment variables passed to kustomize files
Expand Down Expand Up @@ -680,12 +827,19 @@ prod => `` (is empty)
```dotenv
ENV
```
Can have three values: `dev`, `staging`, `prod`
```dotenv
HOSTNAME
```
or
```dotenv
BRATISKA_HOSTNAME
```
the hostname of the app
```dotenv
Expand Down
49 changes: 46 additions & 3 deletions dist/bratiska-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ const deploy_1 = require("./deploy");
const build_image_1 = require("./build_image");
const build_kustomize_1 = require("./build_kustomize");
const tag_1 = require("./tag");
const label_1 = require("./label");
const common_1 = require("./common");
const helpers = __importStar(require("./helpers"));
const version = "2.6.2";
const version = "3.0.0";
const deploy = new deploy_1.Deploy();
const tag = new tag_1.Tag();
const common = new common_1.Common();
const build_image = new build_image_1.BuildImage();
const build_kustomize = new build_kustomize_1.BuildKustomize();
const label = new label_1.Label();
try {
(0, clear_1.default)();
console.log(chalk_1.default.blue(figlet_1.default.textSync("Bratiska-cli", { horizontalLayout: "full" })));
Expand Down Expand Up @@ -92,9 +94,9 @@ try {
/* step 9 */
common.get_git_tags(options);
/* step 10 */
common.check_kubernetes_cluster(options);
//common.check_kubernetes_cluster(options);
/* step 11 */
common.check_kubernetes_enviroment(options);
//common.check_kubernetes_enviroment(options);
/* step 12 */
tag.delete_tag(options);
/* step 13 */
Expand All @@ -112,6 +114,7 @@ try {
.option("-dry_run, --dry_run", "Run without deploying to kubernetes")
.option("-k, --kustomize <file_or_direcotry>", "Specify kustomize file or kustomize directory")
.option("-i, --image <url>", "Specify image from harbour via url")
.option("-tag, --tag <tag>", "Specify an image tag")
.option("-n, --namespace <namespace>", "Namespace")
.option("-d, --deployment <deployment>", "Deployment app")
.option("-h, --host <host>", "Host url address")
Expand Down Expand Up @@ -209,6 +212,8 @@ try {
.option("-force_rebuild, --force_rebuild", "Forcing image rebuild.")
.option("-build_image_no_registry, --build_image_no_registry", "Don`t push to registry")
.option("-s, --sentry <token>", "Specify sentry auth token for build")
.option("-tag, --tag <tag>", "Specify an image tag")
.option("-n, --namespace <namespace>", "Namespace")
.option("-r, --registry <url>", "Docker image registry url", "harbor.bratislava.sk")
.option("-debug, --debug", "Debugging")
.option("-beta, --beta", "Beta features")
Expand Down Expand Up @@ -271,6 +276,7 @@ try {
.argument("[env]", "environment", "")
.option("-k, --kustomize <file_or_direcotry>", "Specify kustomize file or kustomize directory")
.option("-i, --image <url>", "Specify image from harbour via url")
.option("-tag, --tag <tag>", "Specify a image tag")
.option("-n, --namespace <namespace>", "Namespace")
.option("-d, --deployment <deployment>", "Deployment app")
.option("-h, --host <host>", "Host url address")
Expand Down Expand Up @@ -318,6 +324,43 @@ try {
/* step 16 */
build_kustomize.check_kustomize(options);
});
commander_1.program
.command("label")
.summary("Apply labels to kubernetes resources")
.description("This command will add label to all kubernetes resources based on deployment app.")
.argument("[label_value]", "label value like a=xyz", "")
.option("-r, --resources <resources>", "Kubernetes resources types where label will be applied. If none is provided, then all resources all applied. Example in comma separated list: \"deployments,services,ingresses\"")
.option("-s, --secrets <secrets>", "App secrets where label will be applied without app prefix. If none is provided, then default secrets are applied. Example in comma separated list: \"database-secret,redis-secret\"")
.option("-recursive, --recursive", "If label will be applied to spec resources inside of resources, like a matchLabels or template labels")
.option("-n, --namespace <namespace>", "Namespace")
.option("-d, --deployment <deployment>", "Deployment app")
.option("-staging, --staging", "Staging flag")
.option("-production, --production", "Production flag")
.option("-debug, --debug", "Debugging")
.option("-beta, --beta", "Beta features")
.option("-force, --force <pass>", "Force")
.action((label_value, options) => {
/* step 0 */
common.show_version(options, version);
/* step 1 */
common.show_options("", options);
/* step 2 */
label.show_label_info(label_value, options);
/* step 3 */
common.check_kubernetes_cluster(options);
/* step 4 */
deploy.check_kubernetes_connection(options);
/* step 5 */
common.check_kubernetes_enviroment(options);
/* step 6 */
common.check_kubernetes_deployment(options);
/* step 7 */
deploy.check_kubernetes_cluster_conditions(options);
/* step 8 */
label.add_label_to_resources(options);
/* step 9 */
label.add_label_to_secrets(options);
});
commander_1.program.parse(process.argv);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
Loading

0 comments on commit 6abc333

Please sign in to comment.