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

fix and upgrade podTransitionRule webhook #133

Merged
merged 6 commits into from
Dec 28, 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
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 @@
"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 @@ -82,6 +83,10 @@
return c, err
}

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

Check warning on line 89 in pkg/controllers/podtransitionrule/podtransitionrule_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/podtransitionrule/podtransitionrule_controller.go#L88-L89

Added lines #L88 - L89 were not covered by tests
return c, nil
}

Expand Down Expand Up @@ -296,15 +301,6 @@
})
}

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 @@ -331,8 +327,12 @@
}

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
Loading