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