Skip to content

Commit

Permalink
Merge pull request #1176 from shajmakh/fix-pnt
Browse files Browse the repository at this point in the history
validation: fix error string
  • Loading branch information
openshift-merge-bot[bot] authored Feb 3, 2025
2 parents 28becd0 + 845f651 commit 6fa926b
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 7 deletions.
20 changes: 20 additions & 0 deletions api/v1/helper/nodegroup/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ func FindMachineConfigPools(mcps *mcov1.MachineConfigPoolList, nodeGroups []nrop
return flattenTrees(trees), nil
}

// GetTreePoolsNames returns a slice of all the MachineConfigPool matching the configured node groups
func GetTreePoolsNames(tree Tree) []string {
if tree.NodeGroup == nil {
return nil
}

if tree.MachineConfigPools == nil {
if tree.NodeGroup.PoolName == nil {
return nil
}
return []string{*tree.NodeGroup.PoolName}
}

names := []string{}
for _, mcp := range tree.MachineConfigPools {
names = append(names, mcp.Name)
}
return names
}

func flattenTrees(trees []Tree) []*mcov1.MachineConfigPool {
var result []*mcov1.MachineConfigPool
for _, tree := range trees {
Expand Down
57 changes: 57 additions & 0 deletions api/v1/helper/nodegroup/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,60 @@ func findListByNodeGroups(mcps *mcov1.MachineConfigPoolList, nodeGroups []nropv1

return result, nil
}

func TestGetTreePoolsNames(t *testing.T) {
poolName := "pool1"

tests := []struct {
name string
tree Tree
expected []string
}{
{
name: "empty tree",
tree: Tree{},
expected: nil,
},
{
name: "with mcps",
tree: Tree{
NodeGroup: &nropv1.NodeGroup{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label": "test",
},
},
},
MachineConfigPools: []*mcov1.MachineConfigPool{
{
ObjectMeta: metav1.ObjectMeta{
Name: "mcp1",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "mcp2",
},
},
},
},
expected: []string{"mcp1", "mcp2"},
},
{
name: "with node pool",
tree: Tree{
NodeGroup: &nropv1.NodeGroup{
PoolName: &poolName,
},
},
expected: []string{poolName},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetTreePoolsNames(tt.tree); !reflect.DeepEqual(got, tt.expected) {
t.Errorf("got %v, expected %v", got, tt.expected)
}
})
}
}
16 changes: 13 additions & 3 deletions api/v1/numaresourcesoperator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,25 @@ func init() {

func (ngc *NodeGroupConfig) ToString() string {
if ngc == nil {
return ""
return "nil"
}
ngc.SetDefaults()
return fmt.Sprintf("PodsFingerprinting mode: %s InfoRefreshMode: %s InfoRefreshPeriod: %s InfoRefreshPause: %s Tolerations: %+v", *ngc.PodsFingerprinting, *ngc.InfoRefreshMode, *ngc.InfoRefreshPeriod, *ngc.InfoRefreshPause, ngc.Tolerations)
}

func (ng *NodeGroup) ToString() string {
if ng == nil {
return ""
return "nil"
}
return fmt.Sprintf("PoolName: %s MachineConfigPoolSelector: %s Config: %s", *ng.PoolName, ng.MachineConfigPoolSelector.String(), ng.Config.ToString())

pn := "nil"
if ng.PoolName != nil {
pn = *ng.PoolName
}

return fmt.Sprintf("PoolName: %s "+
"MachineConfigPoolSelector: %s "+
"Config: %s "+
"Annotations: %s",
pn, ng.MachineConfigPoolSelector.String(), ng.Config.ToString(), ng.Annotations)
}
19 changes: 16 additions & 3 deletions api/v1/numaresourcesoperator_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestNodeGroupConfigToString(t *testing.T) {
}{
{
name: "nil config",
expected: "",
expected: "nil",
},
{
name: "empty fields should reflect default values",
Expand Down Expand Up @@ -94,7 +94,17 @@ func TestNodeGroupToString(t *testing.T) {
}{
{
name: "nil group",
expected: "",
expected: "nil",
},
{
name: "empty PoolName",
input: &NodeGroup{
PoolName: nil,
Annotations: map[string]string{
"ann1": "val1",
},
},
expected: "PoolName: nil MachineConfigPoolSelector: nil Config: nil Annotations: map[ann1:val1]",
},
{
name: "empty fields should reflect default values",
Expand All @@ -105,8 +115,11 @@ func TestNodeGroupToString(t *testing.T) {
InfoRefreshMode: &refMode,
},
PoolName: &pn, // although not allowed more than a specifier but we still need to display and here is not the right place to perform validations
Annotations: map[string]string{
"ann1": "val1",
},
},
expected: "PoolName: pn MachineConfigPoolSelector: nil Config: PodsFingerprinting mode: Disabled InfoRefreshMode: Events InfoRefreshPeriod: {10s} InfoRefreshPause: Disabled Tolerations: []",
expected: "PoolName: pn MachineConfigPoolSelector: nil Config: PodsFingerprinting mode: Disabled InfoRefreshMode: Events InfoRefreshPeriod: {10s} InfoRefreshPause: Disabled Tolerations: [] Annotations: map[ann1:val1]",
},
}
for _, tc := range testcases {
Expand Down
2 changes: 1 addition & 1 deletion pkg/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func MultipleMCPsPerTree(annot map[string]string, trees []nodegroupv1.Tree) erro
var err error
for _, tree := range trees {
if len(tree.MachineConfigPools) > 1 {
err = errors.Join(err, fmt.Errorf("found multiple pools matches for node group %v but expected one. Pools found %v", &tree.NodeGroup, tree.MachineConfigPools))
err = errors.Join(err, fmt.Errorf("found multiple pools matches for node group %s but expected one. Pools found %v", tree.NodeGroup.ToString(), nodegroupv1.GetTreePoolsNames(tree)))
}
}
return err
Expand Down

0 comments on commit 6fa926b

Please sign in to comment.