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