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

mocks and ci #26

Merged
merged 6 commits into from
Mar 8, 2024
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
6 changes: 2 additions & 4 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ on:
tags:
- v*
paths-ignore:
- '**.md'
- 'docs/**'
- '**/*.md'
pull_request:
branches:
- main
paths-ignore:
- 'docs/**'
- '**/*.md'

permissions:
contents: read
Expand Down
6 changes: 0 additions & 6 deletions config/lcmi/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
resources:
- manager.yaml
- service.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: ironcore-dev/lifecycle-service
newTag: v0.0.1-dev-202403061552
6 changes: 0 additions & 6 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: ironcore-dev/lifecycle-controller-manager
newTag: v0.0.1-dev-202403061527
4 changes: 2 additions & 2 deletions internal/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func (r *MachineReconciler) reconcileScan(
Name: obj.Name,
Namespace: obj.Namespace,
}))
scanResponse := resp.Msg
if err != nil {
log.Error(err, "failed to send scan request")
return ctrl.Result{}, err
}
scanResponse := resp.Msg
if LCIMRequestResultToString[scanResponse.Result].IsScheduled() {
obj.Status.Message = StatusMessageScanRequestSubmitted
return ctrl.Result{}, nil
Expand Down Expand Up @@ -135,11 +135,11 @@ func (r *MachineReconciler) reconcileInstall(
Name: obj.Name,
Namespace: obj.Namespace,
}))
installResponse := resp.Msg
if err != nil {
log.Error(err, "failed to send install request")
return ctrl.Result{}, err
}
installResponse := resp.Msg
if LCIMRequestResultToString[installResponse.Result].IsScheduled() {
obj.Status.Message = StatusMessageInstallationScheduled
return ctrl.Result{}, nil
Expand Down
121 changes: 55 additions & 66 deletions internal/controllers/machine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/ironcore-dev/lifecycle-manager/util/testutil/mock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"

lifecyclev1alpha1 "github.com/ironcore-dev/lifecycle-manager/api/lifecycle/v1alpha1"
commonv1alpha1 "github.com/ironcore-dev/lifecycle-manager/lcmi/api/common/v1alpha1"
machinev1alpha1 "github.com/ironcore-dev/lifecycle-manager/lcmi/api/machine/v1alpha1"
"github.com/ironcore-dev/lifecycle-manager/util/apiutil"
"github.com/ironcore-dev/lifecycle-manager/util/testutil"
"github.com/ironcore-dev/lifecycle-manager/util/uuidutil"
)
Expand All @@ -39,11 +39,16 @@ var _ = Describe("Machine controller", func() {

Context("When Machine object is being deleted", func() {
It("Should interrupt reconciliation and return empty result with no error", func() {
now := metav1.Now()
machine := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
WithDeletionTimestamp(&now).
WithFinalizers([]string{"test-suite-finalizer"}).
MachineFromUnstructured().Complete()
Expect(machine).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineObject("sample", "default",
mock.MachineWithDeletionTimestamp(),
mock.MachineWithFinalizer(),
)))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machine))
machineRec := NewMachineReconciler(c, s)
req := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "sample"}}
res, err := machineRec.Reconcile(context.Background(), req)
Expand All @@ -55,11 +60,14 @@ var _ = Describe("Machine controller", func() {
Context("When referred MachineType object is not found", func() {
It("Should interrupt reconciliation and return empty result with error", func() {
now := metav1.Now()
machine := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
MachineFromUnstructured().WithMachineTypeRef("sample").Complete()
Expect(machine).NotTo(BeNil())
machineKey := types.NamespacedName{Namespace: "default", Name: "sample"}
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineObject("sample", "default",
mock.MachineWithMachineTypeRef("sample"),
)))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machine))
machineRec := NewMachineReconciler(c, s)
brokerClient := fake.NewMachineClient(map[string]*machinev1alpha1.MachineStatus{
uuidutil.UUIDFromObjectKey(machineKey): {
Expand All @@ -73,26 +81,28 @@ var _ = Describe("Machine controller", func() {
req := ctrl.Request{NamespacedName: machineKey}
res, err := machineRec.Reconcile(context.Background(), req)
Expect(err).To(HaveOccurred())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
Expect(res).To(Equal(ctrl.Result{}))
})
})

Context("When new scan request submitted", func() {
It("Should update Machine object's status with corresponding message", func() {
machineKey := types.NamespacedName{Namespace: "default", Name: "sample"}
machine := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
MachineFromUnstructured().Complete()
Expect(machine).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineObject("sample", "default")))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machine))
machineRec := NewMachineReconciler(c, s)
brokerClient := fake.NewMachineClient(map[string]*machinev1alpha1.MachineStatus{})
machineRec.MachineServiceClient = brokerClient
req := ctrl.Request{NamespacedName: machineKey}
res, err := machineRec.Reconcile(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(ctrl.Result{}))

broker, _ := machineRec.MachineServiceClient.(*fake.MachineClient)
entry := broker.ReadCache(uuidutil.UUIDFromObjectKey(machineKey))
Expect(entry).NotTo(BeNil())
reconciledMachine := &lifecyclev1alpha1.Machine{}
err = machineRec.Get(context.Background(), machineKey, reconciledMachine)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -103,8 +113,13 @@ var _ = Describe("Machine controller", func() {
Context("When failed to send scan request", func() {
It("Should interrupt reconciliation and return empty result with error", func() {
machineKey := types.NamespacedName{Namespace: "default", Name: "failed-scan"}
machine := mock.NewUnstructuredBuilder().
WithName("failed-scan").
WithNamespace("default").
MachineFromUnstructured().Complete()
Expect(machine).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineObject("failed-scan", "default")))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machine))
machineRec := NewMachineReconciler(c, s)
brokerClient := fake.NewMachineClient(map[string]*machinev1alpha1.MachineStatus{})
machineRec.MachineServiceClient = brokerClient
Expand All @@ -115,56 +130,25 @@ var _ = Describe("Machine controller", func() {
})
})

Context("When installed packages match desired state", func() {
It("Should update Machine object's status with scan timestamp and result", func() {
now := metav1.Now()
expectedPackages := []*commonv1alpha1.PackageVersion{{Name: "bios", Version: "1.0.0"}}
machine := mock.NewMachineObject("sample", "default",
mock.MachineWithMachineTypeRef("sample"),
mock.MachineWithLabels(map[string]string{"env": "test"}),
mock.MachineStatusWithInstalledPackages(apiutil.PackageVersionsToKubeAPI(expectedPackages)))
machineType := mock.NewMachineTypeObject("sample", "default",
mock.MachineTypeWithGroup(lifecyclev1alpha1.MachineGroup{
MachineSelector: metav1.LabelSelector{MatchLabels: map[string]string{"env": "test"}},
Packages: apiutil.PackageVersionsToKubeAPI(expectedPackages)}))

machineKey := types.NamespacedName{Namespace: "default", Name: "sample"}
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s,
testutil.WithRuntimeObject(machine),
testutil.WithRuntimeObject(machineType))
machineRec := NewMachineReconciler(c, s)
brokerClient := fake.NewMachineClient(map[string]*machinev1alpha1.MachineStatus{
uuidutil.UUIDFromObjectKey(machineKey): {
LastScanTime: convertutil.TimeToTimestampPtr(now),
LastScanResult: 1,
InstalledPackages: expectedPackages,
}})
machineRec.MachineServiceClient = brokerClient
req := ctrl.Request{NamespacedName: machineKey}
res, err := machineRec.Reconcile(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(ctrl.Result{}))

reconciledMachine := &lifecyclev1alpha1.Machine{}
err = machineRec.Get(context.Background(), machineKey, reconciledMachine)
Expect(err).NotTo(HaveOccurred())
Expect(reconciledMachine.Status.InstalledPackages).To(Equal(apiutil.PackageVersionsToKubeAPI(expectedPackages)))
})
})

Context("When packages installation scheduled", func() {
It("Should update Machine object's status with corresponding message", func() {
now := metav1.Now()
desiredPackages := []lifecyclev1alpha1.PackageVersion{{Name: "bios", Version: "1.0.0"}}
machine := mock.NewMachineObject("sample", "default",
mock.MachineWithMachineTypeRef("sample"),
mock.MachineWithLabels(map[string]string{"env": "test"}))
machineType := mock.NewMachineTypeObject("sample", "default",
mock.MachineTypeWithGroup(lifecyclev1alpha1.MachineGroup{
machine := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
WithLabels(map[string]string{"env": "test"}).
MachineFromUnstructured().WithMachineTypeRef("sample").Complete()
Expect(machine).NotTo(BeNil())
machineType := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
MachineTypeFromUnstructured().
WithMachineGroups([]lifecyclev1alpha1.MachineGroup{{
MachineSelector: metav1.LabelSelector{MatchLabels: map[string]string{"env": "test"}},
Packages: desiredPackages}))

Packages: desiredPackages}}).
Complete()
Expect(machineType).NotTo(BeNil())
machineKey := types.NamespacedName{Namespace: "default", Name: "sample"}
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s,
Expand Down Expand Up @@ -193,14 +177,19 @@ var _ = Describe("Machine controller", func() {
It("Should interrupt reconciliation and return empty result with error", func() {
now := metav1.Now()
desiredPackages := []lifecyclev1alpha1.PackageVersion{{Name: "bios", Version: "1.0.0"}}
machine := mock.NewMachineObject("failed-install", "default",
mock.MachineWithMachineTypeRef("sample"),
mock.MachineWithLabels(map[string]string{"env": "test"}))
machineType := mock.NewMachineTypeObject("sample", "default",
mock.MachineTypeWithGroup(lifecyclev1alpha1.MachineGroup{
machine := mock.NewUnstructuredBuilder().
WithName("failed-install").
WithNamespace("default").
WithLabels(map[string]string{"env": "test"}).
MachineFromUnstructured().WithMachineTypeRef("sample").Complete()
Expect(machine).NotTo(BeNil())
machineType := mock.NewUnstructuredBuilder().
WithName("sample").WithNamespace("default").MachineTypeFromUnstructured().
WithMachineGroups([]lifecyclev1alpha1.MachineGroup{{
MachineSelector: metav1.LabelSelector{MatchLabels: map[string]string{"env": "test"}},
Packages: desiredPackages}))

Packages: desiredPackages}}).
Complete()
Expect(machineType).NotTo(BeNil())
machineKey := types.NamespacedName{Namespace: "default", Name: "failed-install"}
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s,
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/machinetype_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (r *MachineTypeReconciler) reconcile(
Name: obj.Name,
Namespace: obj.Namespace,
}))
scanResponse := resp.Msg
if err != nil {
log.Error(err, "failed to send scan request")
return ctrl.Result{}, err
}
scanResponse := resp.Msg
if LCIMRequestResultToString[scanResponse.Result].IsScheduled() {
obj.Status.Message = StatusMessageScanRequestSubmitted
return ctrl.Result{}, nil
Expand Down
41 changes: 28 additions & 13 deletions internal/controllers/machinetype_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ var _ = Describe("MachineType controller", func() {

Context("When MachineType object is being deleted", func() {
It("Should interrupt reconciliation and return empty result with no error", func() {
now := metav1.Now()
machineType := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
WithDeletionTimestamp(&now).
WithFinalizers([]string{"test-suite-finalizer"}).
MachineTypeFromUnstructured().Complete()
Expect(machineType).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineTypeObject("sample", "default",
mock.MachineTypeWithDeletionTimestamp(),
mock.MachineTypeWithFinalizer(),
)))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machineType))
machinetypeRec := NewMachineTypeReconciler(c, s)
req := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "sample"}}
res, err := machinetypeRec.Reconcile(context.Background(), req)
Expand All @@ -53,18 +58,20 @@ var _ = Describe("MachineType controller", func() {
Context("When new scan request submitted", func() {
It("Should update MachineType object's status with corresponding message", func() {
machinetypeKey := types.NamespacedName{Namespace: "default", Name: "sample"}
machineType := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
MachineTypeFromUnstructured().Complete()
Expect(machineType).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineTypeObject("sample", "default")))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machineType))
machinetypeRec := NewMachineTypeReconciler(c, s)
brokerClient := fake.NewMachineTypeClient(map[string]*machinetypev1alpha1.MachineTypeStatus{})
machinetypeRec.MachineTypeServiceClient = brokerClient
req := ctrl.Request{NamespacedName: machinetypeKey}
res, err := machinetypeRec.Reconcile(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(ctrl.Result{}))
broker, _ := machinetypeRec.MachineTypeServiceClient.(*fake.MachineTypeClient)
entry := broker.ReadCache(uuidutil.UUIDFromObjectKey(machinetypeKey))
Expect(entry).NotTo(BeNil())
reconciledMachineType := &lifecyclev1alpha1.MachineType{}
err = machinetypeRec.Get(context.Background(), machinetypeKey, reconciledMachineType)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -75,8 +82,13 @@ var _ = Describe("MachineType controller", func() {
Context("When failed to send scan request", func() {
It("Should interrupt reconciliation and return empty result with error", func() {
machinetypeKey := types.NamespacedName{Namespace: "default", Name: "failed-scan"}
machineType := mock.NewUnstructuredBuilder().
WithName("failed-scan").
WithNamespace("default").
MachineTypeFromUnstructured().Complete()
Expect(machineType).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineTypeObject("failed-scan", "default")))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machineType))
machinetypeRec := NewMachineTypeReconciler(c, s)
brokerClient := fake.NewMachineTypeClient(map[string]*machinetypev1alpha1.MachineTypeStatus{})
machinetypeRec.MachineTypeServiceClient = brokerClient
Expand All @@ -91,8 +103,13 @@ var _ = Describe("MachineType controller", func() {
It("Should update MachineType object's status with scan timestamp and result", func() {
now := metav1.Now()
machinetypeKey := types.NamespacedName{Namespace: "default", Name: "sample"}
machineType := mock.NewUnstructuredBuilder().
WithName("sample").
WithNamespace("default").
MachineTypeFromUnstructured().Complete()
Expect(machineType).NotTo(BeNil())
s := testutil.SetupScheme(testutil.WithGroupVersion(lifecyclev1alpha1.AddToScheme))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(mock.NewMachineTypeObject("sample", "default")))
c := testutil.SetupClient(s, testutil.WithRuntimeObject(machineType))
machinetypeRec := NewMachineTypeReconciler(c, s)
brokerClient := fake.NewMachineTypeClient(map[string]*machinetypev1alpha1.MachineTypeStatus{
uuidutil.UUIDFromObjectKey(machinetypeKey): {
Expand All @@ -108,12 +125,10 @@ var _ = Describe("MachineType controller", func() {
res, err := machinetypeRec.Reconcile(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(ctrl.Result{}))
broker, _ := machinetypeRec.MachineTypeServiceClient.(*fake.MachineTypeClient)
entry := broker.ReadCache(uuidutil.UUIDFromObjectKey(machinetypeKey))
Expect(entry).NotTo(BeNil())
reconciledMachineType := &lifecyclev1alpha1.MachineType{}
err = machinetypeRec.Get(context.Background(), machinetypeKey, reconciledMachineType)
Expect(err).NotTo(HaveOccurred())
Expect(reconciledMachineType.Status).To(Equal(machineType.Status))
})
})
})
11 changes: 7 additions & 4 deletions internal/controllers/onboarding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -101,7 +102,7 @@ func (r *OnboardingReconciler) onboardMachineType(ctx context.Context, obj *oobv
typeName := obj.Status.SKU[:4]
machineType := &v1alpha1.MachineType{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", manufacturer, typeName),
Name: fmt.Sprintf("%s-%s", strings.ToLower(manufacturer), strings.ToLower(typeName)),
Namespace: obj.Namespace,
},
Spec: v1alpha1.MachineTypeSpec{
Expand Down Expand Up @@ -132,9 +133,11 @@ func (r *OnboardingReconciler) onboardMachine(ctx context.Context, obj *oobv1alp
Namespace: obj.Namespace,
},
Spec: v1alpha1.MachineSpec{
MachineTypeRef: corev1.LocalObjectReference{Name: fmt.Sprintf("%s-%s", manufacturer, typeName)},
OOBMachineRef: corev1.LocalObjectReference{Name: obj.Name},
ScanPeriod: r.ScanPeriod,
MachineTypeRef: corev1.LocalObjectReference{
Name: fmt.Sprintf("%s-%s", strings.ToLower(manufacturer), strings.ToLower(typeName)),
},
OOBMachineRef: corev1.LocalObjectReference{Name: obj.Name},
ScanPeriod: r.ScanPeriod,
},
}
if err := r.Create(ctx, machine); err != nil {
Expand Down
Loading
Loading