Skip to content

Commit

Permalink
Merge pull request #3237 from RachaelSTamakloe/xpk_dockerfile
Browse files Browse the repository at this point in the history
Adds Cluster Toolkit Dockerfile for backend integration with XPK
  • Loading branch information
RachaelSTamakloe authored Nov 18, 2024
2 parents f8d43f9 + 7a7e04b commit 0470615
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tools/cloud-build/images/cluster-toolkit-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2024 Google LLC
#
# 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.


# Use a google-cloud-cli image as the base
ARG BASE_IMAGE=gcr.io/google.com/cloudsdktool/google-cloud-cli:stable
FROM ${BASE_IMAGE}

# Install necessary tools and libraries
RUN apt-get update && \
apt-get install -y --no-install-recommends \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release \
git \
make \
unzip \
wget

# Install Terraform
ARG TERRAFORM_VERSION=1.5.2
RUN wget -q "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" -O terraform.zip && \
unzip terraform.zip && \
mv terraform /usr/local/bin/ && \
rm terraform.zip

# Install Packer
ARG PACKER_VERSION=1.8.6
RUN wget -q "https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip" -O packer.zip && \
unzip packer.zip && \
mv packer /usr/local/bin/ && \
rm packer.zip

# Install Go
ARG GO_VERSION=1.21.0
RUN wget -q "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -O go.tar.gz && \
tar -C /usr/local -xzf go.tar.gz && \
rm go.tar.gz

# Set GOPATH and add to PATH
ENV GOPATH /go
ENV PATH $PATH:/usr/local/go/bin:$GOPATH/bin

# Clone the Cluster Toolkit repository
ARG CLUSTER_TOOLKIT_REF=main
RUN git clone --branch ${CLUSTER_TOOLKIT_REF} https://github.com/GoogleCloudPlatform/cluster-toolkit.git /cluster-toolkit

# Build the gcluster binary
WORKDIR /cluster-toolkit
RUN make

# Make gcluster available
RUN cp /cluster-toolkit/gcluster /usr/local/bin/gcluster
RUN echo $PATH

# Create /out directory
RUN mkdir /out
WORKDIR /out

# Command to execute when running the container (placeholder)
ENTRYPOINT ["gcluster"]
CMD ["--help"]
57 changes: 57 additions & 0 deletions tools/cloud-build/images/cluster-toolkit-dockerfile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Cluster Toolkit Dockerfile

This repository contains a Dockerfile for building a Docker image with Cluster Toolkit and its dependencies installed and the `gcluster` binary readily available for use.

## System Requirements

* [Docker Engine](https://docs.docker.com/engine/) needs to be installed to build a Docker image from this Dockerfile.

## Build Arguments
The following build arguments can be used to customize the build process:
* **`BASE_IMAGE`**: The base image to use for the build. Defaults to `gcr.io/google.com/cloudsdktool/google-cloud-cli:stable`.
* **`TERRAFORM_VERSION`**: The version of Terraform to install. Defaults to `1.5.2`.
* **`PACKER_VERSION`**: The version of Packer to install. Defaults to `1.8.6`.
* **`GO_VERSION`**: The version of Go to install. Defaults to `1.21.0`.
* **`CLUSTER_TOOLKIT_REF`**: The [Cluster Toolkit repository's](https://github.com/GoogleCloudPlatform/cluster-toolkit/releases) branch or tag from which to build Cluster Toolkit. Defaults to the `main` branch, which is the latest official release.

## Build the Cluster Toolkit Docker Image
To build the Cluster Toolkit Docker image, navigate to the directory the Dockerfile is present in and run the following command:

```bash
docker build --build-arg BASE_IMAGE=<base_image> \
--build-arg TERRAFORM_VERSION=<terraform_version> \
--build-arg PACKER_VERSION=<packer_version> \
--build-arg GO_VERSION=<go_version> \
--build-arg CLUSTER_TOOLKIT_REF=<cluster_toolkit_ref> \
-t <image_name> .
```

Example:

```bash
docker build --build-arg CLUSTER_TOOLKIT_REF=v1.40.0 -t gcluster -t ghpc .
```

The above example builds an image tagged `gcluster` and sets the `CLUSTER_TOOLKIT_REF` to the Git tag `v1.40.0` while using the default values for other arguments.

## Run the Cluster Toolkit Docker Image
To run the Cluster Toolkit Docker image, use the following command:

```bash
docker run -v ~/.config/gcloud/:/root/.config/gcloud -v $(pwd):/out <image_name> <gcluster_command>
```

This command runs the Cluster Toolkit Docker image and allows the `gcluster` binary to access your Google Cloud credentials and local files. Here's a breakdown:

* `-v ~/.config/gcloud/:/root/.config/gcloud`: This argument mounts your local Google Cloud configuration directory `(~/.config/gcloud)` to the `/root/.config/gcloud` directory inside the container. This allows the `gcluster` binary to access your credentials and interact with Google Cloud resources when needed.
* `-v $(pwd):/out`: This argument mounts your current working directory `$(pwd)` to the `/out` directory inside the container. This is important because the Cluster Toolkit Dockerfile is designed to automatically output deployment folders to the `/out` directory. Due to this automatic output behavior, you should not provide the `--out` argument to the `create` and `deploy` gcluster subcommands when using this Dockerfile. Instead, mount a local directory (as shown in the example above with $(pwd)) to the `/out` directory within the container. This ensures that the deployment folder persists even after the container exits, allowing you to access and manage the deployment artifacts even after the container is removed. Additionally, this allows the container to access any files in your current directory (like blueprint files) from within the container. You can then reference these files in your `<gcluster_command>` using the `/out` path.
* `<image_name>`: Replace this with the name of your Docker image.
* `<gcluster_command>`: Replace this with the `gcluster` command you want to execute. The Cluster Toolkit Docker image has `ENTRYPOINT ["gcluster"]` in its Dockerfile. This means that the `gcluster` command is automatically executed when the container starts, and any arguments provided after `<image_name>` in the `docker run` command are passed as arguments to `gcluster`.

Example:

```bash
docker run -v ~/.config/gcloud/:/root/.config/gcloud -v $(pwd):/out gcluster deploy /out/my-blueprint.yaml --auto-approve
```

This example runs the `deploy` command with the blueprint file `my-blueprint.yaml` located in your current directory. The deployment folder generated by `gcluster deploy` will be saved to your current directory (which is mounted to `/out`). `--auto-approve` automatically approves any prompts from `gcluster`, streamlining the deployment process.

0 comments on commit 0470615

Please sign in to comment.