...
1
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
48
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