LoginSignup
8
5

More than 1 year has passed since last update.

Kubebuilder は Kubernetes のカスタムコントローラを作成するための支援ツールです。
カスタムコントローラを作成する上でデファクトとなりつつあるツールですが、M1 Mac(Apple Silicon) 向けのバイナリは現在リリースされていません。

2022/02/10 追記
Homebrew 経由で Apple Silicon 向けのバイナリをインストールできるようになったようです。
https://github.com/kubernetes-sigs/kubebuilder/issues/1932#issuecomment-985317316
https://formulae.brew.sh/formula/kubebuilder

そこで M1 Mac でも Kubebuilder を利用するため、Docker イメージを作成します。
本記事で作成したイメージは Docker Hub にも公開しています。

Dockerfile

kubebuilder をコンテナ上で動かすため、golang のイメージをもとに kubebuilder をインストールしていきます。
kubebuilder の利用に bash, make, gcc が必要なので、追加でインストールしておきます。

Dockerfile
FROM golang:1.17.3-alpine
WORKDIR /workspace

RUN apk add --no-cache bash make gcc libc-dev

ENV KUBEBUILDER_VERSION v3.2.0
RUN wget -O /usr/local/bin/kubebuilder https://github.com/kubernetes-sigs/kubebuilder/releases/download/$KUBEBUILDER_VERSION/kubebuilder_$(go env GOOS)_$(go env GOARCH) && \
chmod +x /usr/local/bin/kubebuilder

ENTRYPOINT [ "kubebuilder" ]

Dockerfile を用意したら、イメージをビルドします。
Dockerfile を配置したディレクトリで以下コマンドを実行。

$ docker build -t hitsumabushi845/kubebuilder:3.2.0 .

利用方法例

引数で実行したいコマンドを叩きます。プロジェクトの初期化だと以下のようになります。
通常はディレクトリ名がプロジェクト名になるのですが、マウントしていることで正しいディレクトリ名が取れないので、--project-name でディレクトリ名を指定しています。

bash
$ mkdir -p sample-project
$ cd sample-project
$ docker run -it --rm \
-v $(pwd):/workspace \
hitsumabushi845/kubebuilder:3.2.0 \
init --domain hitsumabushi845.github.io --repo sample-project --project-name $(pwd | xargs basename)

プロジェクト作成後の make コマンドの実行はおおむねローカルでも行えます。
プロジェクト作成時に、./bincontroller-gen などのバイナリが格納されていますが、こちらはコンテナ向けのの(linux 向けの)バイナリになっているので、初めて実行する際は rm -rf ./bin などで削除してから実行します。

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5