Skip to content

Commit

Permalink
Create machine utils file
Browse files Browse the repository at this point in the history
Currenly contains only single method to decide if the machine
object has healthy state.
  • Loading branch information
Artyom Lukianov committed May 7, 2019
1 parent 8255c40 commit 62b7616
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/util/machines/machines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package machines

import (
"context"
"fmt"

"github.com/golang/glog"
mapiv1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
healthcheckingv1alpha1 "github.com/openshift/machine-api-operator/pkg/apis/healthchecking/v1alpha1"
"github.com/openshift/machine-api-operator/pkg/util/conditions"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"sigs.k8s.io/controller-runtime/pkg/client"
)

// IsMachineHealthy returns true if the the machine is running and machine node is healthy
func IsMachineHealthy(c client.Client, machine *mapiv1.Machine) bool {
if machine.Status.NodeRef == nil {
glog.Infof("machine %s does not have NodeRef", machine.Name)
return false
}

node := &v1.Node{}
key := client.ObjectKey{Namespace: metav1.NamespaceNone, Name: machine.Status.NodeRef.Name}
err := c.Get(context.TODO(), key, node)
if err != nil {
glog.Errorf("failed to fetch node for machine %s", machine.Name)
}

readyCond := conditions.GetNodeCondition(node, v1.NodeReady)
if readyCond == nil {
glog.Infof("node %s does have 'Ready' condition", machine.Name)
return false
}

if readyCond.Status != v1.ConditionTrue {
glog.Infof("node %s does have has 'Ready' condition with the status %s", machine.Name, readyCond.Status)
return false
}
return true
}

// GetMachineMachineDisruptionBudgets returns list of machine disruption budgets that suit for the machine
func GetMachineMachineDisruptionBudgets(c client.Client, machine *mapiv1.Machine) ([]*healthcheckingv1alpha1.MachineDisruptionBudget, error) {
if len(machine.Labels) == 0 {
return nil, fmt.Errorf("no MachineDisruptionBudgets found for machine %v because it has no labels", machine.Name)
}

list := &healthcheckingv1alpha1.MachineDisruptionBudgetList{}
listOptions := &client.ListOptions{Namespace: machine.Namespace}
err := c.List(context.TODO(), listOptions, list)
if err != nil {
return nil, err
}

var mdbs []*healthcheckingv1alpha1.MachineDisruptionBudget
for i := range list.Items {
mdb := &list.Items[i]
selector, err := metav1.LabelSelectorAsSelector(mdb.Spec.Selector)
if err != nil {
glog.Warningf("invalid selector: %v", err)
continue
}

// If a mdb with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(machine.Labels)) {
continue
}
mdbs = append(mdbs, mdb)
}

return mdbs, nil
}

0 comments on commit 62b7616

Please sign in to comment.