k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/pkg/apis/ee.kubermatic/v1/helper/helper.go (about)

     1  /*
     2  Copyright 2023 The Kubermatic Kubernetes Platform contributors.
     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 helper
    18  
    19  import (
    20  	kubermaticeev1 "k8c.io/api/v3/pkg/apis/ee.kubermatic/v1"
    21  	kubermaticv1 "k8c.io/api/v3/pkg/apis/kubermatic/v1"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  // SetClusterCondition sets a condition on the given cluster using the provided type, status,
    29  // reason and message. It also adds the Kubermatic version and timestamps.
    30  func SetClusterCondition(
    31  	c *kubermaticeev1.Cluster,
    32  	kkpVersion string,
    33  	conditionType kubermaticv1.ClusterConditionType,
    34  	status corev1.ConditionStatus,
    35  	reason string,
    36  	message string,
    37  ) {
    38  	newCondition := kubermaticv1.ClusterCondition{
    39  		Status:            status,
    40  		KubermaticVersion: kkpVersion,
    41  		Reason:            reason,
    42  		Message:           message,
    43  	}
    44  
    45  	oldCondition, hadCondition := c.Status.Conditions[conditionType]
    46  	if hadCondition {
    47  		conditionCopy := oldCondition.DeepCopy()
    48  
    49  		// Reset the times before comparing
    50  		conditionCopy.LastHeartbeatTime.Reset()
    51  		conditionCopy.LastTransitionTime.Reset()
    52  
    53  		if apiequality.Semantic.DeepEqual(*conditionCopy, newCondition) {
    54  			return
    55  		}
    56  	}
    57  
    58  	now := metav1.Now()
    59  	newCondition.LastHeartbeatTime = now
    60  	newCondition.LastTransitionTime = oldCondition.LastTransitionTime
    61  	if hadCondition && oldCondition.Status != status {
    62  		newCondition.LastTransitionTime = now
    63  	}
    64  
    65  	if c.Status.Conditions == nil {
    66  		c.Status.Conditions = map[kubermaticv1.ClusterConditionType]kubermaticv1.ClusterCondition{}
    67  	}
    68  	c.Status.Conditions[conditionType] = newCondition
    69  }
    70  
    71  // SetSeedCondition sets a condition on the given seed using the provided type, status,
    72  // reason and message.
    73  func SetSeedCondition(seed *kubermaticeev1.Seed, conditionType kubermaticeev1.SeedConditionType, status corev1.ConditionStatus, reason string, message string) {
    74  	newCondition := kubermaticeev1.SeedCondition{
    75  		Status:  status,
    76  		Reason:  reason,
    77  		Message: message,
    78  	}
    79  
    80  	oldCondition, hadCondition := seed.Status.Conditions[conditionType]
    81  	if hadCondition {
    82  		conditionCopy := oldCondition.DeepCopy()
    83  
    84  		// Reset the times before comparing
    85  		conditionCopy.LastHeartbeatTime.Reset()
    86  		conditionCopy.LastTransitionTime.Reset()
    87  
    88  		if apiequality.Semantic.DeepEqual(*conditionCopy, newCondition) {
    89  			return
    90  		}
    91  	}
    92  
    93  	now := metav1.Now()
    94  	newCondition.LastHeartbeatTime = now
    95  	newCondition.LastTransitionTime = oldCondition.LastTransitionTime
    96  	if hadCondition && oldCondition.Status != status {
    97  		newCondition.LastTransitionTime = now
    98  	}
    99  
   100  	if seed.Status.Conditions == nil {
   101  		seed.Status.Conditions = map[kubermaticeev1.SeedConditionType]kubermaticeev1.SeedCondition{}
   102  	}
   103  	seed.Status.Conditions[conditionType] = newCondition
   104  }