Skip to content

Commit

Permalink
feat: support 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 1, 2023
1 parent 6f6d586 commit a830ed6
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 220 deletions.
3 changes: 3 additions & 0 deletions pkg/controllers/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ const (
MigrationConfigurationAnnotation = DefaultPrefix + "migration-configuration"
// AppliedMigrationConfigurationAnnotation contains the applied custom migration configuration.
AppliedMigrationConfigurationAnnotation = DefaultPrefix + "applied-migration-configuration"
// DryRunAnnotation indicates resource is in dry run process. It will prevent new resources from being dispatched by the sync controller.
// It works only when resources have not been propagated to member clusters.
DryRunAnnotation = DefaultPrefix + "dry-run"
)

// PropagatedAnnotationKeys and PropagatedLabelKeys are used to store the keys of annotations and labels that are present
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/federate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ var (
common.FollowersAnnotation,
common.DisableFollowingAnnotation,
common.MigrationConfigurationAnnotation,
common.DryRunAnnotation,
)

// List of annotations that should be ignored on the source object
ignoredAnnotationSet = sets.New(
common.LatestReplicasetDigestsAnnotation,
common.AppliedMigrationConfigurationAnnotation,
scheduler.SchedulingAnnotation,
)

federatedLabelSet = sets.New[string](
Expand Down
9 changes: 9 additions & 0 deletions pkg/controllers/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,15 @@ func (s *Scheduler) applySchedulingResult(
}
}

if objectModified {
value, err := getSchedulingAnnotationValue(fedObject, result)
if err != nil {
return false, err
}

Check warning on line 777 in pkg/controllers/scheduler/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/scheduler/scheduler.go#L776-L777

Added lines #L776 - L777 were not covered by tests
annotations[SchedulingAnnotation] = value
annotationsModified = true
}

if annotationsModified {
fedObject.SetAnnotations(annotations)
objectModified = true
Expand Down
66 changes: 66 additions & 0 deletions pkg/controllers/scheduler/schedulingannotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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 scheduler

import (
"encoding/json"

"k8s.io/klog/v2"

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

var SchedulingAnnotation = common.DefaultPrefix + "scheduling"

type Scheduling struct {
// SourceGeneration is the generation of the source object
// observed in the federated object when this placement is sampled.
SourceGeneration int64 `json:"sourceGeneration"`

// FederatedGeneration is the generation of the federated object
// observed when this placement is sampled.
FederatedGeneration int64 `json:"fedGeneration"`

// Placement contains a list of FederatedCluster object names and replicas on them.
Placement map[string]*int64 `json:"placement,omitempty"`
}

func getSchedulingAnnotationValue(
fedObject fedcorev1a1.GenericFederatedObject,
result core.ScheduleResult,
) (value string, err error) {
scheduling := Scheduling{}

srcMeta, err := fedObject.GetSpec().GetTemplateMetadata()
if err != nil {
return "", err
}

Check warning on line 53 in pkg/controllers/scheduler/schedulingannotation.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/scheduler/schedulingannotation.go#L52-L53

Added lines #L52 - L53 were not covered by tests

scheduling.SourceGeneration = srcMeta.GetGeneration()
scheduling.FederatedGeneration = fedObject.GetGeneration()
scheduling.Placement = result.SuggestedClusters

jsonBuf, err := json.Marshal(scheduling)
if err != nil {
klog.Errorf("Cannot marshal JSON: %v", err)
return "", err
}

Check warning on line 63 in pkg/controllers/scheduler/schedulingannotation.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/scheduler/schedulingannotation.go#L61-L63

Added lines #L61 - L63 were not covered by tests

return string(jsonBuf), nil
}
1 change: 1 addition & 0 deletions pkg/controllers/scheduler/schedulingtriggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ var knownSchedulingAnnotations = sets.New(
MaxClustersAnnotations,
FollowsObjectAnnotation,
common.AppliedMigrationConfigurationAnnotation,
common.DryRunAnnotation,
)

func getSchedulingAnnotationsHash(fedObject fedcorev1a1.GenericFederatedObject) (string, error) {
Expand Down
12 changes: 2 additions & 10 deletions pkg/controllers/statusaggregator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
fedcorev1a1informers "github.com/kubewharf/kubeadmiral/pkg/client/informers/externalversions/core/v1alpha1"
"github.com/kubewharf/kubeadmiral/pkg/controllers/federate"
"github.com/kubewharf/kubeadmiral/pkg/controllers/statusaggregator/plugins"
"github.com/kubewharf/kubeadmiral/pkg/controllers/statusaggregator/plugins/sourcefeedback"
"github.com/kubewharf/kubeadmiral/pkg/stats"
"github.com/kubewharf/kubeadmiral/pkg/stats/metrics"
clusterutil "github.com/kubewharf/kubeadmiral/pkg/util/cluster"
Expand Down Expand Up @@ -342,16 +343,7 @@ func (a *StatusAggregator) reconcile(ctx context.Context, key reconcileKey) (sta
return worker.StatusError
}

needUpdate := false
for _, plugin := range plugins.DefaultCommonPlugins {
newObj, newNeedUpdate, err := plugin.AggregateStatuses(ctx, sourceObject, fedObject, clusterObjs, clusterObjsUpToDate)
if err != nil {
return worker.StatusError
}
sourceObject = newObj
needUpdate = needUpdate || newNeedUpdate
}

needUpdate := sourcefeedback.PopulateAnnotations(sourceObject, fedObject)

Check warning on line 346 in pkg/controllers/statusaggregator/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/statusaggregator/controller.go#L346

Added line #L346 was not covered by tests
if needUpdate {
logger.V(1).Info("Updating metadata of source object")
_, err = a.dynamicClient.Resource(ftc.GetSourceTypeGVR()).Namespace(key.namespace).
Expand Down
73 changes: 0 additions & 73 deletions pkg/controllers/statusaggregator/plugins/common/migrationconfig.go

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions pkg/controllers/statusaggregator/plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

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

type Plugin interface {
Expand All @@ -48,10 +47,6 @@ var pluginsMap = map[schema.GroupVersionKind]Plugin{
corev1.SchemeGroupVersion.WithKind(common.PodKind): NewPodPlugin(),
}

var DefaultCommonPlugins = []Plugin{
plugincommon.NewMigrationConfigPlugin(),
}

func GetPlugin(gvk schema.GroupVersionKind) Plugin {
return pluginsMap[gvk]
}
Loading

0 comments on commit a830ed6

Please sign in to comment.