forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
197 lines (167 loc) · 6.05 KB
/
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
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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/build/api"
bld "github.com/openshift/origin/pkg/build/builder"
"github.com/openshift/origin/pkg/build/builder/cmd/scmauth"
"github.com/openshift/origin/pkg/client"
dockerutil "github.com/openshift/origin/pkg/cmd/util/docker"
"github.com/openshift/origin/pkg/generate/git"
)
type builder interface {
Build(dockerClient bld.DockerClient, sock string, buildsClient client.BuildInterface, build *api.Build, gitClient bld.GitClient) error
}
type builderConfig struct {
build *api.Build
sourceSecretDir string
dockerClient *docker.Client
dockerEndpoint string
buildsClient client.BuildInterface
}
func newBuilderConfigFromEnvironment() (*builderConfig, error) {
cfg := &builderConfig{}
var err error
// build (BUILD)
buildStr := os.Getenv("BUILD")
glog.V(4).Infof("$BUILD env var is %s \n", buildStr)
cfg.build = &api.Build{}
if err = latest.Codec.DecodeInto([]byte(buildStr), cfg.build); err != nil {
return nil, fmt.Errorf("unable to parse build: %v", err)
}
// sourceSecretsDir (SOURCE_SECRET_PATH)
cfg.sourceSecretDir = os.Getenv("SOURCE_SECRET_PATH")
// dockerClient and dockerEndpoint (DOCKER_HOST)
// usually not set, defaults to docker socket
cfg.dockerClient, cfg.dockerEndpoint, err = dockerutil.NewHelper().GetClient()
if err != nil {
return nil, fmt.Errorf("error obtaining docker client: %v", err)
}
// buildsClient (KUBERNETES_SERVICE_HOST, KUBERNETES_SERVICE_PORT)
clientConfig, err := kclient.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to get client config: %v", err)
}
osClient, err := client.New(clientConfig)
if err != nil {
return nil, fmt.Errorf("error obtaining OpenShift client: %v", err)
}
cfg.buildsClient = osClient.Builds(cfg.build.Namespace)
return cfg, nil
}
func (c *builderConfig) setupGitEnvironment() ([]string, error) {
gitSource := c.build.Spec.Source.Git
// For now, we only handle git. If not specified, we're done
if gitSource == nil {
return []string{}, nil
}
sourceSecret := c.build.Spec.Source.SourceSecret
gitEnv := []string{"GIT_ASKPASS=true"}
// If a source secret is present, set it up and add its environment variables
if sourceSecret != nil {
// TODO: this should be refactored to let each source type manage which secrets
// it accepts
sourceURL, err := git.ParseRepository(gitSource.URI)
if err != nil {
return nil, fmt.Errorf("cannot parse build URL: %s", gitSource.URI)
}
scmAuths := scmauth.GitAuths(sourceURL)
// TODO: remove when not necessary to fix up the secret dir permission
sourceSecretDir, err := fixSecretPermissions(c.sourceSecretDir)
if err != nil {
return nil, fmt.Errorf("cannot fix source secret permissions: %v", err)
}
secretsEnv, overrideURL, err := scmAuths.Setup(sourceSecretDir)
if err != nil {
return nil, fmt.Errorf("cannot setup source secret: %v", err)
}
if overrideURL != nil {
c.build.Annotations[bld.OriginalSourceURLAnnotationKey] = gitSource.URI
gitSource.URI = overrideURL.String()
}
gitEnv = append(gitEnv, secretsEnv...)
}
if len(gitSource.HTTPProxy) > 0 {
gitEnv = append(gitEnv, fmt.Sprintf("HTTP_PROXY=%s", gitSource.HTTPProxy))
gitEnv = append(gitEnv, fmt.Sprintf("http_proxy=%s", gitSource.HTTPProxy))
}
if len(gitSource.HTTPSProxy) > 0 {
gitEnv = append(gitEnv, fmt.Sprintf("HTTPS_PROXY=%s", gitSource.HTTPSProxy))
gitEnv = append(gitEnv, fmt.Sprintf("https_proxy=%s", gitSource.HTTPSProxy))
}
return bld.MergeEnv(os.Environ(), gitEnv), nil
}
// execute is responsible for running a build
func (c *builderConfig) execute(b builder) error {
gitEnv, err := c.setupGitEnvironment()
if err != nil {
return err
}
gitClient := git.NewRepositoryWithEnv(gitEnv)
if err := b.Build(c.dockerClient, c.dockerEndpoint, c.buildsClient, c.build, gitClient); err != nil {
return fmt.Errorf("build error: %v", err)
}
if c.build.Spec.Output.To == nil || len(c.build.Spec.Output.To.Name) == 0 {
glog.Warning("Build does not have an Output defined, no output image was pushed to a registry.")
}
return nil
}
// fixSecretPermissions loweres access permissions to very low acceptable level
// TODO: this method should be removed as soon as secrets permissions are fixed upstream
// Kubernetes issue: https://github.com/kubernetes/kubernetes/issues/4789
func fixSecretPermissions(secretsDir string) (string, error) {
secretTmpDir, err := ioutil.TempDir("", "tmpsecret")
if err != nil {
return "", err
}
cmd := exec.Command("cp", "-R", ".", secretTmpDir)
cmd.Dir = secretsDir
if err := cmd.Run(); err != nil {
return "", err
}
secretFiles, err := ioutil.ReadDir(secretTmpDir)
if err != nil {
return "", err
}
for _, file := range secretFiles {
if err := os.Chmod(filepath.Join(secretTmpDir, file.Name()), 0600); err != nil {
return "", err
}
}
return secretTmpDir, nil
}
type dockerBuilder struct{}
// Build starts a Docker build.
func (dockerBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient client.BuildInterface, build *api.Build, gitClient bld.GitClient) error {
return bld.NewDockerBuilder(dockerClient, buildsClient, build, gitClient).Build()
}
type s2iBuilder struct{}
// Build starts an S2I build.
func (s2iBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient client.BuildInterface, build *api.Build, gitClient bld.GitClient) error {
return bld.NewS2IBuilder(dockerClient, sock, buildsClient, build, gitClient).Build()
}
func runBuild(builder builder) {
cfg, err := newBuilderConfigFromEnvironment()
if err != nil {
glog.Fatalf("Cannot setup builder configuration: %v", err)
}
err = cfg.execute(builder)
if err != nil {
glog.Fatalf("Error: %v", err)
}
}
// RunDockerBuild creates a docker builder and runs its build
func RunDockerBuild() {
runBuild(dockerBuilder{})
}
// RunSTIBuild creates a STI builder and runs its build
func RunSTIBuild() {
runBuild(s2iBuilder{})
}