Skip to content

Commit a6bff69

Browse files
committed
Fix migration integration test
Signed-off-by: Erik Godding Boye <[email protected]>
1 parent 8546cbb commit a6bff69

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

pkg/bundle/controller/bundle_controller.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ package controller
2020
import (
2121
"context"
2222
"fmt"
23+
"time"
2324

2425
"k8s.io/apimachinery/pkg/api/errors"
2526
"k8s.io/apimachinery/pkg/api/meta"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/util/json"
29+
metav1ac "k8s.io/client-go/applyconfigurations/meta/v1"
30+
"k8s.io/utils/ptr"
2831
ctrl "sigs.k8s.io/controller-runtime"
2932
"sigs.k8s.io/controller-runtime/pkg/client"
3033
logf "sigs.k8s.io/controller-runtime/pkg/log"
3134

3235
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
3336
trustmanagerapi "github.com/cert-manager/trust-manager/pkg/apis/trustmanager/v1alpha2"
37+
trustapiac "github.com/cert-manager/trust-manager/pkg/applyconfigurations/trust/v1alpha1"
3438
trustmanagerac "github.com/cert-manager/trust-manager/pkg/applyconfigurations/trustmanager/v1alpha2"
3539
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
3640
)
@@ -70,26 +74,26 @@ func (r *BundleReconciler) reconcile(ctx context.Context, bundle *trustapi.Bundl
7074
if err := r.unmanageClusterBundle(ctx, clusterBundle); client.IgnoreNotFound(err) != nil {
7175
return fmt.Errorf("failed to unmanaged ClusterBundle: %w", err)
7276
}
73-
return r.applyBundleCondition(ctx, bundle, metav1.Condition{
74-
Type: trustapi.BundleConditionMigrated,
75-
Status: metav1.ConditionTrue,
76-
Reason: "MigrationDetected",
77-
Message: "Bundle is migrated to ClusterBundle by user; this resource can be safely deleted.",
78-
ObservedGeneration: bundle.Generation,
79-
})
77+
return r.applyBundleCondition(ctx, bundle, metav1ac.Condition().
78+
WithType(trustapi.BundleConditionMigrated).
79+
WithStatus(metav1.ConditionTrue).
80+
WithReason("MigrationDetected").
81+
WithMessage("Bundle is migrated to ClusterBundle by user; this resource can be safely deleted.").
82+
WithObservedGeneration(bundle.Generation),
83+
)
8084
}
8185

8286
if err := r.applyClusterBundle(ctx, bundle); err != nil {
8387
return fmt.Errorf("failed to apply ClusterBundle: %w", err)
8488
}
8589

86-
return r.applyBundleCondition(ctx, bundle, metav1.Condition{
87-
Type: trustapi.BundleConditionDeprecated,
88-
Status: metav1.ConditionTrue,
89-
Reason: "MigrationRequired",
90-
Message: "Bundle is deprecated; please migrate to ClusterBundle.",
91-
ObservedGeneration: bundle.Generation,
92-
})
90+
return r.applyBundleCondition(ctx, bundle, metav1ac.Condition().
91+
WithType(trustapi.BundleConditionDeprecated).
92+
WithStatus(metav1.ConditionTrue).
93+
WithReason("MigrationRequired").
94+
WithMessage("Bundle is deprecated; please migrate to ClusterBundle.").
95+
WithObservedGeneration(bundle.Generation),
96+
)
9397
}
9498

9599
func (r *BundleReconciler) applyClusterBundle(ctx context.Context, bundle *trustapi.Bundle) error {
@@ -108,18 +112,26 @@ func (r *BundleReconciler) applyClusterBundle(ctx context.Context, bundle *trust
108112
return r.Patch(ctx, clusterBundle, ssa_client.ApplyPatch{Patch: encodedPatch}, ssa_client.FieldManager, client.ForceOwnership)
109113
}
110114

111-
func (r *BundleReconciler) applyBundleCondition(ctx context.Context, bundle *trustapi.Bundle, condition metav1.Condition) error {
112-
meta.SetStatusCondition(&bundle.Status.Conditions, condition)
113-
114-
bundleStatus := &trustmanagerapi.BundleStatus{
115-
Conditions: []metav1.Condition{*meta.FindStatusCondition(bundle.Status.Conditions, condition.Type)},
115+
func (r *BundleReconciler) applyBundleCondition(ctx context.Context, bundle *trustapi.Bundle, condition *metav1ac.ConditionApplyConfiguration) error {
116+
existingCondition := meta.FindStatusCondition(bundle.Status.Conditions, *condition.Type)
117+
if existingCondition != nil && existingCondition.Status == *condition.Status {
118+
condition.LastTransitionTime = &existingCondition.LastTransitionTime
119+
} else {
120+
condition.LastTransitionTime = ptr.To(metav1.NewTime(time.Now()))
116121
}
117-
b, patch, err := ssa_client.GenerateBundleStatusPatch(bundle.Name, bundleStatus)
122+
123+
ac := trustapiac.Bundle(bundle.Name).
124+
WithStatus(
125+
trustapiac.BundleStatus().
126+
WithConditions(condition),
127+
)
128+
129+
encodedPatch, err := json.Marshal(ac)
118130
if err != nil {
119-
return fmt.Errorf("failed to generate bundle status patch: %w", err)
131+
return fmt.Errorf("failed to marshal Bundle status patch: %w", err)
120132
}
121133

122-
return r.Status().Patch(ctx, b, patch, ssa_client.FieldManager, client.ForceOwnership)
134+
return r.Status().Patch(ctx, bundle, ssa_client.ApplyPatch{Patch: encodedPatch}, ssa_client.FieldManager, client.ForceOwnership)
123135
}
124136

125137
func (r *BundleReconciler) unmanageClusterBundle(ctx context.Context, cb *trustmanagerapi.ClusterBundle) error {

0 commit comments

Comments
 (0)