Skip to content

Commit 8d8c486

Browse files
Add helper script
1 parent 1d4a063 commit 8d8c486

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/bin/bash
2+
3+
# Script to create a kubeconfig secret for the Multicluster Failover Operator
4+
5+
set -e
6+
7+
# Default values
8+
NAMESPACE="my-operator-namespace"
9+
KUBECONFIG_PATH="${HOME}/.kube/config"
10+
KUBECONFIG_CONTEXT=""
11+
SECRET_NAME=""
12+
DRY_RUN="false"
13+
14+
# Function to display usage information
15+
function show_help {
16+
echo "Usage: $0 [options]"
17+
echo " -n, --name NAME Name for the secret (will be used as cluster identifier)"
18+
echo " -s, --namespace NS Namespace to create the secret in (default: ${NAMESPACE})"
19+
echo " -k, --kubeconfig PATH Path to kubeconfig file (default: ${KUBECONFIG_PATH})"
20+
echo " -c, --context CONTEXT Kubeconfig context to use (default: current-context)"
21+
echo " -d, --dry-run Dry run, print YAML but don't apply"
22+
echo " -h, --help Show this help message"
23+
echo ""
24+
echo "Example: $0 -n cluster1 -c prod-cluster -k ~/.kube/config"
25+
}
26+
27+
# Parse command line options
28+
while [[ $# -gt 0 ]]; do
29+
key="$1"
30+
case $key in
31+
-n|--name)
32+
SECRET_NAME="$2"
33+
shift 2
34+
;;
35+
-s|--namespace)
36+
NAMESPACE="$2"
37+
shift 2
38+
;;
39+
-k|--kubeconfig)
40+
KUBECONFIG_PATH="$2"
41+
shift 2
42+
;;
43+
-c|--context)
44+
KUBECONFIG_CONTEXT="$2"
45+
shift 2
46+
;;
47+
-d|--dry-run)
48+
DRY_RUN="true"
49+
shift 1
50+
;;
51+
-h|--help)
52+
show_help
53+
exit 0
54+
;;
55+
*)
56+
echo "Unknown option: $1"
57+
show_help
58+
exit 1
59+
;;
60+
esac
61+
done
62+
63+
# Validate required arguments
64+
if [ -z "$SECRET_NAME" ]; then
65+
echo "ERROR: Secret name is required (-n, --name)"
66+
show_help
67+
exit 1
68+
fi
69+
70+
if [ ! -f "$KUBECONFIG_PATH" ]; then
71+
echo "ERROR: Kubeconfig file not found at: $KUBECONFIG_PATH"
72+
exit 1
73+
fi
74+
75+
# Process the kubeconfig
76+
echo "Processing kubeconfig..."
77+
TEMP_KUBECONFIG=$(mktemp)
78+
trap "rm -f $TEMP_KUBECONFIG" EXIT
79+
80+
if [ -n "$KUBECONFIG_CONTEXT" ]; then
81+
kubectl config view --raw --minify --flatten --context="$KUBECONFIG_CONTEXT" > "$TEMP_KUBECONFIG"
82+
if [ $? -ne 0 ]; then
83+
echo "ERROR: Failed to extract context '$KUBECONFIG_CONTEXT' from kubeconfig"
84+
exit 1
85+
fi
86+
echo "Extracted context '$KUBECONFIG_CONTEXT' from kubeconfig"
87+
else
88+
cp "$KUBECONFIG_PATH" "$TEMP_KUBECONFIG"
89+
echo "Using entire kubeconfig file"
90+
fi
91+
92+
# Encode the kubeconfig
93+
KUBECONFIG_B64=$(base64 < "$TEMP_KUBECONFIG" | tr -d '\n')
94+
95+
# Create the namespace if it doesn't exist
96+
if [ "$DRY_RUN" != "true" ]; then
97+
kubectl get namespace "$NAMESPACE" &>/dev/null || kubectl create namespace "$NAMESPACE"
98+
fi
99+
100+
# Generate the secret YAML
101+
SECRET_YAML=$(cat <<EOF
102+
apiVersion: v1
103+
kind: Secret
104+
metadata:
105+
name: ${SECRET_NAME}
106+
namespace: ${NAMESPACE}
107+
labels:
108+
sigs.k8s.io/multicluster-runtime-kubeconfig: "true"
109+
type: Opaque
110+
data:
111+
kubeconfig: ${KUBECONFIG_B64}
112+
EOF
113+
)
114+
115+
# Apply or print the secret
116+
if [ "$DRY_RUN" == "true" ]; then
117+
echo "# YAML that would be applied:"
118+
echo "$SECRET_YAML"
119+
else
120+
echo "$SECRET_YAML" | kubectl apply -f -
121+
echo "Secret '${SECRET_NAME}' created in namespace '${NAMESPACE}'"
122+
echo "The operator should now discover and connect to this cluster"
123+
fi

0 commit comments

Comments
 (0)