Skip to content

Commit 3cd318b

Browse files
nader-ziadaknative-prow-robot
authored andcommitted
refactor and enhancements to artifact bucket code
- addressed comments from pr tektoncd#444 review
1 parent fd9ac1a commit 3cd318b

File tree

7 files changed

+54
-79
lines changed

7 files changed

+54
-79
lines changed

Diff for: docs/using.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ following alternatives:
196196
that tasks cannot have any volume mounted under path `/pvc`.
197197

198198
- [GCS storage bucket](https://cloud.google.com/storage/docs/json_api/v1/buckets)
199-
A storage bucket can be configured using a ConfigMap with the name `config-artifact-bucket` with the following attributes:
200-
- location: the address of the bucket (for example gs://mybucket)
201-
- bucket.service.account.secret.name: the name of the secret that will contain the credentials for the service account
199+
A storage bucket can be configured using a ConfigMap named [`config-artifact-bucket`](./../config/config-artifact-bucket.yaml).
200+
with the following attributes:
201+
- `location`: the address of the bucket (for example gs://mybucket)
202+
- `bucket.service.account.secret.name`: the name of the secret that will contain the credentials for the service account
202203
with access to the bucket
203-
- bucket.service.account.secret.key: the key in the secret with the required service account json
204+
- `bucket.service.account.secret.key`: the key in the secret with the required service account json
204205
The bucket is configured with a retention policy of 24 hours after which files will be deleted
205206

206207
### Outputs

Diff for: pkg/reconciler/v1alpha1/pipelinerun/config/store.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Store struct {
4545
}
4646

4747
func NewStore(logger configmap.Logger) *Store {
48-
store := &Store{
48+
return &Store{
4949
UntypedStore: configmap.NewUntypedStore(
5050
"pipelinerun",
5151
logger,
@@ -54,8 +54,6 @@ func NewStore(logger configmap.Logger) *Store {
5454
},
5555
),
5656
}
57-
58-
return store
5957
}
6058

6159
func (s *Store) ToContext(ctx context.Context) context.Context {

Diff for: pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go

+26-62
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,6 @@ func Test_StorageInputResource(t *testing.T) {
11261126
}
11271127

11281128
func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) {
1129-
boolTrue := true
11301129
task := &v1alpha1.Task{
11311130
ObjectMeta: metav1.ObjectMeta{
11321131
Name: "build-from-repo",
@@ -1162,8 +1161,7 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) {
11621161
task *v1alpha1.Task
11631162
taskRun *v1alpha1.TaskRun
11641163
build *buildv1alpha1.Build
1165-
wantErr bool
1166-
want *buildv1alpha1.Build
1164+
want buildv1alpha1.BuildSpec
11671165
}{{
11681166
desc: "git resource as input from previous task - copy to bucket",
11691167
task: task,
@@ -1188,35 +1186,18 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) {
11881186
},
11891187
},
11901188
},
1191-
build: build(),
1192-
wantErr: false,
1193-
want: &buildv1alpha1.Build{
1194-
TypeMeta: metav1.TypeMeta{
1195-
Kind: "Build",
1196-
APIVersion: "build.knative.dev/v1alpha1"},
1197-
ObjectMeta: metav1.ObjectMeta{
1198-
Name: "build-from-repo",
1199-
Namespace: "marshmallow",
1200-
OwnerReferences: []metav1.OwnerReference{{
1201-
APIVersion: "pipeline.knative.dev/v1alpha1",
1202-
Kind: "TaskRun",
1203-
Name: "build-from-repo-run",
1204-
Controller: &boolTrue,
1205-
BlockOwnerDeletion: &boolTrue,
1206-
}},
1207-
},
1208-
Spec: buildv1alpha1.BuildSpec{
1209-
Steps: []corev1.Container{{
1210-
Name: "artifact-dest-mkdir-gitspace-0",
1211-
Image: "override-with-bash-noop:latest",
1212-
Args: []string{"-args", "mkdir -p /workspace/gitspace"},
1213-
},
1214-
{
1215-
Name: "artifact-copy-from-gitspace-0",
1216-
Image: "override-with-gsutil-image:latest",
1217-
Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gitspace"},
1218-
}},
1189+
build: build(),
1190+
want: buildv1alpha1.BuildSpec{
1191+
Steps: []corev1.Container{{
1192+
Name: "artifact-dest-mkdir-gitspace-0",
1193+
Image: "override-with-bash-noop:latest",
1194+
Args: []string{"-args", "mkdir -p /workspace/gitspace"},
12191195
},
1196+
{
1197+
Name: "artifact-copy-from-gitspace-0",
1198+
Image: "override-with-gsutil-image:latest",
1199+
Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gitspace"},
1200+
}},
12201201
},
12211202
}, {
12221203
desc: "storage resource as input from previous task - copy from bucket",
@@ -1242,35 +1223,18 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) {
12421223
},
12431224
},
12441225
},
1245-
build: build(),
1246-
wantErr: false,
1247-
want: &buildv1alpha1.Build{
1248-
TypeMeta: metav1.TypeMeta{
1249-
Kind: "Build",
1250-
APIVersion: "build.knative.dev/v1alpha1"},
1251-
ObjectMeta: metav1.ObjectMeta{
1252-
Name: "build-from-repo",
1253-
Namespace: "marshmallow",
1254-
OwnerReferences: []metav1.OwnerReference{{
1255-
APIVersion: "pipeline.knative.dev/v1alpha1",
1256-
Kind: "TaskRun",
1257-
Name: "build-from-repo-run",
1258-
Controller: &boolTrue,
1259-
BlockOwnerDeletion: &boolTrue,
1260-
}},
1261-
},
1262-
Spec: buildv1alpha1.BuildSpec{
1263-
Steps: []corev1.Container{{
1264-
Name: "artifact-dest-mkdir-workspace-0",
1265-
Image: "override-with-bash-noop:latest",
1266-
Args: []string{"-args", "mkdir -p /workspace/gcs-dir"},
1267-
},
1268-
{
1269-
Name: "artifact-copy-from-workspace-0",
1270-
Image: "override-with-gsutil-image:latest",
1271-
Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gcs-dir"},
1272-
}},
1226+
build: build(),
1227+
want: buildv1alpha1.BuildSpec{
1228+
Steps: []corev1.Container{{
1229+
Name: "artifact-dest-mkdir-workspace-0",
1230+
Image: "override-with-bash-noop:latest",
1231+
Args: []string{"-args", "mkdir -p /workspace/gcs-dir"},
12731232
},
1233+
{
1234+
Name: "artifact-copy-from-workspace-0",
1235+
Image: "override-with-gsutil-image:latest",
1236+
Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gcs-dir"},
1237+
}},
12741238
},
12751239
}} {
12761240
t.Run(c.desc, func(t *testing.T) {
@@ -1287,10 +1251,10 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) {
12871251
},
12881252
)
12891253
got, err := AddInputResource(fakekubeclient, c.build, c.task.Name, &c.task.Spec, c.taskRun, pipelineResourceLister, logger)
1290-
if (err != nil) != c.wantErr {
1291-
t.Errorf("Test: %q; AddInputResource() error = %v, WantErr %v", c.desc, err, c.wantErr)
1254+
if err != nil {
1255+
t.Errorf("Test: %q; AddInputResource() error = %v", c.desc, err)
12921256
}
1293-
if d := cmp.Diff(got, c.want); d != "" {
1257+
if d := cmp.Diff(got.Spec, c.want); d != "" {
12941258
t.Errorf("Diff:\n%s", d)
12951259
}
12961260
})

Diff for: test/README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,17 @@ pipelineRunsInformer.Informer().GetIndexer().Add(obj)
107107
Besides the environment variable `KO_DOCKER_REPO`, you may also need the
108108
permissions inside the TaskRun to run the Kaniko e2e test and GCS taskrun test.
109109
110-
- In Kaniko e2e test, setting `KANIKO_SECRET_CONFIG_FILE` as the path of the GCP
110+
- In Kaniko e2e test, setting `GCP_SERVICE_ACCOUNT_KEY_PATH` as the path of the GCP
111111
service account JSON key which has permissions to push to the registry
112112
specified in `KO_DOCKER_REPO` will enable Kaniko to use those credentials when
113113
pushing an image.
114114
- In GCS taskrun test, GCP service account JSON key file at path
115-
`KANIKO_SECRET_CONFIG_FILE` is used to generate Kubernetes secret to access
115+
`GCP_SERVICE_ACCOUNT_KEY_PATH` is used to generate Kubernetes secret to access
116116
GCS bucket. This e2e test requires valid service account configuration json
117117
but it does not require any role binding.
118-
- In Storage artifact bucket, GCP service account JSON key file at path
119-
`KANIKO_SECRET_CONFIG_FILE` is used to create/delete a bucket.
118+
- In Storage artifact bucket, setting the `GCP_SERVICE_ACCOUNT_KEY_PATH` as the
119+
path of the GCP service account JSON key which has permissions to create/delete
120+
a bucket.
120121
121122
To reduce e2e test setup developers can use the same environment variable for
122123
both Kaniko e2e test and GCS taskrun test. To create a service account usable in
@@ -138,7 +139,7 @@ gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$EMAI
138139
# create the JSON key
139140
gcloud iam service-accounts keys create config.json --iam-account $EMAIL
140141
141-
export KANIKO_SECRET_CONFIG_FILE="$PWD/config.json"
142+
export GCP_SERVICE_ACCOUNT_KEY_PATH="$PWD/config.json"
142143
```
143144
144145
### Running

Diff for: test/artifact_bucket_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ const (
4444
// TestStorageBucketPipelineRun is an integration test that will verify a pipeline
4545
// can use a bucket for temporary storage of artifacts shared between tasks
4646
func TestStorageBucketPipelineRun(t *testing.T) {
47-
configFilePath := os.Getenv("KANIKO_SECRET_CONFIG_FILE")
47+
configFilePath := os.Getenv("GCP_SERVICE_ACCOUNT_KEY_PATH")
4848
if configFilePath == "" {
49-
t.Skip("KANIKO_SECRET_CONFIG_FILE variable is not set.")
49+
t.Skip("GCP_SERVICE_ACCOUNT_KEY_PATH variable is not set.")
5050
}
5151
logger := logging.GetContextLogger(t.Name())
5252
c, namespace := setup(t, logger)
@@ -100,13 +100,20 @@ func TestStorageBucketPipelineRun(t *testing.T) {
100100

101101
defer runTaskToDeleteBucket(c, t, logger, namespace, bucketName, bucketSecretName, bucketSecretKey)
102102

103+
originalConfigMap, err := c.KubeClient.Kube.CoreV1().ConfigMaps(systemNamespace).Get(v1alpha1.BucketConfigName, metav1.GetOptions{})
104+
if err != nil {
105+
t.Fatalf("Failed to get ConfigMap `%s`: %s", v1alpha1.BucketConfigName, err)
106+
}
107+
originalConfigMapData := originalConfigMap.Data
108+
103109
logger.Infof("Creating ConfigMap %s", v1alpha1.BucketConfigName)
104110
configMapData := map[string]string{
105111
v1alpha1.BucketLocationKey: fmt.Sprintf("gs://%s", bucketName),
106112
v1alpha1.BucketServiceAccountSecretName: bucketSecretName,
107113
v1alpha1.BucketServiceAccountSecretKey: bucketSecretKey,
108114
}
109115
c.KubeClient.UpdateConfigMap(systemNamespace, v1alpha1.BucketConfigName, configMapData)
116+
defer resetConfigMap(c, systemNamespace, v1alpha1.BucketConfigName, originalConfigMapData)
110117

111118
logger.Infof("Creating Git PipelineResource %s", helloworldResourceName)
112119
helloworldResource := tb.PipelineResource(helloworldResourceName, namespace, tb.PipelineResourceSpec(
@@ -205,6 +212,10 @@ func deleteBucketSecret(c *clients, t *testing.T, logger *logging.BaseLogger, na
205212
}
206213
}
207214

215+
func resetConfigMap(c *clients, namespace, configName string, values map[string]string) error {
216+
return c.KubeClient.UpdateConfigMap(namespace, configName, values)
217+
}
218+
208219
func runTaskToDeleteBucket(c *clients, t *testing.T, logger *logging.BaseLogger, namespace, bucketName, bucketSecretName, bucketSecretKey string) {
209220
deletelbuckettask := tb.Task("deletelbuckettask", namespace, tb.TaskSpec(
210221
tb.TaskVolume("bucket-secret-volume", tb.VolumeSource(corev1.VolumeSource{

Diff for: test/gcs_taskrun_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import (
3535
// - places files in expected place
3636

3737
func TestStorageTaskRun(t *testing.T) {
38-
configFile := os.Getenv("KANIKO_SECRET_CONFIG_FILE")
38+
configFile := os.Getenv("GCP_SERVICE_ACCOUNT_KEY_PATH")
3939
if configFile == "" {
40-
t.Skip("KANIKO_SECRET_CONFIG_FILE variable is not set.")
40+
t.Skip("GCP_SERVICE_ACCOUNT_KEY_PATH variable is not set.")
4141
}
4242
logger := logging.GetContextLogger(t.Name())
4343
t.Parallel()

Diff for: test/kaniko_task_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func getDockerRepo() (string, error) {
6565

6666
func createSecret(c *knativetest.KubeClient, namespace string) (bool, error) {
6767
// when running e2e in cluster, this will not be set so just hop out early
68-
file := os.Getenv("KANIKO_SECRET_CONFIG_FILE")
68+
file := os.Getenv("GCP_SERVICE_ACCOUNT_KEY_PATH")
6969
if file == "" {
7070
return false, nil
7171
}

0 commit comments

Comments
 (0)