Skip to content
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

Add kubernetes tentacle docker build #699

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ services:
BUILD_NUMBER: ${BUILD_NUMBER:?err}
image: docker.packages.octopushq.com/octopusdeploy/tentacle:${BUILD_NUMBER?err}-linux

octopusdeploy-kubernetes-tentacle-linux:
platform: "linux/${BUILD_ARCH:?err}"
build:
context: .
dockerfile: ./docker/kubernetes-tentacle/Dockerfile
args:
BUILD_DATE: ${BUILD_DATE:?err}
BUILD_NUMBER: ${BUILD_NUMBER:?err}
BUILD_ARCH: ${BUILD_ARCH:?err}
image: docker.packages.octopushq.com/octopusdeploy/kubernetes-tentacle:${BUILD_NUMBER?err}-linux-${BUILD_ARCH:?err}

octopusdeploy-tentacle-windows-2019:
build:
context: .
Expand Down
61 changes: 61 additions & 0 deletions docker/kubernetes-tentacle/Dockerfile
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to remove a bunch of stuff relating to including Docker into this container, which is not supported

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM mcr.microsoft.com/dotnet/runtime-deps:6.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we get things running, I'd love to see if 6.0-alpine works...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it probably will, the only trick is that it doesn't have bash installed which the install script needs 😁

Copy link
Contributor Author

@APErebus APErebus Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, as @eddymoulton said, the new chiseled images for ubuntu could be good too


ARG BUILD_NUMBER
ARG BUILD_DATE
ARG BUILD_ARCH

EXPOSE 10933

WORKDIR /tmp

COPY docker/kubernetes-tentacle/scripts/* /scripts/
RUN chmod +x /scripts/*.sh

# Install Tentacle
COPY _artifacts/deb/tentacle_${BUILD_NUMBER}_${BUILD_ARCH}.deb /tmp/
RUN apt-get update
RUN apt install ./tentacle_${BUILD_NUMBER}_${BUILD_ARCH}.deb -y
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

WORKDIR /

# We know this won't reduce the image size at all. It's just to make the filesystem a little tidier.
RUN rm -rf /tmp/*

ENV OCTOPUS_RUNNING_IN_CONTAINER=Y
ENV ACCEPT_EULA=N
ENV CustomPublicHostName=""
ENV ListeningPort=""
ENV MachinePolicy="Default Machine Policy"
ENV PublicHostNameConfiguration="ComputerName"
ENV ServerApiKey=""
ENV ServerPassword=""
ENV ServerUsername=""
ENV ServerCommsAddress=""
ENV ServerPort=""
ENV ServerUrl=""
ENV Space="Default"
ENV TargetEnvironment=""
ENV TargetName=""
ENV TargetRole=""
ENV TargetTenant=""
ENV TargetTenantTag=""
ENV TargetTenantedDeploymentParticipation=""
ENV OCTOPUS__K8STENTACLE__NAMESPACE=""
ENV OCTOPUS__K8STENTACLE__USEJOBS="True"
ENV OCTOPUS__K8STENTACLE__JOBSERVICEACCOUNTNAME=""
ENV OCTOPUS__K8STENTACLE__JOBVOLUMEYAML=""
Comment on lines +45 to +48
Copy link
Contributor Author

@APErebus APErebus Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k8s specific environment variables defined in #690


CMD /scripts/configure-tentacle.sh && /scripts/run-tentacle.sh

LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="Octopus Deploy Kubernetes Tentacle" \
org.label-schema.vendor="Octopus Deploy" \
org.label-schema.url="https://octopus.com" \
org.label-schema.vcs-url="https://github.com/OctopusDeploy/OctopusTentacle" \
org.label-schema.license="Apache" \
org.label-schema.description="Octopus Kubernetes Tentacle instance with auto-registration to Octopus Server" \
org.label-schema.version=${BUILD_NUMBER} \
org.label-schema.build-date=${BUILD_DATE}
211 changes: 211 additions & 0 deletions docker/kubernetes-tentacle/scripts/configure-tentacle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#!/bin/bash
set -e

if [[ "$ACCEPT_EULA" != "Y" ]]; then
echo "ERROR: You must accept the EULA at https://octopus.com/company/legal by passing an environment variable 'ACCEPT_EULA=Y'"
exit 1
fi

# Tentacle Docker images only support once instance per container. Running multiple instances can be achieved by running multiple containers.
instanceName=Tentacle
configurationDirectory=/etc/octopus
applicationsDirectory=/home/Octopus/Applications
internalListeningPort=10933

mkdir -p $configurationDirectory
mkdir -p $applicationsDirectory

if [ ! -f /usr/bin/tentacle ]; then
ln -s /opt/octopus/tentacle/Tentacle /usr/bin/tentacle
fi

function getPublicHostName() {
if [[ "$PublicHostNameConfiguration" == "PublicIp" ]]; then
curl https://api.ipify.org/
elif [[ "$PublicHostNameConfiguration" == "FQDN" ]]; then
hostname --fqdn
elif [[ "$PublicHostNameConfiguration" == "ComputerName" ]]; then
hostname
else
echo $CustomPublicHostName
fi
}

function validateVariables() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I know we are running as a kubernetes tentacle, I was able to clean up a lot of the validation here

if [[ -z "$ServerApiKey" && -z "$BearerToken" ]]; then
if [[ -z "$ServerPassword" || -z "$ServerUsername" ]]; then
echo "Please specify either an API key, a Bearer Token or a username/password with the 'ServerApiKey' or 'ServerUsername'/'ServerPassword' environment variables" >&2
exit 1
fi
fi

if [[ -z "$ServerUrl" ]]; then
echo "Please specify an Octopus Server with the 'ServerUrl' environment variable" >&2
exit 1
fi

if [[ -z "$TargetEnvironment" ]]; then
echo "Please specify one or more environment names (comma delimited) with the 'TargetEnvironment' environment variable" >&2
exit 1
fi

if [[ -z "$TargetRole" ]]; then
echo "Please specify one or more role names (comma delimited) with the 'TargetRole' environment variable" >&2
exit 1
fi

echo " - server endpoint '$ServerUrl'"
echo " - api key '##########'"

if [[ ! -z "$ServerCommsAddress" || ! -z "$ServerPort" ]]; then
echo " - communication mode 'Kubernetes' (Polling)"

if [[ ! -z "$ServerCommsAddress" ]]; then
echo " - server comms address $ServerCommsAddress"
fi
if [[ ! -z "$ServerPort" ]]; then
echo " - server port $ServerPort"
fi
else
echo " - communication mode 'Kubernetes' (Listening)"
echo " - registered port $ListeningPort"
fi

echo " - environment '$TargetEnvironment'"
echo " - role '$TargetRole'"
echo " - host '$PublicHostNameConfiguration'"

if [[ ! -z "$TargetName" ]]; then
echo " - name '$TargetName'"
fi
if [[ ! -z "$TargetTenant" ]]; then
echo " - tenant '$TargetTenant'"
fi
if [[ ! -z "$TargetTenantTag" ]]; then
echo " - tenant tag '$TargetTenantTag'"
fi
if [[ ! -z "$TargetTenantedDeploymentParticipation" ]]; then
echo " - tenanted deployment participation '$TargetTenantedDeploymentParticipation'"
fi
if [[ ! -z "$Space" ]]; then
echo " - space '$Space'"
fi
}

function configureTentacle() {
tentacle create-instance --instance "$instanceName" --config "$configurationDirectory/tentacle.config"

echo "Setting directory paths ..."
tentacle configure --instance "$instanceName" --app "$applicationsDirectory"

echo "Configuring communication type ..."
if [[ ! -z "$ServerCommsAddress" || ! -z "$ServerPort" ]]; then
tentacle configure --instance "$instanceName" --noListen "True"
else
tentacle configure --instance "$instanceName" --port $internalListeningPort --noListen "False"
fi

echo "Updating trust ..."
tentacle configure --instance "$instanceName" --reset-trust

echo "Creating certificate ..."
tentacle new-certificate --instance "$instanceName" --if-blank
}

function registerTentacle() {
echo "Registering with server ..."

local ARGS=()

ARGS+=('register-k8s-cluster')

if [[ ! -z "$TargetEnvironment" ]]; then
IFS=',' read -ra ENVIRONMENTS <<<"$TargetEnvironment"
for i in "${ENVIRONMENTS[@]}"; do
ARGS+=('--environment' "$i")
done
fi
Comment on lines +122 to +127
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do support multiple environments/tenants via a command separated list in the environment variable in the container.


if [[ ! -z "$TargetRole" ]]; then
IFS=',' read -ra ROLES <<<"$TargetRole"
for i in "${ROLES[@]}"; do
ARGS+=('--role' "$i")
done
fi

if [[ ! -z "$TargetTenant" ]]; then
IFS=',' read -ra TENANTS <<<"$TargetTenant"
for i in "${TENANTS[@]}"; do
ARGS+=('--tenant' "$i")
done
fi

if [[ ! -z "$TargetTenantTag" ]]; then
IFS=',' read -ra TENANTTAGS <<<"$TargetTenantTag"
for i in "${TENANTTAGS[@]}"; do
ARGS+=('--tenanttag' "$i")
done
fi

ARGS+=(
'--instance' "$instanceName"
'--server' "$ServerUrl"
'--space' "$Space"
'--policy' "$MachinePolicy")

if [[ ! -z "$ServerCommsAddress" || ! -z "$ServerPort" ]]; then
ARGS+=('--comms-style' 'TentacleActive')

if [[ ! -z "$ServerCommsAddress" ]]; then
ARGS+=('--server-comms-address' $ServerCommsAddress)
fi

if [[ ! -z "$ServerPort" ]]; then
ARGS+=('--server-comms-port' $ServerPort)
fi
else
ARGS+=(
'--comms-style' 'TentaclePassive'
'--publicHostName' $(getPublicHostName))

if [[ ! -z "$ListeningPort" && "$ListeningPort" != "$internalListeningPort" ]]; then
ARGS+=('--tentacle-comms-port' $ListeningPort)
fi
fi

if [[ ! -z "$ServerApiKey" ]]; then
echo "Registering Tentacle with API key"
ARGS+=('--apiKey' $ServerApiKey)
elif [[ ! -z "$BearerToken" ]]; then
echo "Registering Tentacle with Bearer Token"
ARGS+=('--bearerToken' "$BearerToken")
else
echo "Registering Tentacle with username/password"
ARGS+=(
'--username' "$ServerUsername"
'--password' "$ServerPassword")
fi

if [[ ! -z "$TargetName" ]]; then
ARGS+=('--name' "$TargetName")
fi

if [[ ! -z "$TargetTenantedDeploymentParticipation" ]]; then
ARGS+=('--tenanted-deployment-participation' "$TargetTenantedDeploymentParticipation")
fi

tentacle "${ARGS[@]}"
}

echo "==============================================="
echo "Configuring Octopus Deploy Kubernetes Tentacle"

validateVariables

echo "==============================================="

configureTentacle
registerTentacle

echo "Configuration successful."
echo ""
4 changes: 4 additions & 0 deletions docker/kubernetes-tentacle/scripts/run-tentacle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eux

tentacle agent --instance Tentacle --noninteractive