Skip to content

Commit

Permalink
Check machine status and log details if it is not running
Browse files Browse the repository at this point in the history
  • Loading branch information
ventifus committed Oct 4, 2024
1 parent 69378fb commit 89c0c33
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
26 changes: 26 additions & 0 deletions pkg/cluster/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ func (m *manager) apiServersReady(ctx context.Context) (bool, error) {
}

func (m *manager) minimumWorkerNodesReady(ctx context.Context) (bool, error) {
machines, err := m.maocli.MachineV1beta1().Machines("openshift-machine-api").List(ctx, metav1.ListOptions{
LabelSelector: "machine.openshift.io/cluster-api-machine-role=worker",
})
if err != nil {
m.log.Error(err)
return false, nil
}

readyWorkerMachines := 0
for _, machine := range machines.Items {
if *machine.Status.Phase == "Running" {
readyWorkerMachines++
} else {
if err == nil {
m.log.Errorf("Machine %s is %s; status: %v", machine.Name, *machine.Status.Phase, string(machine.Status.ProviderStatus.Raw))
} else {
m.log.Errorf("Machine %s is %s; error decoding status: %s", machine.Name, *machine.Status.Phase, err)
}
}
}

if readyWorkerMachines < minimumWorkerNodes {
m.log.Infof("%d machines out of %d machines ready", readyWorkerMachines, minimumWorkerNodes)
return false, nil
}

nodes, err := m.kubernetescli.CoreV1().Nodes().List(ctx, metav1.ListOptions{
LabelSelector: "node-role.kubernetes.io/worker",
})
Expand Down
58 changes: 57 additions & 1 deletion pkg/cluster/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package cluster

import (
"context"
"encoding/json"
"errors"
"testing"
"time"

configv1 "github.com/openshift/api/config/v1"
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
operatorv1 "github.com/openshift/api/operator/v1"
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
machinefake "github.com/openshift/client-go/machine/clientset/versioned/fake"
operatorfake "github.com/openshift/client-go/operator/clientset/versioned/fake"
cloudcredentialv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
"github.com/sirupsen/logrus"
Expand All @@ -29,6 +32,13 @@ import (

const errMustBeNilMsg = "err must be nil; condition is retried until timeout"

func marshalAzureMachineProviderStatus(t *testing.T, status *machinev1beta1.AzureMachineProviderStatus) *runtime.RawExtension {
buf, _ := json.Marshal(status)
return &runtime.RawExtension{
Raw: buf,
}
}

func TestOperatorConsoleExists(t *testing.T) {
ctx := context.Background()

Expand Down Expand Up @@ -118,6 +128,8 @@ func TestIsOperatorAvailable(t *testing.T) {

func TestMinimumWorkerNodesReady(t *testing.T) {
ctx := context.Background()
machineStateRunning := "Running"
machineStateFailed := "Failed"

for _, tt := range []struct {
name string
Expand Down Expand Up @@ -149,6 +161,50 @@ func TestMinimumWorkerNodesReady(t *testing.T) {
},
} {
m := &manager{
log: logrus.NewEntry(logrus.StandardLogger()),
maocli: machinefake.NewSimpleClientset(
&machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "node1",
Namespace: "openshift-machine-api",
Labels: map[string]string{
"machine.openshift.io/cluster-api-machine-role": "worker",
"machine.openshift.io/cluster-api-machine-type": "worker",
},
},
Status: machinev1beta1.MachineStatus{
Phase: &machineStateRunning,
ProviderStatus: marshalAzureMachineProviderStatus(t, &machinev1beta1.AzureMachineProviderStatus{}),
},
},
&machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "node2",
Namespace: "openshift-machine-api",
Labels: map[string]string{
"machine.openshift.io/cluster-api-machine-role": "worker",
"machine.openshift.io/cluster-api-machine-type": "worker",
},
},
Status: machinev1beta1.MachineStatus{
Phase: &machineStateRunning,
ProviderStatus: marshalAzureMachineProviderStatus(t, &machinev1beta1.AzureMachineProviderStatus{}),
},
},
&machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "node3",
Namespace: "openshift-machine-api",
Labels: map[string]string{
"machine.openshift.io/cluster-api-machine-role": "worker",
"machine.openshift.io/cluster-api-machine-type": "worker",
},
},
Status: machinev1beta1.MachineStatus{
Phase: &machineStateFailed,
ProviderStatus: marshalAzureMachineProviderStatus(t, &machinev1beta1.AzureMachineProviderStatus{}),
},
},
testMachine(t, "openshift-machine-api", "master1", &machinev1beta1.AzureMachineProviderSpec{}),
testMachine(t, "openshift-machine-api", "master2", &machinev1beta1.AzureMachineProviderSpec{}),
),
kubernetescli: fake.NewSimpleClientset(&corev1.NodeList{
Items: []corev1.Node{
{
Expand Down Expand Up @@ -187,7 +243,7 @@ func TestMinimumWorkerNodesReady(t *testing.T) {
t.Error(errMustBeNilMsg)
}
if ready != tt.want {
t.Error(ready)
t.Error(tt.name, ready)
}
}
}
Expand Down

0 comments on commit 89c0c33

Please sign in to comment.