From b9893897dce1f923dda4ce5f3b87e75353f8e4fd Mon Sep 17 00:00:00 2001 From: Jiawen Liu Date: Wed, 6 Nov 2024 22:58:16 +0800 Subject: [PATCH] refactor: use functions in framework/api/common_state.go instead of binder CommonState. --- .../interpodaffinity/interpodaffinity.go | 12 +-- .../interpodaffinity/interpodaffinity_test.go | 3 +- .../podtopologyspread/podtopologyspread.go | 12 +-- .../podtopologyspread_test.go | 3 +- pkg/binder/godel_binder.go | 3 +- pkg/binder/utils/binder_common_state.go | 74 ------------------- pkg/framework/api/common_state.go | 20 +++++ 7 files changed, 35 insertions(+), 92 deletions(-) delete mode 100644 pkg/binder/utils/binder_common_state.go diff --git a/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity.go b/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity.go index 8224b416..40a98c4a 100644 --- a/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity.go +++ b/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity.go @@ -65,12 +65,12 @@ func (pl *InterPodAffinity) CheckTopology(_ context.Context, cycleState *framewo PodInfo: podInfo, } - commonState, err := binderutils.ReadCommonState(cycleState) - if err != nil { - return framework.NewStatus(framework.Error, err.Error()) - } - if commonState != nil { - pl.updateStateByVictims(state, commonState.VictimsGroupByNode, pod) + if cycleState != nil { + victimsGroupByNode, err := framework.GetVictimsGroupByNodeState(cycleState) + if err != nil { + return framework.NewStatus(framework.Error, err.Error()) + } + pl.updateStateByVictims(state, victimsGroupByNode, pod) } if !utils.SatisfyPodAffinity(state, nodeInfo, podLauncher) { diff --git a/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity_test.go b/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity_test.go index 808d1f25..77c494c4 100644 --- a/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity_test.go +++ b/pkg/binder/framework/plugins/interpodaffinity/interpodaffinity_test.go @@ -24,7 +24,6 @@ import ( "github.com/kubewharf/godel-scheduler/pkg/binder/cache" "github.com/kubewharf/godel-scheduler/pkg/binder/framework/handle" pt "github.com/kubewharf/godel-scheduler/pkg/binder/testing" - binderutils "github.com/kubewharf/godel-scheduler/pkg/binder/utils" commoncache "github.com/kubewharf/godel-scheduler/pkg/common/cache" framework "github.com/kubewharf/godel-scheduler/pkg/framework/api" utils "github.com/kubewharf/godel-scheduler/pkg/plugins/interpodaffinity" @@ -1987,7 +1986,7 @@ func TestCheckConflictsWithVictims(t *testing.T) { } victimsGroupByNode[node] = victims } - binderutils.WriteCommonState(state, victimsGroupByNode) + framework.SetVictimsGroupByNodeState(victimsGroupByNode, state) for i, node := range tt.nodes { nodeInfo := frameworkHandle.GetNodeInfo(node.Name) diff --git a/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread.go b/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread.go index 57b41796..7c3f6a9a 100644 --- a/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread.go +++ b/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread.go @@ -72,12 +72,12 @@ func (pl *PodTopologySpreadCheck) CheckTopology(_ context.Context, cycleState *f state := utils.GetPreFilterState(pod, nodeInfos, constraints) - commonState, err := binderutils.ReadCommonState(cycleState) - if err != nil { - return framework.NewStatus(framework.Error, err.Error()) - } - if commonState != nil { - pl.updateStateByVictims(&state, commonState.VictimsGroupByNode) + if cycleState != nil { + victimsGroupByNode, err := framework.GetVictimsGroupByNodeState(cycleState) + if err != nil { + return framework.NewStatus(framework.Error, err.Error()) + } + pl.updateStateByVictims(&state, victimsGroupByNode) } return utils.IsSatisfyPodTopologySpreadConstraints(&state, pod, nodeInfo, podLauncher) diff --git a/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread_test.go b/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread_test.go index d359120b..23ad380c 100644 --- a/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread_test.go +++ b/pkg/binder/framework/plugins/podtopologyspread/podtopologyspread_test.go @@ -26,7 +26,6 @@ import ( "github.com/kubewharf/godel-scheduler/pkg/binder/cache" "github.com/kubewharf/godel-scheduler/pkg/binder/framework/handle" pt "github.com/kubewharf/godel-scheduler/pkg/binder/testing" - binderutils "github.com/kubewharf/godel-scheduler/pkg/binder/utils" commoncache "github.com/kubewharf/godel-scheduler/pkg/common/cache" framework "github.com/kubewharf/godel-scheduler/pkg/framework/api" utils "github.com/kubewharf/godel-scheduler/pkg/plugins/podtopologyspread" @@ -752,7 +751,7 @@ func TestCheckConflictsWithVictims(t *testing.T) { } victimsGroupByNode[node] = victims } - binderutils.WriteCommonState(state, victimsGroupByNode) + framework.SetVictimsGroupByNodeState(victimsGroupByNode, state) for _, node := range tt.nodes { nodeInfo := frameworkHandle.GetNodeInfo(node.Name) diff --git a/pkg/binder/godel_binder.go b/pkg/binder/godel_binder.go index 954bbb6a..79d6d458 100644 --- a/pkg/binder/godel_binder.go +++ b/pkg/binder/godel_binder.go @@ -467,8 +467,7 @@ func (binder *Binder) CheckCrossNodeTopologyForUnit(ctx context.Context, unitInf commonState := framework.NewCycleState() // TODO // Step 1: PrepareCommonState - - binderutils.WriteCommonState(commonState, unitInfo.newTasks.VictimsGroupByNode) + framework.SetVictimsGroupByNodeState(unitInfo.newTasks.VictimsGroupByNode, commonState) for _, newTask := range unitInfo.GetNewTasks() { nodeName := newTask.suggestedNode diff --git a/pkg/binder/utils/binder_common_state.go b/pkg/binder/utils/binder_common_state.go deleted file mode 100644 index ed9acf7e..00000000 --- a/pkg/binder/utils/binder_common_state.go +++ /dev/null @@ -1,74 +0,0 @@ -/* -Copyright 2024 The Godel Scheduler 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 utils - -import ( - "fmt" - - framework "github.com/kubewharf/godel-scheduler/pkg/framework/api" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/types" -) - -const ( - BinderCommonStateKey = "BinderCommonState" -) - -type CommonState struct { - VictimsGroupByNode map[string]map[types.UID]*v1.Pod -} - -func (s CommonState) Clone() framework.StateData { - clonedVictimsGroupByNode := make(map[string]map[types.UID]*v1.Pod) - - for nodeName, victimsMap := range s.VictimsGroupByNode { - clonedVictimsMap := make(map[types.UID]*v1.Pod) - for uid, pod := range victimsMap { - clonedVictimsMap[uid] = pod.DeepCopy() - } - clonedVictimsGroupByNode[nodeName] = clonedVictimsMap - } - - return CommonState{ - VictimsGroupByNode: clonedVictimsGroupByNode, - } -} - -func WriteCommonState(cycleState *framework.CycleState, victimsGroupByNode map[string]map[types.UID]*v1.Pod) error { - commonState := &CommonState{ - VictimsGroupByNode: victimsGroupByNode, - } - cycleState.Write(BinderCommonStateKey, commonState) - - return nil -} - -func ReadCommonState(cycleState *framework.CycleState) (*CommonState, error) { - if cycleState == nil { - return nil, nil - } - commonStateData, err := cycleState.Read(BinderCommonStateKey) - if err != nil { - return nil, err - } - - commonState, ok := commonStateData.(*CommonState) - if !ok { - return nil, fmt.Errorf("%+v convert to helper.AllNodeInfos error", commonStateData) - } - return commonState, nil -} diff --git a/pkg/framework/api/common_state.go b/pkg/framework/api/common_state.go index 46b5f06e..d8ad5c61 100644 --- a/pkg/framework/api/common_state.go +++ b/pkg/framework/api/common_state.go @@ -19,6 +19,8 @@ package api import ( "fmt" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" podutil "github.com/kubewharf/godel-scheduler/pkg/util/pod" @@ -40,6 +42,7 @@ const ( VictimCountOfDeployKey = "VictimCountOfDeployKey" IndexOfPDBKey = "IndexOfPDBKey" EverScheduledKey = "EverScheduledKey" + VictimsGroupByNodeKey = "VictimsGroupByNode" // Error Message NodePartitionTypeMissedErrorString = "failed to get NodePartitionType, supposed to be set in cycle state" @@ -341,3 +344,20 @@ func GetEverScheduledState(state *CycleState) (bool, error) { } return false, fmt.Errorf("everScheduled state not found") } + +func SetVictimsGroupByNodeState(victimsGroupByNode map[string]map[types.UID]*v1.Pod, state *CycleState) { + data := &stateData{data: victimsGroupByNode} + state.Write(VictimsGroupByNodeKey, data) +} + +func GetVictimsGroupByNodeState(state *CycleState) (map[string]map[types.UID]*v1.Pod, error) { + if state == nil { + return nil, fmt.Errorf("nil cycle state") + } + if data, err := state.Read(VictimsGroupByNodeKey); err == nil { + if s, ok := data.(*stateData); ok { + return s.data.(map[string]map[types.UID]*v1.Pod), nil + } + } + return nil, fmt.Errorf("victimsGroupByNode state not found") +}