github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/cluster/rancher/gitjobs.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 gitJobResource = "gitjob.gitjob.cattle.io"
    17  
    18  // Minimal definition of object that only contains the fields that will be analyzed
    19  type gitJobList struct {
    20  	metav1.TypeMeta `json:",inline"`
    21  	metav1.ListMeta `json:"metadata,omitempty"`
    22  	Items           []gitJob `json:"items"`
    23  }
    24  type gitJob struct {
    25  	metav1.TypeMeta   `json:",inline"`
    26  	metav1.ObjectMeta `json:"metadata,omitempty"`
    27  	Status            gitJobStatus `json:"status,omitempty"`
    28  }
    29  type gitJobStatus struct {
    30  	Conditions []cattleCondition `json:"conditions,omitempty"`
    31  	JobStatus  string            `json:"jobStatus,omitempty"`
    32  }
    33  
    34  // AnalyzeGitJobs - analyze the status of GitJob objects
    35  func AnalyzeGitJobs(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 := &gitJobList{}
    42  	err := files.UnmarshallFileInClusterRoot(resourceRoot, fmt.Sprintf("%s.json", gitJobResource), list)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	for _, job := range list.Items {
    48  		err = analyzeGitJob(clusterRoot, job, issueReporter)
    49  		if err != nil {
    50  			return err
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // analyzeGitJob - analyze a single GitJob and report any issues
    58  func analyzeGitJob(clusterRoot string, job gitJob, issueReporter *report.IssueReporter) error {
    59  
    60  	var messages []string
    61  	var subMessage string
    62  	status := job.Status
    63  	for _, condition := range status.Conditions {
    64  		switch condition.Type {
    65  		case "Stalled":
    66  			if condition.Status == corev1.ConditionTrue {
    67  				subMessage = "is stalled"
    68  			}
    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  	if len(messages) > 0 {
    86  		if len(status.JobStatus) > 0 {
    87  			messages = append([]string{fmt.Sprintf("\tthe Rancher GitJob status is %s", status.JobStatus)}, messages...)
    88  		}
    89  		messages = append([]string{fmt.Sprintf("Rancher %s resource %q in namespace %s", gitJobResource, job.Name, job.Namespace)}, messages...)
    90  		issueReporter.AddKnownIssueMessagesFiles(report.RancherIssues, clusterRoot, messages, []string{})
    91  	}
    92  
    93  	return nil
    94  }