/
container_builder.go
138 lines (115 loc) · 3.96 KB
/
container_builder.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
/*
Copyright 2019 the Velero contributors.
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 builder
import (
"strings"
corev1api "k8s.io/api/core/v1"
)
// ContainerBuilder builds Container objects
type ContainerBuilder struct {
object *corev1api.Container
}
// ForContainer is the constructor for ContainerBuilder.
func ForContainer(name, image string) *ContainerBuilder {
return &ContainerBuilder{
object: &corev1api.Container{
Name: name,
Image: image,
},
}
}
// ForPluginContainer is a helper builder specifically for plugin init containers
func ForPluginContainer(image string, pullPolicy corev1api.PullPolicy) *ContainerBuilder {
volumeMount := ForVolumeMount("plugins", "/target").Result()
return ForContainer(getName(image), image).PullPolicy(pullPolicy).VolumeMounts(volumeMount)
}
// getName returns the 'name' component of a docker
// image that includes the entire string except the registry name, and transforms the combined
// string into a RFC-1123 compatible name.
func getName(image string) string {
slashIndex := strings.Index(image, "/")
slashCount := 0
if slashIndex >= 0 {
slashCount = strings.Count(image[slashIndex:], "/")
}
start := 0
if slashCount > 1 || slashIndex == 0 {
// always start after the first slash when there is a registry name
// or if the string starts with a slash.
start = slashIndex + 1
}
// If the image spec is by digest, remove the digest.
// If it is by tag, remove the tag.
// Otherwise (implicit :latest) leave it alone.
end := len(image)
atIndex := strings.LastIndex(image, "@")
if atIndex > 0 {
end = atIndex
} else {
colonIndex := strings.LastIndex(image, ":")
if colonIndex > 0 {
end = colonIndex
}
}
// https://github.com/distribution/distribution/blob/main/docs/spec/api.md#overview
// valid repository names match the regex [a-z0-9]+(?:[._-][a-z0-9]+)*
// image repository names can container [._] but [._] are not allowed in RFC-1123 labels.
// replace '/', '_' and '.' with '-'
re := strings.NewReplacer("/", "-",
"_", "-",
".", "-")
return re.Replace(image[start:end])
}
// Result returns the built Container.
func (b *ContainerBuilder) Result() *corev1api.Container {
return b.object
}
// Args sets the container's Args.
func (b *ContainerBuilder) Args(args ...string) *ContainerBuilder {
b.object.Args = append(b.object.Args, args...)
return b
}
// VolumeMounts sets the container's VolumeMounts.
func (b *ContainerBuilder) VolumeMounts(volumeMounts ...*corev1api.VolumeMount) *ContainerBuilder {
for _, v := range volumeMounts {
b.object.VolumeMounts = append(b.object.VolumeMounts, *v)
}
return b
}
// Resources sets the container's Resources.
func (b *ContainerBuilder) Resources(resources *corev1api.ResourceRequirements) *ContainerBuilder {
b.object.Resources = *resources
return b
}
// SecurityContext sets the container's SecurityContext.
func (b *ContainerBuilder) SecurityContext(securityContext *corev1api.SecurityContext) *ContainerBuilder {
b.object.SecurityContext = securityContext
return b
}
func (b *ContainerBuilder) Env(vars ...*corev1api.EnvVar) *ContainerBuilder {
for _, v := range vars {
b.object.Env = append(b.object.Env, *v)
}
return b
}
func (b *ContainerBuilder) PullPolicy(pullPolicy corev1api.PullPolicy) *ContainerBuilder {
b.object.ImagePullPolicy = pullPolicy
return b
}
func (b *ContainerBuilder) Command(command []string) *ContainerBuilder {
if b.object.Command == nil {
b.object.Command = []string{}
}
b.object.Command = append(b.object.Command, command...)
return b
}