Skip to content

Commit

Permalink
Adjust Node Label Conditions Based on Full Label Name on strechcluster
Browse files Browse the repository at this point in the history
Signed-off-by: Oded Viner <[email protected]>
  • Loading branch information
OdedViner committed Jan 16, 2025
1 parent 0fbee25 commit ed67907
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
28 changes: 21 additions & 7 deletions api/v1/topologymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package v1

import (
"strings"
corev1 "k8s.io/api/core/v1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/defaults"
)

// NewNodeTopologyMap returns an initialized NodeTopologyMap
Expand Down Expand Up @@ -61,18 +62,31 @@ func (m *NodeTopologyMap) Add(topologyKey string, value string) {
m.Labels[topologyKey] = append(m.Labels[topologyKey], value)
}

// GetKeyValues returns a node label matching the topologyKey and all values
// for that label across all storage nodes
// GetKeyValues returns a node label matching the supported topologyKey and all values
// for that label across all storage nodes.
func (m *NodeTopologyMap) GetKeyValues(topologyKey string) (string, []string) {
values := []string{}

// Supported failure domain labels
supportedLabels := map[string]string{
"rack": defaults.RackTopologyKey,
"hostname": "kubernetes.io/hostname",
"zone": corev1.LabelZoneFailureDomainStable,
}

// Get the specific label based on the topologyKey
expectedLabel, exists := supportedLabels[topologyKey]
if !exists {
return "", values // Return empty if the topologyKey is unsupported
}

// Match the expected label and fetch the values
for label, labelValues := range m.Labels {
if strings.Contains(label, topologyKey) {
topologyKey = label
if label == expectedLabel {
values = labelValues
break
return label, values
}
}

return topologyKey, values
return "", values // Return empty if no match is found
}
26 changes: 26 additions & 0 deletions controllers/storagecluster/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storagecluster
import (
"context"
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -992,3 +993,28 @@ func TestDeterminePlacementRack(t *testing.T) {
assert.Equalf(t, tc.expectedRack, actual, "[%s]: failed to get correct placement rack", tc.label)
}
}

func TestGetKeyValuesStretchCluster(t *testing.T) {
// Create a new NodeTopologyMap
nodeTopologyMap := ocsv1.NewNodeTopologyMap()

// Add valid and invalid labels
nodeTopologyMap.Add(corev1.LabelZoneFailureDomainStable, "zone1")
nodeTopologyMap.Add(corev1.LabelZoneFailureDomainStable, "zone2")
nodeTopologyMap.Add("topology.kubernetes.io/zone-principal", "zone-principal1")

// Call the method
topologyKey, values := nodeTopologyMap.GetKeyValuesStrechCluster()

// Expected values
expectedKey := corev1.LabelZoneFailureDomainStable
expectedValues := []string{"zone1", "zone2"}

// Validate topologyKey
assert.Equal(t, expectedKey, topologyKey, "Mismatch in topologyKey")

// Validate values using reflect.DeepEqual
if !reflect.DeepEqual(values, expectedValues) {
t.Errorf("Expected values: %v, got: %v", expectedValues, values)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed67907

Please sign in to comment.