Skip to content

Commit f51d3a2

Browse files
author
Sivaram Mothiki
committed
Merge pull request #196 from smothiki/bucket
feat(storage): configure bucket name
2 parents aefc6e4 + e2a1c11 commit f51d3a2

6 files changed

Lines changed: 30 additions & 26 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
6363
return fmt.Errorf("unable to create tmpdir %s (%s)", buildDir, err)
6464
}
6565

66-
slugBuilderInfo := storage.NewSlugBuilderInfo(s3Client.Endpoint, appName, slugName, gitSha)
66+
slugBuilderInfo := storage.NewSlugBuilderInfo(s3Client.Endpoint, conf.Bucket, appName, slugName, gitSha)
6767

6868
// Get the application config from the controller, so we can check for a custom buildpack URL
6969
appConf, err := getAppConfig(conf, builderKey, conf.Username, appName)
@@ -111,8 +111,7 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
111111
}
112112
}
113113

114-
bucketName := "git"
115-
if err := storage.CreateBucket(s3Client, bucketName); err != nil {
114+
if err := storage.CreateBucket(s3Client, conf.Bucket); err != nil {
116115
log.Warn("create bucket error: %+v", err)
117116
}
118117

@@ -121,9 +120,9 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
121120
return fmt.Errorf("opening %s for read (%s)", appTgz, err)
122121
}
123122

124-
log.Debug("Uploading tar to %s/%s/%s", s3Client.Endpoint, bucketName, slugBuilderInfo.TarKey())
125-
if err := storage.UploadObject(s3Client, bucketName, slugBuilderInfo.TarKey(), appTgzReader); err != nil {
126-
return fmt.Errorf("uploading %s to %s/%s (%v)", absAppTgz, bucketName, slugBuilderInfo.TarKey(), err)
123+
log.Debug("Uploading tar to %s/%s/%s", s3Client.Endpoint, conf.Bucket, slugBuilderInfo.TarKey())
124+
if err := storage.UploadObject(s3Client, conf.Bucket, slugBuilderInfo.TarKey(), appTgzReader); err != nil {
125+
return fmt.Errorf("uploading %s to %s/%s (%v)", absAppTgz, conf.Bucket, slugBuilderInfo.TarKey(), err)
127126
}
128127

129128
creds := storage.CredsOK()
@@ -140,6 +139,7 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
140139
appConf.Values,
141140
slugBuilderInfo.TarURL(),
142141
slugName,
142+
conf.StorageRegion,
143143
)
144144
} else {
145145
buildPodName = slugBuilderPodName(appName, gitSha.Short())
@@ -211,9 +211,9 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
211211

212212
// poll the s3 server to ensure the slug exists
213213
err = wait.PollImmediate(conf.ObjectStorageTickDuration(), conf.ObjectStorageWaitDuration(), func() (bool, error) {
214-
exists, err := storage.ObjectExists(s3Client, bucketName, slugBuilderInfo.PushKey())
214+
exists, err := storage.ObjectExists(s3Client, conf.Bucket, slugBuilderInfo.PushKey())
215215
if err != nil {
216-
return false, fmt.Errorf("Checking if object %s/%s exists (%s)", bucketName, slugBuilderInfo.PushKey(), err)
216+
return false, fmt.Errorf("Checking if object %s/%s exists (%s)", conf.Bucket, slugBuilderInfo.PushKey(), err)
217217
}
218218
return exists, nil
219219
})

pkg/gitreceive/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Config struct {
3030
BuilderPodWaitDurationMSec int `envconfig:"BUILDER_POD_WAIT_DURATION" default:"300000"` // 5 minutes
3131
ObjectStorageTickDurationMSec int `envconfing:"OBJECT_STORAGE_TICK_DURATION" default:"500"`
3232
ObjectStorageWaitDurationMSec int `envconfig:"OBJECT_STORAGE_WAIT_DURATION" default:"300000"` // 5 minutes
33+
Bucket string `envconfig:"BUCKET" default:"git"`
3334
}
3435

3536
func (c Config) App() string {

pkg/gitreceive/k8s_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func slugBuilderPodName(appName, shortSha string) string {
3535
return fmt.Sprintf("slugbuild-%s-%s-%s", appName, shortSha, uid)
3636
}
3737

38-
func dockerBuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, imageName string) *api.Pod {
38+
func dockerBuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, imageName, region string) *api.Pod {
3939
pod := buildPod(debug, withAuth, name, namespace, env)
4040

4141
pod.Spec.Containers[0].Name = dockerBuilderName
@@ -46,6 +46,7 @@ func dockerBuilderPod(debug, withAuth bool, name, namespace string, env map[stri
4646

4747
addEnvToPod(pod, tarURLKey, tarURL)
4848
addEnvToPod(pod, "IMG_NAME", imageName)
49+
addEnvToPod(pod, "REGION", region)
4950

5051
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, api.VolumeMount{
5152
Name: dockerSocketName,

pkg/gitreceive/k8s_util_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type dockerBuildCase struct {
4141
env map[string]interface{}
4242
tarURL string
4343
imgName string
44+
region string
4445
}
4546

4647
func TestBuildPod(t *testing.T) {
@@ -82,18 +83,18 @@ func TestBuildPod(t *testing.T) {
8283
}
8384

8485
dockerBuilds := []dockerBuildCase{
85-
{true, true, "test", "default", emptyEnv, "tar", ""},
86-
{true, false, "test", "default", emptyEnv, "tar", ""},
87-
{true, true, "test", "default", env, "tar", ""},
88-
{true, false, "test", "default", env, "tar", ""},
89-
{true, true, "test", "default", emptyEnv, "tar", "img"},
90-
{true, false, "test", "default", emptyEnv, "tar", "img"},
91-
{true, true, "test", "default", env, "tar", "img"},
92-
{true, false, "test", "default", env, "tar", "img"},
86+
{true, true, "test", "default", emptyEnv, "tar", "", "us-east-1"},
87+
{true, false, "test", "default", emptyEnv, "tar", "", "us-east-1"},
88+
{true, true, "test", "default", env, "tar", "", "us-east-1"},
89+
{true, false, "test", "default", env, "tar", "", "us-east-1"},
90+
{true, true, "test", "default", emptyEnv, "tar", "img", "us-east-1"},
91+
{true, false, "test", "default", emptyEnv, "tar", "img", "us-east-1"},
92+
{true, true, "test", "default", env, "tar", "img", "us-east-1"},
93+
{true, false, "test", "default", env, "tar", "img", "us-east-1"},
9394
}
9495

9596
for _, build := range dockerBuilds {
96-
pod = dockerBuilderPod(build.debug, build.withAuth, build.name, build.namespace, build.env, build.tarURL, build.imgName)
97+
pod = dockerBuilderPod(build.debug, build.withAuth, build.name, build.namespace, build.env, build.tarURL, build.imgName, build.region)
9798

9899
if pod.ObjectMeta.Name != build.name {
99100
t.Errorf("expected %v but returned %v ", build.name, pod.ObjectMeta.Name)

pkg/gitreceive/storage/slug_builder_info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ type SlugBuilderInfo struct {
1515
}
1616

1717
// NewSlugBuilderInfo creates and populates a new SlugBuilderInfo based on the given data
18-
func NewSlugBuilderInfo(s3Endpoint, appName, slugName string, gitSha *git.SHA) *SlugBuilderInfo {
18+
func NewSlugBuilderInfo(s3Endpoint, bucket, appName, slugName string, gitSha *git.SHA) *SlugBuilderInfo {
1919
tarKey := fmt.Sprintf("home/%s/tar", slugName)
2020
// this is where workflow tells slugrunner to download the slug from, so we have to tell slugbuilder to upload it to here
2121
pushKey := fmt.Sprintf("home/%s:git-%s/push", appName, gitSha.Short())
2222

2323
return &SlugBuilderInfo{
2424
pushKey: pushKey,
25-
pushURL: fmt.Sprintf("%s/git/%s", s3Endpoint, pushKey),
25+
pushURL: fmt.Sprintf("%s/%s/%s", s3Endpoint, bucket, pushKey),
2626
tarKey: tarKey,
27-
tarURL: fmt.Sprintf("%s/git/%s", s3Endpoint, tarKey),
27+
tarURL: fmt.Sprintf("%s/%s/%s", s3Endpoint, bucket, tarKey),
2828
}
2929
}
3030

pkg/gitreceive/storage/slug_builder_info_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ const (
1111
s3Endpoint = "http://10.1.2.3:9090"
1212
appName = "myapp"
1313
slugName = "myslug"
14+
bucket = "git"
1415
)
1516

1617
func TestS3Endpoint(t *testing.T) {
1718
sha, err := git.NewSha(rawSha)
1819
if err != nil {
1920
t.Fatalf("error building git sha (%s)", err)
2021
}
21-
sbi := NewSlugBuilderInfo(s3Endpoint, appName, slugName, sha)
22+
sbi := NewSlugBuilderInfo(s3Endpoint, bucket, appName, slugName, sha)
2223

23-
expectedPushURL := s3Endpoint + "/git/" + sbi.PushKey()
24+
expectedPushURL := s3Endpoint + "/" + bucket + "/" + sbi.PushKey()
2425
if sbi.PushURL() != expectedPushURL {
2526
t.Errorf("push URL %s didn't match expected %s", sbi.PushURL(), expectedPushURL)
2627
}
27-
expectedTarURL := s3Endpoint + "/git/" + sbi.TarKey()
28+
expectedTarURL := s3Endpoint + "/" + bucket + "/" + sbi.TarKey()
2829
if sbi.TarURL() != expectedTarURL {
2930
t.Errorf("tar URL %s didn't match expected %s", sbi.TarURL(), expectedTarURL)
3031
}
@@ -35,7 +36,7 @@ func TestPushKey(t *testing.T) {
3536
if err != nil {
3637
t.Fatalf("error building git sha (%s)", err)
3738
}
38-
sbi := NewSlugBuilderInfo(s3Endpoint, appName, slugName, sha)
39+
sbi := NewSlugBuilderInfo(s3Endpoint, bucket, appName, slugName, sha)
3940
expectedPushKey := "home/" + appName + ":git-" + sha.Short() + "/push"
4041
if sbi.PushKey() != expectedPushKey {
4142
t.Errorf("push key %s didn't match expected %s", sbi.PushKey(), expectedPushKey)
@@ -47,7 +48,7 @@ func TestTarKey(t *testing.T) {
4748
if err != nil {
4849
t.Fatalf("error building git sha (%s)", err)
4950
}
50-
sbi := NewSlugBuilderInfo(s3Endpoint, appName, slugName, sha)
51+
sbi := NewSlugBuilderInfo(s3Endpoint, bucket, appName, slugName, sha)
5152
expectedTarKey := "home/" + slugName + "/tar"
5253
if sbi.TarKey() != expectedTarKey {
5354
t.Errorf("tar key %s didn't match expected %s", sbi.TarKey(), expectedTarKey)

0 commit comments

Comments
 (0)