forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
298 lines (255 loc) · 9.83 KB
/
utils.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package utils
import (
"fmt"
"strconv"
"github.com/docker/go-connections/nat"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
adaptersCommon "github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/lclient"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/util"
"github.com/golang/glog"
"github.com/pkg/errors"
)
const (
supervisordVolume = "supervisord"
projectsVolume = "projects"
)
// ComponentExists checks if Docker containers labeled with the specified component name exists
func ComponentExists(client lclient.Client, name string) bool {
containers, err := GetComponentContainers(client, name)
if err != nil {
// log the error since this function basically returns a bool
log.Error(err)
return false
}
return len(containers) != 0
}
// GetComponentContainers returns a list of the running component containers
func GetComponentContainers(client lclient.Client, componentName string) (containers []types.Container, err error) {
containerList, err := client.GetContainerList()
if err != nil {
return
}
containers = client.GetContainersByComponent(componentName, containerList)
return
}
// GetContainerIDForAlias returns the container ID for the devfile alias from a list of containers
func GetContainerIDForAlias(containers []types.Container, alias string) string {
containerID := ""
for _, container := range containers {
if container.Labels["alias"] == alias {
containerID = container.ID
}
}
return containerID
}
// ConvertEnvs converts environment variables from the devfile structure to an array of strings, as expected by Docker
func ConvertEnvs(vars []common.DockerimageEnv) []string {
dockerVars := []string{}
for _, env := range vars {
envString := fmt.Sprintf("%s=%s", *env.Name, *env.Value)
dockerVars = append(dockerVars, envString)
}
return dockerVars
}
// ConvertPorts converts endpoints from the devfile structure to PortSet, which is expected by Docker
func ConvertPorts(endpoints []common.DockerimageEndpoint) nat.PortSet {
portSet := nat.PortSet{}
for _, endpoint := range endpoints {
port := nat.Port(strconv.Itoa(int(*endpoint.Port)) + "/tcp")
portSet[port] = struct{}{}
}
return portSet
}
// DoesContainerNeedUpdating returns true if a given container needs to be removed and recreated
// This function compares values in the container vs corresponding values in the devfile component.
// If any of the values between the two differ, a restart is required (and this function returns true)
// Unlike Kube, Docker doesn't provide a mechanism to update a container in place only when necesary
// so this function is necessary to prevent having to restart the container on every odo pushs
func DoesContainerNeedUpdating(component common.DevfileComponent, containerConfig *container.Config, hostConfig *container.HostConfig, devfileMounts []mount.Mount, containerMounts []types.MountPoint, portMap nat.PortMap) bool {
// If the image was changed in the devfile, the container needs to be updated
if *component.Image != containerConfig.Image {
return true
}
// Update the container if the volumes were updated in the devfile
for _, devfileMount := range devfileMounts {
if !containerHasMount(devfileMount, containerMounts) {
return true
}
}
// Update the container if the env vars were updated in the devfile
// Need to convert the devfile envvars to the format expected by Docker
devfileEnvVars := ConvertEnvs(component.Env)
for _, envVar := range devfileEnvVars {
if !containerHasEnvVar(envVar, containerConfig.Env) {
return true
}
}
devfilePorts := ConvertPorts(component.Endpoints)
for port := range devfilePorts {
if !containerHasPort(port, containerConfig.ExposedPorts) {
return true
}
}
for internalPort, portbinding := range portMap {
if hostConfig.PortBindings[internalPort] == nil || hostConfig.PortBindings[internalPort][0].HostPort != portbinding[0].HostPort {
// if there is no exposed port assigned to the internal port, or if the exposed port has changed
return true
}
}
return false
}
// AddVolumeToContainer adds the volume name and mount to the container host config
func AddVolumeToContainer(volumeName, volumeMount string, hostConfig *container.HostConfig) *container.HostConfig {
mount := mount.Mount{
Type: mount.TypeVolume,
Source: volumeName,
Target: volumeMount,
}
hostConfig.Mounts = append(hostConfig.Mounts, mount)
return hostConfig
}
// GetProjectVolumeLabels returns the label selectors used to retrieve the project/source volume for a given component
func GetProjectVolumeLabels(componentName string) map[string]string {
volumeLabels := map[string]string{
"component": componentName,
"type": projectsVolume,
}
return volumeLabels
}
// GetContainerLabels returns the label selectors used to retrieve/create the component container
func GetContainerLabels(componentName, alias string) map[string]string {
containerLabels := map[string]string{
"component": componentName,
"alias": alias,
}
return containerLabels
}
// GetSupervisordVolumeLabels returns the label selectors used to retrieve the supervisord volume
func GetSupervisordVolumeLabels() map[string]string {
image := adaptersCommon.GetBootstrapperImage()
_, _, _, imageTag := util.ParseComponentImageName(image)
supervisordLabels := map[string]string{
"name": adaptersCommon.SupervisordVolumeName,
"type": supervisordVolume,
"version": imageTag,
}
return supervisordLabels
}
// containerHasEnvVar returns true if the specified env var (and value) exist in the list of container env vars
func containerHasEnvVar(envVar string, containerEnv []string) bool {
for _, env := range containerEnv {
if envVar == env {
return true
}
}
return false
}
// containerHasMount returns true if the specified volume is mounted in the given container
func containerHasMount(devfileMount mount.Mount, containerMounts []types.MountPoint) bool {
for _, mount := range containerMounts {
if devfileMount.Source == mount.Name && devfileMount.Target == mount.Destination {
return true
}
}
return false
}
func containerHasPort(devfilePort nat.Port, exposedPorts nat.PortSet) bool {
for port := range exposedPorts {
if devfilePort.Port() == port.Port() {
return true
}
}
return false
}
// UpdateComponentWithSupervisord updates the devfile component's
// 1. command and args with supervisord, if absent
// 2. env with ODO_COMMAND_RUN and ODO_COMMAND_RUN_WORKING_DIR, if absent
func UpdateComponentWithSupervisord(comp *common.DevfileComponent, runCommand common.DevfileCommand, supervisordVolumeName string, hostConfig *container.HostConfig) {
// Mount the supervisord volume for the run command container
for _, action := range runCommand.Actions {
if *action.Component == *comp.Alias {
AddVolumeToContainer(supervisordVolumeName, adaptersCommon.SupervisordMountPath, hostConfig)
if len(comp.Command) == 0 && len(comp.Args) == 0 {
glog.V(4).Infof("Updating container %v entrypoint with supervisord", *comp.Alias)
comp.Command = append(comp.Command, adaptersCommon.SupervisordBinaryPath)
comp.Args = append(comp.Args, "-c", adaptersCommon.SupervisordConfFile)
}
if !adaptersCommon.IsEnvPresent(comp.Env, adaptersCommon.EnvOdoCommandRun) {
envName := adaptersCommon.EnvOdoCommandRun
envValue := *action.Command
comp.Env = append(comp.Env, common.DockerimageEnv{
Name: &envName,
Value: &envValue,
})
}
if !adaptersCommon.IsEnvPresent(comp.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && action.Workdir != nil {
envName := adaptersCommon.EnvOdoCommandRunWorkingDir
envValue := *action.Workdir
comp.Env = append(comp.Env, common.DockerimageEnv{
Name: &envName,
Value: &envValue,
})
}
}
}
}
// CreateAndInitSupervisordVolume creates the supervisord volume and initializes
// it with supervisord bootstrap image - assembly files and supervisord binary
func CreateAndInitSupervisordVolume(client lclient.Client) (string, error) {
supervisordLabels := GetSupervisordVolumeLabels()
supervisordVolume, err := client.CreateVolume(adaptersCommon.SupervisordVolumeName, supervisordLabels)
if err != nil {
return "", errors.Wrapf(err, "Unable to create supervisord volume for component")
}
supervisordVolumeName := supervisordVolume.Name
err = StartBootstrapSupervisordInitContainer(client, supervisordVolumeName)
if err != nil {
return "", errors.Wrapf(err, "Unable to start supervisord container for component")
}
return supervisordVolumeName, nil
}
// StartBootstrapSupervisordInitContainer pulls the supervisord bootstrap image, mounts the supervisord
// volume, starts the bootstrap container and initializes the supervisord volume via its entrypoint
func StartBootstrapSupervisordInitContainer(client lclient.Client, supervisordVolumeName string) error {
supervisordLabels := GetSupervisordVolumeLabels()
image := adaptersCommon.GetBootstrapperImage()
command := []string{"/usr/bin/cp"}
args := []string{
"-r",
adaptersCommon.OdoInitImageContents,
adaptersCommon.SupervisordMountPath,
}
var s *log.Status
if log.IsDebug() {
s = log.Spinnerf("Pulling image %s", image)
defer s.End(false)
}
err := client.PullImage(image)
if err != nil {
return errors.Wrapf(err, "unable to pull %s image", image)
}
if log.IsDebug() {
s.End(true)
}
containerConfig := client.GenerateContainerConfig(image, command, args, nil, supervisordLabels, nil)
hostConfig := container.HostConfig{}
AddVolumeToContainer(supervisordVolumeName, adaptersCommon.SupervisordMountPath, &hostConfig)
// Create the docker container
if log.IsDebug() {
s = log.Spinnerf("Starting container for %s", image)
defer s.End(false)
}
err = client.StartContainer(&containerConfig, &hostConfig, nil)
if err != nil {
return err
}
if log.IsDebug() {
s.End(true)
}
return nil
}