Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restructure HW information in Server resources (v1alpha2) #823

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ RUN protoc -I/src/app/sidero-controller-manager/internal/api \
api.proto
RUN --mount=type=cache,target=/.cache controller-gen object:headerFile="./hack/boilerplate.go.txt" paths="./..."
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs="./app/caps-controller-manager/api/v1alpha2" --output-base ./ --output-file-base="zz_generated.conversion" --go-header-file="./hack/boilerplate.go.txt"
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs="./app/sidero-controller-manager/api/v1alpha1" --output-base ./ --output-file-base="zz_generated.conversion" --go-header-file="./hack/boilerplate.go.txt"
ARG MODULE
RUN --mount=type=cache,target=/.cache gofumports -w -local ${MODULE} .

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
caBundle: Cg==
service:
namespace: system
name: webhook-service
name: caps-webhook-service
path: /convert
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
caBundle: Cg==
service:
namespace: system
name: webhook-service
name: caps-webhook-service
path: /convert
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
caBundle: Cg==
service:
namespace: system
name: webhook-service
name: caps-webhook-service
path: /convert
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
caBundle: Cg==
service:
namespace: system
name: webhook-service
name: caps-webhook-service
path: /convert
9 changes: 9 additions & 0 deletions app/sidero-controller-manager/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ resources:
- group: metal
kind: ServerClass
version: v1alpha1
- group: metal
kind: Environment
version: v1alpha2
- group: metal
kind: Server
version: v1alpha2
- group: metal
kind: ServerClass
version: v1alpha2
version: "2"
7 changes: 7 additions & 0 deletions app/sidero-controller-manager/api/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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

// +k8s:conversion-gen=github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha2
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/.

//nolint:golint,stylecheck
package v1alpha1

import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

metalv1alpha2 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha2"
)

// ConvertTo converts this Environment to the Hub version (v1alpha2).
func (src *Environment) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*metalv1alpha2.Environment)
if err := Convert_v1alpha1_Environment_To_v1alpha2_Environment(src, dst, nil); err != nil {
return err
}

// Manually restore data from annotations
restored := &metalv1alpha2.Environment{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *Environment) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*metalv1alpha2.Environment)
if err := Convert_v1alpha2_Environment_To_v1alpha1_Environment(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}

// ConvertTo converts this MetalMachineTemplateList to the Hub version (v1alpha3).
func (src *EnvironmentList) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*metalv1alpha2.EnvironmentList)
return Convert_v1alpha1_EnvironmentList_To_v1alpha2_EnvironmentList(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *EnvironmentList) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*metalv1alpha2.EnvironmentList)
return Convert_v1alpha2_EnvironmentList_To_v1alpha1_EnvironmentList(src, dst, nil)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ var (

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme

// localSchemeBuilder is used for type conversions.
localSchemeBuilder = SchemeBuilder.SchemeBuilder
)
133 changes: 133 additions & 0 deletions app/sidero-controller-manager/api/v1alpha1/server_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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/.

//nolint:golint,stylecheck
package v1alpha1

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

metalv1alpha2 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha2"
)

// ConvertTo converts this Server to the Hub version (v1alpha2).
func (src *Server) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*metalv1alpha2.Server)
if err := Convert_v1alpha1_Server_To_v1alpha2_Server(src, dst, nil); err != nil {
return err
}

// Manually restore data from annotations
restored := &metalv1alpha2.Server{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *Server) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*metalv1alpha2.Server)
if err := Convert_v1alpha2_Server_To_v1alpha1_Server(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}

// ConvertTo converts this MetalMachineTemplateList to the Hub version (v1alpha3).
func (src *ServerList) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*metalv1alpha2.ServerList)
return Convert_v1alpha1_ServerList_To_v1alpha2_ServerList(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *ServerList) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*metalv1alpha2.ServerList)
return Convert_v1alpha2_ServerList_To_v1alpha1_ServerList(src, dst, nil)
}

// Convert_v1alpha1_ServerSpec_To_v1alpha2_ServerSpec converts to the Hub version (v1alpha2).
func Convert_v1alpha1_ServerSpec_To_v1alpha2_ServerSpec(in *ServerSpec, out *metalv1alpha2.ServerSpec, s apiconversion.Scope) error {
if err := autoConvert_v1alpha1_ServerSpec_To_v1alpha2_ServerSpec(in, out, s); err != nil {
return err
}

// Manually convert SystemInformation to Hardware.
if in.SystemInformation != nil {
if out.Hardware == nil {
out.Hardware = &metalv1alpha2.HardwareInformation{}
}

out.Hardware.System = &metalv1alpha2.SystemInformation{
Manufacturer: in.SystemInformation.Manufacturer,
ProductName: in.SystemInformation.ProductName,
Version: in.SystemInformation.Version,
SerialNumber: in.SystemInformation.SerialNumber,
SKUNumber: in.SystemInformation.SKUNumber,
Family: in.SystemInformation.Family,
}
}

// Manually convert CPU to Hardware.
if in.CPU != nil {
if out.Hardware == nil {
out.Hardware = &metalv1alpha2.HardwareInformation{}
}

out.Hardware.Compute = &metalv1alpha2.ComputeInformation{
Processors: []*metalv1alpha2.Processor{
{
Manufacturer: in.CPU.Manufacturer,
ProductName: in.CPU.Version,
},
},
}
}

return nil
}

// Convert_v1alpha2_ServerSpec_To_v1alpha1_ServerSpec converts from the Hub version (v1alpha2).
func Convert_v1alpha2_ServerSpec_To_v1alpha1_ServerSpec(in *metalv1alpha2.ServerSpec, out *ServerSpec, s apiconversion.Scope) error {
if err := autoConvert_v1alpha2_ServerSpec_To_v1alpha1_ServerSpec(in, out, s); err != nil {
return err
}

// Manually convert Hardware to SystemInformation.
if in.Hardware != nil && in.Hardware.System != nil {
out.SystemInformation = &SystemInformation{
Manufacturer: in.Hardware.System.Manufacturer,
ProductName: in.Hardware.System.ProductName,
Version: in.Hardware.System.Version,
SerialNumber: in.Hardware.System.SerialNumber,
SKUNumber: in.Hardware.System.SKUNumber,
Family: in.Hardware.System.Family,
}
}

// Manually convert Hardware to CPU.
if in.Hardware != nil && in.Hardware.Compute != nil && len(in.Hardware.Compute.Processors) > 0 {
cpu := in.Hardware.Compute.Processors[0]

out.CPU = &CPUInformation{
Manufacturer: cpu.Manufacturer,
Version: cpu.ProductName,
}
}

return nil
}

func Convert_v1alpha2_SystemInformation_To_v1alpha1_SystemInformation(in *metalv1alpha2.SystemInformation, out *SystemInformation, s apiconversion.Scope) error {
return autoConvert_v1alpha2_SystemInformation_To_v1alpha1_SystemInformation(in, out, s)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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"
"github.com/stretchr/testify/require"

metalv1alpha1 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha1"
metalv1alpha2 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha2"
)

func TestServerConvertV1alpha1V1Alpha2(t *testing.T) {
src := &metalv1alpha1.Server{
Spec: metalv1alpha1.ServerSpec{
Hostname: "example.com",
SystemInformation: &metalv1alpha1.SystemInformation{
Manufacturer: "Sidero",
ProductName: "Server",
Version: "v1.0",
},
CPU: &metalv1alpha1.CPUInformation{
Manufacturer: "Sidero CPU",
Version: "v1",
},
},
}
dst := &metalv1alpha2.Server{}

require.NoError(t, src.ConvertTo(dst))

assert.Equal(t, "example.com", dst.Spec.Hostname)
assert.Equal(t,
&metalv1alpha2.SystemInformation{
Manufacturer: "Sidero",
ProductName: "Server",
Version: "v1.0",
},
dst.Spec.Hardware.System,
)
assert.Equal(t,
&metalv1alpha2.ComputeInformation{
Processors: []*metalv1alpha2.Processor{
{
Manufacturer: "Sidero CPU",
ProductName: "v1",
},
},
},
dst.Spec.Hardware.Compute,
)
}

func TestServerConvertV1alpha2V1Alpha1(t *testing.T) {
src := &metalv1alpha2.Server{
Spec: metalv1alpha2.ServerSpec{
Hostname: "example.com",
Hardware: &metalv1alpha2.HardwareInformation{
System: &metalv1alpha2.SystemInformation{
Manufacturer: "Sidero",
ProductName: "Server",
Version: "v1.0",
},
Compute: &metalv1alpha2.ComputeInformation{
Processors: []*metalv1alpha2.Processor{
{
Manufacturer: "Sidero CPU",
ProductName: "v1",
},
},
},
},
},
}
dst := &metalv1alpha1.Server{}

require.NoError(t, dst.ConvertFrom(src))

assert.Equal(t, "example.com", dst.Spec.Hostname)
assert.Equal(t,
&metalv1alpha1.SystemInformation{
Manufacturer: "Sidero",
ProductName: "Server",
Version: "v1.0",
},
dst.Spec.SystemInformation,
)
assert.Equal(t,
&metalv1alpha1.CPUInformation{
Manufacturer: "Sidero CPU",
Version: "v1",
},
dst.Spec.CPU,
)
}
Loading