Skip to content

Commit

Permalink
feat: do not follower scheduling when dry run
Browse files Browse the repository at this point in the history
Signed-off-by: shentiecheng <[email protected]>
  • Loading branch information
Poor12 committed Dec 12, 2023
1 parent 4200c1b commit ec4b269
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 80 deletions.
3 changes: 2 additions & 1 deletion pkg/controllers/follower/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ func (c *Controller) inferFollowers(
sourceGK schema.GroupKind,
fedObj fedcorev1a1.GenericFederatedObject,
) (sets.Set[FollowerReference], error) {
if fedObj.GetAnnotations()[common.EnableFollowerSchedulingAnnotation] != common.AnnotationValueTrue {
if fedObj.GetAnnotations()[common.EnableFollowerSchedulingAnnotation] != common.AnnotationValueTrue ||
fedObj.GetAnnotations()[common.NoSchedulingAnnotation] == common.AnnotationValueTrue || skipSync(fedObj) {

Check warning on line 357 in pkg/controllers/follower/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/follower/controller.go#L356-L357

Added lines #L356 - L357 were not covered by tests
// follower scheduling is not enabled
return nil, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/controllers/follower/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,12 @@ func getFollowersFromPod(

return followers
}

func skipSync(federatedObject fedcorev1a1.GenericFederatedObject) bool {
// if workloads have been propagated to member clusters,
// sync is always not skipped.
if len(federatedObject.GetStatus().Clusters) > 0 {
return false
}
return federatedObject.GetAnnotations()[common.DryRunAnnotation] == common.AnnotationValueTrue
}
22 changes: 22 additions & 0 deletions pkg/controllers/follower/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import (

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"

fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
"github.com/kubewharf/kubeadmiral/pkg/controllers/common"
)

func TestGetFollowersFromPod(t *testing.T) {
Expand Down Expand Up @@ -313,3 +317,21 @@ func TestGetFollowersFromPod(t *testing.T) {
assert := assert.New(t)
assert.Equal(expectedFollowers, followers)
}

func Test_skipSync(t *testing.T) {
federatedObject := &fedcorev1a1.FederatedObject{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{common.DryRunAnnotation: common.AnnotationValueTrue},
},
}
assert.True(t, skipSync(federatedObject))
federatedObject.GetAnnotations()[common.DryRunAnnotation] = common.AnnotationValueFalse
assert.False(t, skipSync(federatedObject))
federatedObject.GetAnnotations()[common.DryRunAnnotation] = common.AnnotationValueTrue
federatedObject.GetStatus().Clusters = []fedcorev1a1.PropagationStatus{
{
Cluster: "cluster1",
},
}
assert.False(t, skipSync(federatedObject))
}
9 changes: 6 additions & 3 deletions pkg/controllers/sync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,12 @@ func (s *SyncController) reconcile(ctx context.Context, federatedName common.Qua
return worker.StatusError
}

if skipSync(fedResource.Object()) {
fedResource.RecordEvent("SyncSkipped", "Skip Syncing for %s", fedResource.FederatedName())
return worker.StatusAllOK
if fedResource.Object().GetAnnotations()[common.DryRunAnnotation] == common.AnnotationValueTrue {
if len(fedResource.Object().GetStatus().Clusters) == 0 {
fedResource.RecordEvent("DryRunWorked", "Dry run worked for %s", fedResource.FederatedName())
return worker.StatusAllOK
}
fedResource.RecordEvent("DryRunSkipped", "Dry run skipped because resource has been propagated")

Check warning on line 431 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L426-L431

Added lines #L426 - L431 were not covered by tests
}

clustersToSync, selectedClusters, err := s.prepareToSync(ctx, fedResource)
Expand Down
31 changes: 0 additions & 31 deletions pkg/controllers/sync/util.go

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/controllers/sync/util_test.go

This file was deleted.

0 comments on commit ec4b269

Please sign in to comment.