-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(hack): add flag extraction script for Karmada components #7136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarthakkjha
wants to merge
2
commits into
karmada-io:master
Choose a base branch
from
sarthakkjha:flag-extraction-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright 2024 The Karmada Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # This script extracts command-line flags from Karmada component binaries. | ||
| # It generates JSON output that can be used for documentation and change detection. | ||
| # | ||
| # Usage: | ||
| # ./hack/extract-flags.sh [--binary-dir=./bin] [--output=flags.json] | ||
| # | ||
| # Requirements: | ||
| # - Built Karmada binaries in the specified directory | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Configuration | ||
| BINARY_DIR="${BINARY_DIR:-_output/bin/linux/amd64}" | ||
| OUTPUT_FILE="${OUTPUT_FILE:-}" | ||
| VERSION="${VERSION:-$(git describe --tags --always 2>/dev/null || echo 'unknown')}" | ||
|
|
||
| # Karmada component binaries to extract flags from | ||
| COMPONENTS=( | ||
| "karmada-controller-manager" | ||
| "karmada-scheduler" | ||
| "karmada-webhook" | ||
| "karmada-search" | ||
| "karmada-aggregated-apiserver" | ||
| "karmada-descheduler" | ||
| "karmada-metrics-adapter" | ||
| "karmada-scheduler-estimator" | ||
| "karmada-agent" | ||
| ) | ||
|
|
||
| # Parse command line arguments | ||
| for arg in "$@"; do | ||
| case $arg in | ||
| --binary-dir=*) | ||
| BINARY_DIR="${arg#*=}" | ||
| shift | ||
| ;; | ||
| --output=*) | ||
| OUTPUT_FILE="${arg#*=}" | ||
| shift | ||
| ;; | ||
| --version=*) | ||
| VERSION="${arg#*=}" | ||
| shift | ||
| ;; | ||
| --help) | ||
| echo "Usage: $0 [--binary-dir=./bin] [--output=flags.json] [--version=v1.x.x]" | ||
| echo "" | ||
| echo "Extracts command-line flags from Karmada component binaries." | ||
| exit 0 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| # Validate binary directory | ||
| if [[ ! -d "$BINARY_DIR" ]]; then | ||
| echo "Error: Binary directory '$BINARY_DIR' not found." | ||
| echo "Please build Karmada first with: make all" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Function to extract flags from a single binary (outputs to stdout, status to stderr) | ||
| extract_flags_from_binary() { | ||
| local binary="$1" | ||
| local binary_path="${BINARY_DIR}/${binary}" | ||
|
|
||
| if [[ ! -x "$binary_path" ]]; then | ||
| return | ||
| fi | ||
|
|
||
| # Run --help and extract unique flag names | ||
| "$binary_path" --help 2>&1 | grep -oE -- '--[a-zA-Z0-9_-]+' | sed 's/^--//' | sort -u | ||
| } | ||
sarthakkjha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Function to count flags for a component | ||
| count_flags() { | ||
| local binary="$1" | ||
| local binary_path="${BINARY_DIR}/${binary}" | ||
|
|
||
| if [[ ! -x "$binary_path" ]]; then | ||
| echo "0" | ||
| return | ||
| fi | ||
|
|
||
| "$binary_path" --help 2>&1 | grep -oE -- '--[a-zA-Z0-9_-]+' | sort -u | wc -l | tr -d ' ' | ||
| } | ||
sarthakkjha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Generate JSON output for a single component | ||
| generate_component_json() { | ||
| local component="$1" | ||
| local flag_count | ||
| local flags | ||
|
|
||
| flag_count=$(count_flags "$component") | ||
| flags=$(extract_flags_from_binary "$component") | ||
sarthakkjha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| echo " {" | ||
| echo " \"name\": \"$component\"," | ||
| echo " \"flagCount\": $flag_count," | ||
| echo " \"flags\": [" | ||
|
|
||
| local first=true | ||
| while IFS= read -r flag_name; do | ||
| if [[ -n "$flag_name" ]]; then | ||
| if [[ "$first" == "true" ]]; then | ||
| first=false | ||
| else | ||
| echo "," | ||
| fi | ||
| echo -n " \"$flag_name\"" | ||
| fi | ||
| done <<< "$flags" | ||
|
|
||
| echo "" | ||
| echo " ]" | ||
| echo -n " }" | ||
| } | ||
|
|
||
| # Main JSON generation | ||
| generate_json() { | ||
| echo "{" | ||
| echo " \"version\": \"$VERSION\"," | ||
| echo " \"generatedAt\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"," | ||
| echo " \"components\": [" | ||
|
|
||
| local first=true | ||
| for component in "${COMPONENTS[@]}"; do | ||
| local binary_path="${BINARY_DIR}/${component}" | ||
|
|
||
| if [[ ! -x "$binary_path" ]]; then | ||
| echo " Skipping $component (not found)" >&2 | ||
| continue | ||
| fi | ||
|
|
||
| echo " Extracting flags from $component..." >&2 | ||
|
|
||
| if [[ "$first" == "true" ]]; then | ||
| first=false | ||
| else | ||
| echo " ," | ||
| fi | ||
|
|
||
| generate_component_json "$component" | ||
| done | ||
|
|
||
| echo "" | ||
| echo " ]" | ||
| echo "}" | ||
| } | ||
|
|
||
| # Main | ||
| echo "Karmada Flag Extractor" >&2 | ||
| echo "======================" >&2 | ||
| echo "Binary directory: $BINARY_DIR" >&2 | ||
| echo "Version: $VERSION" >&2 | ||
| echo "" >&2 | ||
|
|
||
| if [[ -n "$OUTPUT_FILE" ]]; then | ||
| generate_json > "$OUTPUT_FILE" | ||
| echo "" >&2 | ||
| echo "Output written to: $OUTPUT_FILE" >&2 | ||
| else | ||
| generate_json | ||
| fi | ||
|
|
||
| echo "" >&2 | ||
| echo "Done!" >&2 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.