Skip to content

Commit f978ba9

Browse files
authored
Merge pull request #1017 from sagor999/snapshot-labels
add support for setting snapshot labels
2 parents b78d414 + fb49fed commit f978ba9

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

pkg/common/parameters.go

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type SnapshotParameters struct {
8383
SnapshotType string
8484
ImageFamily string
8585
Tags map[string]string
86+
Labels map[string]string
8687
}
8788

8889
// ExtractAndDefaultParameters will take the relevant parameters from a map and
@@ -149,6 +150,7 @@ func ExtractAndDefaultSnapshotParameters(parameters map[string]string, driverNam
149150
StorageLocations: []string{},
150151
SnapshotType: DiskSnapshotType,
151152
Tags: make(map[string]string), // Default
153+
Labels: make(map[string]string), // Default
152154
}
153155
for k, v := range parameters {
154156
switch strings.ToLower(k) {
@@ -172,6 +174,15 @@ func ExtractAndDefaultSnapshotParameters(parameters map[string]string, driverNam
172174
p.Tags[tagKeyCreatedForSnapshotNamespace] = v
173175
case ParameterKeyVolumeSnapshotContentName:
174176
p.Tags[tagKeyCreatedForSnapshotContentName] = v
177+
case ParameterKeyLabels:
178+
paramLabels, err := ConvertLabelsStringToMap(v)
179+
if err != nil {
180+
return p, fmt.Errorf("parameters contain invalid labels parameter: %w", err)
181+
}
182+
// Override any existing labels with those from this parameter.
183+
for labelKey, labelValue := range paramLabels {
184+
p.Labels[labelKey] = labelValue
185+
}
175186
default:
176187
return p, fmt.Errorf("parameters contains invalid option %q", k)
177188
}

pkg/common/parameters_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func TestSnapshotParameters(t *testing.T) {
183183
ParameterKeyVolumeSnapshotName: "snapshot-name",
184184
ParameterKeyVolumeSnapshotContentName: "snapshot-content-name",
185185
ParameterKeyVolumeSnapshotNamespace: "snapshot-namespace",
186+
ParameterKeyLabels: "label-1=value-a,key1=value1",
186187
},
187188
expectedSnapshotParames: SnapshotParameters{
188189
StorageLocations: []string{"asia"},
@@ -194,6 +195,7 @@ func TestSnapshotParameters(t *testing.T) {
194195
tagKeyCreatedForSnapshotNamespace: "snapshot-namespace",
195196
tagKeyCreatedBy: "test-driver",
196197
},
198+
Labels: map[string]string{"label-1": "value-a", "key1": "value1"},
197199
},
198200
expectError: false,
199201
},
@@ -204,6 +206,7 @@ func TestSnapshotParameters(t *testing.T) {
204206
StorageLocations: []string{},
205207
SnapshotType: DiskSnapshotType,
206208
Tags: make(map[string]string),
209+
Labels: map[string]string{},
207210
},
208211
expectError: false,
209212
},

pkg/gce-cloud-provider/compute/fake-gce.go

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, project stri
344344
Status: "UPLOADING",
345345
SelfLink: cloud.getGlobalSnapshotURI(project, snapshotName),
346346
StorageLocations: snapshotParams.StorageLocations,
347+
Labels: snapshotParams.Labels,
347348
}
348349
switch volKey.Type() {
349350
case meta.Zonal:

pkg/gce-cloud-provider/compute/gce-compute.go

+2
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, project
10591059
Name: snapshotName,
10601060
StorageLocations: snapshotParams.StorageLocations,
10611061
Description: description,
1062+
Labels: snapshotParams.Labels,
10621063
}
10631064

10641065
_, err := cloud.service.Disks.CreateSnapshot(project, volKey.Zone, volKey.Name, snapshotToCreate).Context(ctx).Do()
@@ -1075,6 +1076,7 @@ func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, proj
10751076
Name: snapshotName,
10761077
StorageLocations: snapshotParams.StorageLocations,
10771078
Description: description,
1079+
Labels: snapshotParams.Labels,
10781080
}
10791081

10801082
_, err := cloud.service.RegionDisks.CreateSnapshot(project, volKey.Region, volKey.Name, snapshotToCreate).Context(ctx).Do()

0 commit comments

Comments
 (0)