(最新版じゃなくて)指定したversionのKustomizeをインストールする

2022.04.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

version指定失敗

KustomizeをCodeBuild上で実行しています。buildspec.ymlはこんな感じ↓で、公式サイトにあるダウンロードスクリプトを使用しています。公式サイトに書かれているスクリプトのURLはGitHubのブランチ指定にあたるURLセグメントがmasterになっているので、そこを変更してインストールするKustomizeのversion指定をしています。

buildspec.yml

---
version: 0.2
env:
  variables:
    KUSTOMIZE_VERSION: "v4.2.0"
phases:
  install:
    commands:
      - curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/${KUSTOMIZE_VERSION}/hack/install_kustomize.sh" | bash
      - yum install sudo -y --quiet
      - sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
      - kustomize version

version指定ができていると思っていたのは勘違いでした!以下はCodeBuildのBuild logsです。指定したv4.2.0ではなく、最新版v4.5.4がインストールされています。

CodeBuildのBuild logs

[Container] 2022/04/29 13:17:06 Entering phase INSTALL
[Container] 2022/04/29 13:17:06 Running command curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/${KUSTOMIZE_VERSION}/hack/install_kustomize.sh" | bash
{Version:kustomize/v4.5.4 GitCommit:cf3a452ddd6f83945d39d582243b8592ec627ae3 BuildDate:2022-03-28T23:12:45Z GoOs:linux GoArch:amd64}
kustomize installed to /codebuild/output/src065560130/src/kustomize

[Container] 2022/04/29 13:17:10 Running command yum install sudo -y --quiet
Package sudo-1.8.23-10.amzn2.1.x86_64 already installed and latest version

[Container] 2022/04/29 13:17:42 Running command sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize

[Container] 2022/04/29 13:17:43 Running command kustomize version
{Version:kustomize/v4.5.4 GitCommit:cf3a452ddd6f83945d39d582243b8592ec627ae3 BuildDate:2022-03-28T23:12:45Z GoOs:linux GoArch:amd64}

[Container] 2022/04/29 13:17:43 Phase complete: INSTALL State: SUCCEEDED
[Container] 2022/04/29 13:17:43 Phase context status code:  Message:

version指定成功

ダウンロードスクリプトを読んでみます。

#!/usr/bin/env bash
# Copyright 2022 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0


# If no argument is given -> Downloads the most recently released
# kustomize binary to your current working directory.
# (e.g. 'install_kustomize.sh')
#
# If one argument is given -> 
# If that argument is in the format of #.#.#, downloads the specified
# version of the kustomize binary to your current working directory.
# If that argument is something else, downloads the most recently released
# kustomize binary to the specified directory.
# (e.g. 'install_kustomize.sh 3.8.2' or 'install_kustomize.sh $(go env GOPATH)/bin')
#
# If two arguments are given -> Downloads the specified version of the
# kustomize binary to the specified directory.
# (e.g. 'install_kustomize.sh 3.8.2 $(go env GOPATH)/bin')
#
# Fails if the file already exists.

set -e

# Unset CDPATH to restore default cd behavior. An exported CDPATH can
# cause cd to output the current directory to STDOUT.
unset CDPATH

where=$PWD
(続く)

version指定の方法がコメントで書かれていますね…

If one argument is given ->
If that argument is in the format of #.#.#, downloads the specified
version of the kustomize binary to your current working directory.
If that argument is something else, downloads the most recently released
kustomize binary to the specified directory.
(e.g. 'install_kustomize.sh 3.8.2' or 'install_kustomize.sh $(go env GOPATH)/bin')

なるほどスクリプトの引数としてversionを指定するんですね。buildspec.ymlを修正します。

buildspec.yml

 ---
 version: 0.2
 env:
   variables:
-    KUSTOMIZE_VERSION: "v4.2.0"
+    KUSTOMIZE_VERSION: "4.2.0"
 phases:
   install:
     commands:
-      - curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/${KUSTOMIZE_VERSION}/hack/install_kustomize.sh" | bash
+      - curl -Os "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${KUSTOMIZE_VERSION}/hack/install_kustomize.sh"
+      - bash install_kustomize.sh ${KUSTOMIZE_VERSION}
       - yum install sudo -y --quiet
       - sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
       - kustomize version

狙い通りv4.2.0をインストールできました?

CodeBuildのBuild logs

[Container] 2022/04/29 13:43:52 Entering phase INSTALL
[Container] 2022/04/29 13:43:52 Running command curl -Os "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${KUSTOMIZE_VERSION}/hack/install_kustomize.sh"

[Container] 2022/04/29 13:43:53 Running command bash install_kustomize.sh ${KUSTOMIZE_VERSION}
{Version:kustomize/v4.2.0 GitCommit:d53a2ad45d04b0264bcee9e19879437d851cb778 BuildDate:2021-06-30T22:49:26Z GoOs:linux GoArch:amd64}
kustomize installed to /codebuild/output/src579243376/src/kustomize

[Container] 2022/04/29 13:43:55 Running command sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize

[Container] 2022/04/29 13:43:56 Running command kustomize version
{Version:kustomize/v4.2.0 GitCommit:d53a2ad45d04b0264bcee9e19879437d851cb778 BuildDate:2021-06-30T22:49:26Z GoOs:linux GoArch:amd64}

[Container] 2022/04/29 13:43:56 Phase complete: INSTALL State: SUCCEEDED
[Container] 2022/04/29 13:43:56 Phase context status code:  Message:

installコマンドも省略できる

もう一度ダウンロードスクリプトを読んでみます。

#!/usr/bin/env bash
# Copyright 2022 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0


# If no argument is given -> Downloads the most recently released
# kustomize binary to your current working directory.
# (e.g. 'install_kustomize.sh')
#
# If one argument is given -> 
# If that argument is in the format of #.#.#, downloads the specified
# version of the kustomize binary to your current working directory.
# If that argument is something else, downloads the most recently released
# kustomize binary to the specified directory.
# (e.g. 'install_kustomize.sh 3.8.2' or 'install_kustomize.sh $(go env GOPATH)/bin')
#
# If two arguments are given -> Downloads the specified version of the
# kustomize binary to the specified directory.
# (e.g. 'install_kustomize.sh 3.8.2 $(go env GOPATH)/bin')
#
# Fails if the file already exists.

set -e

# Unset CDPATH to restore default cd behavior. An exported CDPATH can
# cause cd to output the current directory to STDOUT.
unset CDPATH

where=$PWD
(続く)

If two arguments are given -> Downloads the specified version of the
kustomize binary to the specified directory.
(e.g. 'install_kustomize.sh 3.8.2 $(go env GOPATH)/bin')

なるほど。ダウンロード先も引数で指定できるんですね。

buildspec.yml

 ---
 version: 0.2
 env:
   variables:
     KUSTOMIZE_VERSION: "4.2.0"
 phases:
   install:
     commands:
       - curl -Os "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${KUSTOMIZE_VERSION}/hack/install_kustomize.sh"
-      - bash install_kustomize.sh ${KUSTOMIZE_VERSION}
+      - bash install_kustomize.sh ${KUSTOMIZE_VERSION} /usr/local/bin/
-      - yum install sudo -y --quiet
-      - sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
       - kustomize version

第2引数を指定することで、sudoコマンドのインストール(Dockerイメージに入ってたのでそもそも不要でしたが)もinstallコマンドの実行も不要になりました?

おまけ:似たようなミスをTFLintでもしました