Skip to content

Commit

Permalink
fix ruleset checker GetState (#48)
Browse files Browse the repository at this point in the history
* fix ruleset checker GetState

* adjust the field name with seconds
  • Loading branch information
Eikykun committed Aug 21, 2023
1 parent a4c064c commit 460d36b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 63 deletions.
4 changes: 2 additions & 2 deletions apis/apps/v1alpha1/ruleset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ type ClientConfig struct {

// interval give the request time interval, default 5s
// +optional
Interval *int64 `json:"interval,omitempty"`
IntervalSeconds *int64 `json:"intervalSeconds,omitempty"`

// timeout give the request time timeout, default 60s
// +optional
TraceTimeout *int64 `json:"traceTimeout,omitempty"`
TraceTimeoutSeconds *int64 `json:"traceTimeoutSeconds,omitempty"`
}

// RuleSetStatus defines the observed state of RuleSet
Expand Down
8 changes: 4 additions & 4 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/crd/bases/apps.kusionstack.io_rulesets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ spec:
certificate. If unspecified, system trust roots on
the apiserver are used. After Base64.'
type: string
interval:
intervalSeconds:
description: interval give the request time interval,
default 5s
format: int64
type: integer
traceTimeout:
traceTimeoutSeconds:
description: timeout give the request time timeout,
default 60s
format: int64
Expand Down
34 changes: 16 additions & 18 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,24 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
r.logger.Errorf("failed to get pod %s state: %s", key, err)
return reconcile.Result{}, err
}
if state.Passed {
var labels map[string]string
if state.InStage(v1alpha1.PodOpsLifecyclePreCheckStage) {
labels, err = r.preCheckStage(pod)
} else if state.InStage(v1alpha1.PodOpsLifecyclePostCheckStage) {
labels, err = r.postCheckStage(pod)
}
if err != nil {
return reconcile.Result{}, err
}
var labels map[string]string
if state.InStageAndPassed(v1alpha1.PodOpsLifecyclePreCheckStage) {
labels, err = r.preCheckStage(pod)
} else if state.InStageAndPassed(v1alpha1.PodOpsLifecyclePostCheckStage) {
labels, err = r.postCheckStage(pod)
}
if err != nil {
return reconcile.Result{}, err
}

if len(labels) > 0 {
expectation.ExpectUpdate(key, pod.ResourceVersion)
err = r.addLabels(ctx, pod, labels)
if err != nil {
r.logger.Errorf("failed to update pod %s: %s", key, err)
expectation.DeleteExpectations(key)
}
return reconcile.Result{}, err
if len(labels) > 0 {
expectation.ExpectUpdate(key, pod.ResourceVersion)
err = r.addLabels(ctx, pod, labels)
if err != nil {
r.logger.Errorf("failed to update pod %s: %s", key, err)
expectation.DeleteExpectations(key)
}
return reconcile.Result{}, err
}

if !r.expectation.SatisfiedExpectations(key, pod.ResourceVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ var _ = Describe("podopslifecycle controller", func() {
podOpsLifecycle.ruleSetManager = &mockRuleSetManager{CheckState: &checker.CheckState{
States: []checker.State{
{
Detail: v1alpha1.Detail{
Stage: v1alpha1.PodOpsLifecyclePreCheckStage,
Detail: &v1alpha1.Detail{
Stage: v1alpha1.PodOpsLifecyclePreCheckStage,
Passed: true,
},
},
},
Passed: true,
}}

pod.ObjectMeta.Labels = map[string]string{
Expand Down
50 changes: 25 additions & 25 deletions pkg/controllers/ruleset/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,11 @@ type checker struct {
policy register.Policy
}

// GetState get item current check state from all related ruleSets
func (c *checker) GetState(client client.Client, item client.Object) (CheckState, error) {

result := CheckState{
Passed: true,
}
stage := c.policy.Stage(item)
if stage == "" {
result.Info = "no stage found"
return result, nil
}

result := CheckState{}
rulesetNames := utils.GetRuleSets(item)
// TODO: list ruleset, check match ruleset
// TODO: add ruleset anno by webhook

for _, name := range rulesetNames {
rs := &appsv1alpha1.RuleSet{}
if err := client.Get(context.TODO(), types.NamespacedName{Namespace: item.GetNamespace(), Name: name}, rs); err != nil {
Expand All @@ -70,32 +60,32 @@ func (c *checker) GetState(client client.Client, item client.Object) (CheckState
if detail.Name != item.GetName() {
continue
}
if detail.Stage != stage {
break
}
findStatus = true
if !detail.Passed {
result.Passed = false
result.Info += CollectInfo(name, rs.Status.Details[i])
result.Message += CollectInfo(name, rs.Status.Details[i])
}
result.States = append(result.States, State{
RuleSetName: name,
Detail: *rs.Status.Details[i],
Detail: rs.Status.Details[i],
})
}
if !findStatus {
result.Passed = false
result.Info = fmt.Sprintf("waiting for ruleset %s processing. ", rs.Name)
result.States = append(result.States, State{
RuleSetName: name,
Detail: &appsv1alpha1.Detail{
Passed: false,
},
})
result.Message += fmt.Sprintf("[waiting for ruleset %s processing. ]", rs.Name)
return result, nil
}
}
return result, nil
}

type CheckState struct {
States []State
Passed bool
Info string
States []State
Message string
}

func (cs *CheckState) InStage(stage string) bool {
Expand All @@ -104,12 +94,22 @@ func (cs *CheckState) InStage(stage string) bool {
return false
}
}
return true
return len(cs.States) > 0
}

func (cs *CheckState) InStageAndPassed(stage string) bool {
for _, state := range cs.States {
if state.Detail.Stage != stage || !state.Detail.Passed {
return false
}
}
return len(cs.States) > 0
}

type State struct {
RuleSetName string
Detail appsv1alpha1.Detail
Message string
Detail *appsv1alpha1.Detail
}

func CollectInfo(ruleset string, detail *appsv1alpha1.Detail) string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/ruleset/processor/rules/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ func (w *Webhook) timeOut(traceId string) bool {
if tm == nil || tm.BeginTime == nil {
return false
}
if w.Webhook.ClientConfig.TraceTimeout != nil {
timeOut = time.Duration(*w.Webhook.ClientConfig.TraceTimeout) * time.Second
if w.Webhook.ClientConfig.TraceTimeoutSeconds != nil {
timeOut = time.Duration(*w.Webhook.ClientConfig.TraceTimeoutSeconds) * time.Second
}
return timeNow.Sub(tm.BeginTime.Time) > timeOut
}
Expand All @@ -385,8 +385,8 @@ func (w *Webhook) outInterval(traceId string) (bool, time.Duration, time.Duratio
return true, 0, 0
}
interval := defaultInterval
if w.Webhook.ClientConfig.Interval != nil {
interval = time.Duration(*w.Webhook.ClientConfig.Interval) * time.Second
if w.Webhook.ClientConfig.IntervalSeconds != nil {
interval = time.Duration(*w.Webhook.ClientConfig.IntervalSeconds) * time.Second
}
allCost := time.Since(tm.BeginTime.Time)
nowInterval := time.Since(tm.LastTime.Time)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/ruleset/ruleset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestRuleset(t *testing.T) {
for i := range podList.Items {
state, err := RuleSetManager().GetState(c, &podList.Items[i])
g.Expect(err).NotTo(gomega.HaveOccurred())
if state.Passed && state.InStage(PreTrafficOffStage) {
if state.InStageAndPassed(PreTrafficOffStage) {
passedCount++
}
printJson(state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) (re
func SetDefaultRuleSet(rs *appsv1alpha1.RuleSet) {
for i := range rs.Spec.Rules {
if rs.Spec.Rules[i].Webhook != nil {
if rs.Spec.Rules[i].Webhook.ClientConfig.Interval == nil {
if rs.Spec.Rules[i].Webhook.ClientConfig.IntervalSeconds == nil {
interval := appsv1alpha1.DefaultWebhookInterval
rs.Spec.Rules[i].Webhook.ClientConfig.Interval = &interval
rs.Spec.Rules[i].Webhook.ClientConfig.IntervalSeconds = &interval
}
if rs.Spec.Rules[i].Webhook.ClientConfig.TraceTimeout == nil {
if rs.Spec.Rules[i].Webhook.ClientConfig.TraceTimeoutSeconds == nil {
timeout := appsv1alpha1.DefaultWebhookTimeout
rs.Spec.Rules[i].Webhook.ClientConfig.TraceTimeout = &timeout
rs.Spec.Rules[i].Webhook.ClientConfig.TraceTimeoutSeconds = &timeout
}
if rs.Spec.Rules[i].Webhook.FailurePolicy == nil {
failurePolicy := appsv1alpha1.Ignore
Expand Down

0 comments on commit 460d36b

Please sign in to comment.