Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: use pkg/platforms instead of containerd/platforms to … #8579

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"syscall"
"time"

"github.com/containerd/containerd/platforms"
"github.com/tektoncd/pipeline/cmd/entrypoint/subcommands"
featureFlags "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
Expand All @@ -37,6 +36,7 @@ import (
"github.com/tektoncd/pipeline/pkg/credentials/dockercreds"
"github.com/tektoncd/pipeline/pkg/credentials/gitcreds"
"github.com/tektoncd/pipeline/pkg/entrypoint"
"github.com/tektoncd/pipeline/pkg/platforms"
"github.com/tektoncd/pipeline/pkg/spire"
"github.com/tektoncd/pipeline/pkg/spire/config"
"github.com/tektoncd/pipeline/pkg/termination"
Expand Down Expand Up @@ -66,6 +66,7 @@ var (

const (
defaultWaitPollingInterval = time.Second
TektonPlatformCommandsEnv = "TEKTON_PLATFORM_COMMANDS"
)

func main() {
Expand Down Expand Up @@ -107,7 +108,7 @@ func main() {
if *ep != "" {
cmd = []string{*ep}
} else {
env := os.Getenv("TEKTON_PLATFORM_COMMANDS")
env := os.Getenv(TektonPlatformCommandsEnv)
var cmds map[string][]string
if err := json.Unmarshal([]byte(env), &cmds); err != nil {
log.Fatal(err)
Expand All @@ -116,7 +117,7 @@ func main() {
// It doesn't include osversion, which is necessary to
// disambiguate two images both for e.g., Windows, that only
// differ by osversion.
plat := platforms.DefaultString()
plat := platforms.NewPlatform().Format()
var err error
cmd, err = selectCommandForPlatform(cmds, plat)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ahmetb/gen-crd-api-reference-docs v0.3.1-0.20220720053627-e327d0730470 // Waiting for https://github.com/ahmetb/gen-crd-api-reference-docs/pull/43/files to merge
github.com/cloudevents/sdk-go/v2 v2.15.2
github.com/containerd/containerd v1.7.25
github.com/go-git/go-git/v5 v5.13.2
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.20.2
Expand All @@ -18,7 +17,7 @@ require (
github.com/hashicorp/golang-lru v1.0.2
github.com/jenkins-x/go-scm v1.14.37
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/image-spec v1.1.0
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sigstore/sigstore v1.8.12
github.com/spiffe/go-spiffe/v2 v2.4.0
Expand Down Expand Up @@ -85,15 +84,12 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.1.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.1 // indirect
github.com/Microsoft/hcsshim v0.11.7 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/kms v1.37.8 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.7 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cyphar/filepath-securejoin v0.3.6 // indirect
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions pkg/platforms/platforms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
Copyright 2025 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 platforms

import (
"errors"
"fmt"
"log/slog"
"path"
"runtime"
"sync"
)

var (
errNotImplemented = errors.New("not implemented")
)

const (
Unknown = "unknown"
Arm = "arm"
Arm64 = "arm64"
Windows = "windows"
Darwin = "darwin"
FreeBSD = "freebsd"
)

// Platform describes the platform which the image in the manifest runs on.
type Platform struct {
// Architecture field specifies the CPU architecture, for example
// `amd64` or `ppc64le`.
Architecture string `json:"architecture"`

// OS specifies the operating system, for example `linux` or `windows`.
OS string `json:"os"`

// OSVersion is an optional field specifying the operating system
// version, for example on Windows `10.0.14393.1066`.
OSVersion string `json:"os.version,omitempty"`

// OSFeatures is an optional field specifying an array of strings,
// each listing a required OS feature (for example on Windows `win32k`).
OSFeatures []string `json:"os.features,omitempty"`

// Variant is an optional field specifying a variant of the CPU, for
// example `v7` to specify ARMv7 when architecture is `arm`.
Variant string `json:"variant,omitempty"`
}

func NewPlatform() *Platform {
p := &Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
Variant: cpuVariant(),
}
return p
}

func (p *Platform) Format() string {
if p.OS == "" {
return Unknown
}

return path.Join(p.OS, p.Architecture, p.Variant)
}

// Present the ARM instruction set architecture, eg: v7, v8
// Don't use this value directly; call cpuVariant() instead.
var cpuVariantValue string

var cpuVariantOnce sync.Once

func cpuVariant() string {
cpuVariantOnce.Do(func() {
if isArmArch(runtime.GOARCH) {
var err error
cpuVariantValue, err = getCPUVariant()
if err != nil {
slog.Error("failed to get CPU variant", "os", runtime.GOOS, "error", err)
}
}
})
return cpuVariantValue
}

// isArmArch returns true if the architecture is ARM.
//
// The arch value should be normalized before being passed to this function.
func isArmArch(arch string) bool {
switch arch {
case Arm, Arm64:
return true
}
return false
}

func getCPUVariant() (string, error) {
var variant string

switch runtime.GOOS {
case Windows, Darwin:
// Windows/Darwin only supports v7 for ARM32 and v8 for ARM64
switch runtime.GOARCH {
case Arm64:
variant = "v8"
case Arm:
variant = "v7"
default:
variant = Unknown
}
case FreeBSD:
// FreeBSD supports ARMv6 and ARMv7 as well as ARMv4 and ARMv5 (though deprecated)
// detecting those variants is currently unimplemented
switch runtime.GOARCH {
case Arm64:
variant = "v8"
default:
variant = Unknown
}
default:
return "", fmt.Errorf("getCPUVariant for OS %s: %w", runtime.GOOS, errNotImplemented)
}

return variant, nil
}
67 changes: 67 additions & 0 deletions pkg/platforms/platforms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2025 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 platforms

import (
"testing"
)

func TestNewPlatform(t *testing.T) {
p := NewPlatform()
if p.OS == "" {
t.Errorf("Expected OS to be set, got empty string")
}
if p.Architecture == "" {
t.Errorf("Expected Arch to be set, got empty string")
}
}

func TestPlatformFormat(t *testing.T) {
p := &Platform{
OS: "linux",
Architecture: "amd64",
Variant: "v8",
}
expected := "linux/amd64/v8"
if got := p.Format(); got != expected {
t.Errorf("Expected %s, got %s", expected, got)
}
}

func TestIsArmArch(t *testing.T) {
tests := []struct {
arch string
expected bool
}{
{"arm", true},
{"arm64", true},
{"amd64", false},
{"ppc64le", false},
}

for _, tt := range tests {
if got := isArmArch(tt.arch); got != tt.expected {
t.Errorf("isArmArch(%s) = %v, expected %v", tt.arch, got, tt.expected)
}
}
}

func TestGetCPUVariant(t *testing.T) {
variant, err := getCPUVariant()
if err != nil && variant != "" {
t.Errorf("Expected error but got variant: %s", variant)
}
}
29 changes: 15 additions & 14 deletions pkg/pod/entrypoint_lookup_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"errors"
"fmt"

"github.com/containerd/containerd/platforms"
"github.com/google/go-containerregistry/pkg/authn/k8schain"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
lru "github.com/hashicorp/golang-lru"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/tektoncd/pipeline/pkg/platforms"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -165,6 +164,7 @@ func imageInfo(img v1.Image, hasArgs bool) (cmd []string, platform string, err e
if err != nil {
return nil, "", err
}

ep := cf.Config.Entrypoint
if len(ep) == 0 {
ep = cf.Config.Cmd
Expand All @@ -173,16 +173,17 @@ func imageInfo(img v1.Image, hasArgs bool) (cmd []string, platform string, err e
ep = append(ep, cf.Config.Cmd...)
}

plat := platforms.Format(specs.Platform{
OS: cf.OS,
Architecture: cf.Architecture,
// A single image's config metadata doesn't include the CPU
// architecture variant, but we'll assume this is okay since
// the runtime node's image selection will also select the same
// image. This will only be a problem if the image is a
// single-platform image that happens to specify a variant, and
// the runtime node it gets assigned to has a value for
// runtime.GOARM.
})
return ep, plat, nil
platformObj := platforms.NewPlatform()
platformObj.OS = cf.OS
platformObj.Architecture = cf.Architecture
// A single image's config metadata doesn't include the CPU
// architecture variant, but we'll assume this is okay since
// the runtime node's image selection will also select the same
// image. This will only be a problem if the image is a
// single-platform image that happens to specify a variant, and
// the runtime node it gets assigned to has a value for
// runtime.GOARM.
platform = platformObj.Format()

return ep, platform, nil
}
21 changes: 0 additions & 21 deletions vendor/github.com/Microsoft/hcsshim/LICENSE

This file was deleted.

Loading
Loading