Skip to content

Commit

Permalink
feat(initial-release): Initial release create docker image with
Browse files Browse the repository at this point in the history
kubectl 1.29.2
kubectlxtra 1.0.0
kustomize 5.4.1
opkustomize 1.0.0
helm 3.14.3
kubeconform 0.6.3
  • Loading branch information
alexbaeza committed Apr 9, 2024
0 parents commit 069e1e2
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# macOS
._*
.DS_Store
.LSOverride

# Windows
[Dd]esktop.ini
ehthumbs.db
ehthumbs_vista.db
[Tt]humbs.db

# Backups
*.bak

# Logs
*.log
log/
logs/

# Temporary
*~
*.tmp
*.temp
tmp/
temp/

# Vim
.netrwhist
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]*.un~
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

#IDEA
.idea

#VScode
.vscode
35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM alpine:3.19.1

# Versions can be overriden via --build-arg
# e.g docker build -t <image_name> --build-arg KUBECTL_VERSION=X.XX.X .
ARG KUBECTL_VERSION=1.29.2
ARG KUSTOMIZE_VERSION=5.4.1
ARG HELM_VERSION=3.14.3
ARG KUBECONFORM_VERSION=0.6.3

COPY ./scripts/binary_installer.sh ./binary_installer.sh

#Dependencies
RUN apk add --update --no-cache curl ca-certificates bash git gettext

#Change to shell to bash
SHELL ["/bin/bash", "-c"]

# Uses custom script to install binaries
# ./binary_installer.sh <binary_name> <script_mode> <release_url> <install_path> [verify_cmd] [--debug]
# If ARCH is needed use '{INJECT_ARCH}' to let the script inject the value into the urls

#Install kubectl
RUN ./binary_installer.sh kubectl binary "https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/{INJECT_ARCH}/kubectl" /usr/bin/ help --debug
## Install kubectlxtra (wrapper)
RUN ./binary_installer.sh kubectlxtra binary "https://github.com/alexbaeza/kubectlxtra/raw/main/kubectlxtra.sh" /usr/bin/ help
## Install kustomize
RUN ./binary_installer.sh kustomize tar "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv${KUSTOMIZE_VERSION}/kustomize_v${KUSTOMIZE_VERSION}_linux_{INJECT_ARCH}.tar.gz" /usr/bin/ version
## Install opkustomize (wrapper)
RUN ./binary_installer.sh opkustomize binary "https://github.com/alexbaeza/opkustomize/raw/main/opkustomize.sh" /usr/bin/ help
## Install Helm
RUN ./binary_installer.sh helm tar "https://get.helm.sh/helm-v${HELM_VERSION}-linux-{INJECT_ARCH}.tar.gz" /usr/bin/ "version"
## Install kubeconform
RUN ./binary_installer.sh kubeconform tar "https://github.com/yannh/kubeconform/releases/download/v${KUBECONFORM_VERSION}/kubeconform-linux-{INJECT_ARCH}.tar.gz" /usr/bin/ "-v" --debug

WORKDIR /apps
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Alejandro Baeza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# All-in-one Kubernetes CI/CD Docker Image

This all-in-one Docker image is designed to provide a minimal environment with all the necessary tools for a CI/CD
deployment in Kubernetes, no more installing tools on your ci/cd workflows.

## Tools Included

| Tool | Description | Version |
| --------------------------------------------------------- | ----------------------------------------------------------------- | ------- |
| [kubectl](https://github.com/kubernetes/kubectl) | Kubernetes command-line tool. | 1.29.2 |
| [kubectlxtra](https://github.com/alexbaeza/kubectlxtra) | Wrapper around kubectl with additional functionalities. | 1.0.0 |
| [kustomize](https://github.com/kubernetes-sigs/kustomize) | Customization of Kubernetes YAML configurations. | 5.4.1 |
| [opkustomize](https://github.com/alexbaeza/opkustomize) | Wrapper around kustomize with additional functionalities. | 1.0.0 |
| [helm](https://github.com/helm/helm) | Kubernetes package manager. | 3.14.3 |
| [kubeconform](https://github.com/yannh/kubeconform) | Tool for validating Kubernetes YAML files against best practices. | 0.6.3 |

## Building from Source

If you want to build this image from source, ensure you have Docker installed and follow these steps:

1. Clone the repository containing the Dockerfile and related scripts.
2. Navigate to the directory containing the Dockerfile.
3. Build the Docker image using the following command:

```bash
docker build -t <image_name>:<tag> .
```

### Selecting Tool Versions

This Docker image allows you to select specific versions of the tools included. The versions are defined as build
arguments in the Dockerfile. To select a particular version for a tool, follow these steps:

1. Identify the name of the tool and its corresponding build argument in the Dockerfile. Here are the tools and their
associated build arguments:

- **kubectl**: `KUBECTL_VERSION`
- **kustomize**: `KUSTOMIZE_VERSION`
- **Helm**: `HELM_VERSION`
- **kubeconform**: `KUBECONFORM_VERSION`

2. When building the Docker image using the `docker build` command, provide the desired versions as build arguments
using the `--build-arg` flag. For example:

```bash
#Override kubectl and kustomize versions only
docker build -t k8s-ci-cd \
--build-arg KUBECTL_VERSION=1.29.1 \
--build-arg KUSTOMIZE_VERSION=3.5.4 \
. --no-cache
```

## Executing Bash Shell in Container

To open a Bash shell within the container, you can use the following command:

```shell
docker run -it k8s-ci-cd bash
```

## License

This Docker image is released under the [MIT License](LICENSE). Feel free to use and modify them
according to your needs.
172 changes: 172 additions & 0 deletions scripts/binary_installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/usr/bin/env bash

# This script is used to install binaries from remote URLs to a specified directory.
# Usage:
# ./binary_installer.sh <binary_name> <script_mode> <release_url> <install_path> [verify_cmd] [--debug]
#
# Parameters:
# <binary_name>: Name of the binary to install.
# <script_mode>: Mode of installation, either "tar" or "binary".
# <release_url>: URL of the binary release.
# <install_path>: Directory where the binary will be installed.
# [verify_cmd]: Optional command to verify the installation.
# [--debug]: Optional flag to enable debug mode for verbose output.
#
# Note:
# - If <binary_name> already exists in <install_path>, the installation fails.
# - Supported values for <script_mode> are "tar" and "binary".
# - Supported architectures: amd64, arm64, ppc64le, s390x.
# - For ARM64 architecture, if the binary is not available, it falls back to downloading the amd64 binary.
# - The script attempts to determine the OS and architecture automatically.
# - After installation, the script can optionally perform a verification command.
# - Use the --debug flag to enable debug mode for verbose output.

set -e

# Function to emulate `readlink -f` behavior on macOS
# See: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
function readlink_f() {
local target_file="$1"
local phys_dir
local result

# Loop until the target_file is not a symbolic link
while [ -L "$target_file" ]; do
# Get the physical directory path of the current target file
phys_dir=$(cd "$(dirname "$target_file")" && pwd -P)
# Resolve the symbolic link and update target_file with its target
target_file=$(readlink "$target_file")
# Extract the base name of the target file
target_file=$(basename "$target_file")
# Combine the physical directory path and the base name to get the new target_file
target_file="$phys_dir/$target_file"
done

# Get the final physical directory path of the target file
phys_dir=$(cd "$(dirname "$target_file")" && pwd -P)
# Combine the final physical directory path and the base name to get the result
result="$phys_dir/$(basename "$target_file")"
# Output the final result
echo "$result"
}

# Function to echo debug messages
debug_echo() {
if [ "$debug" = true ]; then
echo "$@"
fi
}

# Function to install the binary
install_binary() {
local binary_name=$1
local script_mode=$2
local release_url=$3
local install_path=$4
local verify_cmd=$5

if [ -e "${install_path}${binary_name}" ]; then
echo "[FAIL] ${install_path}${binary_name} exists. Remove it first."
exit 1
fi

local tmpDir
tmpDir=$(mktemp -d) || {
echo "[FAIL] Could not create temp dir."
exit 1
}
trap 'rm -rf "$tmpDir"' EXIT ERR

pushd "$tmpDir" >&/dev/null || exit 1

local opsys
local arch
opsys=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)

# Supported values of 'arch': amd64, arm64, ppc64le, s390x
case "$arch" in
x86_64) arch=amd64 ;;
arm64 | aarch64) arch=arm64 ;;
ppc64le) arch=ppc64le ;;
s390x) arch=s390x ;;
*) arch=amd64 ;;
esac

# Constructing RELEASE_URL with the current architecture
local RELEASE_URL="${release_url//"{INJECT_ARCH}"/$arch}"
debug_echo "[DEBUG] Attempting to install $binary_name from $RELEASE_URL to $install_path"

# Extracting the binary based on script_mode
local extracted_binary

if [[ "$script_mode" == "tar" ]]; then
# Downloading the resource and saving as downloaded_resource
debug_echo "[DEBUG] Downloading tool using tar mode"
curl -sL -o downloaded_resource "$RELEASE_URL"
tar xzf downloaded_resource
rm -f downloaded_resource
# Let's find the find the executable with a max depth of 2
extracted_binary=$(find . -maxdepth 2 -type f -executable -print -quit)
debug_echo "[DEBUG] Found binary is $extracted_binary"
elif [[ "$script_mode" == "binary" ]]; then
# Downloading the resource and saving
debug_echo "[DEBUG] Downloading tool using binary mode"
curl -sLO "$RELEASE_URL"
extracted_binary=$(basename "$RELEASE_URL")
debug_echo "[DEBUG] Found binary is $extracted_binary"
fi

# Installing the binary
if [[ -n "$extracted_binary" ]]; then
chmod +x "./$extracted_binary"
mv "./$extracted_binary" "${install_path}${binary_name}"
echo "[OK] Installed successfully"
else
echo "[FAIL] Failed while installing binary."
exit 1
fi

popd >&/dev/null || exit 1

# Verifying the installation with verify_cmd if provided
if [[ "$verify_cmd" ]]; then
debug_echo "[DEBUG] Attempting to verify installation using command '${install_path}${binary_name} $verify_cmd'"
if output=$("${install_path}${binary_name}" "$verify_cmd" 2>&1); then
echo "[OK] Verification command successful"
else
echo "[FAIL] Verification command failed: $output"
exit 1
fi
fi
}

# Main
debug=false
binary_name=$1
script_mode=$2
release_url=$3
install_path="$(readlink_f "$4")/"
verify_cmd=$5

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
echo "[DEBUG] Enabling debug mode for verbose output"
debug=true
shift
;;
*)
shift
;;
esac
done

if [ ! -d "$install_path" ]; then
echo "$install_path does not exist. You need to create it first."
exit 1
fi

echo "[INFO] Using binary installer in $script_mode mode"
install_binary "$binary_name" "$script_mode" "$release_url" "$install_path" "$verify_cmd"

0 comments on commit 069e1e2

Please sign in to comment.