-
Notifications
You must be signed in to change notification settings - Fork 108
/
images.go
145 lines (126 loc) · 4.37 KB
/
images.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package defaulting
import (
"fmt"
"regexp"
"strings"
)
// ContainerRegistry represent a container registry URL
type ContainerRegistry string
const (
// AgentLatestVersion corresponds to the latest stable agent release
AgentLatestVersion = "7.60.0"
// ClusterAgentLatestVersion corresponds to the latest stable cluster-agent release
ClusterAgentLatestVersion = "7.60.0"
// FIPSProxyLatestVersion corresponds to the latest stable fips-proxy release
FIPSProxyLatestVersion = "1.1.6"
// GCRContainerRegistry corresponds to the datadoghq GCR registry
GCRContainerRegistry ContainerRegistry = "gcr.io/datadoghq"
// DockerHubContainerRegistry corresponds to the datadoghq docker.io registry
DockerHubContainerRegistry ContainerRegistry = "docker.io/datadog"
// PublicECSContainerRegistry corresponds to the datadoghq PublicECSContainerRegistry registry
PublicECSContainerRegistry ContainerRegistry = "public.ecr.aws/datadog"
// DefaultImageRegistry corresponds to the datadoghq containers registry
DefaultImageRegistry = GCRContainerRegistry // TODO: this is also defined elsewhere and not used; consolidate
// JMXTagSuffix prefix tag for agent JMX images
JMXTagSuffix = "-jmx"
agentImageName = "agent"
clusterAgentImageName = "cluster-agent"
)
// imageHasTag identifies whether an image string contains a tag suffix
// Ref: https://github.com/distribution/distribution/blob/v2.7.1/reference/reference.go
var imageHasTag = regexp.MustCompile(`.+:[\w][\w.-]{0,127}$`)
// IsImageNameContainsTag return true if the image name contains a tag
func IsImageNameContainsTag(name string) bool {
// The image name corresponds to a full image string
return imageHasTag.MatchString(name)
}
// Image represents a container image information
type Image struct {
registry ContainerRegistry
imageName string
tag string
isJMX bool
}
// NewImage return a new Image instance
func NewImage(name, tag string, isJMX bool) *Image {
return &Image{
registry: DefaultImageRegistry,
imageName: name,
tag: strings.TrimSuffix(tag, JMXTagSuffix),
isJMX: strings.HasSuffix(tag, JMXTagSuffix) || isJMX,
}
}
// ImageOptions use to allow extra Image configuration
type ImageOptions func(*Image)
// GetLatestAgentImage return the latest stable agent release version
func GetLatestAgentImage(opts ...ImageOptions) string {
image := &Image{
registry: DefaultImageRegistry,
imageName: agentImageName,
tag: AgentLatestVersion,
}
processOptions(image, opts...)
return image.String()
}
// GetLatestAgentImageJMX return the latest JMX stable agent release version
func GetLatestAgentImageJMX(opts ...ImageOptions) string {
image := &Image{
registry: DefaultImageRegistry,
imageName: agentImageName,
tag: AgentLatestVersion,
}
processOptions(image, opts...)
image.tag = fmt.Sprintf("%s%s", image.tag, JMXTagSuffix)
return image.String()
}
// GetLatestClusterAgentImage return the latest stable agent release version
func GetLatestClusterAgentImage(opts ...ImageOptions) string {
image := &Image{
registry: DefaultImageRegistry,
imageName: clusterAgentImageName,
tag: ClusterAgentLatestVersion,
}
processOptions(image, opts...)
return image.String()
}
// WithRegistry ImageOptions to specify the container registry
func WithRegistry(registry ContainerRegistry) ImageOptions {
return func(image *Image) {
image.registry = registry
}
}
// WithTag ImageOptions to specify the container image tag.
func WithTag(tag string) ImageOptions {
return func(image *Image) {
image.tag = tag
}
}
// WithImageName ImageOptions to specify the image name
func WithImageName(name string) ImageOptions {
return func(image *Image) {
image.imageName = name
}
}
// WithJMX ImageOptions to specify if the JMX prefix should be added
func WithJMX(jmx bool) ImageOptions {
return func(image *Image) {
image.isJMX = jmx
}
}
func processOptions(image *Image, opts ...ImageOptions) {
for _, option := range opts {
option(image)
}
}
// String return the string representation of an image
func (i *Image) String() string {
suffix := ""
if i.isJMX {
suffix = JMXTagSuffix
}
return fmt.Sprintf("%s/%s:%s%s", i.registry, i.imageName, i.tag, suffix)
}