github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/cluster/clusterapi/machines.go (about)

     1  // Copyright (c) 2023, 2024, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package clusterapi
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  
    10  	"github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/files"
    11  	"github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/report"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  )
    15  
    16  const machinesResource = "machines.cluster.x-k8s.io"
    17  
    18  // Minimal definition of machines.cluster.x-k8s.io object that only contains the fields that will be analyzed.
    19  type machineList struct {
    20  	metav1.TypeMeta `json:",inline"`
    21  	metav1.ListMeta `json:"metadata,omitempty"`
    22  	Items           []machine `json:"items"`
    23  }
    24  type machine struct {
    25  	metav1.TypeMeta   `json:",inline"`
    26  	metav1.ObjectMeta `json:"metadata,omitempty"`
    27  	Status            capiStatus `json:"status,omitempty"`
    28  }
    29  
    30  // AnalyzeMachines handles the checking of the status of cluster-api machine resources.
    31  func AnalyzeMachines(clusterRoot string, namespace string, issueReporter *report.IssueReporter) error {
    32  	resourceRoot := clusterRoot
    33  	if len(namespace) != 0 {
    34  		resourceRoot = filepath.Join(clusterRoot, namespace)
    35  	}
    36  	list := &machineList{}
    37  	err := files.UnmarshallFileInClusterRoot(resourceRoot, "machine.cluster.x-k8s.io.json", list)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	for _, machine := range list.Items {
    43  		analyzeMachine(clusterRoot, machine, issueReporter)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  // analyzeMachine - analyze a single cluster API machine and report any issues
    50  func analyzeMachine(clusterRoot string, machine machine, issueReporter *report.IssueReporter) {
    51  
    52  	var messages []string
    53  	var subMessage string
    54  	for _, condition := range machine.Status.Conditions {
    55  		if condition.Status != corev1.ConditionTrue {
    56  			switch condition.Type {
    57  			case "APIServerPodHealthy":
    58  				subMessage = "kube-apiserver pod is not healthy"
    59  			case "BootstrapReady":
    60  				subMessage = "bootstrap provider is not ready"
    61  			case "ControllerManagerPodHealthy":
    62  				subMessage = "kube-controller-manager pod is not healthy"
    63  			case "EtcdMemberHealthy":
    64  				subMessage = "member's etcd is not healthy"
    65  			case "EtcdPodHealthy":
    66  				subMessage = "pod's etcd is not healthy"
    67  			case "InfrastructureReady":
    68  				subMessage = "infrastructure provider is not ready"
    69  			case "NodeHealthy":
    70  				subMessage = "Kubernetes node is not healthy"
    71  			case "SchedulerPodHealthy":
    72  				subMessage = "kube-scheduler pod is not healthy"
    73  			case "Ready":
    74  				subMessage = "is not ready"
    75  			default:
    76  				continue
    77  			}
    78  			// Add a message for the issue
    79  			var message string
    80  			if len(condition.Reason) == 0 {
    81  				message = fmt.Sprintf("\t%s", subMessage)
    82  			} else {
    83  				message = fmt.Sprintf("\t%s - reason is %s", subMessage, condition.Reason)
    84  			}
    85  			messages = append([]string{message}, messages...)
    86  
    87  		}
    88  	}
    89  
    90  	if len(messages) > 0 {
    91  		messages = append([]string{fmt.Sprintf("%q resource %q in namespace %q", machinesResource, machine.Name, machine.Namespace)}, messages...)
    92  		issueReporter.AddKnownIssueMessagesFiles(report.ClusterAPIClusterIssues, clusterRoot, messages, []string{})
    93  	}
    94  }