Skip to content

Commit 44b6314

Browse files
committed
chore(builder): use env replace creds volume
1 parent d97ad29 commit 44b6314

16 files changed

Lines changed: 281 additions & 307 deletions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RUN install-packages git openssh-server coreutils xz-utils tar \
2727
&& install-stack jq $JQ_VERSION \
2828
&& mkdir -p /var/run/sshd \
2929
&& rm -rf /etc/ssh/ssh_host* \
30-
&& chmod +x /bin/create_bucket /bin/normalize_storage /docker-entrypoint.sh
30+
&& chmod +x /bin/create_bucket /docker-entrypoint.sh
3131

3232
USER ${DRYCC_UID}
3333
WORKDIR ${DRYCC_HOME_DIR}

charts/builder/templates/_helpers.tpl

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
{{- define "builder.envs" -}}
22
env:
3-
# NOTE(bacongobbler): use drycc/registry_proxy to work around Docker --insecure-registry requirements
4-
- name: "DRYCC_REGISTRY_PROXY_HOST"
5-
value: "127.0.0.1"
6-
- name: "DRYCC_REGISTRY_PROXY_PORT"
7-
value: "{{ .Values.global.registryProxyPort }}"
83
- name: "HEALTH_SERVER_PORT"
94
value: "8092"
105
- name: "EXTERNAL_PORT"
116
value: "2223"
127
- name: BUILDER_STORAGE
138
value: "{{ .Values.global.storage }}"
14-
- name: "DRYCC_REGISTRY_LOCATION"
15-
value: "{{ .Values.global.registryLocation }}"
169
- name: "TTL_SECONDS_AFTER_FINISHED"
1710
value: "{{ .Values.global.ttlSecondsAfterFinished }}"
1811
# Set GIT_LOCK_TIMEOUT to number of minutes you want to wait to git push again to the same repository
@@ -34,15 +27,62 @@ env:
3427
secretKeyRef:
3528
name: builder-key-auth
3629
key: builder-key
30+
- name: "DRYCC_MINIO_LOOKUP"
31+
valueFrom:
32+
secretKeyRef:
33+
name: minio-creds
34+
key: lookup
35+
- name: "DRYCC_MINIO_BUCKET"
36+
valueFrom:
37+
secretKeyRef:
38+
name: minio-creds
39+
key: builder-bucket
40+
- name: "DRYCC_MINIO_ENDPOINT"
41+
valueFrom:
42+
secretKeyRef:
43+
name: minio-creds
44+
key: endpoint
45+
- name: "DRYCC_MINIO_ACCESSKEY"
46+
valueFrom:
47+
secretKeyRef:
48+
name: minio-creds
49+
key: accesskey
50+
- name: "DRYCC_MINIO_SECRETKEY"
51+
valueFrom:
52+
secretKeyRef:
53+
name: minio-creds
54+
key: secretkey
55+
- name: "DRYCC_REGISTRY_LOCATION"
56+
value: "{{ .Values.global.registryLocation }}"
57+
- name: "DRYCC_REGISTRY_HOST"
58+
valueFrom:
59+
secretKeyRef:
60+
name: registry-secret
61+
key: host
62+
{{- if ne .Values.global.registryLocation "on-cluster" }}
63+
# NOTE(bacongobbler): use drycc/registry_proxy to work around Docker --insecure-registry requirements
64+
- name: "DRYCC_REGISTRY_PROXY_HOST"
65+
value: {{ print "127.0.0.1" ":" .Values.global.registryProxyPort }}
66+
{{- else }}
67+
- name: "DRYCC_REGISTRY_ORGANIZATION"
68+
valueFrom:
69+
secretKeyRef:
70+
name: registry-secret
71+
key: organization
72+
{{- end }}
73+
- name: "DRYCC_REGISTRY_USERNAME"
74+
valueFrom:
75+
secretKeyRef:
76+
name: registry-secret
77+
key: username
78+
- name: "DRYCC_REGISTRY_PASSWORD"
79+
valueFrom:
80+
secretKeyRef:
81+
name: registry-secret
82+
key: password
3783
{{- if (.Values.builder_pod_node_selector) }}
3884
- name: BUILDER_POD_NODE_SELECTOR
3985
value: {{.Values.builder_pod_node_selector}}
40-
{{- if eq .Values.global.minioLocation "on-cluster" }}
41-
- name: "DRYCC_MINIO_ENDPOINT"
42-
value: http://${DRYCC_MINIO_SERVICE_HOST}:${DRYCC_MINIO_SERVICE_PORT}
43-
{{- else }}
44-
- name: "DRYCC_MINIO_ENDPOINT"
45-
value: "{{ .Values.minio.endpoint }}"
4686
{{- end }}
4787
{{- end }}
4888

charts/builder/templates/builder-deployment.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ spec:
6363
- name: builder-ssh-private-keys
6464
mountPath: /var/run/secrets/drycc/builder/ssh
6565
readOnly: true
66-
- name: minio-creds
67-
mountPath: /var/run/secrets/drycc/minio/creds
68-
readOnly: true
6966
- name: imagebuilder-config
7067
mountPath: /etc/imagebuilder
7168
readOnly: true
@@ -76,9 +73,6 @@ spec:
7673
- name: builder-ssh-private-keys
7774
secret:
7875
secretName: builder-ssh-private-keys
79-
- name: minio-creds
80-
secret:
81-
secretName: minio-creds
8276
- name: imagebuilder-config
8377
configMap:
8478
name: imagebuilder-config

pkg/conf/config.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import (
55
"io/ioutil"
66
"net"
77
"net/url"
8-
"os"
98
"strings"
109

1110
"github.com/drycc/builder/pkg/sys"
1211
)
1312

1413
const (
15-
storageCredLocation = "/var/run/secrets/drycc/minio/creds/"
16-
minioEndpointVar = "DRYCC_MINIO_ENDPOINT"
14+
minioLookupEnvVar = "DRYCC_MINIO_LOOKUP"
15+
minioBucketEnvVar = "DRYCC_MINIO_BUCKET"
16+
minioEndpointEnvVar = "DRYCC_MINIO_ENDPOINT"
17+
minioAccesskeyEnvVar = "DRYCC_MINIO_ACCESSKEY"
18+
minioSecretkeyEnvVar = "DRYCC_MINIO_SECRETKEY"
1719
)
1820

1921
// BuilderKeyLocation holds the path of the builder key secret.
@@ -35,33 +37,23 @@ func GetBuilderKey() (string, error) {
3537
// GetStorageParams returns the credentials required for connecting to object storage
3638
func GetStorageParams(env sys.Env) (Parameters, error) {
3739
params := make(map[string]interface{})
38-
params["builder-bucket"] = "builder" // default
39-
files, err := ioutil.ReadDir(storageCredLocation)
40-
if err != nil {
41-
return nil, err
42-
}
43-
44-
for _, file := range files {
45-
if file.IsDir() || file.Name() == "..data" {
46-
continue
47-
}
48-
data, err := ioutil.ReadFile(storageCredLocation + file.Name())
49-
if err != nil {
50-
return nil, err
51-
}
5240

53-
params[file.Name()] = string(data)
54-
}
55-
params["bucket"] = params["builder-bucket"]
56-
mEndpoint := env.Get(minioEndpointVar)
41+
mEndpoint := env.Get(minioEndpointEnvVar)
5742
params["regionendpoint"] = mEndpoint
5843
region := "us-east-1" //region is required in distribution
5944
if endpointURL, err := url.Parse(mEndpoint); err == nil {
6045
if endpointURL.Hostname() != "" && net.ParseIP(endpointURL.Hostname()) == nil {
6146
region = strings.Split(endpointURL.Hostname(), ".")[0]
6247
}
6348
}
64-
os.Setenv("REGISTRY_STORAGE_S3_REGION", region)
49+
params["region"] = region
50+
51+
params["accesskey"] = env.Get(minioAccesskeyEnvVar)
52+
params["secretkey"] = env.Get(minioSecretkeyEnvVar)
53+
params["bucket"] = env.Get(minioBucketEnvVar)
54+
if env.Get(minioLookupEnvVar) == "path" {
55+
params["forcepathstyle"] = "true"
56+
}
6557

6658
return params, nil
6759
}

pkg/conf/config_test.go

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package conf
33
import (
44
"io/ioutil"
55
"os"
6-
"os/user"
76
"path/filepath"
87
"testing"
98

@@ -12,73 +11,26 @@ import (
1211
)
1312

1413
func TestGetStorageParams(t *testing.T) {
15-
usr, err := user.Current()
16-
if err != nil {
17-
t.Logf("could not retrieve current user: %v", err)
18-
t.SkipNow()
19-
}
20-
if usr.Uid != "0" {
21-
t.Logf("current user does not have UID of zero (got %s) "+
22-
"so cannot create storage cred location, skipping", usr.Uid)
23-
t.SkipNow()
24-
}
25-
26-
if err := os.MkdirAll(storageCredLocation, os.ModeDir); err != nil {
27-
t.Fatalf("could not create storage cred location: %v", err)
28-
}
29-
30-
// start by writing out a file to storageCredLocation
31-
data := []byte("hello world\n")
32-
if err := ioutil.WriteFile(storageCredLocation+"foo", data, 0644); err != nil {
33-
t.Fatalf("could not write file to storage cred location: %v", err)
34-
}
35-
36-
params, err := GetStorageParams(sys.NewFakeEnv())
37-
if err != nil {
38-
t.Errorf("received error while retrieving storage params: %v", err)
39-
}
40-
41-
val, ok := params["foo"]
42-
if !ok {
43-
t.Error("key foo does not exist in storage params")
44-
}
45-
if val != string(data) {
46-
t.Errorf("expected: %s got: %s", string(data), val)
47-
}
48-
49-
// create a directory inside storage cred location, expecting it to pass
50-
if err := os.Mkdir(storageCredLocation+"bar", os.ModeDir); err != nil {
51-
t.Fatalf("could not create dir %s: %v", storageCredLocation+"bar", err)
52-
}
53-
54-
_, err = GetStorageParams(sys.NewFakeEnv())
55-
if err != nil {
56-
t.Errorf("received error while retrieving storage params: %v", err)
57-
}
58-
59-
// create the special "..data" directory symlink, expecting it to pass
60-
if err := os.Symlink(storageCredLocation+"bar", storageCredLocation+"..data"); err != nil {
61-
t.Fatalf("could not create dir symlink ..data -> %s: %v", storageCredLocation+"bar", err)
62-
}
63-
64-
_, err = GetStorageParams(sys.NewFakeEnv())
65-
if err != nil {
66-
t.Errorf("received error while retrieving storage params: %v", err)
67-
}
6814

6915
env := sys.NewFakeEnv()
7016
env.Envs = map[string]string{
71-
"BUILDER_STORAGE": "minio",
72-
"DRYCC_MINIO_ENDPOINT": "http://localhost:8088",
73-
}
74-
params, err = GetStorageParams(env)
17+
"BUILDER_STORAGE": "minio",
18+
"DRYCC_MINIO_LOOKUP": "path",
19+
"DRYCC_MINIO_BUCKET": "builder",
20+
"DRYCC_MINIO_ENDPOINT": "http://localhost:8088",
21+
"DRYCC_MINIO_ACCESSKEY": "admin",
22+
"DRYCC_MINIO_SECRETKEY": "adminpass",
23+
}
24+
params, err := GetStorageParams(env)
7525
if err != nil {
7626
t.Errorf("received error while retrieving storage params: %v", err)
7727
}
28+
assert.Equal(t, params["forcepathstyle"], "true", "forcepathstyle")
7829
assert.Equal(t, params["regionendpoint"], "http://localhost:8088", "region endpoint")
79-
assert.Equal(t, params["secure"], false, "secure")
80-
assert.Equal(t, params["region"], "us-east-1", "region")
30+
assert.Equal(t, params["region"], "localhost", "region")
8131
assert.Equal(t, params["bucket"], "builder", "bucket")
32+
assert.Equal(t, params["accesskey"], "admin", "accesskey")
33+
assert.Equal(t, params["secretkey"], "adminpass", "secretkey")
8234
}
8335

8436
func TestGetControllerClient(t *testing.T) {

pkg/gitreceive/build.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,12 @@ func build(
149149

150150
imageName := fmt.Sprintf("%s:git-%s", appName, gitSha.Short())
151151
buildJobName := imagebuilderJobName(appName, gitSha.Short())
152-
registryLocation := conf.RegistryLocation
153-
builderImageEnv := make(map[string]string)
154-
if registryLocation != "on-cluster" {
155-
builderImageEnv, err = getRegistryDetails(kubeClient.CoreV1(), &imageName, registryLocation, conf.PodNamespace)
156-
if err != nil {
157-
return fmt.Errorf("error getting private registry details %s", err)
158-
}
152+
153+
builderImageEnv, err := getImagebuilderEnv(&imageName, conf, env)
154+
if err != nil {
155+
return fmt.Errorf("error getting private registry details %s", err)
159156
}
160157
builderImageEnv["DRYCC_STACK"] = stack["name"]
161-
builderImageEnv["DRYCC_REGISTRY_LOCATION"] = registryLocation
162158

163159
job := createBuilderJob(
164160
conf.Debug,
@@ -171,8 +167,6 @@ func build(
171167
conf.StorageType,
172168
builderName,
173169
stack["image"],
174-
conf.RegistryHost,
175-
conf.RegistryPort,
176170
builderImageEnv,
177171
imagePullPolicy,
178172
securityContext,
@@ -290,7 +284,7 @@ func buildBuilderPodNodeSelector(config string) (map[string]string, error) {
290284
for _, line := range strings.Split(config, ",") {
291285
param := strings.Split(line, ":")
292286
if len(param) != 2 {
293-
return nil, fmt.Errorf("Invalid BuilderPodNodeSelector value format: %s", config)
287+
return nil, fmt.Errorf("invalid BuilderPodNodeSelector value format: %s", config)
294288
}
295289
selector[strings.TrimSpace(param[0])] = strings.TrimSpace(param[1])
296290
}

pkg/gitreceive/config.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ const (
1414
// builder's git-receive hook.
1515
type Config struct {
1616
// k8s service discovery env vars
17-
ControllerHost string `envconfig:"DRYCC_CONTROLLER_SERVICE_HOST" required:"true"`
18-
ControllerPort string `envconfig:"DRYCC_CONTROLLER_SERVICE_PORT" required:"true"`
19-
RegistryHost string `envconfig:"DRYCC_REGISTRY_PROXY_HOST" required:"true"`
20-
RegistryPort string `envconfig:"DRYCC_REGISTRY_PROXY_PORT" required:"true"`
21-
RegistryLocation string `envconfig:"DRYCC_REGISTRY_LOCATION" default:"on-cluster"`
17+
ControllerHost string `envconfig:"DRYCC_CONTROLLER_SERVICE_HOST" required:"true"`
18+
ControllerPort string `envconfig:"DRYCC_CONTROLLER_SERVICE_PORT" required:"true"`
2219

2320
GitHome string `envconfig:"GIT_HOME" required:"true"`
2421
SSHConnection string `envconfig:"SSH_CONNECTION" required:"true"`

pkg/gitreceive/imagebuilder.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package gitreceive
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/drycc/builder/pkg/sys"
8+
)
9+
10+
var (
11+
requiredEnvNames = []string{
12+
"DRYCC_MINIO_LOOKUP",
13+
"DRYCC_MINIO_BUCKET",
14+
"DRYCC_MINIO_ENDPOINT",
15+
"DRYCC_REGISTRY_HOST",
16+
}
17+
)
18+
19+
func checkImagebuilderRequiredEnv(imagebuilderEnv map[string]string) error {
20+
for index := range requiredEnvNames {
21+
envName := requiredEnvNames[index]
22+
if _, hasKey := imagebuilderEnv[envName]; !hasKey {
23+
msg := fmt.Sprintf("the environment variable %s is required", envName)
24+
return errors.New(msg)
25+
}
26+
}
27+
if imagebuilderEnv["DRYCC_REGISTRY_LOCATION"] == "off-cluster" {
28+
if imagebuilderEnv["DRYCC_REGISTRY_ORGANIZATION"] == "" {
29+
return errors.New("the environment variable DRYCC_REGISTRY_ORGANIZATION is required")
30+
}
31+
} else {
32+
if imagebuilderEnv["DRYCC_REGISTRY_PROXY_HOST"] == "" {
33+
return errors.New("the environment variable DRYCC_REGISTRY_PROXY_HOST is required")
34+
}
35+
}
36+
return nil
37+
}
38+
39+
func getImagebuilderEnv(image *string, config *Config, env sys.Env) (map[string]string, error) {
40+
imagebuilderEnv := env.Environ([]string{"DRYCC_REGISTRY_", "DRYCC_MINIO_"})
41+
if err := checkImagebuilderRequiredEnv(imagebuilderEnv); err != nil {
42+
return nil, err
43+
}
44+
if imagebuilderEnv["DRYCC_REGISTRY_LOCATION"] == "off-cluster" {
45+
*image = fmt.Sprintf(
46+
"%s/%s/%s",
47+
imagebuilderEnv["DRYCC_REGISTRY_HOST"],
48+
imagebuilderEnv["DRYCC_REGISTRY_ORGANIZATION"],
49+
*image,
50+
)
51+
} else {
52+
imagebuilderEnv["DRYCC_REGISTRY_ORGANIZATION"] = config.App()
53+
*image = fmt.Sprintf(
54+
"%s/%s/%s",
55+
imagebuilderEnv["DRYCC_REGISTRY_PROXY_HOST"],
56+
imagebuilderEnv["DRYCC_REGISTRY_ORGANIZATION"],
57+
*image,
58+
)
59+
}
60+
return imagebuilderEnv, nil
61+
}

0 commit comments

Comments
 (0)