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: change PodOpsLifecycle phase names #55

Merged
merged 2 commits 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
4 changes: 2 additions & 2 deletions apis/apps/v1alpha1/well_known_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ const (

// well known variables
const (
PodOpsLifecyclePreCheckStage = "pre-check"
PodOpsLifecyclePostCheckStage = "post-check"
PodOpsLifecyclePreTrafficOffStage = "PreTrafficOff"
PodOpsLifecyclePreTrafficOnStage = "PreTrafficOn"
)
24 changes: 12 additions & 12 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
}

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)

if state.InStageAndPassed() {
switch state.Stage {
case v1alpha1.PodOpsLifecyclePreTrafficOffStage:
labels, err = r.preTrafficOffStage(pod)
case v1alpha1.PodOpsLifecyclePreTrafficOnStage:
labels, err = r.preTrafficOnStage(pod)
}
}

klog.Infof("pod %s in stage %q, labels: %v, error: %v", key, stage, labels, err)
klog.Infof("pod %s in stage %q, labels: %v, error: %v", key, state.Stage, labels, err)
if err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (r *ReconcilePodOpsLifecycle) setServiceReadiness(pod *corev1.Pod, isReady
return true, fmt.Sprintf("update service readiness gate to: %s", string(status))
}

func (r *ReconcilePodOpsLifecycle) preCheckStage(pod *corev1.Pod) (labels map[string]string, err error) {
func (r *ReconcilePodOpsLifecycle) preTrafficOffStage(pod *corev1.Pod) (labels map[string]string, err error) {
idToLabelsMap, _, err := PodIDAndTypesMap(pod)
if err != nil {
return nil, err
Expand Down Expand Up @@ -276,7 +276,7 @@ func (r *ReconcilePodOpsLifecycle) preCheckStage(pod *corev1.Pod) (labels map[st
return
}

func (r *ReconcilePodOpsLifecycle) postCheckStage(pod *corev1.Pod) (labels map[string]string, err error) {
func (r *ReconcilePodOpsLifecycle) preTrafficOnStage(pod *corev1.Pod) (labels map[string]string, err error) {
idToLabelsMap, _, err := PodIDAndTypesMap(pod)
if err != nil {
return nil, err
Expand Down Expand Up @@ -308,11 +308,11 @@ func (r *ReconcilePodOpsLifecycle) addLabels(ctx context.Context, pod *corev1.Po
}

func (r *ReconcilePodOpsLifecycle) initRuleSetManager() {
r.ruleSetManager.RegisterStage(v1alpha1.PodOpsLifecyclePreCheckStage, func(po client.Object) bool {
r.ruleSetManager.RegisterStage(v1alpha1.PodOpsLifecyclePreTrafficOffStage, func(po client.Object) bool {
labels := po.GetLabels()
return labels != nil && labelHasPrefix(labels, v1alpha1.PodPreCheckLabelPrefix)
})
r.ruleSetManager.RegisterStage(v1alpha1.PodOpsLifecyclePostCheckStage, func(po client.Object) bool {
r.ruleSetManager.RegisterStage(v1alpha1.PodOpsLifecyclePreTrafficOnStage, func(po client.Object) bool {
labels := po.GetLabels()
return labels != nil && labelHasPrefix(labels, v1alpha1.PodPostCheckLabelPrefix)
})
Expand Down
27 changes: 2 additions & 25 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,16 @@ var _ = Describe("podopslifecycle controller", func() {

podOpsLifecycle.ruleSetManager = &mockRuleSetManager{
CheckState: &checker.CheckState{
Stage: v1alpha1.PodOpsLifecyclePreTrafficOffStage,
States: []checker.State{
{
Detail: &v1alpha1.Detail{
Stage: v1alpha1.PodOpsLifecyclePreCheckStage,
Stage: v1alpha1.PodOpsLifecyclePreTrafficOffStage,
Passed: true,
},
},
},
},
stage: v1alpha1.PodOpsLifecyclePreCheckStage,
inStage: true,
}

pod.ObjectMeta.Labels = map[string]string{
Expand Down Expand Up @@ -311,28 +310,6 @@ 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
13 changes: 8 additions & 5 deletions pkg/controllers/ruleset/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ type checker struct {
// GetState get item current check state from all related ruleSets
func (c *checker) GetState(cl client.Client, item client.Object) (CheckState, error) {

result := CheckState{}
result := CheckState{
Stage: c.policy.Stage(item),
}
ruleSetList := &appsv1alpha1.RuleSetList{}
if err := cl.List(context.TODO(), ruleSetList, &client.ListOptions{FieldSelector: fields.OneTermEqualSelector(rulesetutils.FieldIndexRuleSet, item.GetName())}); err != nil {
return result, err
Expand Down Expand Up @@ -83,22 +85,23 @@ func (c *checker) GetState(cl client.Client, item client.Object) (CheckState, er
}

type CheckState struct {
Stage string
States []State
Message string
}

func (cs *CheckState) InStage(stage string) bool {
func (cs *CheckState) InStage() bool {
for _, state := range cs.States {
if state.Detail.Stage != stage {
if state.Detail.Stage != cs.Stage {
return false
}
}
return true
}

func (cs *CheckState) InStageAndPassed(stage string) bool {
func (cs *CheckState) InStageAndPassed() bool {
for _, state := range cs.States {
if state.Detail.Stage != stage || !state.Detail.Passed {
if state.Detail.Stage != cs.Stage || !state.Detail.Passed {
return false
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/controllers/ruleset/ruleset_controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ type ManagerInterface interface {
// 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
}
Expand All @@ -54,14 +51,12 @@ func AddUnAvailableFunc(f func(pod *corev1.Pod) (bool, *int64)) {
func newRulesetManager() ManagerInterface {
return &rsManager{
Register: register.DefaultRegister(),
Policy: register.DefaultPolicy(),
Checker: checker.NewCheck(),
}
}

type rsManager struct {
register.Register
register.Policy
checker.Checker
controller controller.Controller
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/ruleset/ruleset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestRuleSet(t *testing.T) {
g.Expect(err).NotTo(gomega.HaveOccurred())
printJson(state)
printJson(podList.Items[i])
if state.InStageAndPassed(PreTrafficOffStage) {
if state.InStageAndPassed() {
passedCount++
g.Expect(podList.Items[i].Name).NotTo(gomega.Equal("pod-test-3"))
g.Expect(podList.Items[i].Name).NotTo(gomega.Equal("pod-test-4"))
Expand All @@ -129,7 +129,7 @@ func TestRuleSet(t *testing.T) {
waitProcessFinished(request)
g.Expect(c.Get(ctx, types.NamespacedName{Namespace: "default", Name: "pod-test-3"}, po)).NotTo(gomega.HaveOccurred())
state, _ := RuleSetManager().GetState(c, po)
g.Expect(state.InStageAndPassed(PreTrafficOffStage)).Should(gomega.BeTrue())
g.Expect(state.InStageAndPassed()).Should(gomega.BeTrue())
printJson(state)

ruleSetList := &appsv1alpha1.RuleSetList{}
Expand Down
Loading