diff --git a/pkg/app/piped/toolregistry/install.go b/pkg/app/piped/toolregistry/install.go index b2acb36abf..2cdca69b11 100644 --- a/pkg/app/piped/toolregistry/install.go +++ b/pkg/app/piped/toolregistry/install.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "os/exec" + "runtime" "text/template" "go.uber.org/zap" @@ -58,6 +59,7 @@ func (r *registry) installKubectl(ctx context.Context, version string) error { "Version": version, "BinDir": r.binDir, "AsDefault": asDefault, + "Arch": runtime.GOARCH, } ) if err := kubectlInstallScriptTmpl.Execute(&buf, data); err != nil { @@ -105,6 +107,7 @@ func (r *registry) installKustomize(ctx context.Context, version string) error { "Version": version, "BinDir": r.binDir, "AsDefault": asDefault, + "Arch": runtime.GOARCH, } ) if err := kustomizeInstallScriptTmpl.Execute(&buf, data); err != nil { @@ -152,6 +155,7 @@ func (r *registry) installHelm(ctx context.Context, version string) error { "Version": version, "BinDir": r.binDir, "AsDefault": asDefault, + "Arch": runtime.GOARCH, } ) if err := helmInstallScriptTmpl.Execute(&buf, data); err != nil { @@ -199,6 +203,7 @@ func (r *registry) installTerraform(ctx context.Context, version string) error { "Version": version, "BinDir": r.binDir, "AsDefault": asDefault, + "Arch": runtime.GOARCH, } ) if err := terraformInstallScriptTmpl.Execute(&buf, data); err != nil { diff --git a/pkg/app/piped/toolregistry/tool_darwin.go b/pkg/app/piped/toolregistry/tool_darwin.go index c2d715e138..991e90e9c1 100644 --- a/pkg/app/piped/toolregistry/tool_darwin.go +++ b/pkg/app/piped/toolregistry/tool_darwin.go @@ -16,7 +16,7 @@ package toolregistry var kubectlInstallScript = ` cd {{ .WorkingDir }} -curl -LO https://dl.k8s.io/release/v{{ .Version }}/bin/darwin/amd64/kubectl +curl -LO https://dl.k8s.io/release/v{{ .Version }}/bin/darwin/{{ .Arch }}/kubectl mv kubectl {{ .BinDir }}/kubectl-{{ .Version }} chmod +x {{ .BinDir }}/kubectl-{{ .Version }} {{ if .AsDefault }} @@ -26,7 +26,7 @@ cp -f {{ .BinDir }}/kubectl-{{ .Version }} {{ .BinDir }}/kubectl var kustomizeInstallScript = ` cd {{ .WorkingDir }} -curl -L https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v{{ .Version }}/kustomize_v{{ .Version }}_darwin_amd64.tar.gz | tar xvz +curl -L https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v{{ .Version }}/kustomize_v{{ .Version }}_darwin_{{ .Arch }}.tar.gz | tar xvz mv kustomize {{ .BinDir }}/kustomize-{{ .Version }} chmod +x {{ .BinDir }}/kustomize-{{ .Version }} {{ if .AsDefault }} @@ -36,8 +36,8 @@ cp -f {{ .BinDir }}/kustomize-{{ .Version }} {{ .BinDir }}/kustomize var helmInstallScript = ` cd {{ .WorkingDir }} -curl -L https://get.helm.sh/helm-v{{ .Version }}-darwin-amd64.tar.gz | tar xvz -mv darwin-amd64/helm {{ .BinDir }}/helm-{{ .Version }} +curl -L https://get.helm.sh/helm-v{{ .Version }}-darwin-{{ .Arch }}.tar.gz | tar xvz +mv darwin-{{ .Arch }}/helm {{ .BinDir }}/helm-{{ .Version }} chmod +x {{ .BinDir }}/helm-{{ .Version }} {{ if .AsDefault }} cp -f {{ .BinDir }}/helm-{{ .Version }} {{ .BinDir }}/helm @@ -46,8 +46,8 @@ cp -f {{ .BinDir }}/helm-{{ .Version }} {{ .BinDir }}/helm var terraformInstallScript = ` cd {{ .WorkingDir }} -curl https://releases.hashicorp.com/terraform/{{ .Version }}/terraform_{{ .Version }}_darwin_amd64.zip -o terraform_{{ .Version }}_linux_amd64.zip -unzip terraform_{{ .Version }}_linux_amd64.zip +curl https://releases.hashicorp.com/terraform/{{ .Version }}/terraform_{{ .Version }}_darwin_{{ .Arch }}.zip -o terraform_{{ .Version }}_darwin_{{ .Arch }}.zip +unzip terraform_{{ .Version }}_darwin_{{ .Arch }}.zip mv terraform {{ .BinDir }}/terraform-{{ .Version }} {{ if .AsDefault }} cp -f {{ .BinDir }}/terraform-{{ .Version }} {{ .BinDir }}/terraform diff --git a/pkg/app/piped/toolregistry/tool_darwin_test.go b/pkg/app/piped/toolregistry/tool_darwin_test.go new file mode 100644 index 0000000000..0f424e187f --- /dev/null +++ b/pkg/app/piped/toolregistry/tool_darwin_test.go @@ -0,0 +1,54 @@ +// Copyright 2024 The PipeCD Authors. +// +// 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. + +package toolregistry + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDarwinInstallScriptsUseHostArch(t *testing.T) { + data := map[string]interface{}{ + "WorkingDir": "/tmp/work", + "Version": "1.2.3", + "BinDir": "/tmp/bin", + "AsDefault": false, + "Arch": "arm64", + } + + var buf bytes.Buffer + + buf.Reset() + assert.NoError(t, kubectlInstallScriptTmpl.Execute(&buf, data)) + assert.Contains(t, buf.String(), "bin/darwin/arm64/kubectl") + assert.NotContains(t, buf.String(), "bin/darwin/amd64/kubectl") + + buf.Reset() + assert.NoError(t, kustomizeInstallScriptTmpl.Execute(&buf, data)) + assert.Contains(t, buf.String(), "kustomize_v1.2.3_darwin_arm64.tar.gz") + assert.NotContains(t, buf.String(), "kustomize_v1.2.3_darwin_amd64.tar.gz") + + buf.Reset() + assert.NoError(t, helmInstallScriptTmpl.Execute(&buf, data)) + assert.Contains(t, buf.String(), "helm-v1.2.3-darwin-arm64.tar.gz") + assert.NotContains(t, buf.String(), "helm-v1.2.3-darwin-amd64.tar.gz") + + buf.Reset() + assert.NoError(t, terraformInstallScriptTmpl.Execute(&buf, data)) + assert.Contains(t, buf.String(), "terraform_1.2.3_darwin_arm64.zip") + assert.NotContains(t, buf.String(), "terraform_1.2.3_darwin_amd64.zip") +}