Demo about Edge Computing in the Retail vertical using Red Hat products
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

83 lines
2.7 KiB

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: flightctl-update-digest
spec:
params:
- name: new-image-digest
type: string
- description: The label selector to identify the fleet(s) to update.
name: fleet-label-selector
type: string
default: ""
workspaces:
- description: An optional workspace that contains the flightctl configuration file (client.yaml).
name: flightctl-config
optional: true
steps:
- name: flightctl-update-digest
image: quay.io/nmasse-redhat/flightctl:latest
env:
- name: SCRIPT_DEBUG
value: "false"
- name: FLEET_LABEL_SELECTOR
value: "$(params.fleet-label-selector)"
script: |
#!/bin/bash
set -Eeuo pipefail
# If debug is enabled, print out command that are executed
if [[ "${SCRIPT_DEBUG:-false}" == "true" ]]; then
set -x
fi
# Check if the fleet label selector is set
if [ -z "${FLEET_LABEL_SELECTOR}" ]; then
echo "FLEET_LABEL_SELECTOR is not set. No action taken."
exit 0
fi
# Print versions of the program we use
echo "=== Environment ==="
echo "---> flightctl"
flightctl version
echo
echo "---> bash"
bash --version
echo
echo "---> OS"
cat /etc/redhat-release
echo
echo "---> yq"
yq --version
echo
echo "---> Current user"
id
echo
echo "---> flightctl"
flightctl version
echo
# List fleets matching the label selector and update their image to the new digest
flightctl --config-dir $(workspaces.flightctl-config.path) get fleets --limit 0 -l "${FLEET_LABEL_SELECTOR}" -o name | while read -r fleet; do
echo "Updating fleet $fleet to image digest $(params.new-image-digest)"
# Get the current fleet definition
flightctl --config-dir $(workspaces.flightctl-config.path) get fleet/$fleet -o yaml > /tmp/fleet.yaml
# Extract the current image from the fleet definition
CURRENT_IMAGE="$(yq eval '.spec.template.spec.os.image' /tmp/fleet.yaml)"
# Splits the CURRENT_IMAGE on the "@" or ":" character and takes the first part (the image name without tag or digest)
# Using only bash built-in features to avoid dependencies on other tools
IMAGE_NAME="${CURRENT_IMAGE%%[@:]*}"
# Construct the new image with the new digest
NEW_IMAGE="${IMAGE_NAME}@$(params.new-image-digest)"
# Update the fleet definition with the new image
yq eval -i ".spec.template.spec.os.image = \"$NEW_IMAGE\"" /tmp/fleet.yaml
# Apply the updated fleet definition
flightctl --config-dir $(workspaces.flightctl-config.path) apply -f /tmp/fleet.yaml
done