-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
image.go
63 lines (55 loc) · 1.81 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package addons
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws/endpoints"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
corev1 "k8s.io/api/core/v1"
)
// awsDNSSuffixForRegion returns the AWS DNS suffix (amazonaws.com or amazonaws.com.cn) for the specified region
func awsDNSSuffixForRegion(region string) (string, error) {
for _, p := range endpoints.DefaultPartitions() {
if _, ok := p.Regions()[region]; ok {
return p.DNSSuffix(), nil
}
}
return "", fmt.Errorf("failed to find DNS suffix for region %s", region)
}
// UseRegionalImage sets the region and AWS DNS suffix for a container image
// in format '%s.dkr.ecr.%s.%s/image:tag'
func UseRegionalImage(spec *corev1.PodTemplateSpec, region string) error {
imageFormat := spec.Spec.Containers[0].Image
dnsSuffix, err := awsDNSSuffixForRegion(region)
if err != nil {
return err
}
regionalImage := fmt.Sprintf(imageFormat, api.EKSResourceAccountID(region), region, dnsSuffix)
spec.Spec.Containers[0].Image = regionalImage
if len(spec.Spec.InitContainers) > 0 {
imageFormat = spec.Spec.InitContainers[0].Image
regionalImage = fmt.Sprintf(imageFormat, api.EKSResourceAccountID(region), region, dnsSuffix)
spec.Spec.InitContainers[0].Image = regionalImage
}
return nil
}
// ImageTag extracts the container image's tag.
func ImageTag(image string) (string, error) {
parts := strings.Split(image, ":")
if len(parts) != 2 {
return "", fmt.Errorf("unexpected image format %q", image)
}
return parts[1], nil
}
// ImageTagsDiffer returns true if the image tags are not the same
// while ignoring the image name.
func ImageTagsDiffer(image1, image2 string) (bool, error) {
tag1, err := ImageTag(image1)
if err != nil {
return false, err
}
tag2, err := ImageTag(image2)
if err != nil {
return false, err
}
return tag1 != tag2, nil
}