/
machines.go
61 lines (51 loc) · 1.8 KB
/
machines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package healthchecks
import (
"context"
"fmt"
"log"
machineapi "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/osde2e/pkg/common/logging"
"github.com/openshift/osde2e/pkg/common/metadata"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
const (
runningPhase = "Running"
machinesNamespace = "openshift-machine-api"
)
// CheckMachinesObjectState lists all openshift machines and validates that they are "Running"
func CheckMachinesObjectState(dynamicClient dynamic.Interface, logger *log.Logger) (bool, error) {
logger = logging.CreateNewStdLoggerOrUseExistingLogger(logger)
logger.Print("Checking that machines are healthy...")
mc := dynamicClient.
Resource(schema.GroupVersionResource{Group: "machine.openshift.io", Resource: "machines", Version: "v1beta1"}).
Namespace(machinesNamespace)
obj, err := mc.List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
if len(obj.Items) == 0 {
return false, fmt.Errorf("no machines found in the %s namespace", machinesNamespace)
}
var metadataState []string
for _, item := range obj.Items {
var machine machineapi.Machine
err = runtime.DefaultUnstructuredConverter.
FromUnstructured(item.UnstructuredContent(), &machine)
if err != nil {
return false, fmt.Errorf("error casting object: %s", err.Error())
}
if machine.Status.Phase == nil || *machine.Status.Phase != runningPhase {
metadataState = append(metadataState, fmt.Sprintf("%v", machine))
logger.Printf("machine %s not ready", machine.Name)
}
}
if len(metadataState) > 0 {
metadata.Instance.SetHealthcheckValue("machines", metadataState)
return false, nil
}
metadata.Instance.ClearHealthcheckValue("machines")
return true, nil
}