Skip to content

Commit b3afd17

Browse files
AlekSitalos-bot
authored andcommitted
feat: ship ServerClass "any"
Closes #310. Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
1 parent 94ff33b commit b3afd17

File tree

24 files changed

+293
-208
lines changed

24 files changed

+293
-208
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
!config
55
!hack
66
!app
7+
!internal
78
!pkg
89
!sfyra
910
!templates

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ COPY ./go.sum ./
4545
RUN --mount=type=cache,target=/.cache go mod download
4646
RUN --mount=type=cache,target=/.cache go mod verify
4747
COPY ./app/ ./app/
48+
COPY ./internal/ ./internal/
4849
COPY ./hack/ ./hack/
4950
RUN --mount=type=cache,target=/.cache go list -mod=readonly all >/dev/null
5051
RUN --mount=type=cache,target=/.cache ! go mod tidy -v 2>&1 | grep .

app/cluster-api-provider-sidero/api/v1alpha3/metalmachinetemplate_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
)
1010

11-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
12-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
13-
1411
// MetalMachineTemplateSpec defines the desired state of MetalMachineTemplate.
1512
type MetalMachineTemplateSpec struct {
1613
Template MetalMachineTemplateResource `json:"template"`

app/metal-controller-manager/api/v1alpha1/environment_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
)
1010

11-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
12-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
13-
1411
type Asset struct {
1512
URL string `json:"url,omitempty"`
1613
SHA512 string `json:"sha512,omitempty"`

app/metal-controller-manager/api/v1alpha1/server_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/client"
1717
)
1818

19-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
20-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
21-
2219
// BMC defines data about how to talk to the node via ipmitool.
2320
type BMC struct {
2421
// BMC endpoint.

app/metal-controller-manager/api/v1alpha1/server_types_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ func Test_PartialEqual(t *testing.T) {
5858
},
5959
want: false,
6060
},
61-
{
62-
name: "partially equal value",
63-
args: args{
64-
a: v1alpha1.CPUInformation{
65-
Manufacturer: "QEMU",
66-
},
67-
b: v1alpha1.CPUInformation{
68-
Manufacturer: "QEMU",
69-
Version: "1.2.0",
70-
},
71-
},
72-
want: true,
73-
},
7461
}
7562

7663
for _, tt := range tests {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package v1alpha1
6+
7+
import "sort"
8+
9+
// FilterAcceptedServers returns a new slice of Servers that are accepted and qualify.
10+
//
11+
// Returned Servers are always sorted by name for stable results.
12+
func FilterAcceptedServers(servers []Server, q Qualifiers) []Server {
13+
res := make([]Server, 0, len(servers))
14+
15+
for _, server := range servers {
16+
// skip non-accepted servers
17+
if !server.Spec.Accepted {
18+
continue
19+
}
20+
21+
// check CPU qualifiers if they are present
22+
if filters := q.CPU; len(filters) > 0 {
23+
var match bool
24+
25+
for _, filter := range filters {
26+
if cpu := server.Spec.CPU; cpu != nil && filter.PartialEqual(cpu) {
27+
match = true
28+
break
29+
}
30+
}
31+
32+
if !match {
33+
continue
34+
}
35+
}
36+
37+
if filters := q.SystemInformation; len(filters) > 0 {
38+
var match bool
39+
40+
for _, filter := range filters {
41+
if sysInfo := server.Spec.SystemInformation; sysInfo != nil && filter.PartialEqual(sysInfo) {
42+
match = true
43+
break
44+
}
45+
}
46+
47+
if !match {
48+
continue
49+
}
50+
}
51+
52+
if filters := q.LabelSelectors; len(filters) > 0 {
53+
var match bool
54+
55+
for _, filter := range filters {
56+
for labelKey, labelVal := range filter {
57+
if val, ok := server.ObjectMeta.Labels[labelKey]; ok && labelVal == val {
58+
match = true
59+
break
60+
}
61+
}
62+
}
63+
64+
if !match {
65+
continue
66+
}
67+
}
68+
69+
res = append(res, server)
70+
}
71+
72+
sort.Slice(res, func(i, j int) bool { return res[i].Name < res[j].Name })
73+
74+
return res
75+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package v1alpha1_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
13+
metalv1alpha1 "github.com/talos-systems/sidero/app/metal-controller-manager/api/v1alpha1"
14+
)
15+
16+
func TestFilterAcceptedServers(t *testing.T) {
17+
t.Parallel()
18+
19+
atom := metalv1alpha1.Server{
20+
Spec: metalv1alpha1.ServerSpec{
21+
Accepted: true,
22+
CPU: &metalv1alpha1.CPUInformation{
23+
Manufacturer: "Intel(R) Corporation",
24+
Version: "Intel(R) Atom(TM) CPU C3558 @ 2.20GHz",
25+
},
26+
},
27+
}
28+
ryzen := metalv1alpha1.Server{
29+
ObjectMeta: metav1.ObjectMeta{
30+
Labels: map[string]string{
31+
"my-server-label": "true",
32+
},
33+
},
34+
Spec: metalv1alpha1.ServerSpec{
35+
Accepted: true,
36+
CPU: &metalv1alpha1.CPUInformation{
37+
Manufacturer: "Advanced Micro Devices, Inc.",
38+
Version: "AMD Ryzen 7 2700X Eight-Core Processor",
39+
},
40+
SystemInformation: &metalv1alpha1.SystemInformation{
41+
Manufacturer: "QEMU",
42+
},
43+
},
44+
}
45+
notAccepted := metalv1alpha1.Server{
46+
ObjectMeta: metav1.ObjectMeta{
47+
Labels: map[string]string{
48+
"my-server-label": "true",
49+
},
50+
},
51+
Spec: metalv1alpha1.ServerSpec{
52+
Accepted: false,
53+
CPU: &metalv1alpha1.CPUInformation{
54+
Manufacturer: "Advanced Micro Devices, Inc.",
55+
Version: "AMD Ryzen 7 2700X Eight-Core Processor",
56+
},
57+
SystemInformation: &metalv1alpha1.SystemInformation{
58+
Manufacturer: "QEMU",
59+
},
60+
},
61+
}
62+
63+
servers := []metalv1alpha1.Server{atom, ryzen, notAccepted}
64+
65+
testdata := map[string]struct {
66+
q metalv1alpha1.Qualifiers
67+
expected []metalv1alpha1.Server
68+
}{
69+
"Intel only": {
70+
q: metalv1alpha1.Qualifiers{
71+
CPU: []metalv1alpha1.CPUInformation{
72+
{
73+
Manufacturer: "Intel(R) Corporation",
74+
},
75+
},
76+
},
77+
expected: []metalv1alpha1.Server{atom},
78+
},
79+
"QEMU only": {
80+
q: metalv1alpha1.Qualifiers{
81+
SystemInformation: []metalv1alpha1.SystemInformation{
82+
{
83+
Manufacturer: "QEMU",
84+
},
85+
},
86+
},
87+
expected: []metalv1alpha1.Server{ryzen},
88+
},
89+
"with label": {
90+
q: metalv1alpha1.Qualifiers{
91+
LabelSelectors: []map[string]string{
92+
{
93+
"my-server-label": "true",
94+
},
95+
},
96+
},
97+
expected: []metalv1alpha1.Server{ryzen},
98+
},
99+
metalv1alpha1.ServerClassAny: {
100+
expected: []metalv1alpha1.Server{atom, ryzen},
101+
},
102+
}
103+
104+
for name, td := range testdata {
105+
name, td := name, td
106+
t.Run(name, func(t *testing.T) {
107+
t.Parallel()
108+
109+
actual := metalv1alpha1.FilterAcceptedServers(servers, td.q)
110+
assert.Equal(t, actual, td.expected)
111+
})
112+
}
113+
}

app/metal-controller-manager/api/v1alpha1/serverclass_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
)
1111

12+
// ServerClassAny is an automatically created ServerClass that includes all Servers.
13+
const ServerClassAny = "any"
14+
1215
type Qualifiers struct {
1316
CPU []CPUInformation `json:"cpu,omitempty"`
1417
SystemInformation []SystemInformation `json:"systemInformation,omitempty"`

app/metal-controller-manager/api/v1alpha1/v1alpha1_test.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)