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
GCE and AWS provisioners, dynamic provisioning: admins can configure zone(s) where PVs shall be created #38505
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,3 +410,27 @@ func JoinMountOptions(userOptions []string, systemOptions []string) []string { | |
} | ||
return allMountOptions.UnsortedList() | ||
} | ||
|
||
// ZonesToSet converts a string containing a comma separated list of zones to set | ||
func ZonesToSet(zonesString string) (sets.String, error) { | ||
zonesSlice := strings.Split(zonesString, ",") | ||
zonesSet := make(sets.String) | ||
for _, zone := range zonesSlice { | ||
trimmedZone := strings.TrimSpace(zone) | ||
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. If we defined it as a space separated string, we could just use strings.Fields() and maybe not need a helper function at all? 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. It is necessary to convert the input string with list of zones to a set because the func ChooseZoneForVolume requires a set as one of the input parameters. As the 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. ack |
||
if trimmedZone == "" { | ||
return make(sets.String), fmt.Errorf("comma separated list of zones (%q) must not contain an empty zone", zonesString) | ||
} | ||
zonesSet.Insert(trimmedZone) | ||
} | ||
return zonesSet, nil | ||
} | ||
|
||
// ValidateZone returns: | ||
// - an error in case zone is an empty string or contains only any combination of spaces and tab characters | ||
// - nil otherwise | ||
func ValidateZone(zone string) error { | ||
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. Can we in-line this at call-sites? It's sort of silly to out-of-line this. 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. IMHO, it's a matter of subjective preference. |
||
if strings.TrimSpace(zone) == "" { | ||
return fmt.Errorf("the provided %q zone is not valid, it's an empty string or contains only spaces and tab characters", zone) | ||
} | ||
return nil | ||
} |
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.
Do you need these two booleans? Can you just infer from if the string is empty?
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.
IMHO, I can approach it using one of the below ways:
A, riskier: infer from the empty string
AvailabilityZone
orAvailabilityZones
that the optional parameterzone
orzones
wasn't present. As the Persistent Volume Claim parameters are provided asmap[string]string
the values"zone": ""
or"zones": ""
are perfectly valid values in Go lang even though K8s yaml file parsing shall never provide such values. So in case I relay on the K8s yaml file parsing I can use theAvailabilityZone
orAvailabilityZones
empty string values as an indication that thezone
orzones
parameter were not specified in a Persistent Volume Claim. However, if my code is "fed" with unexpected input the incorrect input value"zone": ""
or"zones": ""
will remain undetected.B, defensive: use a dedicated booleans to indicate whether the
zone
orzones
optional parameters were present or not.I chose the defensive approach.
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.
So if I understand correctly, zone or zones == "" is invalid, and should return an error, while zone && zones == nil is valid. Can you add unit tests for all these permutations of zone/zones == nil, "", and value?
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.
The check for
zones == ""
was already present in thefunc ZonesToSet
.I added
func ValidateZone
to check forzone == ""
.