Skip to content

Commit 6cb0f4c

Browse files
imjasonhtekton-robot
authored andcommitted
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.
1 parent ed4200b commit 6cb0f4c

File tree

6 files changed

+120
-5
lines changed

6 files changed

+120
-5
lines changed

cmd/entrypoint/main.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"encoding/json"
2121
"flag"
22+
"fmt"
2223
"log"
2324
"os"
2425
"os/exec"
@@ -110,11 +111,10 @@ func main() {
110111
log.Fatal(err)
111112
}
112113
plat := platforms.DefaultString()
113-
114-
var found bool
115-
cmd, found = cmds[plat]
116-
if !found {
117-
log.Fatalf("could not find command for platform %q", plat)
114+
var err error
115+
cmd, err = selectCommandForPlatform(cmds, plat)
116+
if err != nil {
117+
log.Fatal(err)
118118
}
119119
}
120120

@@ -173,3 +173,20 @@ func main() {
173173
}
174174
}
175175
}
176+
177+
func selectCommandForPlatform(cmds map[string][]string, plat string) ([]string, error) {
178+
cmd, found := cmds[plat]
179+
if found {
180+
return cmd, nil
181+
}
182+
183+
// If the command wasn't found, check if there's a
184+
// command defined for the same platform without a CPU
185+
// variant specified.
186+
platWithoutVariant := plat[:strings.LastIndex(plat, "/")]
187+
cmd, found = cmds[platWithoutVariant]
188+
if found {
189+
return cmd, nil
190+
}
191+
return nil, fmt.Errorf("could not find command for platform %q", plat)
192+
}

cmd/entrypoint/namespaces.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !linux
12
// +build !linux
23

34
package main

cmd/entrypoint/namespaces_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
//go:build linux
12
// +build linux
23

4+
/*
5+
Copyright 2022 The Tekton Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
320
package main
421

522
import (

cmd/entrypoint/platform_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2022 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
)
24+
25+
func TestSelectCommandForPlatform(t *testing.T) {
26+
for _, c := range []struct {
27+
desc string
28+
m map[string][]string
29+
plat string
30+
want []string
31+
wantErr bool
32+
}{{
33+
desc: "platform exists",
34+
m: map[string][]string{
35+
"linux/amd64": {"my", "command"},
36+
"linux/s390x": {"other", "one"},
37+
},
38+
plat: "linux/amd64",
39+
want: []string{"my", "command"},
40+
}, {
41+
desc: "platform not found",
42+
m: map[string][]string{
43+
"linux/amd64": {"my", "command"},
44+
"linux/s390x": {"other", "one"},
45+
},
46+
plat: "linux/ppc64le",
47+
wantErr: true,
48+
}, {
49+
desc: "platform fallback",
50+
m: map[string][]string{
51+
"linux/amd64": {"my", "command"},
52+
"linux/s390x": {"other", "one"},
53+
},
54+
plat: "linux/amd64/v8",
55+
want: []string{"my", "command"},
56+
}, {
57+
desc: "platform fallback not needed",
58+
m: map[string][]string{
59+
"linux/amd64": {"other", "one"},
60+
"linux/amd64/v8": {"my", "command"},
61+
},
62+
plat: "linux/amd64/v8",
63+
want: []string{"my", "command"},
64+
}} {
65+
t.Run(c.desc, func(t *testing.T) {
66+
got, err := selectCommandForPlatform(c.m, c.plat)
67+
if err != nil {
68+
if c.wantErr {
69+
return
70+
}
71+
t.Fatalf("Unexpected error: %v", err)
72+
}
73+
if d := cmp.Diff(c.want, got); d != "" {
74+
t.Fatalf("Diff(-want,+got):\n%s", d)
75+
}
76+
})
77+
}
78+
}

cmd/entrypoint/runner.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
/*

cmd/entrypoint/runner_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
/*

0 commit comments

Comments
 (0)