forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sti.go
140 lines (123 loc) · 4.1 KB
/
sti.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
package strategy
import (
"fmt"
"io/ioutil"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/admission"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/controller/serviceaccount"
"k8s.io/kubernetes/pkg/runtime"
buildapi "github.com/openshift/origin/pkg/build/api"
buildutil "github.com/openshift/origin/pkg/build/util"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
// SourceBuildStrategy creates STI(source to image) builds
type SourceBuildStrategy struct {
Image string
TempDirectoryCreator TempDirectoryCreator
// Codec is the codec to use for encoding the output pod.
// IMPORTANT: This may break backwards compatibility when
// it changes.
Codec runtime.Codec
AdmissionControl admission.Interface
}
type TempDirectoryCreator interface {
CreateTempDirectory() (string, error)
}
type tempDirectoryCreator struct{}
func (tc *tempDirectoryCreator) CreateTempDirectory() (string, error) {
return ioutil.TempDir("", "stibuild")
}
var STITempDirectoryCreator = &tempDirectoryCreator{}
// CreateBuildPod creates a pod that will execute the STI build
// TODO: Make the Pod definition configurable
func (bs *SourceBuildStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod, error) {
data, err := bs.Codec.Encode(build)
if err != nil {
return nil, fmt.Errorf("failed to encode the Build %s/%s: %v", build.Namespace, build.Name, err)
}
containerEnv := []kapi.EnvVar{
{Name: "BUILD", Value: string(data)},
{Name: "BUILD_LOGLEVEL", Value: fmt.Sprintf("%d", cmdutil.GetLogLevel())},
}
addSourceEnvVars(build.Spec.Source, &containerEnv)
strategy := build.Spec.Strategy.SourceStrategy
if len(strategy.Env) > 0 {
mergeTrustedEnvWithoutDuplicates(strategy.Env, &containerEnv)
}
// check if can run container as root
if !bs.canRunAsRoot(build) {
containerEnv = append(containerEnv, kapi.EnvVar{Name: "ALLOWED_UIDS", Value: "1-"})
}
privileged := true
pod := &kapi.Pod{
ObjectMeta: kapi.ObjectMeta{
Name: buildutil.GetBuildPodName(build),
Namespace: build.Namespace,
Labels: getPodLabels(build),
},
Spec: kapi.PodSpec{
ServiceAccountName: build.Spec.ServiceAccount,
Containers: []kapi.Container{
{
Name: "sti-build",
Image: bs.Image,
Env: containerEnv,
// TODO: run unprivileged https://github.com/openshift/origin/issues/662
SecurityContext: &kapi.SecurityContext{
Privileged: &privileged,
},
Args: []string{"--loglevel=" + getContainerVerbosity(containerEnv)},
},
},
RestartPolicy: kapi.RestartPolicyNever,
},
}
pod.Spec.Containers[0].ImagePullPolicy = kapi.PullIfNotPresent
pod.Spec.Containers[0].Resources = build.Spec.Resources
if build.Spec.CompletionDeadlineSeconds != nil {
pod.Spec.ActiveDeadlineSeconds = build.Spec.CompletionDeadlineSeconds
}
if build.Spec.Source.Binary != nil {
pod.Spec.Containers[0].Stdin = true
pod.Spec.Containers[0].StdinOnce = true
}
setupDockerSocket(pod)
var sourceImageSecret *kapi.LocalObjectReference
if build.Spec.Source.Image != nil {
sourceImageSecret = build.Spec.Source.Image.PullSecret
}
setupDockerSecrets(pod, build.Spec.Output.PushSecret, strategy.PullSecret, sourceImageSecret)
setupSourceSecrets(pod, build.Spec.Source.SourceSecret)
return pod, nil
}
func (bs *SourceBuildStrategy) canRunAsRoot(build *buildapi.Build) bool {
var rootUser int64
rootUser = 0
pod := &kapi.Pod{
ObjectMeta: kapi.ObjectMeta{
Name: buildutil.GetBuildPodName(build),
Namespace: build.Namespace,
},
Spec: kapi.PodSpec{
ServiceAccountName: build.Spec.ServiceAccount,
Containers: []kapi.Container{
{
Name: "sti-build",
Image: bs.Image,
SecurityContext: &kapi.SecurityContext{
RunAsUser: &rootUser,
},
},
},
RestartPolicy: kapi.RestartPolicyNever,
},
}
userInfo := serviceaccount.UserInfo(build.Namespace, build.Spec.ServiceAccount, "")
attrs := admission.NewAttributesRecord(pod, "Pod", pod.Namespace, pod.Name, "pods", "", admission.Create, userInfo)
err := bs.AdmissionControl.Admit(attrs)
if err != nil {
glog.V(2).Infof("Admit for root user returned error: %v", err)
}
return err == nil
}