Skip to content

Commit 6de7fae

Browse files
Merge pull request #872 from bharath-b-rh/cfe-682
CFE-682 : Add user defined labels to the GCP buckets created
2 parents 5305777 + 32fa70d commit 6de7fae

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pkg/storage/gcs/gcp_labels_tags.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package gcs
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/klog/v2"
7+
8+
configlisters "github.com/openshift/client-go/config/listers/config/v1"
9+
"github.com/openshift/cluster-image-registry-operator/pkg/storage/util"
10+
)
11+
12+
const (
13+
// ocpDefaultLabelFmt is the format string for the default label
14+
// added to the OpenShift created GCP resources.
15+
ocpDefaultLabelFmt = "kubernetes-io-cluster-%s"
16+
)
17+
18+
func getUserLabels(infraLister configlisters.InfrastructureLister) (map[string]string, error) {
19+
infra, err := util.GetInfrastructure(infraLister)
20+
if err != nil {
21+
klog.Errorf("getUserLabels: failed to read infrastructure/cluster resource: %w", err)
22+
return nil, err
23+
}
24+
// add OCP default label along with user-defined labels
25+
labels := map[string]string{
26+
fmt.Sprintf(ocpDefaultLabelFmt, infra.Status.InfrastructureName): "owned",
27+
}
28+
// get user-defined labels in Infrastructure.Status.GCP
29+
if infra.Status.PlatformStatus != nil &&
30+
infra.Status.PlatformStatus.GCP != nil &&
31+
infra.Status.PlatformStatus.GCP.ResourceLabels != nil {
32+
for _, label := range infra.Status.PlatformStatus.GCP.ResourceLabels {
33+
labels[label.Key] = label.Value
34+
}
35+
}
36+
return labels, nil
37+
}

pkg/storage/gcs/gcs.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,14 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
255255
bucketAttrs := gstorage.BucketAttrs{Location: d.Config.Region}
256256
bucket = gclient.Bucket(d.Config.Bucket)
257257

258-
err := bucket.Create(d.Context, d.Config.ProjectID, &bucketAttrs)
258+
labels, err := getUserLabels(d.Listers.Infrastructures)
259259
if err != nil {
260+
return err
261+
}
262+
klog.V(1).Infof("createStorage: %v list of labels will be applied to %s bucket", labels, d.Config.Bucket)
263+
bucketAttrs.Labels = labels
264+
265+
if err := bucket.Create(d.Context, d.Config.ProjectID, &bucketAttrs); err != nil {
260266
if gerr, ok := err.(*gapi.Error); ok {
261267
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionFalse, strconv.Itoa(gerr.Code), gerr.Error())
262268
return err
@@ -275,6 +281,10 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
275281
cr.Spec.Storage.GCS = d.Config.DeepCopy()
276282

277283
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionTrue, "Creation Successful", "GCS bucket was successfully created")
284+
if len(bucketAttrs.Labels) > 0 {
285+
util.UpdateCondition(cr, defaults.StorageLabeled, operatorapi.ConditionTrue, "Bucket Labeled Successfully",
286+
fmt.Sprintf("Successfully added user-defined labels to %s storage bucket", d.Config.Bucket))
287+
}
278288
}
279289

280290
// TODO: Wait until the bucket exists

0 commit comments

Comments
 (0)