github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/cluster/rancher/clusterregistration.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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  )
    14  
    15  const clusterRegistrationResource = "clusterregistration.fleet.cattle.io"
    16  
    17  // Minimal definition of object that only contains the fields that will be analyzed
    18  type clusterRegistrationList struct {
    19  	metav1.TypeMeta `json:",inline"`
    20  	metav1.ListMeta `json:"metadata,omitempty"`
    21  	Items           []clusterRegistration `json:"items"`
    22  }
    23  type clusterRegistration struct {
    24  	metav1.TypeMeta   `json:",inline"`
    25  	metav1.ObjectMeta `json:"metadata,omitempty"`
    26  	Status            clusterRegistrationStatus `json:"status,omitempty"`
    27  }
    28  type clusterRegistrationStatus struct {
    29  	ClusterName string `json:"clusterName,omitempty"`
    30  	Granted     bool   `json:"granted,omitempty"`
    31  }
    32  
    33  // AnalyzeClusterRegistrations - analyze the status of ClusterRegistration objects
    34  func AnalyzeClusterRegistrations(clusterRoot string, namespace string, issueReporter *report.IssueReporter) error {
    35  	resourceRoot := clusterRoot
    36  	if len(namespace) != 0 {
    37  		resourceRoot = filepath.Join(clusterRoot, namespace)
    38  	}
    39  
    40  	list := &clusterRegistrationList{}
    41  	err := files.UnmarshallFileInClusterRoot(resourceRoot, fmt.Sprintf("%s.json", clusterRegistrationResource), list)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	for _, clusterRegistration := range list.Items {
    47  		err = analyzeClusterRegistration(clusterRoot, clusterRegistration, issueReporter)
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // analyzeClusterRegistration - analyze a single ClusterRegistration and report any issues
    57  func analyzeClusterRegistration(clusterRoot string, clusterRegistration clusterRegistration, issueReporter *report.IssueReporter) error {
    58  	var messages []string
    59  
    60  	if !clusterRegistration.Status.Granted {
    61  		message := fmt.Sprintf("Rancher %s resource %q in namespace %s is not granted for cluster %s", clusterRegistrationResource, clusterRegistration.Name, clusterRegistration.Namespace, clusterRegistration.Status.ClusterName)
    62  		messages = append([]string{message}, messages...)
    63  	}
    64  
    65  	if len(messages) > 0 {
    66  		issueReporter.AddKnownIssueMessagesFiles(report.RancherIssues, clusterRoot, messages, []string{})
    67  	}
    68  
    69  	return nil
    70  }