...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/condition.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s
    16  
    17  import (
    18  	"encoding/json"
    19  	"time"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  func NewCustomReadyCondition(status v1.ConditionStatus, rs, msg string) v1alpha1.Condition {
    28  	return v1alpha1.Condition{
    29  		LastTransitionTime: metav1.Now().Format(time.RFC3339),
    30  		Type:               v1alpha1.ReadyConditionType,
    31  		Status:             status,
    32  		Reason:             rs,
    33  		Message:            msg,
    34  	}
    35  }
    36  
    37  func NewReadyCondition() v1alpha1.Condition {
    38  	return v1alpha1.Condition{
    39  		LastTransitionTime: metav1.Now().Format(time.RFC3339),
    40  		Type:               v1alpha1.ReadyConditionType,
    41  	}
    42  }
    43  
    44  func NewReadyConditionWithError(err error) v1alpha1.Condition {
    45  	var readyCondition v1alpha1.Condition
    46  	if errWithReason, ok := err.(ErrorWithReason); ok {
    47  		readyCondition = NewCustomReadyCondition(v1.ConditionFalse, errWithReason.Reason, errWithReason.Message)
    48  	} else {
    49  		readyCondition = NewReadyCondition()
    50  		readyCondition.Status = v1.ConditionFalse
    51  		readyCondition.Message = err.Error()
    52  	}
    53  	return readyCondition
    54  }
    55  
    56  func ConditionsEqualIgnoreTransitionTime(c1, c2 v1alpha1.Condition) bool {
    57  	return c1.Message == c2.Message &&
    58  		c1.Reason == c2.Reason &&
    59  		c1.Status == c2.Status &&
    60  		c1.Type == c2.Type
    61  }
    62  
    63  func ConditionSlicesEqual(conditions1, conditions2 []v1alpha1.Condition) bool {
    64  	if len(conditions1) != len(conditions2) {
    65  		return false
    66  	}
    67  	for i, c1 := range conditions1 {
    68  		c2 := conditions2[i]
    69  		if !ConditionsEqualIgnoreTransitionTime(c1, c2) {
    70  			return false
    71  		}
    72  	}
    73  	return true
    74  }
    75  
    76  func MarshalAsConditionsSlice(obj []interface{}) ([]v1alpha1.Condition, error) {
    77  	b, err := json.Marshal(obj)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	ret := make([]v1alpha1.Condition, 0)
    82  	if err := json.Unmarshal(b, &ret); err != nil {
    83  		return nil, err
    84  	}
    85  	return ret, nil
    86  }
    87  

View as plain text