forked from kube-burner/kube-burner-ocp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_health.go
107 lines (99 loc) · 3.74 KB
/
cluster_health.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2024 The Kube-burner Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ocp
import (
"context"
"os"
"github.com/kube-burner/kube-burner/pkg/config"
"github.com/kube-burner/kube-burner/pkg/util"
v1 "github.com/openshift/api/config/v1"
"github.com/openshift/client-go/config/clientset/versioned"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// cluster health check
func ClusterHealth() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster-health",
Short: "Checks for ocp cluster health",
Run: func(cmd *cobra.Command, args []string) {
ClusterHealthCheck()
},
}
return cmd
}
func ClusterHealthCheck() {
log.Infof("❤️ Checking for Cluster Health")
kubeClientProvider := config.NewKubeClientProvider("", "")
clientSet, restConfig := kubeClientProvider.ClientSet(0, 0)
openshiftClientset, err := versioned.NewForConfig(restConfig)
if err != nil {
log.Fatalf("Error creating OpenShift clientset: %v", err)
}
if util.ClusterHealthyVanillaK8s(clientSet) && isClusterHealthy(clientSet, openshiftClientset) {
log.Infof("Cluster is Healthy")
} else {
log.Fatalf("Cluster is Unhealthy")
}
}
func isClusterHealthy(clientset kubernetes.Interface, openshiftClientset *versioned.Clientset) bool {
var isHealthy = true
operators, err := openshiftClientset.ConfigV1().ClusterOperators().List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Errorf("Error retrieving Cluster Operators: %v", err)
os.Exit(1)
}
for _, operator := range operators.Items {
// Check availability conditions
for _, condition := range operator.Status.Conditions {
if condition.Type == v1.OperatorAvailable && condition.Status != v1.ConditionTrue {
isHealthy = false
log.Errorf("Cluster Operator: %s, Condition: %s, Status: %v, Reason: %s", operator.Name, condition.Type, condition.Status, condition.Reason)
}
}
}
// Rosa osd-cluster-ready check
job, err := clientset.BatchV1().Jobs("openshift-monitoring").Get(context.TODO(), "osd-cluster-ready", metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return isHealthy
}
log.Errorf("Error getting job/osd-cluster-ready in namespace openshift-monitoring: %v", err)
isHealthy = false
} else {
log.Infof("Checking for status of rosa job/osd-cluster-ready in namespace openshift-monitoring")
for _, condition := range job.Status.Conditions {
if condition.Type == "Complete" && condition.Status != "True" { //nolint:goconst
isHealthy = false
log.Errorf("job: %s, Condition: %s, Status: %s, Reason: %s", job.Name, condition.Type, condition.Status, condition.Reason)
}
}
}
return isHealthy
}
func clusterImageRegistryCheck(clientset kubernetes.Interface) bool {
deployment, err := clientset.AppsV1().Deployments("openshift-image-registry").Get(context.TODO(), "image-registry", metav1.GetOptions{})
if err != nil {
log.Errorf("Error getting deployment: %v", err)
return false
}
if deployment.Status.AvailableReplicas > 0 {
log.Debugf("Deployment image-registry in namespace openshift-image-registry is available with %d replicas", deployment.Status.AvailableReplicas)
return true
}
return false
}