CLI Tools and Scripts Reference
CLI Tools and Scripts Reference
Section titled “CLI Tools and Scripts Reference”Complete reference for OptiPod command-line tools and utility scripts.
Overview
Section titled “Overview”OptiPod provides several command-line tools and scripts to help with:
- Viewing and analyzing recommendations
- Setting up development environments
- Managing Prometheus authentication
- Validating documentation
- Running local CI checks
All scripts are located in the scripts/ directory of the OptiPod repository.
optipod-recommendation-report.sh
Section titled “optipod-recommendation-report.sh”Generates comprehensive reports comparing current CPU/memory requests/limits versus OptiPod recommended values.
Synopsis
Section titled “Synopsis”scripts/optipod-recommendation-report.sh [OPTIONS]Description
Section titled “Description”The recommendation report script reads OptiPod recommendations from workload annotations and generates detailed comparison reports. It supports both JSON and HTML output formats, with filtering and sorting capabilities.
Requirements
Section titled “Requirements”kubectl- Kubernetes command-line tooljq- JSON processor- Access to a Kubernetes cluster with OptiPod installed
Options
Section titled “Options”—output, -o json|html Output format. Default: json
json- Machine-readable JSON formathtml- Interactive HTML report with filtering and sorting
—file, -f PATH Write output to file instead of stdout
—namespace, -n NAMESPACE Restrict report to a specific namespace. Default: all namespaces
—kinds KINDS Comma-separated list of workload kinds to scan. Default: deploy,sts,ds
deploy- Deploymentssts- StatefulSetsds- DaemonSets
—help, -h Show help message
Examples
Section titled “Examples”Generate JSON report (default):
./scripts/optipod-recommendation-report.shGenerate HTML report:
./scripts/optipod-recommendation-report.sh --output html --file report.htmlopen report.html # macOSxdg-open report.html # LinuxFilter by namespace:
./scripts/optipod-recommendation-report.sh --namespace productionOnly scan Deployments:
./scripts/optipod-recommendation-report.sh --kinds deployScan Deployments and StatefulSets:
./scripts/optipod-recommendation-report.sh --kinds deploy,stsJSON Output Format
Section titled “JSON Output Format”The JSON output includes:
Metadata:
{ "generatedAt": "2025-01-28T10:30:00Z", "context": "my-cluster", "namespace": null, "kinds": ["deploy", "sts", "ds"]}Counts:
{ "counts": { "scannedWorkloads": 10, "scannedNamespaces": 3, "workloadsWithRecommendations": 8, "recordsWithRecommendations": 12, "warningRecords": 2 }}Totals (aggregated across all workloads):
{ "totals": { "cpuRequests": { "currentMilli": 5000, "recommendedMilli": 3000, "deltaMilli": -2000 }, "memoryRequests": { "currentBytes": 10737418240, "recommendedBytes": 5368709120, "deltaBytes": -5368709120 }, "impact": { "cpuRequests": { "currentMilli": 15000, "recommendedMilli": 9000, "deltaMilli": -6000 } } }}Records (per-container details):
{ "records": [ { "kind": "Deployment", "namespace": "production", "workload": "my-app", "pods": 3, "container": "app", "policy": "production-policy", "policyUID": "abc123...", "lastRecommendation": "2025-01-28T10:30:00Z", "hasRecommendation": true, "warnings": [], "current": { "requests": { "cpu": "1000m", "memory": "2Gi" }, "limits": { "cpu": "2000m", "memory": "4Gi" } }, "recommended": { "requests": { "cpu": "500m", "memory": "1Gi" }, "limits": null }, "delta": { "cpuReqMilli": -500, "memReqBytes": -1073741824, "impactCpuReqMilli": -1500, "impactMemReqBytes": -3221225472, "cpuReqDirection": "decrease", "memReqDirection": "decrease", "cpuReqPercent": -50.00, "memReqPercent": -50.00 } } ]}HTML Output Features
Section titled “HTML Output Features”The HTML report provides:
Summary Dashboard:
- Workloads optimized percentage (donut chart)
- Total CPU and memory savings
- Requests summary table
Interactive Filtering:
- Search by workload, container, or policy name
- Filter by namespace
- Filter by workload type (Deployment, StatefulSet, DaemonSet)
- Filter by change direction (increases, decreases)
- Filter by warnings
Sorting Options:
- Largest total decrease (default)
- CPU delta
- Memory delta
- Warnings
Detailed Table:
- Per-container current vs recommended resources
- Delta calculations with visual indicators
- Warning badges with tooltips
- Last recommendation timestamp
Warnings
Section titled “Warnings”The script detects and reports potential issues:
CPU_REQ_EXCEEDS_LIMIT:
- Recommended CPU request exceeds current CPU limit
- May cause validation errors if applied with
updateRequestsOnly: true
MEM_REQ_EXCEEDS_LIMIT:
- Recommended memory request exceeds current memory limit
- May cause validation errors if applied with
updateRequestsOnly: true
UPDATE_REQUESTS_ONLY_CONFLICT:
- Policy has
updateRequestsOnly: truebut recommendations exceed limits - Requires either updating limits or changing policy configuration
Annotation Format
Section titled “Annotation Format”The script reads these annotations from workloads:
Management annotations:
optipod.io/managed- Indicates OptiPod managementoptipod.io/policy- Policy nameoptipod.io/policy-uid- Policy UIDoptipod.io/last-recommendation- Timestamp
Recommendation annotations (per container):
optipod.io/recommendation.<container>.cpu-requestoptipod.io/recommendation.<container>.memory-requestoptipod.io/recommendation.<container>.cpu-limitoptipod.io/recommendation.<container>.memory-limit
Exit Codes
Section titled “Exit Codes”0- Success2- Invalid arguments or missing dependencies
- Requires OptiPod to be installed and generating recommendations
- Only shows workloads with OptiPod annotations
- Impact calculations multiply per-container deltas by pod count
- HTML report is self-contained (no external dependencies)
create-prometheus-secrets.sh
Section titled “create-prometheus-secrets.sh”Creates Kubernetes secrets for Prometheus authentication.
Synopsis
Section titled “Synopsis”scripts/create-prometheus-secrets.shDescription
Section titled “Description”Interactive script that creates Kubernetes secrets for authenticating with Prometheus. Supports basic auth, bearer token, and mTLS authentication methods.
Requirements
Section titled “Requirements”kubectl- Kubernetes command-line tool- Access to a Kubernetes cluster
Run the script and follow the interactive prompts:
./scripts/create-prometheus-secrets.shThe script will:
- Prompt for authentication method (basic auth, bearer token, or mTLS)
- Collect required credentials
- Create the appropriate Kubernetes secret
- Provide Helm values for using the secret
Authentication Methods
Section titled “Authentication Methods”Basic Auth:
- Prompts for username and password
- Creates secret with
usernameandpasswordkeys
Bearer Token:
- Prompts for token value
- Creates secret with
tokenkey
mTLS:
- Prompts for client certificate and key file paths
- Creates secret with
tls.crtandtls.keykeys
Example Output
Section titled “Example Output”$ ./scripts/create-prometheus-secrets.shSelect authentication method:1) Basic Auth2) Bearer Token3) mTLSChoice: 1
Enter username: adminEnter password: ********
Creating secret 'prometheus-auth' in namespace 'optipod-system'...secret/prometheus-auth created
Add this to your Helm values: prometheus: auth: type: basic secretName: prometheus-authci-checks-local.sh
Section titled “ci-checks-local.sh”Runs local CI checks before committing code.
Synopsis
Section titled “Synopsis”scripts/ci-checks-local.shDescription
Section titled “Description”Runs the same checks that CI runs, allowing you to catch issues before pushing code. This includes linting, formatting, tests, and validation.
Requirements
Section titled “Requirements”- Go 1.22+
- golangci-lint
- kubectl
- All project dependencies
Checks Performed
Section titled “Checks Performed”- Go formatting - Verifies code is formatted with
gofmt - Go linting - Runs
golangci-lint - Go tests - Runs unit tests
- Go vet - Runs
go vet - Manifest validation - Validates Kubernetes manifests
- Documentation validation - Checks documentation links and format
./scripts/ci-checks-local.shExit Codes
Section titled “Exit Codes”0- All checks passed1- One or more checks failed
lint-local.sh
Section titled “lint-local.sh”Runs linting checks on the codebase.
Synopsis
Section titled “Synopsis”scripts/lint-local.shDescription
Section titled “Description”Runs golangci-lint with the project’s configuration to check for code quality issues.
Requirements
Section titled “Requirements”- golangci-lint
./scripts/lint-local.shConfiguration
Section titled “Configuration”Uses .golangci.yml in the project root for linter configuration.
setup-pre-commit.sh
Section titled “setup-pre-commit.sh”Sets up pre-commit hooks for the repository.
Synopsis
Section titled “Synopsis”scripts/setup-pre-commit.shDescription
Section titled “Description”Installs and configures pre-commit hooks to automatically run checks before each commit.
Requirements
Section titled “Requirements”- pre-commit (Python package)
Installation
Section titled “Installation”# Install pre-commitpip install pre-commit
# Run setup script./scripts/setup-pre-commit.shHooks Configured
Section titled “Hooks Configured”- Go formatting check
- Go linting
- YAML validation
- Markdown linting
- Secret detection
- Trailing whitespace removal
validate-docs.sh
Section titled “validate-docs.sh”Validates documentation for correctness and consistency.
Synopsis
Section titled “Synopsis”scripts/validate-docs.shDescription
Section titled “Description”Checks documentation files for:
- Broken internal links
- Invalid markdown syntax
- Missing required sections
- Inconsistent formatting
Requirements
Section titled “Requirements”- markdown-link-check (optional)
- markdownlint (optional)
./scripts/validate-docs.shExit Codes
Section titled “Exit Codes”0- All documentation is valid1- Validation errors found
sync-docs.sh
Section titled “sync-docs.sh”Synchronizes documentation between docs/ and website/.
Synopsis
Section titled “Synopsis”scripts/sync-docs.shDescription
Section titled “Description”Ensures documentation is synchronized between the Markdown (docs/) and MDX (website/) versions.
./scripts/sync-docs.shWhat It Does
Section titled “What It Does”- Compares content between
docs/andwebsite/src/content/docs/docs/ - Reports any differences
- Optionally syncs changes
generate-docs-index.sh
Section titled “generate-docs-index.sh”Generates documentation index files.
Synopsis
Section titled “Synopsis”scripts/generate-docs-index.shDescription
Section titled “Description”Creates index files that list all documentation pages for easy navigation.
./scripts/generate-docs-index.shOutput
Section titled “Output”Generates docs/INDEX.md with links to all documentation files.
build-docs.sh
Section titled “build-docs.sh”Builds the documentation website.
Synopsis
Section titled “Synopsis”scripts/build-docs.shDescription
Section titled “Description”Builds the Astro-based documentation website for production deployment.
Requirements
Section titled “Requirements”- Node.js 18+
- npm
./scripts/build-docs.shOutput
Section titled “Output”Builds website to website/dist/ directory.
Common Workflows
Section titled “Common Workflows”Before Committing Code
Section titled “Before Committing Code”# Run all CI checks locally./scripts/ci-checks-local.sh
# If checks pass, commitgit add .git commit -m "feat: add new feature"Reviewing Recommendations
Section titled “Reviewing Recommendations”# Generate HTML report./scripts/optipod-recommendation-report.sh --output html --file report.html
# Open in browseropen report.html # macOSxdg-open report.html # LinuxSetting Up Prometheus Authentication
Section titled “Setting Up Prometheus Authentication”# Create secret interactively./scripts/create-prometheus-secrets.sh
# Update Helm values with provided configurationhelm upgrade optipod charts/optipod -f values.yamlValidating Documentation Changes
Section titled “Validating Documentation Changes”# Validate documentation./scripts/validate-docs.sh
# Sync docs/ and website/./scripts/sync-docs.sh
# Build website to test./scripts/build-docs.shTroubleshooting
Section titled “Troubleshooting”Script Not Executable
Section titled “Script Not Executable”If you get “Permission denied”:
chmod +x scripts/*.shMissing Dependencies
Section titled “Missing Dependencies”Install required tools:
# macOSbrew install kubectl jq golangci-lint
# Linux (Ubuntu/Debian)apt-get install kubectl jqgo install github.com/golangci/golangci-lint/cmd/golangci-lint@latestkubectl Not Configured
Section titled “kubectl Not Configured”Ensure kubectl is configured to access your cluster:
kubectl config current-contextkubectl get nodesRelated Documentation
Section titled “Related Documentation”- Annotations Reference - Annotation format used by recommendation report
- Reviewing Recommendations - Guide to reviewing recommendations
- CRD Specification - OptimizationPolicy field reference
- Contributing Guide - Development workflow and guidelines