[fix] Add regional PV support for GKE

fix #4663.
For GKE pv, when create backup, return all zones retrived from node affinity.

Signed-off-by: Xun Jiang <jxun@vmware.com>
This commit is contained in:
Xun Jiang
2022-02-21 22:04:21 +08:00
parent ea3c72310b
commit 7be12a9220
3 changed files with 31 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
Support regional pv for GKE
+15 -1
View File
@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
@@ -384,6 +385,7 @@ const (
awsEbsCsiZoneKey = "topology.ebs.csi.aws.com/zone"
azureCsiZoneKey = "topology.disk.csi.azure.com/zone"
gkeCsiZoneKey = "topology.gke.io/zone"
zoneSeparator = "__"
)
// takePVSnapshot triggers a snapshot for the volume/disk underlying a PersistentVolume if the provided
@@ -539,15 +541,27 @@ func zoneFromPVNodeAffinity(res *corev1api.PersistentVolume, topologyKeys ...str
return "", ""
}
keySet := sets.NewString(topologyKeys...)
providerGke := false
zones := make([]string, 0)
for _, term := range nodeAffinity.Required.NodeSelectorTerms {
if term.MatchExpressions == nil {
continue
}
for _, exp := range term.MatchExpressions {
if keySet.Has(exp.Key) && exp.Operator == "In" && len(exp.Values) > 0 {
return exp.Key, exp.Values[0]
if exp.Key == gkeCsiZoneKey {
providerGke = true
zones = append(zones, exp.Values[0])
} else {
return exp.Key, exp.Values[0]
}
}
}
}
if providerGke {
return gkeCsiZoneKey, strings.Join(zones, zoneSeparator)
}
return "", ""
}
+15
View File
@@ -131,6 +131,21 @@ func Test_zoneFromPVNodeAffinity(t *testing.T) {
wantKey: "topology.disk.csi.azure.com/zone",
wantValue: "us-central",
},
{
name: "Volume with multiple valid keys, and provider is gke, returns the all match", // it should never happen
pv: builder.ForPersistentVolume("multi-matching-pv").NodeAffinityRequired(
builder.ForNodeSelector(
*builder.NewNodeSelectorTermBuilder().WithMatchExpression("topology.gke.io/zone",
"In", "us-central1-c").Result(),
*builder.NewNodeSelectorTermBuilder().WithMatchExpression("topology.gke.io/zone",
"In", "us-east-2c", "us-east-2b").Result(),
*builder.NewNodeSelectorTermBuilder().WithMatchExpression("topology.gke.io/zone",
"In", "europe-north1-a").Result(),
).Result(),
).Result(),
wantKey: "topology.gke.io/zone",
wantValue: "us-central1-c__us-east-2c__europe-north1-a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {