...

Source file src/k8s.io/kubernetes/pkg/controller/storageversionmigrator/util.go

Documentation: k8s.io/kubernetes/pkg/controller/storageversionmigrator

     1  /*
     2  Copyright 2024 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 storageversionmigrator
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	svmv1alpha1 "k8s.io/api/storagemigration/v1alpha1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  )
    29  
    30  func convertResourceVersionToInt(rv string) (int64, error) {
    31  	resourceVersion, err := strconv.ParseInt(rv, 10, 64)
    32  	if err != nil {
    33  		return 0, fmt.Errorf("failed to parse resource version %q: %w", rv, err)
    34  	}
    35  
    36  	return resourceVersion, nil
    37  }
    38  
    39  func getGVRFromResource(svm *svmv1alpha1.StorageVersionMigration) schema.GroupVersionResource {
    40  	return schema.GroupVersionResource{
    41  		Group:    svm.Spec.Resource.Group,
    42  		Version:  svm.Spec.Resource.Version,
    43  		Resource: svm.Spec.Resource.Resource,
    44  	}
    45  }
    46  
    47  // IsConditionTrue returns true if the StorageVersionMigration has the given condition
    48  // It is exported for use in tests
    49  func IsConditionTrue(svm *svmv1alpha1.StorageVersionMigration, conditionType svmv1alpha1.MigrationConditionType) bool {
    50  	return indexOfCondition(svm, conditionType) != -1
    51  }
    52  
    53  func indexOfCondition(svm *svmv1alpha1.StorageVersionMigration, conditionType svmv1alpha1.MigrationConditionType) int {
    54  	for i, c := range svm.Status.Conditions {
    55  		if c.Type == conditionType && c.Status == corev1.ConditionTrue {
    56  			return i
    57  		}
    58  	}
    59  	return -1
    60  }
    61  
    62  func setStatusConditions(
    63  	toBeUpdatedSVM *svmv1alpha1.StorageVersionMigration,
    64  	conditionType svmv1alpha1.MigrationConditionType,
    65  	reason string,
    66  ) *svmv1alpha1.StorageVersionMigration {
    67  	if !IsConditionTrue(toBeUpdatedSVM, conditionType) {
    68  		if conditionType == svmv1alpha1.MigrationSucceeded || conditionType == svmv1alpha1.MigrationFailed {
    69  			runningConditionIdx := indexOfCondition(toBeUpdatedSVM, svmv1alpha1.MigrationRunning)
    70  			if runningConditionIdx != -1 {
    71  				toBeUpdatedSVM.Status.Conditions[runningConditionIdx].Status = corev1.ConditionFalse
    72  			}
    73  		}
    74  
    75  		toBeUpdatedSVM.Status.Conditions = append(toBeUpdatedSVM.Status.Conditions, svmv1alpha1.MigrationCondition{
    76  			Type:           conditionType,
    77  			Status:         corev1.ConditionTrue,
    78  			LastUpdateTime: metav1.Now(),
    79  			Reason:         reason,
    80  		})
    81  	}
    82  
    83  	return toBeUpdatedSVM
    84  }
    85  

View as plain text