-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #310. Signed-off-by: Alexey Palazhchenko <[email protected]>
- Loading branch information
Showing
24 changed files
with
293 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
!config | ||
!hack | ||
!app | ||
!internal | ||
!pkg | ||
!sfyra | ||
!templates | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/metal-controller-manager/api/v1alpha1/serverclass_filter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package v1alpha1 | ||
|
||
import "sort" | ||
|
||
// FilterAcceptedServers returns a new slice of Servers that are accepted and qualify. | ||
// | ||
// Returned Servers are always sorted by name for stable results. | ||
func FilterAcceptedServers(servers []Server, q Qualifiers) []Server { | ||
res := make([]Server, 0, len(servers)) | ||
|
||
for _, server := range servers { | ||
// skip non-accepted servers | ||
if !server.Spec.Accepted { | ||
continue | ||
} | ||
|
||
// check CPU qualifiers if they are present | ||
if filters := q.CPU; len(filters) > 0 { | ||
var match bool | ||
|
||
for _, filter := range filters { | ||
if cpu := server.Spec.CPU; cpu != nil && filter.PartialEqual(cpu) { | ||
match = true | ||
break | ||
} | ||
} | ||
|
||
if !match { | ||
continue | ||
} | ||
} | ||
|
||
if filters := q.SystemInformation; len(filters) > 0 { | ||
var match bool | ||
|
||
for _, filter := range filters { | ||
if sysInfo := server.Spec.SystemInformation; sysInfo != nil && filter.PartialEqual(sysInfo) { | ||
match = true | ||
break | ||
} | ||
} | ||
|
||
if !match { | ||
continue | ||
} | ||
} | ||
|
||
if filters := q.LabelSelectors; len(filters) > 0 { | ||
var match bool | ||
|
||
for _, filter := range filters { | ||
for labelKey, labelVal := range filter { | ||
if val, ok := server.ObjectMeta.Labels[labelKey]; ok && labelVal == val { | ||
match = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
if !match { | ||
continue | ||
} | ||
} | ||
|
||
res = append(res, server) | ||
} | ||
|
||
sort.Slice(res, func(i, j int) bool { return res[i].Name < res[j].Name }) | ||
|
||
return res | ||
} |
113 changes: 113 additions & 0 deletions
113
app/metal-controller-manager/api/v1alpha1/serverclass_filter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package v1alpha1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
metalv1alpha1 "github.com/talos-systems/sidero/app/metal-controller-manager/api/v1alpha1" | ||
) | ||
|
||
func TestFilterAcceptedServers(t *testing.T) { | ||
t.Parallel() | ||
|
||
atom := metalv1alpha1.Server{ | ||
Spec: metalv1alpha1.ServerSpec{ | ||
Accepted: true, | ||
CPU: &metalv1alpha1.CPUInformation{ | ||
Manufacturer: "Intel(R) Corporation", | ||
Version: "Intel(R) Atom(TM) CPU C3558 @ 2.20GHz", | ||
}, | ||
}, | ||
} | ||
ryzen := metalv1alpha1.Server{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"my-server-label": "true", | ||
}, | ||
}, | ||
Spec: metalv1alpha1.ServerSpec{ | ||
Accepted: true, | ||
CPU: &metalv1alpha1.CPUInformation{ | ||
Manufacturer: "Advanced Micro Devices, Inc.", | ||
Version: "AMD Ryzen 7 2700X Eight-Core Processor", | ||
}, | ||
SystemInformation: &metalv1alpha1.SystemInformation{ | ||
Manufacturer: "QEMU", | ||
}, | ||
}, | ||
} | ||
notAccepted := metalv1alpha1.Server{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"my-server-label": "true", | ||
}, | ||
}, | ||
Spec: metalv1alpha1.ServerSpec{ | ||
Accepted: false, | ||
CPU: &metalv1alpha1.CPUInformation{ | ||
Manufacturer: "Advanced Micro Devices, Inc.", | ||
Version: "AMD Ryzen 7 2700X Eight-Core Processor", | ||
}, | ||
SystemInformation: &metalv1alpha1.SystemInformation{ | ||
Manufacturer: "QEMU", | ||
}, | ||
}, | ||
} | ||
|
||
servers := []metalv1alpha1.Server{atom, ryzen, notAccepted} | ||
|
||
testdata := map[string]struct { | ||
q metalv1alpha1.Qualifiers | ||
expected []metalv1alpha1.Server | ||
}{ | ||
"Intel only": { | ||
q: metalv1alpha1.Qualifiers{ | ||
CPU: []metalv1alpha1.CPUInformation{ | ||
{ | ||
Manufacturer: "Intel(R) Corporation", | ||
}, | ||
}, | ||
}, | ||
expected: []metalv1alpha1.Server{atom}, | ||
}, | ||
"QEMU only": { | ||
q: metalv1alpha1.Qualifiers{ | ||
SystemInformation: []metalv1alpha1.SystemInformation{ | ||
{ | ||
Manufacturer: "QEMU", | ||
}, | ||
}, | ||
}, | ||
expected: []metalv1alpha1.Server{ryzen}, | ||
}, | ||
"with label": { | ||
q: metalv1alpha1.Qualifiers{ | ||
LabelSelectors: []map[string]string{ | ||
{ | ||
"my-server-label": "true", | ||
}, | ||
}, | ||
}, | ||
expected: []metalv1alpha1.Server{ryzen}, | ||
}, | ||
metalv1alpha1.ServerClassAny: { | ||
expected: []metalv1alpha1.Server{atom, ryzen}, | ||
}, | ||
} | ||
|
||
for name, td := range testdata { | ||
name, td := name, td | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := metalv1alpha1.FilterAcceptedServers(servers, td.q) | ||
assert.Equal(t, actual, td.expected) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
app/metal-controller-manager/api/v1alpha1/v1alpha1_test.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.