github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/cluster/clusterapi/machinesets.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 machineSetsResource = "machinesets.cluster.x-k8s.io"
    17  
    18  // Minimal definition of machinesets.cluster.x-k8s.io object that only contains the fields that will be analyzed.
    19  type machineSetList struct {
    20  	metav1.TypeMeta `json:",inline"`
    21  	metav1.ListMeta `json:"metadata,omitempty"`
    22  	Items           []machineSet `json:"items"`
    23  }
    24  type machineSet struct {
    25  	metav1.TypeMeta   `json:",inline"`
    26  	metav1.ObjectMeta `json:"metadata,omitempty"`
    27  	Status            capiStatus `json:"status,omitempty"`
    28  }
    29  
    30  // AnalyzeMachineSetss handles the checking of the status of cluster-api machineSet resources.
    31  func AnalyzeMachineSets(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 := &machineSetList{}
    37  	err := files.UnmarshallFileInClusterRoot(resourceRoot, "machineset.cluster.x-k8s.io.json", list)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	for _, machineSet := range list.Items {
    43  		analyzeMachineSet(clusterRoot, machineSet, issueReporter)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  // analyzeMachineSet - analyze a single cluster API machineSet and report any issues
    50  func analyzeMachineSet(clusterRoot string, machineSet machineSet, issueReporter *report.IssueReporter) {
    51  
    52  	var messages []string
    53  	var subMessage string
    54  	for _, condition := range machineSet.Status.Conditions {
    55  		if condition.Status != corev1.ConditionTrue {
    56  			switch condition.Type {
    57  			case "MachinesCreated":
    58  				subMessage = "machines are not created"
    59  			case "MachinesReady":
    60  				subMessage = "machines are not ready"
    61  			case "Resized":
    62  				subMessage = "is not resized"
    63  			case "Ready":
    64  				subMessage = "is not ready"
    65  			default:
    66  				continue
    67  			}
    68  			// Add a message for the issue
    69  			var message string
    70  			if len(condition.Reason) == 0 {
    71  				message = fmt.Sprintf("\t%s", subMessage)
    72  			} else {
    73  				message = fmt.Sprintf("\t%s - reason is %s", subMessage, condition.Reason)
    74  			}
    75  			messages = append([]string{message}, messages...)
    76  
    77  		}
    78  	}
    79  
    80  	if len(messages) > 0 {
    81  		messages = append([]string{fmt.Sprintf("%q resource %q in namespace %q", machineSetsResource, machineSet.Name, machineSet.Namespace)}, messages...)
    82  		issueReporter.AddKnownIssueMessagesFiles(report.ClusterAPIClusterIssues, clusterRoot, messages, []string{})
    83  	}
    84  }