...

Source file src/sigs.k8s.io/cli-utils/pkg/kstatus/status/util.go

Documentation: sigs.k8s.io/cli-utils/pkg/kstatus/status

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package status
     5  
     6  import (
     7  	"strings"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  	apiunstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  )
    13  
    14  // newReconcilingCondition creates an reconciling condition with the given
    15  // reason and message.
    16  func newReconcilingCondition(reason, message string) Condition {
    17  	return Condition{
    18  		Type:    ConditionReconciling,
    19  		Status:  corev1.ConditionTrue,
    20  		Reason:  reason,
    21  		Message: message,
    22  	}
    23  }
    24  
    25  func newStalledCondition(reason, message string) Condition {
    26  	return Condition{
    27  		Type:    ConditionStalled,
    28  		Status:  corev1.ConditionTrue,
    29  		Reason:  reason,
    30  		Message: message,
    31  	}
    32  }
    33  
    34  // newInProgressStatus creates a status Result with the InProgress status
    35  // and an InProgress condition.
    36  func newInProgressStatus(reason, message string) *Result {
    37  	return &Result{
    38  		Status:     InProgressStatus,
    39  		Message:    message,
    40  		Conditions: []Condition{newReconcilingCondition(reason, message)},
    41  	}
    42  }
    43  
    44  func newFailedStatus(reason, message string) *Result {
    45  	return &Result{
    46  		Status:     FailedStatus,
    47  		Message:    message,
    48  		Conditions: []Condition{newStalledCondition(reason, message)},
    49  	}
    50  }
    51  
    52  // ObjWithConditions Represent meta object with status.condition array
    53  type ObjWithConditions struct {
    54  	// Status as expected to be present in most compliant kubernetes resources
    55  	Status ConditionStatus `json:"status" yaml:"status"`
    56  }
    57  
    58  // ConditionStatus represent status with condition array
    59  type ConditionStatus struct {
    60  	// Array of Conditions as expected to be present in kubernetes resources
    61  	Conditions []BasicCondition `json:"conditions" yaml:"conditions"`
    62  }
    63  
    64  // BasicCondition fields that are expected in a condition
    65  type BasicCondition struct {
    66  	// Type Condition type
    67  	Type string `json:"type" yaml:"type"`
    68  	// Status is one of True,False,Unknown
    69  	Status corev1.ConditionStatus `json:"status" yaml:"status"`
    70  	// Reason simple single word reason in CamleCase
    71  	// +optional
    72  	Reason string `json:"reason,omitempty" yaml:"reason"`
    73  	// Message human readable reason
    74  	// +optional
    75  	Message string `json:"message,omitempty" yaml:"message"`
    76  }
    77  
    78  // GetObjectWithConditions return typed object
    79  func GetObjectWithConditions(in map[string]interface{}) (*ObjWithConditions, error) {
    80  	var out = new(ObjWithConditions)
    81  	err := runtime.DefaultUnstructuredConverter.FromUnstructured(in, out)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	return out, nil
    86  }
    87  
    88  func hasConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) bool {
    89  	_, found := getConditionWithStatus(conditions, conditionType, status)
    90  	return found
    91  }
    92  
    93  func getConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) (BasicCondition, bool) {
    94  	for _, c := range conditions {
    95  		if c.Type == conditionType && c.Status == status {
    96  			return c, true
    97  		}
    98  	}
    99  	return BasicCondition{}, false
   100  }
   101  
   102  // GetStringField return field as string defaulting to value if not found
   103  func GetStringField(obj map[string]interface{}, fieldPath string, defaultValue string) string {
   104  	var rv = defaultValue
   105  
   106  	fields := strings.Split(fieldPath, ".")
   107  	if fields[0] == "" {
   108  		fields = fields[1:]
   109  	}
   110  
   111  	val, found, err := apiunstructured.NestedFieldNoCopy(obj, fields...)
   112  	if !found || err != nil {
   113  		return rv
   114  	}
   115  
   116  	if v, ok := val.(string); ok {
   117  		return v
   118  	}
   119  	return rv
   120  }
   121  
   122  // GetIntField return field as string defaulting to value if not found
   123  func GetIntField(obj map[string]interface{}, fieldPath string, defaultValue int) int {
   124  	fields := strings.Split(fieldPath, ".")
   125  	if fields[0] == "" {
   126  		fields = fields[1:]
   127  	}
   128  
   129  	val, found, err := apiunstructured.NestedFieldNoCopy(obj, fields...)
   130  	if !found || err != nil {
   131  		return defaultValue
   132  	}
   133  
   134  	switch v := val.(type) {
   135  	case int:
   136  		return v
   137  	case int32:
   138  		return int(v)
   139  	case int64:
   140  		return int(v)
   141  	}
   142  	return defaultValue
   143  }
   144  

View as plain text