Skip to content

Commit 6829594

Browse files
Initialize container setup with Docker and Docker Compose
1 parent 9fd96ee commit 6829594

File tree

5 files changed

+276
-1
lines changed

5 files changed

+276
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ k8sgpt_*.deb
3636

3737
# Docs
3838
microk8s_setup_and_ai_interaction_command_line_instructions.md
39-
/helm-chart/README.md
39+
/helm-chart/README.md
40+
41+
# Models
42+
models/*.gguf

Dockerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Stage 1: Builder
2+
FROM rust:1.75-slim-bookworm as builder
3+
4+
# Install build dependencies
5+
RUN apt-get update && apt-get install -y \
6+
pkg-config \
7+
libssl-dev \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
# Create a new empty shell project
11+
WORKDIR /usr/src/app
12+
COPY Cargo.toml Cargo.lock ./
13+
14+
# Build dependencies - this is the caching Docker layer
15+
RUN mkdir src && \
16+
echo "fn main() {}" > src/main.rs && \
17+
cargo build --release && \
18+
rm -rf src
19+
20+
# Build actual source code
21+
COPY src ./src
22+
RUN touch src/main.rs && cargo build --release
23+
24+
# Stage 2: Runtime
25+
FROM debian:bookworm-slim
26+
27+
# Install runtime dependencies
28+
RUN apt-get update && apt-get install -y \
29+
ca-certificates \
30+
libssl3 \
31+
&& rm -rf /var/lib/apt/lists/* \
32+
&& groupadd -r appuser && useradd -r -g appuser appuser
33+
34+
# Copy the binary from builder
35+
COPY --from=builder /usr/src/app/target/release/kube_app /usr/local/bin/
36+
37+
# Set proper permissions
38+
RUN chown appuser:appuser /usr/local/bin/kube_app
39+
40+
# Use non-root user
41+
USER appuser
42+
43+
# Run the binary
44+
CMD ["kube_app"]
45+

docker-compose.test.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
version: '3.8'
2+
3+
services:
4+
localai:
5+
extends:
6+
file: docker-compose.yml
7+
service: localai
8+
environment:
9+
- LOCALAI_DEBUG=true
10+
- TEST_MODE=true
11+
healthcheck:
12+
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
13+
interval: 10s
14+
timeout: 5s
15+
retries: 3
16+
start_period: 20s
17+
18+
k8sgpt:
19+
extends:
20+
file: docker-compose.yml
21+
service: k8sgpt
22+
environment:
23+
- K8SGPT_TEST_MODE=true
24+
- K8SGPT_LOG_LEVEL=debug
25+
healthcheck:
26+
test: ["CMD", "k8sgpt version"]
27+
interval: 10s
28+
timeout: 5s
29+
retries: 3
30+
31+
kube-app:
32+
extends:
33+
file: docker-compose.yml
34+
service: kube-app
35+
environment:
36+
- RUST_LOG=debug
37+
- TEST_ENV=true
38+
- MOCK_K8S_API=true
39+
depends_on:
40+
localai:
41+
condition: service_healthy
42+
k8sgpt:
43+
condition: service_healthy
44+
healthcheck:
45+
test: ["CMD", "/app/health_check"]
46+
interval: 10s
47+
timeout: 5s
48+
retries: 3
49+
50+
volumes:
51+
test-models:
52+
driver: local
53+
test-output:
54+
driver: local
55+
56+
networks:
57+
test-network:
58+
driver: bridge
59+

docker-compose.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
version: '3.8'
2+
3+
services:
4+
localai:
5+
image: localai/localai:latest
6+
volumes:
7+
- ./models:/build/models
8+
- ./output:/build/output
9+
environment:
10+
- LOCALAI_MODELS_PATH=/build/models
11+
- LOCALAI_MODEL=llama-3.2-1b-instruct
12+
ports:
13+
- "8080:8080"
14+
healthcheck:
15+
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
16+
interval: 30s
17+
timeout: 10s
18+
retries: 3
19+
20+
k8sgpt:
21+
image: ghcr.io/k8sgpt-ai/k8sgpt:v0.3.41
22+
depends_on:
23+
localai:
24+
condition: service_healthy
25+
environment:
26+
- K8SGPT_BACKEND=localai
27+
- K8SGPT_BACKEND_URL=http://localai:8080
28+
- K8SGPT_MODEL=llama-3.2-1b-instruct
29+
- K8SGPT_CACHE=true
30+
volumes:
31+
- ./.k8sgpt:/root/.k8sgpt
32+
healthcheck:
33+
test: ["CMD", "k8sgpt", "version"]
34+
interval: 30s
35+
timeout: 10s
36+
retries: 3
37+
38+
kube-app:
39+
build:
40+
context: .
41+
dockerfile: Dockerfile
42+
depends_on:
43+
localai:
44+
condition: service_healthy
45+
k8sgpt:
46+
condition: service_healthy
47+
volumes:
48+
- ${KUBECONFIG:-~/.kube/config}:/root/.kube/config:ro
49+
50+
volumes:
51+
models:
52+
output:
53+

setup.zsh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env zsh
2+
3+
# Enable zsh-specific error handling
4+
setopt ERR_EXIT
5+
setopt PIPE_FAIL
6+
setopt NO_UNSET
7+
8+
# Color definitions
9+
typeset -A colors
10+
colors=(
11+
[red]=$'\e[31m'
12+
[green]=$'\e[32m'
13+
[yellow]=$'\e[33m'
14+
[reset]=$'\e[0m'
15+
)
16+
17+
# Function to print messages with color
18+
print_message() {
19+
local color="${colors[$1]}"
20+
local message="$2"
21+
echo "${color}${message}${colors[reset]}"
22+
}
23+
24+
# Function to check prerequisites
25+
check_prerequisites() {
26+
local -a required_tools=(docker docker-compose curl)
27+
28+
for tool in $required_tools; do
29+
if ! command -v $tool &> /dev/null; then
30+
print_message "red" "Error: $tool is not installed"
31+
return 1
32+
fi
33+
done
34+
35+
print_message "green" "All prerequisites are satisfied"
36+
}
37+
38+
# Function to create required directories
39+
setup_directories() {
40+
local -a dirs=(models output .k8sgpt)
41+
42+
for dir in $dirs; do
43+
if [[ ! -d $dir ]]; then
44+
mkdir -p $dir
45+
print_message "green" "Created directory: $dir"
46+
fi
47+
done
48+
}
49+
50+
# Function to download model if not present
51+
download_model() {
52+
local model_path="models/llama-3.2-1b-instruct-q4_k_m.gguf"
53+
local model_url="https://huggingface.co/hugging-quants/Llama-3.2-1B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-1b-instruct-q4_k_m.gguf"
54+
55+
if [[ ! -f $model_path ]]; then
56+
print_message "yellow" "Downloading model..."
57+
curl -L $model_url -o $model_path
58+
if [[ $? -eq 0 ]]; then
59+
print_message "green" "Model downloaded successfully"
60+
else
61+
print_message "red" "Failed to download model"
62+
return 1
63+
fi
64+
else
65+
print_message "green" "Model already exists"
66+
fi
67+
}
68+
69+
# Function to run in development mode
70+
run_dev() {
71+
print_message "yellow" "Starting services in development mode..."
72+
docker-compose up -d
73+
print_message "green" "Services started successfully"
74+
}
75+
76+
# Function to run in test mode
77+
run_test() {
78+
print_message "yellow" "Starting services in test mode..."
79+
docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d
80+
print_message "green" "Test environment started successfully"
81+
}
82+
83+
# Main function
84+
main() {
85+
local mode=${1:-dev}
86+
87+
print_message "yellow" "Setting up environment..."
88+
89+
# Check prerequisites
90+
check_prerequisites || return 1
91+
92+
# Setup directories
93+
setup_directories || return 1
94+
95+
# Download model if needed
96+
download_model || return 1
97+
98+
# Run in specified mode
99+
case $mode in
100+
dev)
101+
run_dev
102+
;;
103+
test)
104+
run_test
105+
;;
106+
*)
107+
print_message "red" "Invalid mode. Use 'dev' or 'test'"
108+
return 1
109+
;;
110+
esac
111+
}
112+
113+
# Run main function with provided arguments
114+
main "$@"
115+

0 commit comments

Comments
 (0)