Skip to content

Commit

Permalink
refactor(auto-migrate): standardise info annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
gary-lgy committed Apr 10, 2023
1 parent 05aedc0 commit 8c24552
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions pkg/controllers/automigration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ func (c *Controller) reconcile(qualifiedName common.QualifiedName) (status worke
if unschedulableThreshold == nil {
// Clean up the annotation if auto migration is disabled.
keyedLogger.V(3).Info("Auto migration is disabled")
_, exists := annotations[common.AutoMigrationAnnotation]
_, exists := annotations[common.AutoMigrationInfoAnnotation]
if exists {
delete(annotations, common.AutoMigrationAnnotation)
delete(annotations, common.AutoMigrationInfoAnnotation)
needsUpdate = true
}
} else {
Expand All @@ -228,25 +228,25 @@ func (c *Controller) reconcile(qualifiedName common.QualifiedName) (status worke
}

estimatedCapacity, result = c.estimateCapacity(ctx, clusterObjs, *unschedulableThreshold)
autoMigration := &framework.AutoMigrationInfo{EstimatedCapacity: estimatedCapacity}
autoMigrationInfo := &framework.AutoMigrationInfo{EstimatedCapacity: estimatedCapacity}

// Compare with the existing autoMigration annotation
existingAutoMigration := &framework.AutoMigrationInfo{EstimatedCapacity: nil}
if existingAutoMigrationBytes, exists := annotations[common.AutoMigrationAnnotation]; exists {
err := json.Unmarshal([]byte(existingAutoMigrationBytes), existingAutoMigration)
existingAutoMigrationInfo := &framework.AutoMigrationInfo{EstimatedCapacity: nil}
if existingAutoMigrationInfoBytes, exists := annotations[common.AutoMigrationInfoAnnotation]; exists {
err := json.Unmarshal([]byte(existingAutoMigrationInfoBytes), existingAutoMigrationInfo)
if err != nil {
keyedLogger.Error(err, "Existing auto migration annotation is invalid, ignoring")
// we treat invalid existing annotation as if it doesn't exist
}
}

if !equality.Semantic.DeepEqual(existingAutoMigration, autoMigration) {
autoMigrationBytes, err := json.Marshal(autoMigration)
if !equality.Semantic.DeepEqual(existingAutoMigrationInfo, autoMigrationInfo) {
autoMigrationInfoBytes, err := json.Marshal(autoMigrationInfo)
if err != nil {
keyedLogger.Error(err, "Failed to marshal auto migration")
return worker.StatusAllOK
}
annotations[common.AutoMigrationAnnotation] = string(autoMigrationBytes)
annotations[common.AutoMigrationInfoAnnotation] = string(autoMigrationInfoBytes)
needsUpdate = true
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ const (

// When a pod remains unschedulable beyond this threshold, it becomes eligible for automatic migration.
PodUnschedulableThresholdAnnotation = InternalPrefix + "pod-unschedulable-threshold"
// AutoMigrationAnnotation contains auto migration information.
AutoMigrationAnnotation = DefaultPrefix + "auto-migration"
// AutoMigrationInfoAnnotation contains auto migration information.
AutoMigrationInfoAnnotation = DefaultPrefix + "auto-migration-info"
)

// The following consts are keys used to store information in the federated cluster secret
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/scheduler/framework/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type SchedulingUnit struct {

// Describes the current scheduling state
CurrentClusters map[string]*int64
AutoMigration *AutoMigrationConfig
AutoMigration *AutoMigrationSpec

// Controls the scheduling behavior
SchedulingMode fedcorev1a1.SchedulingMode
Expand All @@ -64,7 +64,7 @@ type SchedulingUnit struct {
Weights map[string]int64
}

type AutoMigrationConfig struct {
type AutoMigrationSpec struct {
Info *AutoMigrationInfo
KeepUnschedulableReplicas bool
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/scheduler/schedulingtriggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *Scheduler) computeSchedulingTriggerHash(
trigger.PolicyGeneration = policy.GetGeneration()
if policy.GetSpec().AutoMigration != nil {
// Only consider auto-migration annotation when auto-migration is enabled in the policy.
if value, exists := fedObject.GetAnnotations()[common.AutoMigrationAnnotation]; exists {
if value, exists := fedObject.GetAnnotations()[common.AutoMigrationInfoAnnotation]; exists {
trigger.AutoMigrationInfo = &value
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/scheduler/schedulingunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *Scheduler) schedulingUnitForFedObject(
if err != nil {
return nil, err
}
schedulingUnit.AutoMigration = &framework.AutoMigrationConfig{
schedulingUnit.AutoMigration = &framework.AutoMigrationSpec{
Info: info,
KeepUnschedulableReplicas: autoMigration.KeepUnschedulableReplicas,
}
Expand Down Expand Up @@ -235,16 +235,16 @@ func getSchedulingModeFromObject(object *unstructured.Unstructured) (fedcorev1a1
}

func getAutoMigrationInfo(fedObject *unstructured.Unstructured) (*framework.AutoMigrationInfo, error) {
value, exists := fedObject.GetAnnotations()[common.AutoMigrationAnnotation]
value, exists := fedObject.GetAnnotations()[common.AutoMigrationInfoAnnotation]
if !exists {
return nil, nil
}

autoMigration := new(framework.AutoMigrationInfo)
if err := json.Unmarshal([]byte(value), autoMigration); err != nil {
autoMigrationInfo := new(framework.AutoMigrationInfo)
if err := json.Unmarshal([]byte(value), autoMigrationInfo); err != nil {
return nil, err
}
return autoMigration, nil
return autoMigrationInfo, nil
}

func getIsStickyClusterFromPolicy(policy fedcorev1a1.GenericPropagationPolicy) bool {
Expand Down

0 comments on commit 8c24552

Please sign in to comment.