sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/tree/util.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tree
    18  
    19  import (
    20  	"fmt"
    21  	"sort"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    28  	"sigs.k8s.io/cluster-api/util/conditions"
    29  )
    30  
    31  // GetReadyCondition returns the ReadyCondition for an object, if defined.
    32  func GetReadyCondition(obj client.Object) *clusterv1.Condition {
    33  	getter := objToGetter(obj)
    34  	if getter == nil {
    35  		return nil
    36  	}
    37  	return conditions.Get(getter, clusterv1.ReadyCondition)
    38  }
    39  
    40  // GetOtherConditions returns the other conditions (all the conditions except ready) for an object, if defined.
    41  func GetOtherConditions(obj client.Object) []*clusterv1.Condition {
    42  	getter := objToGetter(obj)
    43  	if getter == nil {
    44  		return nil
    45  	}
    46  	var conditions []*clusterv1.Condition
    47  	for _, c := range getter.GetConditions() {
    48  		c := c
    49  		if c.Type != clusterv1.ReadyCondition {
    50  			conditions = append(conditions, &c)
    51  		}
    52  	}
    53  	sort.Slice(conditions, func(i, j int) bool {
    54  		return conditions[i].Type < conditions[j].Type
    55  	})
    56  	return conditions
    57  }
    58  
    59  func setReadyCondition(obj client.Object, ready *clusterv1.Condition) {
    60  	setter := objToSetter(obj)
    61  	if setter == nil {
    62  		return
    63  	}
    64  	conditions.Set(setter, ready)
    65  }
    66  
    67  func objToGetter(obj client.Object) conditions.Getter {
    68  	if getter, ok := obj.(conditions.Getter); ok {
    69  		return getter
    70  	}
    71  
    72  	objUnstructured, ok := obj.(*unstructured.Unstructured)
    73  	if !ok {
    74  		return nil
    75  	}
    76  	getter := conditions.UnstructuredGetter(objUnstructured)
    77  	return getter
    78  }
    79  
    80  func objToSetter(obj client.Object) conditions.Setter {
    81  	if setter, ok := obj.(conditions.Setter); ok {
    82  		return setter
    83  	}
    84  
    85  	objUnstructured, ok := obj.(*unstructured.Unstructured)
    86  	if !ok {
    87  		return nil
    88  	}
    89  	setter := conditions.UnstructuredSetter(objUnstructured)
    90  	return setter
    91  }
    92  
    93  // VirtualObject returns a new virtual object.
    94  func VirtualObject(namespace, kind, name string) *unstructured.Unstructured {
    95  	gk := "virtual.cluster.x-k8s.io/v1beta1"
    96  	return &unstructured.Unstructured{
    97  		Object: map[string]interface{}{
    98  			"apiVersion": gk,
    99  			"kind":       kind,
   100  			"metadata": map[string]interface{}{
   101  				"namespace": namespace,
   102  				"name":      name,
   103  				"annotations": map[string]interface{}{
   104  					VirtualObjectAnnotation: "True",
   105  				},
   106  				"uid": fmt.Sprintf("%s, Kind=%s, %s/%s", gk, kind, namespace, name),
   107  			},
   108  		},
   109  	}
   110  }
   111  
   112  // ObjectReferenceObject returns a new object referenced by the objectRef.
   113  func ObjectReferenceObject(objectRef *corev1.ObjectReference) *unstructured.Unstructured {
   114  	return &unstructured.Unstructured{
   115  		Object: map[string]interface{}{
   116  			"apiVersion": objectRef.APIVersion,
   117  			"kind":       objectRef.Kind,
   118  			"metadata": map[string]interface{}{
   119  				"namespace": objectRef.Namespace,
   120  				"name":      objectRef.Name,
   121  				"uid":       fmt.Sprintf("%s, Kind=%s, %s/%s", objectRef.APIVersion, objectRef.Kind, objectRef.Namespace, objectRef.Name),
   122  			},
   123  		},
   124  	}
   125  }