forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
145 lines (129 loc) · 4.46 KB
/
util.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
package strategy
import (
"path/filepath"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
buildapi "github.com/openshift/origin/pkg/build/api"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// dockerSocketPath is the default path for the Docker socket inside the builder
// container
const (
dockerSocketPath = "/var/run/docker.sock"
dockerPushSecretMountPath = "/var/run/secrets/push"
// TODO: The pull secrets is the same as push secret for now.
// This will be replaced using Service Account.
dockerPullSecretMountPath = dockerPushSecretMountPath
)
// setupDockerSocket configures the pod to support the host's Docker socket
func setupDockerSocket(podSpec *kapi.Pod) {
dockerSocketVolume := kapi.Volume{
Name: "docker-socket",
VolumeSource: kapi.VolumeSource{
HostPath: &kapi.HostPathVolumeSource{
Path: dockerSocketPath,
},
},
}
dockerSocketVolumeMount := kapi.VolumeMount{
Name: "docker-socket",
MountPath: dockerSocketPath,
}
podSpec.Spec.Volumes = append(podSpec.Spec.Volumes,
dockerSocketVolume)
podSpec.Spec.Containers[0].VolumeMounts =
append(podSpec.Spec.Containers[0].VolumeMounts,
dockerSocketVolumeMount)
}
// setupBuildEnv injects human-friendly environment variables which provides
// useful information about the current build.
func setupBuildEnv(build *buildapi.Build, pod *kapi.Pod) error {
vars := []kapi.EnvVar{}
switch build.Parameters.Source.Type {
case buildapi.BuildSourceGit:
vars = append(vars, kapi.EnvVar{Name: "SOURCE_URI", Value: build.Parameters.Source.Git.URI})
vars = append(vars, kapi.EnvVar{Name: "SOURCE_REF", Value: build.Parameters.Source.Git.Ref})
default:
// Do nothing for unknown source types
}
ref, err := imageapi.ParseDockerImageReference(build.Parameters.Output.DockerImageReference)
if err != nil {
return err
}
vars = append(vars, kapi.EnvVar{Name: "OUTPUT_REGISTRY", Value: ref.Registry})
ref.Registry = ""
vars = append(vars, kapi.EnvVar{Name: "OUTPUT_IMAGE", Value: ref.String()})
if len(pod.Spec.Containers) > 0 {
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, vars...)
}
return nil
}
// setupDockerSecrets mounts Docker Registry secrets into Pod running the build,
// allowing Docker to authenticate against private registries or Docker Hub.
func setupDockerSecrets(pod *kapi.Pod, pushSecret string) {
if len(pushSecret) == 0 {
return
}
volume := kapi.Volume{
Name: pushSecret,
VolumeSource: kapi.VolumeSource{
Secret: &kapi.SecretVolumeSource{
SecretName: pushSecret,
},
},
}
volumeMount := kapi.VolumeMount{
Name: pushSecret,
MountPath: dockerPushSecretMountPath,
ReadOnly: true,
}
glog.V(3).Infof("Installed %s as docker push secret in Pod %s", volumeMount.MountPath, pod.Name)
pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, volumeMount)
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, []kapi.EnvVar{
{Name: "PUSH_DOCKERCFG_PATH", Value: filepath.Join(dockerPushSecretMountPath, "dockercfg")},
{Name: "PULL_DOCKERCFG_PATH", Value: filepath.Join(dockerPullSecretMountPath, "dockercfg")},
}...)
}
// mergeEnvWithoutDuplicates merges two environment lists without having
// duplicate items in the output list.
func mergeEnvWithoutDuplicates(source []kapi.EnvVar, output *[]kapi.EnvVar) {
type sourceMapItem struct {
index int
value string
}
// Convert source to Map for faster access
sourceMap := make(map[string]sourceMapItem)
for i, env := range source {
sourceMap[env.Name] = sourceMapItem{i, env.Value}
}
result := *output
for i, env := range result {
// If the value exists in output, override it and remove it
// from the source list
if v, found := sourceMap[env.Name]; found {
result[i].Value = v.value
source = append(source[:v.index], source[v.index+1:]...)
}
}
*output = append(result, source...)
}
// getContainerVerbosity returns the defined BUILD_LOGLEVEL value
func getContainerVerbosity(containerEnv []kapi.EnvVar) (verbosity string) {
for _, env := range containerEnv {
if env.Name == "BUILD_LOGLEVEL" {
verbosity = env.Value
break
}
}
return
}
// getPodLabels copies build labels and adds additional one with build name itself
func getPodLabels(build *buildapi.Build) map[string]string {
podLabels := make(map[string]string)
for k, v := range build.Labels {
podLabels[k] = v
}
podLabels[buildapi.BuildLabel] = build.Name
return podLabels
}