Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: promote ruleset manager #56

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 33 additions & 7 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 13 additions & 13 deletions pkg/controllers/ruleset/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}

Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -102,7 +102,7 @@ func (cs *CheckState) InStageAndPassed(stage string) bool {
return false
}
}
return len(cs.States) > 0
return true
}

type State struct {
Expand Down
32 changes: 12 additions & 20 deletions pkg/controllers/ruleset/ruleset_controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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()
Expand All @@ -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
Expand Down
Loading