Skip to content

Commit

Permalink
fix and upgrade podTransitionRule webhook (#133)
Browse files Browse the repository at this point in the history
* fix podTransitionRule webhook interval and rename PodTransitionRule shortName

* enhance: PT webhook polling

* delete unused code

* enhance: delete PT webhook ItemStatus

* add test case

* add podtransitionrule test case
  • Loading branch information
Eikykun committed Dec 28, 2023
1 parent 1cf4fbc commit 4181742
Show file tree
Hide file tree
Showing 15 changed files with 897 additions and 419 deletions.
23 changes: 8 additions & 15 deletions apis/apps/v1alpha1/podtransitionrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,35 +182,28 @@ type RuleState struct {

// WebhookStatus defines the webhook processing status
type WebhookStatus struct {
// PodTransitionRulePodStatus is async request status representing the info of pods
ItemStatus []*ItemStatus `json:"itemStatus,omitempty"`

// TaskStates is a list of tracing info
TaskStates []TaskInfo `json:"taskStates,omitempty"`

// History records history taskStates which were finished or failed. Valid for 10 minutes
History []TaskInfo `json:"history,omitempty"`
}

type TaskInfo struct {
TaskId string `json:"taskId,omitempty"`

Processing []string `json:"processing,omitempty"`

Approved []string `json:"approved,omitempty"`

BeginTime *metav1.Time `json:"beginTime,omitempty"`

LastTime *metav1.Time `json:"lastTime,omitempty"`

Message string `json:"message,omitempty"`
}

// ItemStatus defines async request info of resources
type ItemStatus struct {
// Name representing the name of pod
Name string `json:"name,omitempty"`

// WebhookChecked representing the pod has pass check
WebhookChecked bool `json:"webhookChecked"`

// TraceId representing poll request taskId
TaskId string `json:"taskId,omitempty"`
}

type PodTransitionDetail struct {
// Name representing Pod name
Name string `json:"name,omitempty"`
Expand All @@ -231,7 +224,7 @@ type RejectInfo struct {
// +k8s:openapi-gen=true
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=rs
// +kubebuilder:resource:shortName=ptr

// PodTransitionRule is the Schema for the podtransitionrules API
type PodTransitionRule struct {
Expand Down
41 changes: 16 additions & 25 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.

42 changes: 27 additions & 15 deletions config/crd/bases/apps.kusionstack.io_podtransitionrules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spec:
listKind: PodTransitionRuleList
plural: podtransitionrules
shortNames:
- rs
- ptr
singular: podtransitionrule
scope: Namespaced
versions:
Expand Down Expand Up @@ -374,31 +374,39 @@ spec:
description: WebhookStatus is the webhook status representing
processing progress
properties:
itemStatus:
description: PodTransitionRulePodStatus is async request
status representing the info of pods
history:
description: History records history taskStates which were
finished or failed. Valid for 10 minutes
items:
description: ItemStatus defines async request info of
resources
properties:
name:
description: Name representing the name of pod
approved:
items:
type: string
type: array
beginTime:
format: date-time
type: string
lastTime:
format: date-time
type: string
message:
type: string
processing:
items:
type: string
type: array
taskId:
description: TraceId representing poll request taskId
type: string
webhookChecked:
description: WebhookChecked representing the pod has
pass check
type: boolean
required:
- webhookChecked
type: object
type: array
taskStates:
description: TaskStates is a list of tracing info
items:
properties:
approved:
items:
type: string
type: array
beginTime:
format: date-time
type: string
Expand All @@ -407,6 +415,10 @@ spec:
type: string
message:
type: string
processing:
items:
type: string
type: array
taskId:
type: string
type: object
Expand Down
7 changes: 7 additions & 0 deletions pkg/controllers/podtransitionrule/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"

appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
processorrules "kusionstack.io/operating/pkg/controllers/podtransitionrule/processor/rules"
commonutils "kusionstack.io/operating/pkg/utils"
)

var _ inject.Client = &EventHandler{}
var _ inject.Logger = &EventHandler{}

func NewWebhookGenericEventChannel() <-chan event.GenericEvent {
webhookTriggerChannel := make(chan event.GenericEvent, 1<<10)
processorrules.PollingManager.AddListener(webhookTriggerChannel)
return webhookTriggerChannel
}

type EventHandler struct {
// client and logger will be injected
client client.Client
Expand Down
20 changes: 10 additions & 10 deletions pkg/controllers/podtransitionrule/podtransitionrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -81,6 +82,10 @@ func addToMgr(mgr manager.Manager, r reconcile.Reconciler) (controller.Controlle
return c, err
}

err = c.Watch(&source.Channel{Source: NewWebhookGenericEventChannel()}, &handler.EnqueueRequestForObject{})
if err != nil {
return c, err
}
return c, nil
}

Expand Down Expand Up @@ -295,15 +300,6 @@ func (r *PodTransitionRuleReconciler) updatePodTransitionRuleOnPod(ctx context.C
})
}

func (r *PodTransitionRuleReconciler) hasRunningPod(pods *corev1.PodList) bool {
for _, pod := range pods.Items {
if pod.DeletionTimestamp == nil {
return true
}
}
return false
}

func updateDetail(details map[string]*appsv1alpha1.PodTransitionDetail, passRules *processor.ProcessResult, stage string) {
for po, rules := range passRules.PassRules {
var rejectInfo *appsv1alpha1.RejectInfo
Expand All @@ -330,8 +326,12 @@ func updateDetail(details map[string]*appsv1alpha1.PodTransitionDetail, passRule
}

func equalStatus(updated *appsv1alpha1.PodTransitionRuleStatus, current *appsv1alpha1.PodTransitionRuleStatus) bool {
return equality.Semantic.DeepEqual(updated.Targets, current.Targets) &&
deepEqual := equality.Semantic.DeepEqual(updated.Targets, current.Targets) &&
equality.Semantic.DeepEqual(updated.Details, current.Details) &&
equality.Semantic.DeepEqual(updated.RuleStates, current.RuleStates) &&
updated.ObservedGeneration == current.ObservedGeneration
if !deepEqual {
return utils.DumpJSON(updated) == utils.DumpJSON(current)
}
return deepEqual
}
Loading

0 comments on commit 4181742

Please sign in to comment.