github.com/verrazzano/verrazzano@v1.7.0/tools/vz/pkg/analysis/internal/util/cluster/rancher/nodes.go (about)

     1  // Copyright (c) 2023, 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 rancher
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  
    10  	"github.com/verrazzano/verrazzano/tools/vz/pkg/analysis/internal/util/files"
    11  	"github.com/verrazzano/verrazzano/tools/vz/pkg/analysis/internal/util/report"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  )
    15  
    16  const nodeResource = "node.management.cattle.io"
    17  
    18  // Minimal definition of object that only contains the fields that will be analyzed
    19  type nodeList struct {
    20  	metav1.TypeMeta `json:",inline"`
    21  	metav1.ListMeta `json:"metadata,omitempty"`
    22  	Items           []node `json:"items"`
    23  }
    24  type node struct {
    25  	metav1.TypeMeta   `json:",inline"`
    26  	metav1.ObjectMeta `json:"metadata,omitempty"`
    27  	Status            cattleStatus `json:"status,omitempty"`
    28  }
    29  
    30  // AnalyzeNodes - analyze the status of Node objects
    31  func AnalyzeNodes(clusterRoot string, namespace string, issueReporter *report.IssueReporter) error {
    32  	resourceRoot := clusterRoot
    33  	if len(namespace) != 0 {
    34  		resourceRoot = filepath.Join(clusterRoot, namespace)
    35  	}
    36  
    37  	list := &nodeList{}
    38  	err := files.UnmarshallFileInClusterRoot(resourceRoot, fmt.Sprintf("%s.json", nodeResource), list)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	for _, node := range list.Items {
    44  		err = analyzeNode(clusterRoot, node, issueReporter)
    45  		if err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // analyzeNode - analyze a single Node object and report any issues
    54  func analyzeNode(clusterRoot string, node node, issueReporter *report.IssueReporter) error {
    55  
    56  	var messages []string
    57  	var subMessage string
    58  	for _, condition := range node.Status.Conditions {
    59  		if condition.Status != corev1.ConditionTrue {
    60  			switch condition.Type {
    61  			case "Initialized":
    62  				subMessage = "is not initialized"
    63  			case "Provisioned":
    64  				subMessage = "is not provisioned"
    65  			case "Updated":
    66  				subMessage = "is not updated"
    67  			case "Registered":
    68  				subMessage = "is not registered with Kubernetes"
    69  			case "Removed":
    70  				subMessage = "is not removed"
    71  			case "Saved":
    72  				subMessage = "is not saved"
    73  			case "Ready":
    74  				subMessage = "is not ready"
    75  			case "Drained":
    76  				subMessage = "is not drained"
    77  			case "Upgraded":
    78  				subMessage = "is not upgraded"
    79  			default:
    80  				continue
    81  			}
    82  			// Add a message for the issue
    83  			reason := ""
    84  			msg := ""
    85  			if len(condition.Reason) > 0 {
    86  				reason = fmt.Sprintf(", reason is %q", condition.Reason)
    87  			}
    88  			if len(condition.Message) > 0 {
    89  				msg = fmt.Sprintf(", message is %q", condition.Message)
    90  			}
    91  			message := fmt.Sprintf("\t%s %s%s", subMessage, reason, msg)
    92  			messages = append([]string{message}, messages...)
    93  		}
    94  	}
    95  
    96  	if len(messages) > 0 {
    97  		messages = append([]string{fmt.Sprintf("Rancher %s resource %q in namespace %q", nodeResource, node.Name, node.Namespace)}, messages...)
    98  		issueReporter.AddKnownIssueMessagesFiles(report.RancherIssues, clusterRoot, messages, []string{})
    99  	}
   100  
   101  	return nil
   102  }