|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "math/rand" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/awslabs/amazon-ecr-credential-helper/ecr-login" |
| 13 | + "github.com/chrismellard/docker-credential-acr-env/pkg/credhelper" |
| 14 | + "github.com/google/go-containerregistry/pkg/authn" |
| 15 | + "github.com/google/go-containerregistry/pkg/authn/github" |
| 16 | + "github.com/google/go-containerregistry/pkg/name" |
| 17 | + "github.com/google/go-containerregistry/pkg/v1/google" |
| 18 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 19 | + "github.com/google/go-containerregistry/pkg/v1/types" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | +) |
| 22 | + |
| 23 | +type ArmResult struct { |
| 24 | + Supported bool |
| 25 | + Err error |
| 26 | +} |
| 27 | + |
| 28 | +const MaxConcurrent = 7 |
| 29 | + |
| 30 | +var armResultCache = make(map[string]ArmResult) |
| 31 | +var armResultCacheMutex sync.RWMutex |
| 32 | + |
| 33 | +func CheckAllWorkloadsArm(workloads []Workload) []ArmResult { |
| 34 | + results := make([]ArmResult, len(workloads)) |
| 35 | + |
| 36 | + var wg sync.WaitGroup |
| 37 | + sem := make(chan struct{}, MaxConcurrent) |
| 38 | + |
| 39 | + for idx := range workloads { |
| 40 | + wg.Add(1) |
| 41 | + |
| 42 | + go func(i int) { |
| 43 | + defer wg.Done() |
| 44 | + |
| 45 | + sem <- struct{}{} |
| 46 | + defer func() { <-sem }() |
| 47 | + |
| 48 | + cacheKey := fmt.Sprintf("%s:%s:%s", workloads[i].Kind, workloads[i].Name, workloads[i].Namespace) |
| 49 | + |
| 50 | + armResultCacheMutex.RLock() |
| 51 | + cachedResult, found := armResultCache[cacheKey] |
| 52 | + armResultCacheMutex.RUnlock() |
| 53 | + |
| 54 | + if found { |
| 55 | + results[i] = cachedResult |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + var supported bool |
| 60 | + var err error |
| 61 | + |
| 62 | + for { |
| 63 | + supported, err = CheckWorkloadSupportsArm(&workloads[i]) |
| 64 | + if err != nil && strings.Contains(err.Error(), "TOOMANYREQUESTS") { |
| 65 | + time.Sleep(time.Millisecond * time.Duration(rand.Int63n(800))) |
| 66 | + } else { |
| 67 | + break |
| 68 | + } |
| 69 | + } |
| 70 | + result := ArmResult{Supported: supported, Err: err} |
| 71 | + results[i] = result |
| 72 | + |
| 73 | + armResultCacheMutex.Lock() |
| 74 | + armResultCache[cacheKey] = result |
| 75 | + armResultCacheMutex.Unlock() |
| 76 | + }(idx) |
| 77 | + } |
| 78 | + wg.Wait() |
| 79 | + return results |
| 80 | +} |
| 81 | + |
| 82 | +func CheckWorkloadSupportsArm(w *Workload) (bool, error) { |
| 83 | + var podSpec *corev1.PodSpec |
| 84 | + switch w.Kind { |
| 85 | + case WorkloadDeployment: |
| 86 | + podSpec = &w.deployment.Spec.Template.Spec |
| 87 | + case WorkloadStatefulSet: |
| 88 | + podSpec = &w.statefulSet.Spec.Template.Spec |
| 89 | + default: |
| 90 | + return false, fmt.Errorf("unsupported workload kind: %s", w.Kind) |
| 91 | + } |
| 92 | + images := getPodTemplateImages(*podSpec) |
| 93 | + |
| 94 | + supportArm := true |
| 95 | + for _, image := range images { |
| 96 | + ret, err := imageSupportsArm64(image) |
| 97 | + if err != nil { |
| 98 | + return false, fmt.Errorf("failed to check image %s for arm64 support: %w", image, err) |
| 99 | + } |
| 100 | + if ret == false { |
| 101 | + supportArm = false |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + return supportArm, nil |
| 106 | +} |
| 107 | + |
| 108 | +func getPodTemplateImages(podSpec corev1.PodSpec) []string { |
| 109 | + var images []string |
| 110 | + for _, c := range podSpec.Containers { |
| 111 | + images = append(images, c.Image) |
| 112 | + } |
| 113 | + for _, c := range podSpec.InitContainers { |
| 114 | + images = append(images, c.Image) |
| 115 | + } |
| 116 | + return images |
| 117 | +} |
| 118 | + |
| 119 | +var keychain = authn.NewMultiKeychain( |
| 120 | + authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(io.Discard))), // ECR |
| 121 | + authn.NewKeychainFromHelper(credhelper.NewACRCredentialsHelper()), // ACR |
| 122 | + google.Keychain, // GCR & Artifact Registry |
| 123 | + github.Keychain, // GHCR |
| 124 | + authn.DefaultKeychain, // local ~/.docker/config.json |
| 125 | +) |
| 126 | + |
| 127 | +// imageSupportsArm64 checks whether the given container image supports the linux/arm64 |
| 128 | +// platform. It returns true if the image manifest list (index) contains an arm64 |
| 129 | +// variant, or if the single-arch image itself is built for arm64. |
| 130 | +func imageSupportsArm64(imageRef string) (bool, error) { |
| 131 | + // Parse an arbitrary image reference (registry/name:tag or digest). |
| 132 | + ref, err := name.ParseReference(imageRef, name.WeakValidation) |
| 133 | + if err != nil { |
| 134 | + return false, fmt.Errorf("failed to parse image reference: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + // Pull the descriptor (manifest or index) from the remote registry. |
| 138 | + remoteOpts := []remote.Option{ |
| 139 | + remote.WithAuthFromKeychain(keychain), |
| 140 | + remote.WithContext(context.Background()), |
| 141 | + } |
| 142 | + desc, err := remote.Get(ref, remoteOpts...) |
| 143 | + if err != nil { |
| 144 | + return false, fmt.Errorf("failed to fetch image descriptor: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + mt := desc.Descriptor.MediaType |
| 148 | + // Handle multi-arch images (OCI index / Docker manifest list). |
| 149 | + if mt == types.OCIImageIndex || mt == types.DockerManifestList { |
| 150 | + idx, err := desc.ImageIndex() |
| 151 | + if err != nil { |
| 152 | + return false, fmt.Errorf("failed to load image index: %w", err) |
| 153 | + } |
| 154 | + indexManifest, err := idx.IndexManifest() |
| 155 | + if err != nil { |
| 156 | + return false, fmt.Errorf("failed to read index manifest: %w", err) |
| 157 | + } |
| 158 | + for _, manifest := range indexManifest.Manifests { |
| 159 | + plat := manifest.Platform |
| 160 | + if plat != nil && plat.Architecture == "arm64" && strings.EqualFold(plat.OS, "linux") { |
| 161 | + return true, nil // linux/arm64 variant found |
| 162 | + } |
| 163 | + } |
| 164 | + return false, nil // no arm64 variant in the index |
| 165 | + } |
| 166 | + |
| 167 | + // Handle single-arch images. |
| 168 | + img, err := desc.Image() |
| 169 | + if err != nil { |
| 170 | + return false, fmt.Errorf("failed to load image: %w", err) |
| 171 | + } |
| 172 | + cfg, err := img.ConfigFile() |
| 173 | + if err != nil { |
| 174 | + return false, fmt.Errorf("failed to read image config: %w", err) |
| 175 | + } |
| 176 | + return cfg.Architecture == "arm64", nil |
| 177 | +} |
0 commit comments