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

✨ feat: Accessing old object on watchEvent #517

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

mhkarimi1383
Copy link

Overview

By using OldObject input from K8s we will have access to oldObject when modified

What this PR does / why we need it

Closes #515
I need this here: mhkarimi1383/reflector#3

Sometimes we need to have access to older object specially if we are managing our own CRD

Special notes for your reviewer

Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
@mhkarimi1383
Copy link
Author

I don't know if I have updated docs correctly, Also in Delete event we can fill OldObject instead of Object, Since we can Called removed object 'old'

@yalosev yalosev requested a review from nabokihms July 24, 2023 08:13
@mhkarimi1383
Copy link
Author

@nabokihms Can you review this?

@nabokihms
Copy link
Member

@mhkarimi1383 yes, sure. I've just put it in the queue.

@nabokihms nabokihms added the enhancement New feature or request label Jul 26, 2023
Copy link
Member

@nabokihms nabokihms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of suggestions. One more thing is that the unit test is also required for the feature.

pkg/kube_events_manager/resource_informer.go Outdated Show resolved Hide resolved
pkg/kube_events_manager/resource_informer.go Outdated Show resolved Hide resolved
mhkarimi1383 and others added 2 commits July 27, 2023 20:41
@mhkarimi1383 mhkarimi1383 requested a review from nabokihms July 27, 2023 17:46
@mhkarimi1383
Copy link
Author

mhkarimi1383 commented Jul 27, 2023

I will add unit test for that later on, I have not worked with Golang unit tests alot :)

Can you give me a context of how should I test this thing? I mean testing the input and output is good enough or I have to do a more advaced test

Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
@mhkarimi1383
Copy link
Author

@nabokihms Hi, I have fixed linter and unit test error.!

The only left thing is the feature unit tests :)

Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
@mhkarimi1383
Copy link
Author

@nabokihms

Any updates on this? My project development blocked 😕

@mhkarimi1383
Copy link
Author

@nabokihms
What is this error?

pkg/kube_events_manager/resource_informer.go:302: File is not `gci`-ed with --skip-generated -s standard,default,prefix(github.com/flant/) (gci)

Comment on lines +28 to +75
"oldObject": {
"apiVersion": "apps/v1",
"kind": "DaemonSet",
"metadata": {
"name": "flannel",
"namespace": "d8-flannel"
},
"spec": {
"selector": {
"matchLabels": {
"app": "flannel"
}
},
"template": {
"metadata": {
"labels": {
"app": "flannel",
"tier": "old-node"
}
},
"spec": {
"containers": [
{
"args": [
"--ip-masq",
"--kube-subnet-mgr"
],
"image": "flannel:v0.11",
"name": "kube-flannel",
"securityContext": {
"privileged": true
}
}
],
"hostNetwork": true,
"imagePullSecrets": [
{
"name": "registry"
}
],
"terminationGracePeriodSeconds": 5
}
},
"updateStrategy": {
"type": "RollingUpdate"
}
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"oldObject": {
"apiVersion": "apps/v1",
"kind": "DaemonSet",
"metadata": {
"name": "flannel",
"namespace": "d8-flannel"
},
"spec": {
"selector": {
"matchLabels": {
"app": "flannel"
}
},
"template": {
"metadata": {
"labels": {
"app": "flannel",
"tier": "old-node"
}
},
"spec": {
"containers": [
{
"args": [
"--ip-masq",
"--kube-subnet-mgr"
],
"image": "flannel:v0.11",
"name": "kube-flannel",
"securityContext": {
"privileged": true
}
}
],
"hostNetwork": true,
"imagePullSecrets": [
{
"name": "registry"
}
],
"terminationGracePeriodSeconds": 5
}
},
"updateStrategy": {
"type": "RollingUpdate"
}
}
},

This is not the right doc. The doc is about Kubernetes object modification, not about binding contexts.
The right doc is here https://github.com/flant/shell-operator/blob/main/docs/src/HOOKS.md#kubernetes-binding-context-example

pkg/kube_events_manager/resource_informer.go Show resolved Hide resolved
@nabokihms
Copy link
Member

I fixed the linter, but PR doesn't work because there are other parts missing.

For example, to finally see an old object in a hook, this file requires changes as well
https://github.com/flant/shell-operator/blob/main/pkg/hook/binding_context/binding_context.go

And probably this one

func ConvertKubeEventToBindingContext(kubeEvent KubeEvent, link *KubernetesBindingToMonitorLink) []BindingContext {
bindingContexts := make([]BindingContext, 0)
switch kubeEvent.Type {
case TypeSynchronization:
bc := BindingContext{
Binding: link.BindingConfig.BindingName,
Type: kubeEvent.Type,
Objects: kubeEvent.Objects,
}
bc.Metadata.JqFilter = link.BindingConfig.Monitor.JqFilter
bc.Metadata.BindingType = OnKubernetesEvent
bc.Metadata.IncludeSnapshots = link.BindingConfig.IncludeSnapshotsFrom
bc.Metadata.Group = link.BindingConfig.Group
bindingContexts = append(bindingContexts, bc)
case TypeEvent:
for _, kEvent := range kubeEvent.WatchEvents {
bc := BindingContext{
Binding: link.BindingConfig.BindingName,
Type: kubeEvent.Type,
WatchEvent: kEvent,
Objects: kubeEvent.Objects,
}
bc.Metadata.JqFilter = link.BindingConfig.Monitor.JqFilter
bc.Metadata.BindingType = OnKubernetesEvent
bc.Metadata.IncludeSnapshots = link.BindingConfig.IncludeSnapshotsFrom
bc.Metadata.Group = link.BindingConfig.Group
bindingContexts = append(bindingContexts, bc)
}
}
return bindingContexts
}

One more thing, without adding tests, we will not be able to merge the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accessing old object on watchEvent
2 participants