Skip to content

Commit

Permalink
add start docker task, create terraform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
J0hn-B committed Oct 14, 2023
1 parent e701d7c commit 2c0a036
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 40 deletions.
23 changes: 23 additions & 0 deletions .github/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3"

tasks:
# # Lind and test (local)

lint: # https://github.com/super-linter/super-linter
desc: "GitHub Super-Linter"
cmds:
- source .github/scripts/test.sh && gh_super_linter

# # Scan with Checkov (local)

checkov: # https://www.checkov.io/
desc: "Checkov, find cloud infra misconfigurations"
cmds:
- source .github/scripts/test.sh && checkov

# # Scan with Trivy (local)

trivy: # https://github.com/aquasecurity/trivy
desc: "Trivy security scanner"
cmds:
- source .github/scripts/test.sh && trivy
62 changes: 62 additions & 0 deletions .github/linters/.yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
###########################################
# These are the rules used for #
# linting all the yaml files in the stack #
# NOTE: #
# You can disable line with: #
# # yamllint disable-line #
###########################################
rules:
braces:
disable
# level: warning
# min-spaces-inside: 0
# max-spaces-inside: 0
# min-spaces-inside-empty: 1
# max-spaces-inside-empty: 5
brackets:
disable
# level: warning
# min-spaces-inside: 0
# max-spaces-inside: 0
# min-spaces-inside-empty: 1
# max-spaces-inside-empty: 5
colons:
level: warning
max-spaces-before: 0
max-spaces-after: 1
commas:
level: warning
max-spaces-before: 0
min-spaces-after: 1
max-spaces-after: 1
comments: disable
comments-indentation: disable
document-end: disable
document-start:
level: warning
present: true
empty-lines:
level: warning
max: 2
max-start: 0
max-end: 0
hyphens:
level: warning
max-spaces-after: 1
indentation:
level: warning
spaces: consistent
indent-sequences: true
check-multi-line-strings: false
key-duplicates: enable
line-length:
disable
# level: warning
# max: 180
# allow-non-breakable-words: true
# allow-non-breakable-inline-mappings: true
new-line-at-end-of-file: disable
new-lines:
type: unix
trailing-spaces: disable
13 changes: 13 additions & 0 deletions .github/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Scripts To Rule Them All

How CI works at GitHub and locally. <https://github.com/github/scripts-to-rule-them-all>

> Each of these scripts is responsible for a unit of work.
What is used in this project:

1. [GitHub Super Linter](https://github.com/github/super-linter/blob/main/docs/run-linter-locally.md)

2. [Checkov](https://www.checkov.io/)

3. [Trivy](https://github.com/aquasecurity/trivy)
18 changes: 18 additions & 0 deletions .github/scripts/server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -e

# # # WSL # # #

# Start Docker Desktop if it's not running
if ! docker ps; then
powershell.exe "Start-Process -FilePath 'C:\Program Files\Docker\Docker\Docker Desktop.exe'"
while ! docker ps; do # Wait for Docker to start
echo "==> Docker is starting"
sleep 3
done
else
echo "==> Docker is running"
fi

# # # WSL # # #
41 changes: 41 additions & 0 deletions .github/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

set -e

# # Create a bash function to run super-linter
function gh_super_linter() {
# Start docker desktop if it's not running
source "$PWD/.github/scripts/server.sh"

# Run the docker container
docker run --rm \
-e RUN_LOCAL=true \
--env-file ".github/super-linter.env" \
-v "$PWD":/tmp/lint github/super-linter:slim-v5
}

# Create a bash function to run checkov
function checkov() {
# Start docker desktop if it's not running
source "$PWD/.github/scripts/server.sh"

# Run the docker container
docker run -it --rm \
-v "$PWD":/tmp/lint --workdir /tmp/lint \
bridgecrew/checkov \
--directory /tmp/lint \
--soft-fail \
--quiet
}

# Create a bash function to run trivy
function trivy() {
# Start docker desktop if it's not running
source "$PWD/.github/scripts/server.sh"

# Run the docker container
docker run -it --rm \
-v "$PWD":/tmp/lint --workdir /tmp/lint \
aquasec/trivy:latest \
fs --scanners vuln,config,secret .
}
3 changes: 3 additions & 0 deletions .github/super-linter.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VALIDATE_ALL_CODEBASE=true
VALIDATE_KUBERNETES_KUBECONFORM=false
VALIDATE_TERRAFORM_TERRASCAN=false
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: CI

on:
push:
branches: [main]

permissions: read-all

jobs:
lint:
# Run GH Super-Linter against code base
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: cat .github/super-linter.env >> "$GITHUB_ENV"
- name: Lint Code Base
uses: super-linter/super-linter/slim@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEFAULT_BRANCH: main
11 changes: 8 additions & 3 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ env:
CLUSTER_NAME: kubepal
KUBE: export KUBECONFIG=/mnt/c/Users/{{.USER}}/.kube/config # WSL path to kubeconfig file on Windows host

# Include the helper tasks
# Include the github actions tasks
includes:
run: utils/helper_tasks.yml
run: .github/Taskfile.yml

tasks:
create:
Expand All @@ -25,8 +25,13 @@ tasks:
{{ if and (eq OS "linux") (ne (env "KUBECONFIG") "") }}Kubeconfig path: {{(env "KUBECONFIG")}}{{end}}
cmds:
# Source the server.sh script to start docker
- '{{if eq OS "linux"}}source .github/scripts/server.sh{{end}}'

# If KUBECONFIG env var is not set, set it to point to the Windows host
- '{{if eq OS "linux"}}[ -z "$KUBECONFIG" ] && echo "Set KUBECONFIG env var ==> {{.KUBE}}" && exit 1 || k3d cluster create {{.CLUSTER_NAME}} --api-port 6443 -p 8080:80@loadbalancer --agents 2{{end}}'
- '{{if eq OS "linux"}}[ -z "$KUBECONFIG" ] && echo "Set KUBECONFIG env var ==> {{.KUBE}}" && exit 1 \
|| k3d cluster create {{.CLUSTER_NAME}} --api-port 6443 -p 8080:80@loadbalancer --agents 2{{end}}'

# If host is Windows, create cluster
- '{{if eq OS "windows"}}k3d cluster create {{.CLUSTER_NAME}} --api-port 6443 -p 8080:80@loadbalancer --agents 2{{end}}'

Expand Down
60 changes: 60 additions & 0 deletions docs/kubepal_latest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#

This release focuses on Taskfile tasks and Go templating examples in tasks.

## Features and Tools

- `[Task](https://taskfile.dev/):` a cross-platform alternative to Make, supporting and showcasing `Go templates`.

- `Terraform:` bootstraps the cluster and applies the ArgoCD helm chart.

## Getting Started

> **In this example, a WSL distro using a kubeconfig file located in the Windows user's home directory**
1. Update the `KUBE` variable in [Taskfile.yml](../Taskfile.yaml) to match your path.

```yaml
env:
CLUSTER_NAME: kubepal
KUBE: export KUBECONFIG=/mnt/c/Users/{{.USER}}/.kube/config
```
2. Update the `kube/bootstrap/backend.tf` to match `the path of your local kubeconfig file`:

> Warning: Variables, locals, and functions may not be used here.

```hcl
terraform {
backend "kubernetes" {
secret_suffix = "state"
config_paths = [
"/mnt/c/Users/devops/.kube/config", # WSL path
"c:\\Users\\devops\\.kube\\config" # PowerShell path
]
}
}
```

3. Run `task --list` in WSL or PowerShell to see the available tasks and the updated kubeconfig path.

```shell
$ task
task: Available tasks for this project:
* create: Create and access a k3d cluster.
* delete: Delete cluster
* deploy: Bootstrap cluster and deploy apps
* tf_init: Initialize terraform
* run:connect: Get a shell to a running container
* run:pod: Create a temporary interactive pod, install some tools on it, and experiment
* run:start: Go template if statement
* run:test: Go template functions and examples using Powershell and Bash
```

4. The `task deploy` command will create the cluster, deploy ArgoCD, and deploy the apps.
5. The `task delete` command will delete the cluster.
2 changes: 1 addition & 1 deletion kube/base/argocd_chart_values.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ configs:
# githubAppInstallationID: 2
# githubAppEnterpriseBaseUrl: https://ghe.example.com/api/v3
# githubAppPrivateKey: |
# -----BEGIN OPENSSH PRIVATE KEY-----
# -----BEGIN OPENSSH PRIVATE KEY----- #gitleaks:allow
# ...
# -----END OPENSSH PRIVATE KEY-----
# https-creds:
Expand Down
13 changes: 13 additions & 0 deletions kube/bootstrap/providers.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Extract the config_path values from the backend.tf file
// and use it to configure the kubernetes provider
// and the helm provider
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.0"
}
}
}

locals {

Expand Down Expand Up @@ -29,3 +41,4 @@ provider "helm" {
}
}


30 changes: 30 additions & 0 deletions kube/bootstrap/tests/argocd.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

run "argocd_helm_release" {

command = plan

# Verify ArgoCD helm chart name
assert {
condition = helm_release.argocd.chart == "argo-cd"
error_message = "ArgoCD helm chart did not match expected"
}

# Verify ArgoCD helm chart repository
assert {
condition = helm_release.argocd.repository == "https://argoproj.github.io/argo-helm"
error_message = "ArgoCD helm chart repository did not match expected"
}

}

run "argocd_ingress" {

command = plan

# Verify ArgoCD yaml manifest apiVersion
assert {
condition = kubernetes_manifest.argocd_ingress.manifest.apiVersion == "networking.k8s.io/v1"
error_message = "ArgoCD yaml manifest apiVersion did not match expected"
}

}
36 changes: 0 additions & 36 deletions utils/helper_tasks.yml

This file was deleted.

0 comments on commit 2c0a036

Please sign in to comment.