Skip to content

Commit

Permalink
feat: ship ServerClass "any"
Browse files Browse the repository at this point in the history
Closes #310.

Signed-off-by: Alexey Palazhchenko <[email protected]>
  • Loading branch information
AlekSi authored and talos-bot committed May 7, 2021
1 parent 94ff33b commit b3afd17
Show file tree
Hide file tree
Showing 24 changed files with 293 additions and 208 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
!config
!hack
!app
!internal
!pkg
!sfyra
!templates
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ COPY ./go.sum ./
RUN --mount=type=cache,target=/.cache go mod download
RUN --mount=type=cache,target=/.cache go mod verify
COPY ./app/ ./app/
COPY ./internal/ ./internal/
COPY ./hack/ ./hack/
RUN --mount=type=cache,target=/.cache go list -mod=readonly all >/dev/null
RUN --mount=type=cache,target=/.cache ! go mod tidy -v 2>&1 | grep .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// MetalMachineTemplateSpec defines the desired state of MetalMachineTemplate.
type MetalMachineTemplateSpec struct {
Template MetalMachineTemplateResource `json:"template"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type Asset struct {
URL string `json:"url,omitempty"`
SHA512 string `json:"sha512,omitempty"`
Expand Down
3 changes: 0 additions & 3 deletions app/metal-controller-manager/api/v1alpha1/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// BMC defines data about how to talk to the node via ipmitool.
type BMC struct {
// BMC endpoint.
Expand Down
13 changes: 0 additions & 13 deletions app/metal-controller-manager/api/v1alpha1/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ func Test_PartialEqual(t *testing.T) {
},
want: false,
},
{
name: "partially equal value",
args: args{
a: v1alpha1.CPUInformation{
Manufacturer: "QEMU",
},
b: v1alpha1.CPUInformation{
Manufacturer: "QEMU",
Version: "1.2.0",
},
},
want: true,
},
}

for _, tt := range tests {
Expand Down
75 changes: 75 additions & 0 deletions app/metal-controller-manager/api/v1alpha1/serverclass_filter.go
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 app/metal-controller-manager/api/v1alpha1/serverclass_filter_test.go
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)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ServerClassAny is an automatically created ServerClass that includes all Servers.
const ServerClassAny = "any"

type Qualifiers struct {
CPU []CPUInformation `json:"cpu,omitempty"`
SystemInformation []SystemInformation `json:"systemInformation,omitempty"`
Expand Down
14 changes: 0 additions & 14 deletions app/metal-controller-manager/api/v1alpha1/v1alpha1_test.go

This file was deleted.

Loading

0 comments on commit b3afd17

Please sign in to comment.