Skip to content
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 broker/machinebroker/server/machine_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s *Server) getIronCoreMachineConfig(machine *iri.Machine) (*IronCoreMachin
s.brokerDownwardAPILabels,
machinepoolletv1alpha1.MachineDownwardAPIPrefix,
)
labels[machinepoolletv1alpha1.MachineUIDLabel] = machine.GetMetadata().GetLabels()[machinepoolletv1alpha1.MachineUIDLabel]
annotations, err := s.prepareIronCoreMachineAnnotations(machine)
if err != nil {
return nil, fmt.Errorf("error preparing ironcore machine annotations: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions broker/machinebroker/server/machine_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ var _ = Describe("CreateMachine", func() {
By("inspecting the ironcore machine")
Expect(ironcoreMachine.Labels).To(Equal(map[string]string{
poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, "root-machine-uid"): "foobar",
machinebrokerv1alpha1.CreatedLabel: "true",
machinebrokerv1alpha1.ManagerLabel: machinebrokerv1alpha1.MachineBrokerManager,
machinebrokerv1alpha1.CreatedLabel: "true",
machinebrokerv1alpha1.ManagerLabel: machinebrokerv1alpha1.MachineBrokerManager,
machinepoolletv1alpha1.MachineUIDLabel: "foobar",
}))
encodedIRIAnnotations, err := apiutils.EncodeAnnotationsAnnotation(nil)
Expect(err).NotTo(HaveOccurred())
Expand Down
37 changes: 2 additions & 35 deletions broker/machinebroker/server/machine_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -63,7 +62,7 @@ func (s *Server) listIroncoreMachines(ctx context.Context, filter *iri.MachineFi
machinebrokerv1alpha1.CreatedLabel: "true",
}

if filter != nil {
if filter != nil && filter.LabelSelector != nil {
for k := range filter.LabelSelector {
matchingLabels[k] = filter.LabelSelector[k]
}
Expand Down Expand Up @@ -185,25 +184,6 @@ func (s *Server) listMachines(ctx context.Context, filter *iri.MachineFilter) ([
return res, nil
}

func (s *Server) filterMachines(machines []*iri.Machine, filter *iri.MachineFilter) []*iri.Machine {
if filter == nil {
return machines
}

var (
res []*iri.Machine
sel = labels.SelectorFromSet(filter.LabelSelector)
)
for _, iriMachine := range machines {
if !sel.Matches(labels.Set(iriMachine.Metadata.Labels)) {
continue
}

res = append(res, iriMachine)
}
return res
}

func (s *Server) getMachine(ctx context.Context, id string) (*iri.Machine, error) {
aggregateIronCoreMachine, err := s.getAggregateIronCoreMachine(ctx, id)
if err != nil {
Expand All @@ -230,24 +210,11 @@ func (s *Server) ListMachines(ctx context.Context, req *iri.ListMachinesRequest)
}, nil
}

if labelFilter := req.Filter; labelFilter != nil && labelFilter.LabelSelector != nil {
machines, err := s.listMachines(ctx, labelFilter)
if err != nil {
return nil, convertInternalErrorToGRPC(err)
}

return &iri.ListMachinesResponse{
Machines: machines,
}, nil
}

machines, err := s.listMachines(ctx, nil)
machines, err := s.listMachines(ctx, req.Filter)
if err != nil {
return nil, convertInternalErrorToGRPC(err)
}

machines = s.filterMachines(machines, req.Filter)

return &iri.ListMachinesResponse{
Machines: machines,
}, nil
Expand Down
7 changes: 3 additions & 4 deletions poollet/machinepoollet/api/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
)

const (
MachineUIDLabel = "machinepoollet.ironcore.dev/machine-uid"
MachineNamespaceLabel = "machinepoollet.ironcore.dev/machine-namespace"
MachineNameLabel = "machinepoollet.ironcore.dev/machine-name"
RootMachineUIDLabelSuffix = "root-machine-uid"
MachineUIDLabel = "machinepoollet.ironcore.dev/machine-uid"
MachineNamespaceLabel = "machinepoollet.ironcore.dev/machine-namespace"
MachineNameLabel = "machinepoollet.ironcore.dev/machine-name"

NetworkInterfaceUIDLabel = "machinepoollet.ironcore.dev/nic-uid"
NetworkInterfaceNamespaceLabel = "machinepoollet.ironcore.dev/nic-namespace"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/ironcore/iri/testing/machine"
"github.com/ironcore-dev/ironcore/poollet/irievent"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
machinepoolletclient "github.com/ironcore-dev/ironcore/poollet/machinepoollet/client"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/controllers"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/mcm"
Expand Down Expand Up @@ -234,8 +233,7 @@ func SetupTest() (*corev1.Namespace, *computev1alpha1.MachinePool, *computev1alp
MachineClassMapper: machineClassMapper,
MachinePoolName: mp.Name,
MachineDownwardAPILabels: map[string]string{
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
v1alpha1.RootMachineUIDLabelSuffix: "metadata.uid",
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
},
NicDownwardAPILabels: map[string]string{
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
Expand Down
109 changes: 18 additions & 91 deletions poollet/machinepoollet/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/kubectl/pkg/util/fieldpath"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -71,13 +72,6 @@ type MachineReconciler struct {
MaxConcurrentReconciles int
}

func (r *MachineReconciler) machineKeyLabelSelector(machineKey client.ObjectKey) map[string]string {
return map[string]string{
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, "root-machine-namespace"): machineKey.Namespace,
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, "root-machine-name"): machineKey.Name,
}
}

//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
//+kubebuilder:rbac:groups=compute.ironcore.dev,resources=machines,verbs=get;list;watch;update;patch
Expand All @@ -96,36 +90,25 @@ func (r *MachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("error getting machine %s: %w", req.NamespacedName, err)
}
return r.deleteGone(ctx, log, req.NamespacedName)
return r.deleteGone(ctx, log, machine)
}
return r.reconcileExists(ctx, log, machine)
}

func (r *MachineReconciler) getIRIMachinesForMachine(ctx context.Context, machine *computev1alpha1.Machine) ([]*iri.Machine, error) {
labelKey := poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)
machineUID := machine.Labels[labelKey]
if machineUID == "" {
machineUID = string(machine.GetUID())
}
labels := map[string]string{
labelKey: machineUID,
}
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
Filter: &iri.MachineFilter{LabelSelector: labels},
})
if err != nil {
return nil, fmt.Errorf("error listing machines by machine uid: %w", err)
func (r *MachineReconciler) machineUIDLabelSelector(machineUID types.UID) map[string]string {
return map[string]string{
v1alpha1.MachineUIDLabel: string(machineUID),
}
return res.Machines, nil
}

func (r *MachineReconciler) listMachinesByMachineKey(ctx context.Context, machineKey client.ObjectKey) ([]*iri.Machine, error) {
func (r *MachineReconciler) getIRIMachinesForMachine(ctx context.Context, machine *computev1alpha1.Machine) ([]*iri.Machine, error) {
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
Filter: &iri.MachineFilter{LabelSelector: r.machineKeyLabelSelector(machineKey)},
Filter: &iri.MachineFilter{LabelSelector: r.machineUIDLabelSelector(machine.UID)},
})
if err != nil {
return nil, fmt.Errorf("error listing machines by machine key: %w", err)
return nil, fmt.Errorf("error listing machines by machine uid label filter: %w", err)
}

return res.Machines, nil
}

Expand Down Expand Up @@ -182,11 +165,11 @@ func (r *MachineReconciler) deleteMachines(ctx context.Context, log logr.Logger,
}
}

func (r *MachineReconciler) deleteGone(ctx context.Context, log logr.Logger, machineKey client.ObjectKey) (ctrl.Result, error) {
func (r *MachineReconciler) deleteGone(ctx context.Context, log logr.Logger, machine *computev1alpha1.Machine) (ctrl.Result, error) {
log.V(1).Info("Delete gone")

log.V(1).Info("Listing machines by machine key")
machines, err := r.listMachinesByMachineKey(ctx, machineKey)
log.V(1).Info("Listing IRI machines by machine")
machines, err := r.getIRIMachinesForMachine(ctx, machine)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error listing machines: %w", err)
}
Expand Down Expand Up @@ -220,25 +203,16 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine

log.V(1).Info("Finalizer present")

log.V(1).Info("Deleting machines by UID")
labelKey := poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)
machineUID := machine.Labels[labelKey]
if machineUID == "" {
machineUID = string(machine.GetUID())
}
labels := map[string]string{
labelKey: machineUID,
}
ok, err := r.deleteMachinesByMachineUID(ctx, log, labels)
log.V(1).Info("Deleting IRI machine for machine")
res, err := r.deleteGone(ctx, log, machine)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error deleting machines: %w", err)
}
if !ok {
if !res.IsZero() {
log.V(1).Info("Not all machines are gone, requeueing")
return ctrl.Result{Requeue: true}, nil
return res, nil
}

log.V(1).Info("Deleted iri machines by UID, removing finalizer")
log.V(1).Info("Deleted iri machines for machine, removing finalizer")
if err := clientutils.PatchRemoveFinalizer(ctx, r.Client, machine, v1alpha1.MachineFinalizer); err != nil {
return ctrl.Result{}, fmt.Errorf("error removing finalizer: %w", err)
}
Expand All @@ -247,53 +221,6 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
return ctrl.Result{}, nil
}

func (r *MachineReconciler) deleteMachinesByMachineUID(ctx context.Context, log logr.Logger, labels map[string]string) (bool, error) {
log.V(1).Info("Listing machines")
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
Filter: &iri.MachineFilter{
LabelSelector: labels,
},
})
if err != nil {
return false, fmt.Errorf("error listing machines: %w", err)
}

log.V(1).Info("Listed machines", "NoOfMachines", len(res.Machines))
var (
errs []error
deletingMachineIDs []string
)
for _, machine := range res.Machines {
machineID := machine.Metadata.Id
log := log.WithValues("MachineID", machineID)
log.V(1).Info("Deleting machine")
_, err := r.MachineRuntime.DeleteMachine(ctx, &iri.DeleteMachineRequest{
MachineId: machineID,
})
if err != nil {
if status.Code(err) != codes.NotFound {
errs = append(errs, fmt.Errorf("error deleting machine %s: %w", machineID, err))
} else {
log.V(1).Info("Machine is already gone")
}
} else {
log.V(1).Info("Issued machine deletion")
deletingMachineIDs = append(deletingMachineIDs, machineID)
}
}

switch {
case len(errs) > 0:
return false, fmt.Errorf("error(s) deleting machine(s): %v", errs)
case len(deletingMachineIDs) > 0:
log.V(1).Info("Machines are in deletion", "DeletingMachineIDs", deletingMachineIDs)
return false, nil
default:
log.V(1).Info("All machines are gone")
return true, nil
}
}

func (r *MachineReconciler) reconcile(ctx context.Context, log logr.Logger, machine *computev1alpha1.Machine) (ctrl.Result, error) {
log.V(1).Info("Reconcile")

Expand Down Expand Up @@ -825,7 +752,7 @@ func (r *MachineReconciler) prepareIRIMachine(
errs = append(errs, fmt.Errorf("error preparing iri machine labels: %w", err))
}

annotations, err := r.iriMachineAnnotations(machine, 1, machineNicMappings)
annotations, err := r.iriMachineAnnotations(machine, 0, machineNicMappings)
if err != nil {
errs = append(errs, fmt.Errorf("error preparing iri machine annotations: %w", err))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ var _ = Describe("MachineController", func() {

By("inspecting the iri machine")
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, fooDownwardAPILabel), fooAnnotationValue))
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, machinepoolletv1alpha1.RootMachineUIDLabelSuffix), string(machine.UID)))
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(machinepoolletv1alpha1.MachineUIDLabel, string(machine.UID)))
Expect(iriMachine.Spec.Class).To(Equal(mc.Name))
Expect(iriMachine.Spec.Power).To(Equal(iri.Power_POWER_ON))
Expect(iriMachine.Spec.Volumes).To(ConsistOf(ProtoEqual(&iri.Volume{
Expand Down Expand Up @@ -320,7 +320,7 @@ var _ = Describe("MachineController", func() {

By("inspecting the iri machine")
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, fooDownwardAPILabel), fooAnnotationValue))
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, machinepoolletv1alpha1.RootMachineUIDLabelSuffix), string(machine.UID)))
Expect(iriMachine.Metadata.Labels).To(HaveKeyWithValue(machinepoolletv1alpha1.MachineUIDLabel, string(machine.UID)))
Expect(iriMachine.Spec.Class).To(Equal(mc.Name))
Expect(iriMachine.Spec.Power).To(Equal(iri.Power_POWER_ON))
Expect(iriMachine.Spec.Volumes).To(ConsistOf(ProtoEqual(&iri.Volume{
Expand Down
2 changes: 0 additions & 2 deletions poollet/machinepoollet/mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
fakemachine "github.com/ironcore-dev/ironcore/iri/testing/machine"
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/controllers"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/mcm"
"github.com/ironcore-dev/ironcore/poollet/machinepoollet/mem"
Expand Down Expand Up @@ -83,7 +82,6 @@ var _ = Describe("MachineEventMapper", func() {
MachinePoolName: mp.Name,
MachineDownwardAPILabels: map[string]string{
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
machinepoolletv1alpha1.RootMachineUIDLabelSuffix: "metadata.uid",
},
NicDownwardAPILabels: map[string]string{
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
Expand Down
Loading