diff --git a/pkg/controllers/follower/controller.go b/pkg/controllers/follower/controller.go index f6e1210b..b4e9770e 100644 --- a/pkg/controllers/follower/controller.go +++ b/pkg/controllers/follower/controller.go @@ -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) { // follower scheduling is not enabled return nil, nil } diff --git a/pkg/controllers/follower/util.go b/pkg/controllers/follower/util.go index b8c54e75..90a2735a 100644 --- a/pkg/controllers/follower/util.go +++ b/pkg/controllers/follower/util.go @@ -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 +} diff --git a/pkg/controllers/follower/util_test.go b/pkg/controllers/follower/util_test.go index df46e5cb..e8d2af4c 100644 --- a/pkg/controllers/follower/util_test.go +++ b/pkg/controllers/follower/util_test.go @@ -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) { @@ -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)) +} diff --git a/pkg/controllers/sync/controller.go b/pkg/controllers/sync/controller.go index e63e8ee6..c913f03c 100644 --- a/pkg/controllers/sync/controller.go +++ b/pkg/controllers/sync/controller.go @@ -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") } clustersToSync, selectedClusters, err := s.prepareToSync(ctx, fedResource) diff --git a/pkg/controllers/sync/util.go b/pkg/controllers/sync/util.go deleted file mode 100644 index 65665140..00000000 --- a/pkg/controllers/sync/util.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright 2023 The KubeAdmiral Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sync - -import ( - fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1" - "github.com/kubewharf/kubeadmiral/pkg/controllers/common" -) - -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 -} diff --git a/pkg/controllers/sync/util_test.go b/pkg/controllers/sync/util_test.go deleted file mode 100644 index da143ca6..00000000 --- a/pkg/controllers/sync/util_test.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2023 The KubeAdmiral Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sync - -import ( - "testing" - - "github.com/stretchr/testify/assert" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1" - "github.com/kubewharf/kubeadmiral/pkg/controllers/common" -) - -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)) -}