From 6cb0f4ccfce095495ca2f0aa20e5db8a791a1afe Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Sat, 5 Feb 2022 07:26:17 -0500 Subject: [PATCH] Ignore variant when looking up command for platform in entrypoint This change changes our runtime platform selection logic to allow matching platforms that don't match the CPU variant, since some indexes only provide the variant-less platform, and should be chosen in that case rather than failing. If the platform's command is found including the CPU variant, it will be used. If not, we'll also check if there's a variant-less platform that matches, and if so, we'll use that command. --- cmd/entrypoint/main.go | 27 +++++++++-- cmd/entrypoint/namespaces.go | 1 + cmd/entrypoint/namespaces_test.go | 17 +++++++ cmd/entrypoint/platform_test.go | 78 +++++++++++++++++++++++++++++++ cmd/entrypoint/runner.go | 1 + cmd/entrypoint/runner_windows.go | 1 + 6 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 cmd/entrypoint/platform_test.go diff --git a/cmd/entrypoint/main.go b/cmd/entrypoint/main.go index 3312d91f9b8..00575104930 100644 --- a/cmd/entrypoint/main.go +++ b/cmd/entrypoint/main.go @@ -19,6 +19,7 @@ package main import ( "encoding/json" "flag" + "fmt" "log" "os" "os/exec" @@ -110,11 +111,10 @@ func main() { log.Fatal(err) } plat := platforms.DefaultString() - - var found bool - cmd, found = cmds[plat] - if !found { - log.Fatalf("could not find command for platform %q", plat) + var err error + cmd, err = selectCommandForPlatform(cmds, plat) + if err != nil { + log.Fatal(err) } } @@ -173,3 +173,20 @@ func main() { } } } + +func selectCommandForPlatform(cmds map[string][]string, plat string) ([]string, error) { + cmd, found := cmds[plat] + if found { + return cmd, nil + } + + // If the command wasn't found, check if there's a + // command defined for the same platform without a CPU + // variant specified. + platWithoutVariant := plat[:strings.LastIndex(plat, "/")] + cmd, found = cmds[platWithoutVariant] + if found { + return cmd, nil + } + return nil, fmt.Errorf("could not find command for platform %q", plat) +} diff --git a/cmd/entrypoint/namespaces.go b/cmd/entrypoint/namespaces.go index a41b372051f..513b7a401d7 100644 --- a/cmd/entrypoint/namespaces.go +++ b/cmd/entrypoint/namespaces.go @@ -1,3 +1,4 @@ +//go:build !linux // +build !linux package main diff --git a/cmd/entrypoint/namespaces_test.go b/cmd/entrypoint/namespaces_test.go index 9f6b0fd10ae..ac7262d689c 100644 --- a/cmd/entrypoint/namespaces_test.go +++ b/cmd/entrypoint/namespaces_test.go @@ -1,5 +1,22 @@ +//go:build linux // +build linux +/* +Copyright 2022 The Tekton 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 main import ( diff --git a/cmd/entrypoint/platform_test.go b/cmd/entrypoint/platform_test.go new file mode 100644 index 00000000000..b607a51cdf5 --- /dev/null +++ b/cmd/entrypoint/platform_test.go @@ -0,0 +1,78 @@ +/* +Copyright 2022 The Tekton 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 main + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestSelectCommandForPlatform(t *testing.T) { + for _, c := range []struct { + desc string + m map[string][]string + plat string + want []string + wantErr bool + }{{ + desc: "platform exists", + m: map[string][]string{ + "linux/amd64": {"my", "command"}, + "linux/s390x": {"other", "one"}, + }, + plat: "linux/amd64", + want: []string{"my", "command"}, + }, { + desc: "platform not found", + m: map[string][]string{ + "linux/amd64": {"my", "command"}, + "linux/s390x": {"other", "one"}, + }, + plat: "linux/ppc64le", + wantErr: true, + }, { + desc: "platform fallback", + m: map[string][]string{ + "linux/amd64": {"my", "command"}, + "linux/s390x": {"other", "one"}, + }, + plat: "linux/amd64/v8", + want: []string{"my", "command"}, + }, { + desc: "platform fallback not needed", + m: map[string][]string{ + "linux/amd64": {"other", "one"}, + "linux/amd64/v8": {"my", "command"}, + }, + plat: "linux/amd64/v8", + want: []string{"my", "command"}, + }} { + t.Run(c.desc, func(t *testing.T) { + got, err := selectCommandForPlatform(c.m, c.plat) + if err != nil { + if c.wantErr { + return + } + t.Fatalf("Unexpected error: %v", err) + } + if d := cmp.Diff(c.want, got); d != "" { + t.Fatalf("Diff(-want,+got):\n%s", d) + } + }) + } +} diff --git a/cmd/entrypoint/runner.go b/cmd/entrypoint/runner.go index ed31d15bdd5..5b7c0a129df 100644 --- a/cmd/entrypoint/runner.go +++ b/cmd/entrypoint/runner.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* diff --git a/cmd/entrypoint/runner_windows.go b/cmd/entrypoint/runner_windows.go index c970fdf9eb4..db75f70316d 100644 --- a/cmd/entrypoint/runner_windows.go +++ b/cmd/entrypoint/runner_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows /*