From acd620278d282add0f2f7d1ada9f7392799566c0 Mon Sep 17 00:00:00 2001 From: Chaer Date: Thu, 24 Aug 2023 17:27:27 +0800 Subject: [PATCH] promote ruleset manager --- .../podopslifecycle_controller.go | 20 +++++----- .../podopslifecycle_controller_test.go | 40 +++++++++++++++---- pkg/controllers/ruleset/checker/checker.go | 26 ++++++------ .../ruleset/ruleset_controller_manager.go | 32 ++++++--------- 4 files changed, 68 insertions(+), 50 deletions(-) diff --git a/pkg/controllers/podopslifecycle/podopslifecycle_controller.go b/pkg/controllers/podopslifecycle/podopslifecycle_controller.go index 45e6e401..5896ae7f 100644 --- a/pkg/controllers/podopslifecycle/podopslifecycle_controller.go +++ b/pkg/controllers/podopslifecycle/podopslifecycle_controller.go @@ -122,17 +122,17 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc return reconcile.Result{}, err } - var ( - stage string - labels map[string]string - ) - if state.InStageAndPassed(v1alpha1.PodOpsLifecyclePreCheckStage) { - stage = v1alpha1.PodOpsLifecyclePreCheckStage - labels, err = r.preCheckStage(pod) - } else if state.InStageAndPassed(v1alpha1.PodOpsLifecyclePostCheckStage) { - stage = v1alpha1.PodOpsLifecyclePostCheckStage - labels, err = r.postCheckStage(pod) + var labels map[string]string + stage := r.ruleSetManager.Stage(pod) + if state.InStageAndPassed(stage) { + switch stage { + case v1alpha1.PodOpsLifecyclePreCheckStage: + labels, err = r.preCheckStage(pod) + case v1alpha1.PodOpsLifecyclePostCheckStage: + labels, err = r.postCheckStage(pod) + } } + klog.Infof("pod %s in stage %q, labels: %v, error: %v", key, stage, labels, err) if err != nil { return reconcile.Result{}, err diff --git a/pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go b/pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go index e1ae1643..84bfdfd5 100644 --- a/pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go +++ b/pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go @@ -157,16 +157,20 @@ var _ = Describe("podopslifecycle controller", func() { Expect(string(pod.Status.Conditions[0].Type)).To(Equal(v1alpha1.ReadinessGatePodServiceReady)) Expect(pod.Status.Conditions[0].Status).To(Equal(corev1.ConditionTrue)) - podOpsLifecycle.ruleSetManager = &mockRuleSetManager{CheckState: &checker.CheckState{ - States: []checker.State{ - { - Detail: &v1alpha1.Detail{ - Stage: v1alpha1.PodOpsLifecyclePreCheckStage, - Passed: true, + podOpsLifecycle.ruleSetManager = &mockRuleSetManager{ + CheckState: &checker.CheckState{ + States: []checker.State{ + { + Detail: &v1alpha1.Detail{ + Stage: v1alpha1.PodOpsLifecyclePreCheckStage, + Passed: true, + }, }, }, }, - }} + stage: v1alpha1.PodOpsLifecyclePreCheckStage, + inStage: true, + } pod.ObjectMeta.Labels = map[string]string{ v1alpha1.ControlledByPodOpsLifecycle: "true", @@ -307,6 +311,28 @@ var _ ruleset.ManagerInterface = &mockRuleSetManager{} type mockRuleSetManager struct { *checker.CheckState + stage string + inStage bool +} + +func (rsm *mockRuleSetManager) Stage(obj client.Object) string { + return rsm.stage +} + +func (rsm *mockRuleSetManager) InStage(obj client.Object, key string) bool { + return rsm.inStage +} + +func (rsm *mockRuleSetManager) GetStages() []string { + return nil +} + +func (rsm *mockRuleSetManager) Conditions(obj client.Object) []string { + return nil +} + +func (rsm *mockRuleSetManager) MatchConditions(obj client.Object, conditions ...string) []string { + return nil } func (rsm *mockRuleSetManager) RegisterStage(key string, inStage func(obj client.Object) bool) { diff --git a/pkg/controllers/ruleset/checker/checker.go b/pkg/controllers/ruleset/checker/checker.go index b31b8172..e883341a 100644 --- a/pkg/controllers/ruleset/checker/checker.go +++ b/pkg/controllers/ruleset/checker/checker.go @@ -28,12 +28,12 @@ import ( rulesetutils "kusionstack.io/kafed/pkg/controllers/ruleset/utils" ) -type Check interface { - // GetState get current check state +type Checker interface { + // GetState collects current states from all associated RuleSets, and every state stage is not guaranteed to be consistent GetState(client.Client, client.Object) (CheckState, error) } -func NewCheck() Check { +func NewCheck() Checker { return &checker{policy: register.DefaultPolicy()} } @@ -52,30 +52,33 @@ func (c *checker) GetState(cl client.Client, item client.Object) (CheckState, er for i := range ruleSetList.Items { rs := &ruleSetList.Items[i] findStatus := false - for i, detail := range rs.Status.Details { + for j, detail := range rs.Status.Details { if detail.Name != item.GetName() { continue } findStatus = true if !detail.Passed { - result.Message += CollectInfo(rs.Name, rs.Status.Details[i]) + result.Message += CollectInfo(rs.Name, rs.Status.Details[j]) } result.States = append(result.States, State{ RuleSetName: rs.Name, - Detail: rs.Status.Details[i], + Detail: rs.Status.Details[j], }) + break } if !findStatus { result.States = append(result.States, State{ RuleSetName: rs.Name, Detail: &appsv1alpha1.Detail{ - Passed: false, + Passed: true, }, }) result.Message += fmt.Sprintf("[waiting for ruleset %s processing. ]", rs.Name) - return result, nil } } + if len(ruleSetList.Items) == 0 { + result.Message = "No ruleSets found" + } return result, nil } @@ -85,15 +88,12 @@ type CheckState struct { } func (cs *CheckState) InStage(stage string) bool { - if len(cs.States) == 0 { - return false - } for _, state := range cs.States { if state.Detail.Stage != stage { return false } } - return len(cs.States) > 0 + return true } func (cs *CheckState) InStageAndPassed(stage string) bool { @@ -102,7 +102,7 @@ func (cs *CheckState) InStageAndPassed(stage string) bool { return false } } - return len(cs.States) > 0 + return true } type State struct { diff --git a/pkg/controllers/ruleset/ruleset_controller_manager.go b/pkg/controllers/ruleset/ruleset_controller_manager.go index 2fc80e8d..37fe71f4 100644 --- a/pkg/controllers/ruleset/ruleset_controller_manager.go +++ b/pkg/controllers/ruleset/ruleset_controller_manager.go @@ -18,7 +18,6 @@ package ruleset import ( corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/manager" @@ -32,11 +31,14 @@ type ManagerInterface interface { // Register used to register ruleSet stages and conditions before starting controller register.Register + // Checker is used to check rule state after starting controller + checker.Checker + + // Policy is stages and conditions policy registered by Register + register.Policy + // SetupRuleSetController add a new RuleSetController to manager SetupRuleSetController(manager.Manager) error - - // Check is used to check rule state after starting controller - checker.Check } var defaultManager = newRulesetManager() @@ -51,29 +53,19 @@ func AddUnAvailableFunc(f func(pod *corev1.Pod) (bool, *int64)) { func newRulesetManager() ManagerInterface { return &rsManager{ - register: register.DefaultRegister(), - checker: checker.NewCheck(), + Register: register.DefaultRegister(), + Policy: register.DefaultPolicy(), + Checker: checker.NewCheck(), } } type rsManager struct { - register register.Register - checker checker.Check + register.Register + register.Policy + checker.Checker controller controller.Controller } -func (m *rsManager) RegisterStage(key string, needCheck func(obj client.Object) bool) { - m.register.RegisterStage(key, needCheck) -} - -func (m *rsManager) RegisterCondition(opsCondition string, inCondition func(obj client.Object) bool) { - m.register.RegisterCondition(opsCondition, inCondition) -} - -func (m *rsManager) GetState(c client.Client, item client.Object) (checker.CheckState, error) { - return m.checker.GetState(c, item) -} - func (m *rsManager) SetupRuleSetController(mgr manager.Manager) (err error) { m.controller, err = addToMgr(mgr, newReconciler(mgr)) return err