k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/pkg/apis/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  	kubermaticv1 "k8c.io/api/v3/pkg/apis/kubermatic/v1"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  // SetClusterCondition sets a condition on the given cluster using the provided type, status,
    28  // reason and message. It also adds the Kubermatic version and timestamps.
    29  func SetClusterCondition(
    30  	c *kubermaticv1.Cluster,
    31  	kkpVersion string,
    32  	conditionType kubermaticv1.ClusterConditionType,
    33  	status corev1.ConditionStatus,
    34  	reason string,
    35  	message string,
    36  ) {
    37  	newCondition := kubermaticv1.ClusterCondition{
    38  		Status:            status,
    39  		KubermaticVersion: kkpVersion,
    40  		Reason:            reason,
    41  		Message:           message,
    42  	}
    43  
    44  	oldCondition, hadCondition := c.Status.Conditions[conditionType]
    45  	if hadCondition {
    46  		conditionCopy := oldCondition.DeepCopy()
    47  
    48  		// Reset the times before comparing
    49  		conditionCopy.LastHeartbeatTime.Reset()
    50  		conditionCopy.LastTransitionTime.Reset()
    51  
    52  		if apiequality.Semantic.DeepEqual(*conditionCopy, newCondition) {
    53  			return
    54  		}
    55  	}
    56  
    57  	now := metav1.Now()
    58  	newCondition.LastHeartbeatTime = now
    59  	newCondition.LastTransitionTime = oldCondition.LastTransitionTime
    60  	if hadCondition && oldCondition.Status != status {
    61  		newCondition.LastTransitionTime = now
    62  	}
    63  
    64  	if c.Status.Conditions == nil {
    65  		c.Status.Conditions = map[kubermaticv1.ClusterConditionType]kubermaticv1.ClusterCondition{}
    66  	}
    67  	c.Status.Conditions[conditionType] = newCondition
    68  }