Skip to content

Commit

Permalink
Merge branch 'stage' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeedroza committed Feb 16, 2022
2 parents c2ee4a7 + d0d46ab commit 9019d68
Show file tree
Hide file tree
Showing 176 changed files with 1,332 additions and 3,180 deletions.
80 changes: 53 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ VKPR-CLI is Tool build with Shell that has the objetive to make it easier for th

VKPR-CLI also helps the local development by using k3d to fully provision a Kubernetes cluster instance for testing purposes.

- [VKPR-CLI](#vkpr-cli-tool)
- [Why Ritchie](##Why-Ritchie?)
- [Minimum Required](##Minimum-required)
- [Get VKPR](##Get-VKPR)
- [Usage](##Usage)
- [Initializate](###Init)
- [Create a cluster](###Create-a-cluster)
- [Running the scripts](###Running-the-scripts)
- [Uninstalling the objects](###Uninstalling-the-objects)
- [Tools](##Apps)
- [Documentation](##Docs)
- [License](##License)
- [VKPR-CLI](#vkpr-cli)
- [Why Ritchie](#why-ritchie)
- [Minimum Required](#minimum-required)
- [Setup VKPR](#setup-vkpr)
- [Usage](#usage)
- [Initializate](#init)
- [Create a cluster](#create-a-cluster)
- [Deploy a sample app](deploy-a-sample-app)
- [Uninstalling the objects](#uninstalling-the-objects)
- [Tools](#apps)
- [Documentation](#docs)
- [License](#license)

## Why Ritchie?

Expand All @@ -35,15 +35,14 @@ VKPR-CLI was made to run on Linux / MacOS. It's pre-requisites are:
- [Docker](https://docs.docker.com/get-docker/)
- [Git](https://git-scm.com/downloads)

## Get VKPR
## Setup VKPR

The VKPR CLI tool will do its best to hide its internals (including Ritchie).
VKPR was built on top of Ritchie, but he abstracts most of his interaction with him. To install it, you must run the following command.

### Installing VKPR CLI
```sh
# Install the VKPR
curl -fsSL https://get.vkpr.net/ | bash
# Create alias
alias vkpr="rit vkpr"
echo 'alias vkpr="rit vkpr"' >> ~/.bashrc # If you use another Unix Shell, specify your specific source
```

## Usage
Expand Down Expand Up @@ -76,23 +75,33 @@ To do that, you can run the command:
```sh
vkpr infra up
```
You can peek into it using ```k9s```:

```
~/.vkpr/bin/k9s
```

You can now test your scripts and create your own environments with VKPR commands.

### Running the scripts
### Deploy a sample app

After you have started VKPR and connected to an environment with the Kubernetes cluster, you may be running the scripts.
Scripts follow a standard order of `vkpr + object + verb`.
To test some application using VKPR, we will use whoami as an example.

To start a simple web application, you can run the command:
For this, we will implement an ingress controller and the whoami itself:

```sh
vkpr whoami install
```
vkpr ingress install
vkpr whoami install --default
```
Now you can test this sample application with a simple curl command:

In certain commands, there is a quiz on how you want the application to go up. It can be configured, for example, access domains, HTTPS communication and others. After that, this command will create the objects needed to use whoami.
```
curl whoami.localhost:8000
# OR
curl -H "Host: whoami.localhost" localhost:8000
```

If you don't want to have to be configuring the application, you may be using the `--default` flag to follow the default values
> WARNING: Use the second form if ```whoami.localhost``` does not resolve to ```127.0.0.1```
### Uninstalling the objects

Expand All @@ -102,20 +111,37 @@ To be deleting all dependencies of the installed script, it is necessary to run
vkpr whoami remove
```

### Discard cluster

After all tests, if you want to destroy the created cluster, you may discard his with a single command:

```sh
vkpr infra down
```



## Apps

| Tools | Description |
| ------------------------ | ------------------------------------------------------------- |
| nginx-ingress-controller | Install ingress-nginx |
| ingress | Install nginx-ingress-controller |
| whoami | Install whoami |
| cert-manager | Install cert-manager to manage your certificates |
| external-dns | Install external-dns |
| loki | Install Loki for monitoring and tracing |
| keycloak | Install Keycloak to manage the identity and access management |
| consul | Install consul to service identities and traditional networking practices to securely connect applications|
| kong | Install kong to manage, configure, and route requests to your APIs.|
| postgres | Install postgres to manage the Database |
| prometheus-stack | Installs the kube-prometheus stack, a collection of Kubernetes manifests |
| vault | Install vault to manage secrets & protect sensitive data |
| argocd | Install argocd to automated the application deployment and lifecycle management|
| aws eks | Install aws to fork and setup the GitOps Repo in your Gitlab |

## Docs

The Documentation can be viewed in the following [link](https://github.com/vertigobr/vkpr-cli).
The Documentation can be viewed in the following [link](https://docs.vkpr.net/).

## License

Expand Down
98 changes: 98 additions & 0 deletions lib/functions/gitlab-operations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

## Create a new variable setting cluster-name as environment
## If var already exist, just update your value
# Parameters:
# 1 - PROJECT_ID
# 2 - PARAMETER_KEY
# 3 - PARAMETER_VALUE
# 4 - PARAMETER_MASKED
# 5 - ENVIRONMENT_SCOPE
# 6 - GITLAB_TOKEN
createOrUpdateVariable(){
local PROJECT_ID=$1
local PARAMETER_KEY=$2
local PARAMETER_VALUE=$3
local PARAMETER_MASKED=$4
local ENVIRONMENT_SCOPE=$5
local GITLAB_TOKEN=$6

# Documentation: https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
local VARIABLE_RESPONSE_CODE=$(curl -s -i --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables" \
--form "key=$PARAMETER_KEY" --form "value=$PARAMETER_VALUE" --form "masked=$PARAMETER_MASKED" --form "environment_scope=$ENVIRONMENT_SCOPE" | head -n 1 | awk -F' ' '{print $2}')
# echo "VARIABLE_RESPONSE_CODE = $VARIABLE_RESPONSE_CODE"

if [ $VARIABLE_RESPONSE_CODE = 201 ];then
echoColor yellow "Variable $PARAMETER_KEY created into ${EKS_CLUSTER_NAME} environment"
elif [ $VARIABLE_RESPONSE_CODE = 400 ];then
# echoColor yellow "Variable $PARAMETER_KEY already exists, updating..."
updateVariable $@
elif [ $VARIABLE_RESPONSE_CODE = 401 ];then
echoColor red "Unauthorized access to GitLab API"
exit 1
else
echoColor red "Something wrong while saving $PARAMETER_KEY"
fi

}


## Update gitlab variable
# Parameters:
# 1 - PROJECT_ID
# 2 - PARAMETER_KEY
# 3 - PARAMETER_VALUE
# 4 - PARAMETER_MASKED
# 5 - ENVIRONMENT_SCOPE
# 6 - GITLAB_TOKEN
updateVariable(){
local PROJECT_ID=$1
local PARAMETER_KEY=$2
local PARAMETER_VALUE=$3
local PARAMETER_MASKED=$4
local ENVIRONMENT_SCOPE=$5
local GITLAB_TOKEN=$6

# Documentation: https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
local UPDATE_CODE=$(curl -s -i --request PUT --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables/${PARAMETER_KEY}?filter\[environment_scope\]=${ENVIRONMENT_SCOPE}" \
--form "value=$PARAMETER_VALUE" --form "masked=$PARAMETER_MASKED" | head -n 1 | awk -F' ' '{print $2}')
# echo "UPDATE_CODE= $UPDATE_CODE"
if [ $UPDATE_CODE = 200 ];then
echoColor green "$PARAMETER_KEY updated"
else
echoColor red "error while updating $PARAMETER_KEY, $UPDATE_CODE"
fi
}


## Create a new branch using eks-cluster-name as branch's name, or just start a new pipeline
# Parameters:
# 1 - PROJECT_ID
# 2 - BRANCH_NAME
# 3 - GITLAB_TOKEN
createBranch(){
local PROJECT_ID=$1
local BRANCH_NAME=$2
local GITLAB_TOKEN=$3

echoColor green "Creating branch named $BRANCH_NAME or justing starting a new pipeline"
# echo "https://gitlab.com/api/v4/projects/${PROJECT_ID}/repository/branches?branch=$1&ref=master"

# Documentation: https://docs.gitlab.com/ee/api/branches.html#create-repository-branch
local CREATE_BRANCH_CODE=$(curl -s -i --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/repository/branches?branch=$BRANCH_NAME&ref=master" | head -n 1 | awk -F' ' '{print $2}')
# echo "CREATE_BRANCH_CODE: $CREATE_BRANCH_CODE"

if [ $CREATE_BRANCH_CODE = 400 ];then
createPipeline $@
fi
}

## create a new pipeline
createPipeline(){
local PROJECT_ID=$1
local BRANCH_NAME=$2
local GITLAB_TOKEN=$3
# Documentation: https://docs.gitlab.com/ee/api/pipelines.html#create-a-new-pipeline
local RESPONSE_PIPE=$(curl -s --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/pipeline?ref=$BRANCH_NAME")
# echo "RESPONSE_PIPE: $RESPONSE_PIPE"
echoColor green "Pipeline url: $(echo $RESPONSE_PIPE | $VKPR_JQ -r '.web_url')"
}
11 changes: 6 additions & 5 deletions vkpr/init/src/utils/helper.sh → lib/functions/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ checkGlobalConfig(){
}

# Check if any Pod is already up to use and match with another tools
# $1: name of the pod
# $1: pod namespace
# $2: name of the pod
checkPodName(){
for pod in $($VKPR_KUBECTL get pods -n $VKPR_K8S_NAMESPACE --ignore-not-found | awk 'NR>1{print $1}'); do
if [[ "$pod" == "$1"* ]]; then
for pod in $($VKPR_KUBECTL get pods -n $1 --ignore-not-found | awk 'NR>1{print $1}'); do
if [[ "$pod" == "$2"* ]]; then
echo true # pod name found a match, then returns True
return
fi
Expand All @@ -32,15 +33,15 @@ checkPodName(){
}

# Create a new instance of DB in Postgres;
# $1: Postgres User / $2: Postgres Password / $3: Name of DB to create
# $1: Postgres User / $2: Postgres Password / $3: Name of DB to create / $4: Namespace
createDatabase(){
local PG_HOST="postgres-postgresql"
[[ ! -z $($VKPR_KUBECTL get pod -n $VKPR_K8S_NAMESPACE | grep pgpool) ]] && PG_HOST="postgres-postgresql-pgpool"
$VKPR_KUBECTL run init-db --rm -it --restart="Never" --namespace $VKPR_K8S_NAMESPACE --image docker.io/bitnami/postgresql-repmgr:11.14.0-debian-10-r12 --env="PGUSER=$1" --env="PGPASSWORD=$2" --env="PGHOST=${PG_HOST}" --env="PGPORT=5432" --env="PGDATABASE=postgres" --command -- psql --command="CREATE DATABASE $3"
}

# Check if exist some instace of DB with specified name in Postgres
# $1: Postgres User / $2: Postgres Password / $3: Name of DB to search
# $1: Postgres User / $2: Postgres Password / $3: Name of DB to search / $4: Namespace
checkExistingDatabase(){
local PG_HOST="postgres-postgresql"
[[ ! -z $($VKPR_KUBECTL get pod -n $VKPR_K8S_NAMESPACE | grep pgpool) ]] && PG_HOST="postgres-postgresql-pgpool"
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions vkpr/init/src/utils/validate.sh → lib/functions/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ validateDigitalOceanApiToken() {
if [[ "$1" =~ ^([A-Za-z0-9]{64})$ ]]; then
return
else
echoColor "red" "Invalid AWS Region, fix the credential with the command $(echoColor "bold" "rit set credential")."
echoColor "red" "Invalid Digital Ocean API Token, fix the credential with the command $(echoColor "bold" "rit set credential")."
exit
fi
}
Expand Down Expand Up @@ -102,49 +102,49 @@ validatePostgresqlPassword() {
# -----------------------------------------------------------------------------

validateKubectlVersion() {
if [[ -f $VKPR_KUBECTL ]] && [[ $($VKPR_KUBECTL version --short --client | awk -F " " '{print $3}') = $VKPR_TOOLS_KUBECTL ]]; then
if [[ ! -f $VKPR_KUBECTL ]] || [[ $($VKPR_KUBECTL version --short --client | awk -F " " '{print $3}') = $VKPR_TOOLS_KUBECTL ]]; then
return
else
else
rm $VKPR_KUBECTL
fi
}

validateHelmVersion() {
if [[ -f $VKPR_HELM ]] && [[ $($VKPR_HELM version --short | awk -F "+" '{print $1}') = $VKPR_TOOLS_HELM ]]; then
if [[ ! -f $VKPR_HELM ]] || [[ $($VKPR_HELM version --short | awk -F "+" '{print $1}') = $VKPR_TOOLS_HELM ]]; then
return
else
else
rm $VKPR_HELM
fi
}

validateK3DVersion() {
if [[ -f $VKPR_K3D ]] && [[ $($VKPR_K3D version | awk -F " " '{print $3}' | head -n1) = $VKPR_TOOLS_K3D ]]; then
if [[ ! -f $VKPR_K3D ]] || [[ $($VKPR_K3D version | awk -F " " '{print $3}' | head -n1) = $VKPR_TOOLS_K3D ]]; then
return
else
else
rm $VKPR_K3D
fi
}

validateJQVersion() {
if [[ -f $VKPR_JQ ]] && [[ $($VKPR_JQ --version) = $VKPR_TOOLS_JQ ]]; then
if [[ ! -f $VKPR_JQ ]] || [[ $($VKPR_JQ --version) = $VKPR_TOOLS_JQ ]]; then
return
else
else
rm $VKPR_JQ
fi
}

validateYQVersion() {
if [[ -f $VKPR_YQ ]] && [[ $($VKPR_YQ --version | awk -F " " '$4="v"$4 {print $4}') = $VKPR_TOOLS_YQ ]]; then
if [[ ! -f $VKPR_YQ ]] || [[ $($VKPR_YQ --version | awk -F " " '$4="v"$4 {print $4}') = $VKPR_TOOLS_YQ ]]; then
return
else
else
rm $VKPR_YQ
fi
}

validateK9SVersion() {
if [[ -f $VKPR_K9S ]] && [[ $($VKPR_K9S version --short | awk -F " " '{print $2}' | head -n1) = $VKPR_TOOLS_K9S ]]; then
if [[ ! -f $VKPR_K9S ]] || [[ $($VKPR_K9S version --short | awk -F " " '{print $2}' | head -n1) = $VKPR_TOOLS_K9S ]]; then
return
else
else
rm $VKPR_K9S
fi
}
16 changes: 16 additions & 0 deletions lib/functions/var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

VKPR_HOME=~/.vkpr
VKPR_GLOBAL=$CURRENT_PWD/vkpr.yaml
VKPR_CONFIG=$VKPR_HOME/config

##ALL RESOURCES EXCEPT CERT-MANAGER MUST BE UNDER THIS NAMESPACE
VKPR_K8S_NAMESPACE=vkpr

VKPR_GLAB=$VKPR_HOME/bin/glab
VKPR_K3D=$VKPR_HOME/bin/k3d
VKPR_ARKADE=$VKPR_HOME/bin/arkade
VKPR_KUBECTL=$VKPR_HOME/bin/kubectl
VKPR_HELM=$VKPR_HOME/bin/helm
VKPR_JQ=$VKPR_HOME/bin/jq
VKPR_YQ=$VKPR_HOME/bin/yq
15 changes: 15 additions & 0 deletions lib/functions/versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

VKPR_EXTERNAL_DNS_VERSION="5.4.9"
VKPR_WHOAMI_VERSION="2.5.0"
VKPR_KEYCLOAK_VERSION="6.1.2"
VKPR_LOKI_VERSION="2.5.0"
VKPR_PROMETHEUS_STACK_VERSION="23.1.1"
VKPR_POSTGRES_VERSION="8.2.5"
VKPR_INGRESS_NGINX_VERSION="4.0.13"
VKPR_CERT_VERSION="v1.5.3"
VKPR_KONG_VERSION="2.6.4"
VKPR_CONSUL_VERSION="0.38.0"
VKPR_VAULT_VERSION="0.18.0"
VKPR_ARGOCD_VERSION="3.29.4"
VKPR_ARGOCD_ADDON_APPLICATIONSET_VERSION="1.9.1"
12 changes: 5 additions & 7 deletions vkpr/apply/build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/bin/sh
#!/bin/bash

BIN_FOLDER=bin
BINARY_NAME_UNIX=run.sh
ENTRY_POINT_UNIX=main.sh
LIB_RESOURCES="../../lib/functions/*"

#bash-build:
mkdir -p $BIN_FOLDER
mkdir -p $BIN_FOLDER/src
cp $LIB_RESOURCES $BIN_FOLDER/src
cp -r src/* $BIN_FOLDER
mv $BIN_FOLDER/$ENTRY_POINT_UNIX $BIN_FOLDER/$BINARY_NAME_UNIX
chmod +x $BIN_FOLDER/$BINARY_NAME_UNIX

#bat-build:

#docker:
chmod +x $BIN_FOLDER/$BINARY_NAME_UNIX
Loading

0 comments on commit 9019d68

Please sign in to comment.