Skip to content

Commit d4a2788

Browse files
committed
Support to add ResoureManagerTags to GCP Compute Disk, Image, Snapshot resources
Signed-off-by: arkadeepsen <[email protected]>
1 parent 93d67cb commit d4a2788

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+32634
-30
lines changed

cmd/gce-pd-csi-driver/main.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ var (
6666
maxConcurrentFormatAndMount = flag.Int("max-concurrent-format-and-mount", 1, "If set then format and mount operations are serialized on each node. This is stronger than max-concurrent-format as it includes fsck and other mount operations")
6767
formatAndMountTimeout = flag.Duration("format-and-mount-timeout", 1*time.Minute, "The maximum duration of a format and mount operation before another such operation will be started. Used only if --serialize-format-and-mount")
6868

69+
extraTagsStr = flag.String("extra-tags", "", "Extra tags to attach to each Compute Disk, Image, Snapshot created. It is a comma separated list of parent id, key and value like '<parent_id1>/<tag_key1>/<tag_value1>,...,<parent_idN>/<tag_keyN>/<tag_valueN>'. parent_id is the Organization or the Project ID where the tag key and the tag value resources exist. A maximum of 50 tags bindings is allowed for a resource. See https://cloud.google.com/resource-manager/docs/tags/tags-overview, https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing for details")
70+
6971
version string
7072
)
7173

@@ -119,6 +121,14 @@ func handle() {
119121
klog.Fatalf("Bad extra volume labels: %v", err.Error())
120122
}
121123

124+
if len(*extraTagsStr) > 0 && !*runControllerService {
125+
klog.Fatalf("Extra tags provided but not running controller")
126+
}
127+
extraTags, err := common.ConvertTagsStringToMap(*extraTagsStr)
128+
if err != nil {
129+
klog.Fatalf("Bad extra tags: %v", err.Error())
130+
}
131+
122132
ctx, cancel := context.WithCancel(context.Background())
123133
defer cancel()
124134

@@ -161,7 +171,7 @@ func handle() {
161171
}
162172
}
163173

164-
err = gceDriver.SetupGCEDriver(driverName, version, extraVolumeLabels, identityServer, controllerServer, nodeServer)
174+
err = gceDriver.SetupGCEDriver(driverName, version, extraVolumeLabels, extraTags, identityServer, controllerServer, nodeServer)
165175
if err != nil {
166176
klog.Fatalf("Failed to initialize GCE CSI Driver: %v", err.Error())
167177
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: storage.k8s.io/v1
2+
kind: StorageClass
3+
metadata:
4+
name: csi-gce-pd-with-resource-tags
5+
provisioner: pd.csi.storage.gke.io
6+
parameters:
7+
resource-tags: <parent_id1>/<tag_key1>/<tag_value1>,<parent_id2>/<tag_key2>/<tag_value2>
8+
volumeBindingMode: WaitForFirstConsumer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: snapshot.storage.k8s.io/v1beta1
2+
kind: VolumeSnapshotClass
3+
metadata:
4+
name: csi-gce-pd-snapshot-class-with-resource-tags
5+
parameters:
6+
resource-tags: <parent_id1>/<tag_key1>/<tag_value1>,<parent_id2>/<tag_key2>/<tag_value2>
7+
driver: pd.csi.storage.gke.io
8+
deletionPolicy: Delete

go.mod

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ go 1.20
55
require (
66
cloud.google.com/go/compute/metadata v0.2.3
77
cloud.google.com/go/kms v1.15.5
8+
cloud.google.com/go/resourcemanager v1.9.2
89
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.24.0
910
github.com/container-storage-interface/spec v1.6.0
1011
github.com/google/go-cmp v0.6.0
1112
github.com/google/uuid v1.4.0
13+
github.com/googleapis/gax-go/v2 v2.12.0
1214
github.com/kubernetes-csi/csi-proxy/client v1.1.3
1315
github.com/kubernetes-csi/csi-test/v4 v4.4.0
1416
github.com/onsi/ginkgo/v2 v2.13.0
1517
github.com/onsi/gomega v1.29.0
1618
golang.org/x/oauth2 v0.13.0
1719
golang.org/x/sys v0.14.0
20+
golang.org/x/time v0.4.0
1821
google.golang.org/api v0.150.0
1922
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b
2023
google.golang.org/grpc v1.59.0
@@ -32,8 +35,10 @@ require (
3235
)
3336

3437
require (
38+
cloud.google.com/go v0.110.8 // indirect
3539
cloud.google.com/go/compute v1.23.3 // indirect
3640
cloud.google.com/go/iam v1.1.5 // indirect
41+
cloud.google.com/go/longrunning v0.5.2 // indirect
3742
github.com/Microsoft/go-winio v0.6.1 // indirect
3843
github.com/PuerkitoBio/purell v1.1.1 // indirect
3944
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
@@ -57,7 +62,6 @@ require (
5762
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
5863
github.com/google/s2a-go v0.1.7 // indirect
5964
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
60-
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
6165
github.com/hashicorp/errwrap v1.0.0 // indirect
6266
github.com/hashicorp/go-multierror v1.1.0 // indirect
6367
github.com/imdario/mergo v0.3.12 // indirect
@@ -86,7 +90,6 @@ require (
8690
golang.org/x/sync v0.5.0 // indirect
8791
golang.org/x/term v0.13.0 // indirect
8892
golang.org/x/text v0.14.0 // indirect
89-
golang.org/x/time v0.4.0 // indirect
9093
golang.org/x/tools v0.14.0 // indirect
9194
google.golang.org/appengine v1.6.8 // indirect
9295
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ cloud.google.com/go v0.105.0/go.mod h1:PrLgOJNe5nfE9UMxKxgXj4mD3voiP+YQ6gdt6KMFO
4848
cloud.google.com/go v0.107.0/go.mod h1:wpc2eNrD7hXUTy8EKS10jkxpZBjASrORK7goS+3YX2I=
4949
cloud.google.com/go v0.110.0/go.mod h1:SJnCLqQ0FCFGSZMUNUf84MV3Aia54kn7pi8st7tMzaY=
5050
cloud.google.com/go v0.110.8 h1:tyNdfIxjzaWctIiLYOTalaLKZ17SI44SKFW26QbOhME=
51+
cloud.google.com/go v0.110.8/go.mod h1:Iz8AkXJf1qmxC3Oxoep8R1T36w8B92yU29PcBhHO5fk=
5152
cloud.google.com/go/accessapproval v1.4.0/go.mod h1:zybIuC3KpDOvotz59lFe5qxRZx6C75OtwbisN56xYB4=
5253
cloud.google.com/go/accessapproval v1.5.0/go.mod h1:HFy3tuiGvMdcd/u+Cu5b9NkO1pEICJ46IR82PoUdplw=
5354
cloud.google.com/go/accessapproval v1.6.0/go.mod h1:R0EiYnwV5fsRFiKZkPHr6mwyk2wxUJ30nL4j2pcFY2E=
@@ -361,6 +362,8 @@ cloud.google.com/go/logging v1.7.0/go.mod h1:3xjP2CjkM3ZkO73aj4ASA5wRPGGCRrPIAeN
361362
cloud.google.com/go/longrunning v0.1.1/go.mod h1:UUFxuDWkv22EuY93jjmDMFT5GPQKeFVJBIF6QlTqdsE=
362363
cloud.google.com/go/longrunning v0.3.0/go.mod h1:qth9Y41RRSUE69rDcOn6DdK3HfQfsUI0YSmW3iIlLJc=
363364
cloud.google.com/go/longrunning v0.4.1/go.mod h1:4iWDqhBZ70CvZ6BfETbvam3T8FMvLK+eFj0E6AaRQTo=
365+
cloud.google.com/go/longrunning v0.5.2 h1:u+oFqfEwwU7F9dIELigxbe0XVnBAo9wqMuQLA50CZ5k=
366+
cloud.google.com/go/longrunning v0.5.2/go.mod h1:nqo6DQbNV2pXhGDbDMoN2bWz68MjZUzqv2YttZiveCs=
364367
cloud.google.com/go/managedidentities v1.3.0/go.mod h1:UzlW3cBOiPrzucO5qWkNkh0w33KFtBJU281hacNvsdE=
365368
cloud.google.com/go/managedidentities v1.4.0/go.mod h1:NWSBYbEMgqmbZsLIyKvxrYbtqOsxY1ZrGM+9RgDqInM=
366369
cloud.google.com/go/managedidentities v1.5.0/go.mod h1:+dWcZ0JlUmpuxpIDfyP5pP5y0bLdRwOS4Lp7gMni/LA=
@@ -471,6 +474,8 @@ cloud.google.com/go/resourcemanager v1.4.0/go.mod h1:MwxuzkumyTX7/a3n37gmsT3py7L
471474
cloud.google.com/go/resourcemanager v1.5.0/go.mod h1:eQoXNAiAvCf5PXxWxXjhKQoTMaUSNrEfg+6qdf/wots=
472475
cloud.google.com/go/resourcemanager v1.6.0/go.mod h1:YcpXGRs8fDzcUl1Xw8uOVmI8JEadvhRIkoXXUNVYcVo=
473476
cloud.google.com/go/resourcemanager v1.7.0/go.mod h1:HlD3m6+bwhzj9XCouqmeiGuni95NTrExfhoSrkC/3EI=
477+
cloud.google.com/go/resourcemanager v1.9.2 h1:lC3PjJMHLPlZKqLfan6FkEb3X1F8oCRc1ylY7vRHvDQ=
478+
cloud.google.com/go/resourcemanager v1.9.2/go.mod h1:OujkBg1UZg5lX2yIyMo5Vz9O5hf7XQOSV7WxqxxMtQE=
474479
cloud.google.com/go/resourcesettings v1.3.0/go.mod h1:lzew8VfESA5DQ8gdlHwMrqZs1S9V87v3oCnKCWoOuQU=
475480
cloud.google.com/go/resourcesettings v1.4.0/go.mod h1:ldiH9IJpcrlC3VSuCGvjR5of/ezRrOxFtpJoJo5SmXg=
476481
cloud.google.com/go/resourcesettings v1.5.0/go.mod h1:+xJF7QSG6undsQDfsCJyqWXyBwUoJLhetkRMDRnIoXA=

pkg/common/parameters.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
ParameterKeyProvisionedThroughputOnCreate = "provisioned-throughput-on-create"
3232
ParameterAvailabilityClass = "availability-class"
3333
ParameterKeyEnableConfidentialCompute = "enable-confidential-storage"
34+
ParameterKeyResourceTags = "resource-tags"
3435

3536
// Parameters for VolumeSnapshotClass
3637
ParameterKeyStorageLocations = "storage-locations"
@@ -94,6 +95,9 @@ type DiskParameters struct {
9495
EnableConfidentialCompute bool
9596
// Default: false
9697
ForceAttach bool
98+
// Values: {map[string]string}
99+
// Default: ""
100+
ResourceTags map[string]string
97101
}
98102

99103
// SnapshotParameters contains normalized and defaulted parameters for snapshots
@@ -103,25 +107,31 @@ type SnapshotParameters struct {
103107
ImageFamily string
104108
Tags map[string]string
105109
Labels map[string]string
110+
ResourceTags map[string]string
106111
}
107112

108113
// ExtractAndDefaultParameters will take the relevant parameters from a map and
109114
// put them into a well defined struct making sure to default unspecified fields.
110115
// extraVolumeLabels are added as labels; if there are also labels specified in
111116
// parameters, any matching extraVolumeLabels will be overridden.
112-
func ExtractAndDefaultParameters(parameters map[string]string, driverName string, extraVolumeLabels map[string]string) (DiskParameters, error) {
117+
func ExtractAndDefaultParameters(parameters map[string]string, driverName string, extraVolumeLabels map[string]string, extraTags map[string]string) (DiskParameters, error) {
113118
p := DiskParameters{
114119
DiskType: "pd-standard", // Default
115120
ReplicationType: replicationTypeNone, // Default
116121
DiskEncryptionKMSKey: "", // Default
117122
Tags: make(map[string]string), // Default
118123
Labels: make(map[string]string), // Default
124+
ResourceTags: make(map[string]string), // Default
119125
}
120126

121127
for k, v := range extraVolumeLabels {
122128
p.Labels[k] = v
123129
}
124130

131+
for k, v := range extraTags {
132+
p.ResourceTags[k] = v
133+
}
134+
125135
for k, v := range parameters {
126136
if k == "csiProvisionerSecretName" || k == "csiProvisionerSecretNamespace" {
127137
// These are hardcoded secrets keys required to function but not needed by GCE PD
@@ -188,7 +198,15 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
188198
}
189199

190200
p.EnableConfidentialCompute = paramEnableConfidentialCompute
191-
201+
case ParameterKeyResourceTags:
202+
paramResourceTags, err := ConvertTagsStringToMap(v)
203+
if err != nil {
204+
return p, fmt.Errorf("parameters contain invalid tags parameter: %w", err)
205+
}
206+
// Override any existing resource tags with those from this parameter.
207+
for tagParentIDKey, tagValue := range paramResourceTags {
208+
p.ResourceTags[tagParentIDKey] = tagValue
209+
}
192210
default:
193211
return p, fmt.Errorf("parameters contains invalid option %q", k)
194212
}
@@ -199,13 +217,19 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
199217
return p, nil
200218
}
201219

202-
func ExtractAndDefaultSnapshotParameters(parameters map[string]string, driverName string) (SnapshotParameters, error) {
220+
func ExtractAndDefaultSnapshotParameters(parameters map[string]string, driverName string, extraTags map[string]string) (SnapshotParameters, error) {
203221
p := SnapshotParameters{
204222
StorageLocations: []string{},
205223
SnapshotType: DiskSnapshotType,
206224
Tags: make(map[string]string), // Default
207225
Labels: make(map[string]string), // Default
226+
ResourceTags: make(map[string]string), // Default
227+
}
228+
229+
for k, v := range extraTags {
230+
p.ResourceTags[k] = v
208231
}
232+
209233
for k, v := range parameters {
210234
switch strings.ToLower(k) {
211235
case ParameterKeyStorageLocations:
@@ -237,6 +261,15 @@ func ExtractAndDefaultSnapshotParameters(parameters map[string]string, driverNam
237261
for labelKey, labelValue := range paramLabels {
238262
p.Labels[labelKey] = labelValue
239263
}
264+
case ParameterKeyResourceTags:
265+
paramResourceTags, err := ConvertTagsStringToMap(v)
266+
if err != nil {
267+
return p, fmt.Errorf("parameters contain invalid tags parameter: %w", err)
268+
}
269+
// Override any existing resource tags with those from this parameter.
270+
for tagParentIDKey, tagValue := range paramResourceTags {
271+
p.ResourceTags[tagParentIDKey] = tagValue
272+
}
240273
default:
241274
return p, fmt.Errorf("parameters contains invalid option %q", k)
242275
}

0 commit comments

Comments
 (0)