|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "google.golang.org/protobuf/testing/protocmp" |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + snapRegionKey = "topology.kubernetes.io/region" |
| 30 | + snapZoneKey = "topology.kubernetes.io/zone" |
| 31 | +) |
| 32 | + |
| 33 | +func zoneTerm(values ...string) v1.TopologySelectorTerm { |
| 34 | + return v1.TopologySelectorTerm{ |
| 35 | + MatchLabelExpressions: []v1.TopologySelectorLabelRequirement{ |
| 36 | + {Key: snapZoneKey, Values: values}, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestIntersectSnapshotTopology covers the Immediate-binding intersection of |
| 42 | +// StorageClass.AllowedTopologies with a snapshot's NodeAffinity (KEP-5943). |
| 43 | +func TestIntersectSnapshotTopology(t *testing.T) { |
| 44 | + testcases := map[string]struct { |
| 45 | + scTopology []v1.TopologySelectorTerm |
| 46 | + snapTopology []v1.TopologySelectorTerm |
| 47 | + expected []*csi.Topology |
| 48 | + }{ |
| 49 | + "no snapshot topology falls back to StorageClass terms": { |
| 50 | + scTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2a", "us-west-2b")}, |
| 51 | + snapTopology: nil, |
| 52 | + expected: []*csi.Topology{ |
| 53 | + {Segments: map[string]string{snapZoneKey: "us-west-2a"}}, |
| 54 | + {Segments: map[string]string{snapZoneKey: "us-west-2b"}}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + "no StorageClass topology falls back to snapshot terms": { |
| 58 | + scTopology: nil, |
| 59 | + snapTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2c")}, |
| 60 | + expected: []*csi.Topology{ |
| 61 | + {Segments: map[string]string{snapZoneKey: "us-west-2c"}}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "overlapping zones intersect to the common subset": { |
| 65 | + scTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2a", "us-west-2b")}, |
| 66 | + snapTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2b", "us-west-2c")}, |
| 67 | + expected: []*csi.Topology{ |
| 68 | + {Segments: map[string]string{snapZoneKey: "us-west-2b"}}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + "identical single zone": { |
| 72 | + scTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2a")}, |
| 73 | + snapTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2a")}, |
| 74 | + expected: []*csi.Topology{ |
| 75 | + {Segments: map[string]string{snapZoneKey: "us-west-2a"}}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "disjoint zones yield empty intersection": { |
| 79 | + scTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2d")}, |
| 80 | + snapTopology: []v1.TopologySelectorTerm{zoneTerm("us-west-2a", "us-west-2b", "us-west-2c")}, |
| 81 | + expected: []*csi.Topology{}, |
| 82 | + }, |
| 83 | + "snapshot zone within StorageClass region keeps the more specific snapshot term": { |
| 84 | + scTopology: []v1.TopologySelectorTerm{{ |
| 85 | + MatchLabelExpressions: []v1.TopologySelectorLabelRequirement{ |
| 86 | + {Key: snapRegionKey, Values: []string{"us-west-2"}}, |
| 87 | + }, |
| 88 | + }}, |
| 89 | + snapTopology: []v1.TopologySelectorTerm{{ |
| 90 | + MatchLabelExpressions: []v1.TopologySelectorLabelRequirement{ |
| 91 | + {Key: snapRegionKey, Values: []string{"us-west-2"}}, |
| 92 | + {Key: snapZoneKey, Values: []string{"us-west-2a"}}, |
| 93 | + }, |
| 94 | + }}, |
| 95 | + expected: []*csi.Topology{ |
| 96 | + {Segments: map[string]string{snapRegionKey: "us-west-2", snapZoneKey: "us-west-2a"}}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for name, tc := range testcases { |
| 102 | + t.Run(name, func(t *testing.T) { |
| 103 | + got := IntersectSnapshotTopology(tc.scTopology, tc.snapTopology) |
| 104 | + if !cmp.Equal(got, tc.expected, protocmp.Transform()) { |
| 105 | + t.Errorf("IntersectSnapshotTopology() mismatch (-got +want):\n%s", |
| 106 | + cmp.Diff(got, tc.expected, protocmp.Transform())) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// TestIntersectSnapshotTopologyEmptyIsFatalSignal documents that an empty (but |
| 113 | +// non-nil) result is the signal callers use to fail provisioning fast when the |
| 114 | +// snapshot and StorageClass topologies are incompatible. |
| 115 | +func TestIntersectSnapshotTopologyEmptyIsFatalSignal(t *testing.T) { |
| 116 | + got := IntersectSnapshotTopology( |
| 117 | + []v1.TopologySelectorTerm{zoneTerm("us-west-2d")}, |
| 118 | + []v1.TopologySelectorTerm{zoneTerm("us-west-2a")}, |
| 119 | + ) |
| 120 | + if len(got) != 0 { |
| 121 | + t.Fatalf("expected empty intersection for disjoint topologies, got %v", got) |
| 122 | + } |
| 123 | +} |
0 commit comments