Skip to content

Commit

Permalink
Check for Available BMHs
Browse files Browse the repository at this point in the history
There could be BMHs in other states and we should exclude those.
  • Loading branch information
rabi authored and openshift-cherrypick-robot committed Jan 2, 2024
1 parent fb2a43d commit 303838a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
23 changes: 14 additions & 9 deletions api/v1beta1/common_openstackbaremetalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/go-logr/logr"
metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openstack-k8s-operators/osp-director-operator/api/shared"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -17,9 +17,9 @@ func GetBmhHosts(
c client.Client,
namespace string,
labelSelector map[string]string,
) (*metal3v1alpha1.BareMetalHostList, error) {
) (*metal3v1.BareMetalHostList, error) {

bmhHostsList := &metal3v1alpha1.BareMetalHostList{}
bmhHostsList := &metal3v1.BareMetalHostList{}

listOpts := []client.ListOption{
client.InNamespace(namespace),
Expand Down Expand Up @@ -54,7 +54,7 @@ func GetDeletionAnnotatedBmhHosts(
}

func getDeletionAnnotatedBmhHosts(
baremetalHostList *metal3v1alpha1.BareMetalHostList,
baremetalHostList *metal3v1.BareMetalHostList,
) []string {
annotatedBMHs := []string{}

Expand Down Expand Up @@ -115,7 +115,7 @@ func VerifyBaremetalStatusHostRefs(
}

// VerifyBaremetalSetScaleUp -
func VerifyBaremetalSetScaleUp(log logr.Logger, instance *OpenStackBaremetalSet, allBmhs *metal3v1alpha1.BareMetalHostList, existingBmhs *metal3v1alpha1.BareMetalHostList) ([]string, error) {
func VerifyBaremetalSetScaleUp(log logr.Logger, instance *OpenStackBaremetalSet, allBmhs *metal3v1.BareMetalHostList, existingBmhs *metal3v1.BareMetalHostList) ([]string, error) {
// How many new BaremetalHost allocations do we need (if any)?
newBmhsNeededCount := instance.Spec.Count - len(existingBmhs.Items)
availableBaremetalHosts := []string{}
Expand Down Expand Up @@ -150,6 +150,11 @@ func VerifyBaremetalSetScaleUp(log logr.Logger, instance *OpenStackBaremetalSet,
mismatch = true
}

if baremetalHost.Status.Provisioning.State != metal3v1.StateAvailable {
log.Info("BaremetalHost ProvisioningState is not 'Available'")
mismatch = true
}

// If for any reason we can't use this BMH, do not add to the list of available BMHs
if mismatch {
continue
Expand All @@ -162,7 +167,7 @@ func VerifyBaremetalSetScaleUp(log logr.Logger, instance *OpenStackBaremetalSet,

// If we can't satisfy the new requested replica count, explicitly state so
if newBmhsNeededCount > len(availableBaremetalHosts) {
return nil, fmt.Errorf("Unable to find %d requested BaremetalHost count (%d in use, %d available)%s for OpenStackBaremetalSet %s",
return nil, fmt.Errorf("unable to find %d requested BaremetalHost count (%d in use, %d available)%s for OpenStackBaremetalSet %s",
instance.Spec.Count,
len(existingBmhs.Items),
len(availableBaremetalHosts),
Expand All @@ -177,7 +182,7 @@ func VerifyBaremetalSetScaleUp(log logr.Logger, instance *OpenStackBaremetalSet,
}

// VerifyBaremetalSetScaleDown -
func VerifyBaremetalSetScaleDown(log logr.Logger, instance *OpenStackBaremetalSet, existingBmhs *metal3v1alpha1.BareMetalHostList, removalAnnotatedBmhCount int) error {
func VerifyBaremetalSetScaleDown(log logr.Logger, instance *OpenStackBaremetalSet, existingBmhs *metal3v1.BareMetalHostList, removalAnnotatedBmhCount int) error {
// How many new BaremetalHost de-allocations do we need (if any)?
bmhsToRemoveCount := len(existingBmhs.Items) - instance.Spec.Count

Expand All @@ -191,7 +196,7 @@ func VerifyBaremetalSetScaleDown(log logr.Logger, instance *OpenStackBaremetalSe
func verifyBaremetalSetHardwareMatch(
log logr.Logger,
instance *OpenStackBaremetalSet,
bmh *metal3v1alpha1.BareMetalHost,
bmh *metal3v1.BareMetalHost,
) bool {
// If no requested hardware requirements, we're all set
if instance.Spec.HardwareReqs == (HardwareReqs{}) {
Expand Down Expand Up @@ -278,7 +283,7 @@ func verifyBaremetalSetHardwareMatch(

diskReqs := instance.Spec.HardwareReqs.DiskReqs

var foundDisk *metal3v1alpha1.Storage
var foundDisk *metal3v1.Storage

if diskReqs.GbReq.Gb != 0 {
diskGbBms := float64(diskReqs.GbReq.Gb)
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/openstackbaremetalset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"context"
"fmt"

metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openstack-k8s-operators/osp-director-operator/api/shared"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -91,7 +91,7 @@ func (r *OpenStackBaremetalSet) ValidateCreate() error {
return err
}

if _, err := VerifyBaremetalSetScaleUp(baremetalsetlog, r, baremetalHostsList, &metal3v1alpha1.BareMetalHostList{}); err != nil {
if _, err := VerifyBaremetalSetScaleUp(baremetalsetlog, r, baremetalHostsList, &metal3v1.BareMetalHostList{}); err != nil {
return err
}

Expand Down
14 changes: 7 additions & 7 deletions controllers/openstackbaremetalset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openstack-k8s-operators/osp-director-operator/api/shared"
ospdirectorv1beta1 "github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1"
"github.com/openstack-k8s-operators/osp-director-operator/pkg/baremetalset"
Expand Down Expand Up @@ -529,7 +529,7 @@ func (r *OpenStackBaremetalSetReconciler) SetupWithManager(mgr ctrl.Manager) err
For(&ospdirectorv1beta1.OpenStackBaremetalSet{}).
Owns(&ospdirectorv1beta1.OpenStackProvisionServer{}).
Owns(&ospdirectorv1beta1.OpenStackIPSet{}).
Watches(&source.Kind{Type: &metal3v1alpha1.BareMetalHost{}}, openshiftMachineAPIBareMetalHostsFn).
Watches(&source.Kind{Type: &metal3v1.BareMetalHost{}}, openshiftMachineAPIBareMetalHostsFn).
Complete(r)
}

Expand Down Expand Up @@ -1045,7 +1045,7 @@ outer:
//
// Provision the BaremetalHost
//
foundBaremetalHost := &metal3v1alpha1.BareMetalHost{}
foundBaremetalHost := &metal3v1.BareMetalHost{}
err = r.Get(ctx, types.NamespacedName{Name: bmh, Namespace: "openshift-machine-api"}, foundBaremetalHost)
if err != nil {
cond.Message = fmt.Sprintf("Failed to get %s %s", foundBaremetalHost.Kind, bmh)
Expand All @@ -1070,8 +1070,8 @@ outer:
//
// Ensure the image url is up to date unless already provisioned
//
if string(foundBaremetalHost.Status.Provisioning.State) != "provisioned" {
foundBaremetalHost.Spec.Image = &metal3v1alpha1.Image{
if foundBaremetalHost.Status.Provisioning.State != metal3v1.StateProvisioned {
foundBaremetalHost.Spec.Image = &metal3v1.Image{
URL: localImageURL,
Checksum: fmt.Sprintf("%s.md5sum", localImageURL),
}
Expand Down Expand Up @@ -1143,7 +1143,7 @@ func (r *OpenStackBaremetalSetReconciler) baremetalHostDeprovision(
cond *shared.Condition,
bmh ospdirectorv1beta1.HostStatus,
) (string, error) {
baremetalHost := &metal3v1alpha1.BareMetalHost{}
baremetalHost := &metal3v1.BareMetalHost{}
err := r.Get(ctx, types.NamespacedName{Name: bmh.HostRef, Namespace: "openshift-machine-api"}, baremetalHost)
if err != nil {
cond.Message = fmt.Sprintf("Failed to get %s %s", baremetalHost.Kind, bmh.HostRef)
Expand Down Expand Up @@ -1384,7 +1384,7 @@ func (r *OpenStackBaremetalSetReconciler) getExistingBaremetalHosts(
ctx context.Context,
instance *ospdirectorv1beta1.OpenStackBaremetalSet,
cond *shared.Condition,
) (*metal3v1alpha1.BareMetalHostList, error) {
) (*metal3v1.BareMetalHostList, error) {
baremetalHostsList, err := ospdirectorv1beta1.GetBmhHosts(
ctx,
r.GetClient(),
Expand Down
4 changes: 2 additions & 2 deletions controllers/openstackipset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/go-logr/logr"
metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openstack-k8s-operators/osp-director-operator/api/shared"
ospdirectorv1beta1 "github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1"
common "github.com/openstack-k8s-operators/osp-director-operator/pkg/common"
Expand Down Expand Up @@ -261,7 +261,7 @@ func (r *OpenStackIPSetReconciler) Reconcile(ctx context.Context, req ctrl.Reque
} else {

// Get openshift-machine-api BaremetalHosts with
baremetalHostsList := &metal3v1alpha1.BareMetalHostList{}
baremetalHostsList := &metal3v1.BareMetalHostList{}

labelSelector := map[string]string{
common.OSPHostnameLabelSelector: hostname,
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
nmstatev1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1"
virtv1 "kubevirt.io/api/core/v1"

metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
machinev1beta1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
sriovnetworkv1 "github.com/openshift/sriov-network-operator/api/v1"

Expand Down Expand Up @@ -82,7 +82,7 @@ func init() {
utilruntime.Must(nmstatev1.AddToScheme(scheme))
utilruntime.Must(networkv1.AddToScheme(scheme))
//utilruntime.Must(cdiv1.AddToScheme(scheme))
utilruntime.Must(metal3v1alpha1.AddToScheme(scheme))
utilruntime.Must(metal3v1.AddToScheme(scheme))
utilruntime.Must(machinev1beta1.AddToScheme(scheme))
utilruntime.Must(sriovnetworkv1.AddToScheme(scheme))
utilruntime.Must(storageversionmigrations.AddToScheme(scheme))
Expand Down

0 comments on commit 303838a

Please sign in to comment.