Skip to content

Commit 7d4d32b

Browse files
committed
Add deployment using kubectl directly
In order to deploy to the server running on Kubernetes, the maven/deploy.sh script need adjustments: * properly handle commands as arrays * add the executor command and parameters for kubectl The helm chart also required having a unique component for the server pods to make it easy to lookup using label selector without getting the setup pod too.
1 parent 9bff40e commit 7d4d32b

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

containers/server-helm/templates/setup-job.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
template:
1313
metadata:
1414
labels:
15-
app.kubernetes.io/component: server
15+
app.kubernetes.io/component: server-setup
1616
app.kubernetes.io/part-of: uyuni
1717
spec:
1818
containers:

java/scripts/maven/deploy.sh

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ UYUNI_DIR="$(realpath "$SCRIPT_DIR/../../..")"
1919
DEPLOY_TARGET="backend"
2020
DEPLOY_HOST="server.tf.local"
2121
DEPLOY_MODE="remote-container"
22+
DEPLOY_NAMESPACE="default"
2223
CONTAINER_BACKEND="podman"
2324
RESTART_TOMCAT=false
2425
RESTART_TASKOMATIC=false
@@ -51,9 +52,10 @@ usage() {
5152
print " <type> The type of deployment to perform: backend, frontend, salt or all."
5253
print ""
5354
print "Optional Arguments:"
54-
print " -m,--mode <mode> Deployment mode: local, remote, container, remote-container (default: $DEPLOY_MODE)"
55+
print " -m,--mode <mode> Deployment mode: local, remote, container, remote-container, kubectl (default: $DEPLOY_MODE)"
5556
print " -h,--host <hostname> The target host for the deployment."
5657
print " -b,--backend <backend> Container backend: podman, podman-remote, kubectl (default: $CONTAINER_BACKEND)"
58+
print " -n,--namespace <ns> Kubernetes namespace where to look for the pod (default: $DEPLOY_NAMESPACE)"
5759
print " -r,--restart Restart tomcat and taskomatic at the end of the deployment"
5860
print " --restart-tomcat Restart only tomcat at the end of the deployment"
5961
print " --restart-taskomatic Restart only taskomatic at the end of the deployment"
@@ -82,6 +84,10 @@ while [[ $# -gt 0 ]]; do
8284
CONTAINER_BACKEND="$2"
8385
shift 2
8486
;;
87+
-n|--namespace)
88+
DEPLOY_NAMESPACE="$2"
89+
shift 2
90+
;;
8591
-r|--restart)
8692
RESTART_TOMCAT=true
8793
RESTART_TASKOMATIC=true
@@ -134,9 +140,8 @@ BRANDING_VERSION=""
134140

135141
# Executes a command on the target system using the configured executor.
136142
deploy_execute() {
137-
local cmd_to_run="$1"
138143
# Execute the command with parameters as a proper array
139-
"$EXECUTOR_COMMAND" "${EXECUTOR_PARAMETERS[@]}" "$cmd_to_run"
144+
"$EXECUTOR_COMMAND" "${EXECUTOR_PARAMETERS[@]}" $@
140145
}
141146

142147
# Deploys a local directory to the target
@@ -149,17 +154,16 @@ deploy_directory() {
149154

150155
# Create a remote temporary directory
151156
local temp_dir
152-
temp_dir=$(deploy_execute "mktemp -d")
157+
temp_dir=$(deploy_execute mktemp -d | tr -d "[:space:]")
153158

154159
print_detailed " Streaming local $source_dir to remote $temp_dir..."
155-
local remote_tar_cmd="tar xf - -C ${temp_dir}/ --no-same-owner --no-same-permissions"
156-
tar c -C "$source_dir" -f - . | deploy_execute "$remote_tar_cmd"
160+
tar c -C "$source_dir" -f - . | deploy_execute tar xf - -C ${temp_dir}/ --no-same-owner --no-same-permissions
157161

158162
print_detailed " Syncing from remote $temp_dir to $dest_dir..."
159-
deploy_execute "rsync -a ${rsync_params} ${temp_dir}/ ${dest_dir}"
163+
deploy_execute rsync -a ${rsync_params} ${temp_dir}/ ${dest_dir}
160164

161165
print_detailed " Cleaning up remote temp dir..."
162-
deploy_execute "rm -rf $temp_dir"
166+
deploy_execute rm -rf $temp_dir
163167

164168
print_detailed " Deploy directory finished."
165169
}
@@ -176,9 +180,13 @@ show_configuration() {
176180
print_detailed " Port: $SSH_PORT"
177181
fi
178182

179-
if [ "$DEPLOY_MODE" = "container " ] || [ "$DEPLOY_MODE" = "remote-container" ]; then
183+
if [ "$DEPLOY_MODE" = "container" ] || [ "$DEPLOY_MODE" = "remote-container" ]; then
180184
print_detailed " Backend: $CONTAINER_BACKEND"
181185
fi
186+
187+
if [ "$DEPLOY_MODE" = "kubectl" ]; then
188+
print_detailed " Namespace: $DEPLOY_NAMESPACE"
189+
fi
182190

183191
print_detailed
184192
}
@@ -269,10 +277,28 @@ check_prerequisites() {
269277
# Parameters are SSH args *plus* the remote mgrctl command
270278
EXECUTOR_PARAMETERS=("${ssh_args[@]}" "mgrctl" "exec" "${mgrctl_params[@]}" "-i" "--")
271279
;;
280+
kubectl)
281+
print_detailed " Mode: kubectl"
282+
# Check for local kubectl
283+
if ! command -v kubectl &> /dev/null; then
284+
print_error "Error: kubectl is not in the PATH. Please install kubectl first."
285+
exit 1
286+
fi
287+
print_detailed " Local kubectl found."
288+
289+
EXECUTOR_COMMAND="kubectl"
290+
291+
EXECUTOR_POD=`kubectl get pod -n ${DEPLOY_NAMESPACE} -l 'app.kubernetes.io/part-of=uyuni,app.kubernetes.io/component=server' -o name`
292+
if test $? -ne 0; then
293+
print_error "Error: failed to find pod to work with."
294+
fi
295+
EXECUTOR_PARAMETERS=("exec" "-n" ${DEPLOY_NAMESPACE} "-ti" ${EXECUTOR_POD} "-c" "uyuni" "--")
296+
;;
297+
272298

273299
*)
274300
print_error "Error: The deploy mode '$DEPLOY_MODE' is invalid."
275-
print_error "Valid modes are: local, remote, container, remote-container"
301+
print_error "Valid modes are: local, remote, container, remote-container, kubectl"
276302
exit 1
277303
;;
278304
esac
@@ -298,15 +324,15 @@ deploy_backend() {
298324
deploy_directory "$SOURCE_WEBAPP_DIR" "$TARGET_DIR" "--delete --exclude=log4j2.xml"
299325

300326
print "Linking branding jar..."
301-
deploy_execute "mv ${TARGET_DIR}/WEB-INF/lib/branding-${BRANDING_VERSION}.jar /usr/share/rhn/lib/java-branding.jar"
302-
deploy_execute "ln -sf /usr/share/rhn/lib/java-branding.jar ${TARGET_DIR}/WEB-INF/lib/java-branding.jar"
327+
deploy_execute mv "${TARGET_DIR}/WEB-INF/lib/branding-${BRANDING_VERSION}.jar" /usr/share/rhn/lib/java-branding.jar
328+
deploy_execute ln -sf /usr/share/rhn/lib/java-branding.jar ${TARGET_DIR}/WEB-INF/lib/java-branding.jar
303329

304330
print "Linking rhn jar..."
305-
deploy_execute "mv ${TARGET_DIR}/WEB-INF/lib/core-${SPACEWALK_JAVA_VERSION}.jar /usr/share/rhn/lib/rhn.jar"
306-
deploy_execute "ln -sf /usr/share/rhn/lib/rhn.jar ${TARGET_DIR}/WEB-INF/lib/rhn.jar"
331+
deploy_execute mv ${TARGET_DIR}/WEB-INF/lib/core-${SPACEWALK_JAVA_VERSION}.jar /usr/share/rhn/lib/rhn.jar
332+
deploy_execute ln -sf /usr/share/rhn/lib/rhn.jar "${TARGET_DIR}/WEB-INF/lib/rhn.jar"
307333

308334
print "Linking jars for Taskomatic..."
309-
deploy_execute "ln -sf ${TARGET_DIR}/WEB-INF/lib/*.jar /usr/share/spacewalk/taskomatic"
335+
deploy_execute ln -sf "${TARGET_DIR}/WEB-INF/lib/*.jar" /usr/share/spacewalk/taskomatic
310336
}
311337

312338
deploy_frontend() {
@@ -343,12 +369,12 @@ deploy_salt() {
343369
restart_services() {
344370
if [ "$RESTART_TOMCAT" = true ]; then
345371
print "Launching Tomcat restart..."
346-
deploy_execute "nohup rctomcat restart"
372+
deploy_execute nohup rctomcat restart
347373
fi
348374

349375
if [ "$RESTART_TASKOMATIC" = true ]; then
350376
print "Launching Taskomatic restart..."
351-
deploy_execute "nohup rctaskomatic restart"
377+
deploy_execute nohup rctaskomatic restart
352378
fi
353379
}
354380

0 commit comments

Comments
 (0)