github.com/verrazzano/verrazzano@v1.7.0/tools/vz/pkg/analysis/internal/util/cluster/rancher/clustergroups.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 clusterGroupResource = "clustergroup.fleet.cattle.io"
    17  
    18  // Minimal definition of object that only contains the fields that will be analyzed
    19  type clusterGroupList struct {
    20  	metav1.TypeMeta `json:",inline"`
    21  	metav1.ListMeta `json:"metadata,omitempty"`
    22  	Items           []clusterGroup `json:"items"`
    23  }
    24  type clusterGroup struct {
    25  	metav1.TypeMeta   `json:",inline"`
    26  	metav1.ObjectMeta `json:"metadata,omitempty"`
    27  	Status            clusterGroupStatus `json:"status,omitempty"`
    28  }
    29  type clusterGroupStatus struct {
    30  	NonReadyClusterCount int               `json:"nonReadyClusterCount,omitempty"`
    31  	Conditions           []cattleCondition `json:"conditions,omitempty"`
    32  }
    33  
    34  // AnalyzeClusterGroups - analyze the status of ClusterGroup objects
    35  func AnalyzeClusterGroups(clusterRoot string, namespace string, issueReporter *report.IssueReporter) error {
    36  	resourceRoot := clusterRoot
    37  	if len(namespace) != 0 {
    38  		resourceRoot = filepath.Join(clusterRoot, namespace)
    39  	}
    40  
    41  	list := &clusterGroupList{}
    42  	err := files.UnmarshallFileInClusterRoot(resourceRoot, fmt.Sprintf("%s.json", clusterGroupResource), list)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	for _, clusterGroup := range list.Items {
    48  		err = analyzeClusterGroup(clusterRoot, clusterGroup, issueReporter)
    49  		if err != nil {
    50  			return err
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // analyzeClusterGroup - analyze a single ClusterGroup and report any issues
    58  func analyzeClusterGroup(clusterRoot string, clusterGroup clusterGroup, issueReporter *report.IssueReporter) error {
    59  
    60  	var messages []string
    61  	var subMessage string
    62  	for _, condition := range clusterGroup.Status.Conditions {
    63  		if condition.Status != corev1.ConditionTrue {
    64  			switch condition.Type {
    65  			case "Processed":
    66  				subMessage = "is not processed"
    67  			case "Ready":
    68  				subMessage = "is not ready"
    69  			default:
    70  				continue
    71  			}
    72  			// Add a message for the issue
    73  			reason := ""
    74  			msg := ""
    75  			if len(condition.Reason) > 0 {
    76  				reason = fmt.Sprintf(", reason is %q", condition.Reason)
    77  			}
    78  			if len(condition.Message) > 0 {
    79  				msg = fmt.Sprintf(", message is %q", condition.Message)
    80  			}
    81  			message := fmt.Sprintf("\t%s %s%s", subMessage, reason, msg)
    82  			messages = append([]string{message}, messages...)
    83  		}
    84  	}
    85  
    86  	if clusterGroup.Status.NonReadyClusterCount > 0 {
    87  		message := fmt.Sprintf("\thas %d clusters not ready", clusterGroup.Status.NonReadyClusterCount)
    88  		messages = append([]string{message}, messages...)
    89  	}
    90  	if len(messages) > 0 {
    91  		messages = append([]string{fmt.Sprintf("Rancher %s resource %q", clusterGroupResource, clusterGroup.Name)}, messages...)
    92  		issueReporter.AddKnownIssueMessagesFiles(report.RancherIssues, clusterRoot, messages, []string{})
    93  	}
    94  
    95  	return nil
    96  }