Skip to content

Commit 5222fae

Browse files
committed
Kubernetes-dashboard: feat: add Docker support and enhance application structure
- Introduced `.dockerignore` to exclude unnecessary files from Docker builds. - Created `Dockerfile` for building the application image with Python 3.9 and necessary dependencies. - Added `docker-compose.yml` for simplified multi-container management, including a Redis service for future enhancements. - Refactored `app.py` to improve logging and structure. - Introduced `dashboard_types.py` for type definitions related to system metrics and Kubernetes resources. - Updated `kubernetes_client.py` to utilize new type definitions and improve code clarity. Signed-off-by: NotHarshhaa <reddyharshhaa12@gmail.com>
1 parent 06e5d32 commit 5222fae

File tree

14 files changed

+1353
-987
lines changed

14 files changed

+1353
-987
lines changed

β€Ž.dockerignoreβ€Ž

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Python bytecode and cache files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
MANIFEST
23+
24+
# Virtual environments
25+
venv/
26+
env/
27+
ENV/
28+
env.bak/
29+
venv.bak/
30+
31+
# IDE
32+
.vscode/
33+
.idea/
34+
*.swp
35+
*.swo
36+
*~
37+
38+
# OS
39+
.DS_Store
40+
.DS_Store?
41+
._*
42+
.Spotlight-V100
43+
.Trashes
44+
ehthumbs.db
45+
Thumbs.db
46+
47+
# Logs
48+
*.log
49+
logs/
50+
51+
# Environment variables
52+
.env
53+
.env.local
54+
.env.production
55+
56+
# Backup files
57+
*.backup
58+
*.bak
59+
60+
# Kubernetes config (sensitive)
61+
kubeconfig*
62+
*.kubeconfig
63+
64+
# Test coverage
65+
htmlcov/
66+
.coverage
67+
.coverage.*
68+
coverage.xml
69+
*.cover
70+
.hypothesis/
71+
.pytest_cache/
72+
73+
# Jupyter Notebook
74+
.ipynb_checkpoints
75+
76+
# pyenv
77+
.python-version
78+
79+
# Docker
80+
.dockerignore
81+
82+
# Git
83+
.git/
84+
.gitignore
85+
86+
# Documentation
87+
*.md
88+
!README.md
89+
ARCHITECTURE.md
90+
91+
# Node modules (if any frontend build tools)
92+
node_modules/
93+
npm-debug.log*
94+
yarn-debug.log*
95+
yarn-error.log*
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
name: Build and Push to Docker Hub
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
env:
10+
REGISTRY: docker.io
11+
IMAGE_NAME: kubernetes-dashboard
12+
13+
jobs:
14+
lint:
15+
runs-on: ubuntu-latest
16+
name: Code Quality Checks
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: '3.9'
26+
cache: 'pip' # Enable pip caching
27+
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install flake8 black isort mypy
32+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
33+
34+
- name: Lint with flake8
35+
run: |
36+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
37+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
38+
39+
- name: Check code formatting with black
40+
run: black --check --diff .
41+
42+
- name: Check import sorting with isort
43+
run: isort --check-only --diff .
44+
45+
- name: Type checking with mypy
46+
run: mypy --ignore-missing-imports .
47+
48+
security-scan:
49+
runs-on: ubuntu-latest
50+
name: Security Scanning
51+
52+
steps:
53+
- name: Checkout code
54+
uses: actions/checkout@v4
55+
56+
- name: Run Bandit Security Linter
57+
uses: securecodewarrior/github-action-bandit-scan@v1
58+
with:
59+
path: '.'
60+
61+
- name: Run Semgrep
62+
uses: returntocorp/semgrep-action@v1
63+
with:
64+
config: >-
65+
p/security-audit
66+
p/secrets
67+
p/xss
68+
p/command-injection
69+
70+
- name: Check dependencies for vulnerabilities
71+
run: |
72+
python -m pip install --upgrade pip
73+
pip install safety
74+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
75+
safety check --json --output safety-report.json || true
76+
77+
- name: Upload safety report
78+
uses: actions/upload-artifact@v3
79+
with:
80+
name: safety-report
81+
path: safety-report.json
82+
83+
test:
84+
runs-on: ubuntu-latest
85+
name: Run Tests
86+
strategy:
87+
matrix:
88+
python-version: ['3.9', '3.11'] # Reduced from 4 to 2 versions
89+
90+
steps:
91+
- name: Checkout code
92+
uses: actions/checkout@v4
93+
94+
- name: Set up Python ${{ matrix.python-version }}
95+
uses: actions/setup-python@v4
96+
with:
97+
python-version: ${{ matrix.python-version }}
98+
cache: 'pip' # Enable pip caching
99+
100+
- name: Install dependencies
101+
run: |
102+
python -m pip install --upgrade pip
103+
pip install -r requirements.txt
104+
pip install pytest pytest-cov pytest-mock
105+
106+
- name: Run tests with coverage
107+
run: |
108+
pytest --cov=. --cov-report=xml --cov-report=term-missing --tb=short
109+
110+
- name: Upload coverage to Codecov
111+
uses: codecov/codecov-action@v3
112+
with:
113+
file: ./coverage.xml
114+
flags: unittests
115+
name: codecov-umbrella
116+
117+
validate-docker:
118+
runs-on: ubuntu-latest
119+
name: Validate Docker Configuration
120+
121+
steps:
122+
- name: Checkout code
123+
uses: actions/checkout@v4
124+
125+
- name: Validate Dockerfile
126+
run: |
127+
test -f Dockerfile || (echo "Dockerfile not found" && exit 1)
128+
docker build --dry-run -f Dockerfile .
129+
130+
- name: Build test image
131+
run: |
132+
docker build -t test-image .
133+
134+
- name: Test container startup
135+
run: |
136+
docker run -d --name test-container -p 5000:5000 test-image
137+
sleep 10
138+
curl -f http://localhost:5000/health || exit 1
139+
docker stop test-container
140+
docker rm test-container
141+
142+
build:
143+
needs: [lint, security-scan, test, validate-docker]
144+
runs-on: ubuntu-latest
145+
name: Build Docker Image
146+
if: github.event_name != 'pull_request'
147+
148+
outputs:
149+
image-digest: ${{ steps.build.outputs.digest }}
150+
image-tag: ${{ steps.meta.outputs.tags }}
151+
152+
steps:
153+
- name: Checkout code
154+
uses: actions/checkout@v4
155+
156+
- name: Set up Docker Buildx
157+
uses: docker/setup-buildx-action@v3
158+
159+
- name: Log in to Docker Hub
160+
uses: docker/login-action@v3
161+
with:
162+
username: ${{ secrets.DOCKER_USERNAME }}
163+
password: ${{ secrets.DOCKER_PASSWORD }}
164+
165+
- name: Extract metadata
166+
id: meta
167+
uses: docker/metadata-action@v5
168+
with:
169+
images: ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}
170+
tags: |
171+
type=raw,value=latest,enable={{is_default_branch}}
172+
173+
- name: Build and push Docker image
174+
id: build
175+
uses: docker/build-push-action@v5
176+
with:
177+
context: .
178+
platforms: linux/amd64 # Reduced to single platform for faster builds
179+
push: true
180+
tags: ${{ steps.meta.outputs.tags }}
181+
labels: ${{ steps.meta.outputs.labels }}
182+
cache-from: type=gha
183+
cache-to: type=gha,mode=max
184+
build-args: |
185+
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
186+
VCS_REF=${{ github.sha }}
187+
# Optimize build layers
188+
target: production
189+
190+
- name: Generate SBOM
191+
uses: anchore/sbom-action@v0
192+
with:
193+
image: ${{ steps.meta.outputs.tags }}
194+
format: spdx-json
195+
output-file: sbom.spdx.json
196+
197+
- name: Upload SBOM artifact
198+
uses: actions/upload-artifact@v3
199+
with:
200+
name: sbom
201+
path: sbom.spdx.json
202+
203+
security-scan-image:
204+
needs: build
205+
runs-on: ubuntu-latest
206+
name: Scan Docker Image
207+
if: github.event_name != 'pull_request'
208+
209+
steps:
210+
- name: Run Trivy vulnerability scanner
211+
uses: aquasecurity/trivy-action@master
212+
with:
213+
image-ref: ${{ needs.build.outputs.image-tag }}
214+
format: 'table'
215+
216+
- name: Generate SARIF report
217+
uses: aquasecurity/trivy-action@master
218+
with:
219+
image-ref: ${{ needs.build.outputs.image-tag }}
220+
format: 'sarif'
221+
output: 'trivy-results.sarif'
222+
223+
- name: Upload Trivy scan results as artifact
224+
uses: actions/upload-artifact@v3
225+
with:
226+
name: trivy-scan-results
227+
path: trivy-results.sarif
228+
229+
notify:
230+
needs: [lint, security-scan, test, validate-docker, build, security-scan-image]
231+
runs-on: ubuntu-latest
232+
name: Build Status
233+
if: always()
234+
235+
steps:
236+
- name: Notify on success
237+
if: needs.lint.result == 'success' && needs.security-scan.result == 'success' && needs.test.result == 'success' && needs.validate-docker.result == 'success' && (needs.build.result == 'success' || needs.build.result == 'skipped') && (needs.security-scan-image.result == 'success' || needs.security-scan-image.result == 'skipped')
238+
run: |
239+
echo "βœ… All checks passed! Image ${{ needs.build.outputs.image-tag }} built and pushed successfully"
240+
241+
- name: Notify on failure
242+
if: needs.lint.result == 'failure' || needs.security-scan.result == 'failure' || needs.test.result == 'failure' || needs.validate-docker.result == 'failure' || needs.build.result == 'failure' || needs.security-scan-image.result == 'failure'
243+
run: |
244+
echo "❌ Some checks failed!"
245+
exit 1

β€ŽDockerfileβ€Ž

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Use Python 3.9 slim image as base
2+
FROM python:3.9-slim
3+
4+
# Set working directory
5+
WORKDIR /app
6+
7+
# Set environment variables
8+
ENV PYTHONDONTWRITEBYTECODE=1 \
9+
PYTHONUNBUFFERED=1 \
10+
FLASK_APP=app.py \
11+
FLASK_ENV=production \
12+
API_PORT=5000
13+
14+
# Install system dependencies
15+
RUN apt-get update && apt-get install -y \
16+
curl \
17+
gnupg \
18+
wget \
19+
&& rm -rf /var/lib/apt/lists/*
20+
21+
# Install Trivy for security scanning
22+
RUN wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | apt-key add - \
23+
&& echo "deb https://aquasecurity.github.io/trivy-repo/deb buster main" | tee -a /etc/apt/sources.list.d/trivy.list \
24+
&& apt-get update \
25+
&& apt-get install -y trivy \
26+
&& rm -rf /var/lib/apt/lists/*
27+
28+
# Copy requirements first for better caching
29+
COPY requirements.txt .
30+
31+
# Install Python dependencies
32+
RUN pip install --no-cache-dir -r requirements.txt
33+
34+
# Copy application code
35+
COPY . .
36+
37+
# Create non-root user for security
38+
RUN useradd --create-home --shell /bin/bash app \
39+
&& chown -R app:app /app
40+
USER app
41+
42+
# Health check
43+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
44+
CMD curl -f http://localhost:5000/health || exit 1
45+
46+
# Expose port
47+
EXPOSE 5000
48+
49+
# Start the application
50+
CMD ["python", "app.py"]

0 commit comments

Comments
Β (0)