-
Notifications
You must be signed in to change notification settings - Fork 159
Add gce disk labels support via create volume parameters #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
deploy/kubernetes/overlays/prow-gke-release-staging-rc-master/disk_labels.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# for gce-pd-driver | ||
- op: add | ||
path: /spec/template/spec/containers/4/args/2 | ||
value: "--extra-labels=csi=gce-pd-driver # Used for testing, not to be merged to stable" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Example StorageClass that adds labels to the GCP PD. | ||
# This requires v1.2.1 or higher. | ||
apiVersion: storage.k8s.io/v1 | ||
kind: StorageClass | ||
metadata: | ||
name: csi-gce-pd | ||
provisioner: pd.csi.storage.gke.io | ||
parameters: | ||
labels: key1=value1,key2=value2 | ||
volumeBindingMode: WaitForFirstConsumer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package common | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" | ||
|
@@ -156,3 +157,61 @@ func CreateNodeID(project, zone, name string) string { | |
func CreateZonalVolumeID(project, zone, name string) string { | ||
return fmt.Sprintf(volIDZonalFmt, project, zone, name) | ||
} | ||
|
||
// ConvertLabelsStringToMap converts the labels from string to map | ||
// example: "key1=value1,key2=value2" gets converted into {"key1": "value1", "key2": "value2"} | ||
// See https://cloud.google.com/compute/docs/labeling-resources#label_format for details. | ||
func ConvertLabelsStringToMap(labels string) (map[string]string, error) { | ||
const labelsDelimiter = "," | ||
const labelsKeyValueDelimiter = "=" | ||
|
||
labelsMap := make(map[string]string) | ||
if labels == "" { | ||
return labelsMap, nil | ||
} | ||
|
||
regexKey, _ := regexp.Compile(`^\p{Ll}[\p{Ll}0-9_-]{0,62}$`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can put a pointer to the label format https://cloud.google.com/compute/docs/labeling-resources#label_format There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
checkLabelKeyFn := func(key string) error { | ||
if !regexKey.MatchString(key) { | ||
return fmt.Errorf("label value %q is invalid (should start with lowercase letter / lowercase letter, digit, _ and - chars are allowed / 1-63 characters", key) | ||
} | ||
return nil | ||
} | ||
|
||
regexValue, _ := regexp.Compile(`^[\p{Ll}0-9_-]{0,63}$`) | ||
checkLabelValueFn := func(value string) error { | ||
if !regexValue.MatchString(value) { | ||
return fmt.Errorf("label value %q is invalid (lowercase letter, digit, _ and - chars are allowed / 0-63 characters", value) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
keyValueStrings := strings.Split(labels, labelsDelimiter) | ||
for _, keyValue := range keyValueStrings { | ||
keyValue := strings.Split(keyValue, labelsKeyValueDelimiter) | ||
|
||
if len(keyValue) != 2 { | ||
return nil, fmt.Errorf("labels %q are invalid, correct format: 'key1=value1,key2=value2'", labels) | ||
} | ||
|
||
key := strings.TrimSpace(keyValue[0]) | ||
if err := checkLabelKeyFn(key); err != nil { | ||
return nil, err | ||
} | ||
|
||
value := strings.TrimSpace(keyValue[1]) | ||
if err := checkLabelValueFn(value); err != nil { | ||
return nil, err | ||
} | ||
|
||
labelsMap[key] = value | ||
} | ||
|
||
const maxNumberOfLabels = 64 | ||
if len(labelsMap) > maxNumberOfLabels { | ||
return nil, fmt.Errorf("more than %d labels is not allowed, given: %d", maxNumberOfLabels, len(labelsMap)) | ||
} | ||
|
||
return labelsMap, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should put a comment somewhere, about which overlay build would honor this label?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done