Skip to content

Commit

Permalink
Merge pull request #952 from syself/ani/issues/941
Browse files Browse the repository at this point in the history
✨ Add new API status for the HetznerBareMetalMachineStatus
  • Loading branch information
aniruddha2000 authored Sep 27, 2023
2 parents 10dfe87 + 5765f1f commit 2292447
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/hetznerbaremetalmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ type HetznerBareMetalMachineStatus struct {
// +optional
Ready bool `json:"ready"`

// Phase represents the current phase of HetznerBareMetalMachineStatus actuation.
// E.g. Pending, Running, Terminating, Failed etc.
// +optional
Phase clusterv1.MachinePhase `json:"phase,omitempty"`

// Conditions defines current service state of the HetznerBareMetalMachine.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
Expand All @@ -279,6 +284,7 @@ type HetznerBareMetalMachineStatus struct {
// +kubebuilder:printcolumn:name="ProviderID",type="string",JSONPath=".spec.providerID",description="Provider ID"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.ready",description="hetznerbaremetalmachine is Ready"
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels.cluster\\.x-k8s\\.io/cluster-name",description="Cluster to which this M3Machine belongs"
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="HetznerBareMetalMachine status such as Pending/Provisioning/Running etc"
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].reason"
// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].message"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ spec:
jsonPath: .metadata.labels.cluster\.x-k8s\.io/cluster-name
name: Cluster
type: string
- description: HetznerBareMetalMachine status such as Pending/Provisioning/Running
etc
jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .status.conditions[?(@.type=='Ready')].reason
name: Reason
type: string
Expand Down Expand Up @@ -360,6 +365,10 @@ spec:
description: LastUpdated identifies when this status was last observed.
format: date-time
type: string
phase:
description: Phase represents the current phase of HetznerBareMetalMachineStatus
actuation. E.g. Pending, Running, Terminating, Failed etc.
type: string
ready:
description: Ready is the state of the hetznerbaremetalmachine.
type: boolean
Expand Down
11 changes: 5 additions & 6 deletions controllers/hetznerbaremetalmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"time"

"github.com/go-logr/logr"
infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
"github.com/syself/cluster-api-provider-hetzner/pkg/scope"
secretutil "github.com/syself/cluster-api-provider-hetzner/pkg/secrets"
"github.com/syself/cluster-api-provider-hetzner/pkg/services/baremetal/baremetal"
hcloudclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/client"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
Expand All @@ -38,12 +43,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
"github.com/syself/cluster-api-provider-hetzner/pkg/scope"
secretutil "github.com/syself/cluster-api-provider-hetzner/pkg/secrets"
"github.com/syself/cluster-api-provider-hetzner/pkg/services/baremetal/baremetal"
hcloudclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/client"
)

// HetznerBareMetalMachineReconciler reconciles a HetznerBareMetalMachine object.
Expand Down
23 changes: 23 additions & 0 deletions controllers/hetznerbaremetalmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
return bmMachine.Status.FailureMessage != nil && *bmMachine.Status.FailureMessage == baremetal.FailureMessageMaintenanceMode
}, timeout).Should(BeTrue())
})

It("checks the hetznerBareMetalMachine status running phase", func() {
By("making sure that machine is in running state")
Eventually(func() bool {
if err := testEnv.Get(ctx, key, bmMachine); err != nil {
return false
}

testEnv.GetLogger().Info("status of host and hetznerBareMetalMachine", "hetznerBareMetalMachine phase", bmMachine.Status.Phase, "host state", host.Spec.Status.ProvisioningState)
return bmMachine.Status.Phase == clusterv1.MachinePhaseRunning
}, timeout, time.Second).Should(BeTrue())
})
})
})

Expand Down Expand Up @@ -491,6 +503,17 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
return isPresentAndFalseWithReason(key, bmMachine, infrav1.HostAssociateSucceededCondition, infrav1.NoAvailableHostReason)
}, timeout).Should(BeTrue())
})

It("checks the hetznerBareMetalMachine status pending phase", func() {
Eventually(func() bool {
if err := testEnv.Get(ctx, key, bmMachine); err != nil {
return false
}

testEnv.GetLogger().Info("phase of hetznerBareMetalMachine", "phase", bmMachine.Status.Phase)
return bmMachine.Status.Phase == clusterv1.MachinePhasePending
}, timeout, time.Second).Should(BeTrue())
})
})

Context("hetznerBareMetalMachine validation", func() {
Expand Down
11 changes: 11 additions & 0 deletions pkg/services/baremetal/baremetal/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (s *Service) Reconcile(ctx context.Context) (res reconcile.Result, err erro
// Make sure bootstrap data is available and populated. If not, return, we
// will get an event from the machine update when the flag is set to true.
if !s.scope.IsBootstrapReady() {
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhasePending
conditions.MarkFalse(
s.scope.BareMetalMachine,
infrav1.BootstrapReadyCondition,
Expand Down Expand Up @@ -132,6 +133,8 @@ func (s *Service) Delete(ctx context.Context) (res reconcile.Result, err error)
}

if host != nil && host.Spec.ConsumerRef != nil {
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhaseDeleting

// remove control plane as load balancer target
if s.scope.IsControlPlane() && s.scope.HetznerCluster.Spec.ControlPlaneLoadBalancer.Enabled {
if err := s.removeAttachedServerOfLoadBalancer(ctx, host); err != nil {
Expand Down Expand Up @@ -189,6 +192,8 @@ func (s *Service) Delete(ctx context.Context) (res reconcile.Result, err error)
"HetznerBareMetalMachine with name %s deleted",
s.scope.Name(),
)
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhaseDeleted

return res, nil
}

Expand Down Expand Up @@ -274,6 +279,7 @@ func (s *Service) associate(ctx context.Context) error {
return fmt.Errorf("failed to choose host: %w", err)
}
if host == nil {
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhasePending
s.scope.V(1).Info("No available host found. Requeuing.")
conditions.MarkFalse(
s.scope.BareMetalMachine,
Expand Down Expand Up @@ -307,6 +313,7 @@ func (s *Service) associate(ctx context.Context) error {
}

s.ensureMachineAnnotation(host)

return nil
}

Expand Down Expand Up @@ -538,6 +545,8 @@ func (s *Service) getLabelSelector() labels.Selector {
func (s *Service) setProviderID(ctx context.Context) error {
// nothing to do if providerID is set
if s.scope.BareMetalMachine.Spec.ProviderID != nil {
conditions.MarkTrue(s.scope.BareMetalMachine, infrav1.HostProvisionSucceededCondition)
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhaseRunning
return nil
}

Expand All @@ -553,6 +562,7 @@ func (s *Service) setProviderID(ctx context.Context) error {
}

if host.Spec.Status.ProvisioningState != infrav1.StateProvisioned {
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhaseProvisioning
conditions.MarkFalse(
s.scope.BareMetalMachine,
infrav1.HostProvisionSucceededCondition,
Expand All @@ -568,6 +578,7 @@ func (s *Service) setProviderID(ctx context.Context) error {
providerID := providerIDFromServerID(host.Spec.ServerID)
s.scope.BareMetalMachine.Spec.ProviderID = &providerID
conditions.MarkTrue(s.scope.BareMetalMachine, infrav1.HostProvisionSucceededCondition)
s.scope.BareMetalMachine.Status.Phase = clusterv1.MachinePhaseRunning

return nil
}
Expand Down

0 comments on commit 2292447

Please sign in to comment.