From 2dac6eb6a2edab3cdb5a9b644136bb319f0a6038 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Mon, 24 Jul 2023 10:25:51 +0330 Subject: [PATCH 01/10] =?UTF-8?q?=E2=9C=A8=20feat:=20Accessing=20old=20obj?= =?UTF-8?q?ect=20on=20`watchEvent`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- docs/src/KUBERNETES.md | 57 ++++++++++++++++++-- pkg/kube_events_manager/resource_informer.go | 12 +++-- pkg/kube_events_manager/types/types.go | 1 + 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/docs/src/KUBERNETES.md b/docs/src/KUBERNETES.md index 402aa4e7..81c4633b 100644 --- a/docs/src/KUBERNETES.md +++ b/docs/src/KUBERNETES.md @@ -12,11 +12,11 @@ The path to the file is found in the `$KUBERNETES_PATCH_PATH` environment variab ### Create * `operation` — specifies an operation's type. - * `CreateOrUpdate` — accept a Kubernetes object. + * `CreateOrUpdate` — accept a Kubernetes object. It retrieves an object, and if it already exists, computes a JSON Merge Patch and applies it (will not update .status field). If it does not exist, we create the object. - * `Create` — will fail if an object already exists - * `CreateIfNotExists` — create an object if such an object does not already + * `Create` — will fail if an object already exists + * `CreateIfNotExists` — create an object if such an object does not already exist by namespace/name. * `object` — full object specification including "apiVersion", "kind" and all necessary metadata. Can be a normal JSON or YAML object or a stringified JSON or YAML object. @@ -25,6 +25,54 @@ The path to the file is found in the `$KUBERNETES_PATCH_PATH` environment variab ```json { "operation": "CreateOrUpdate", + "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" + } + } + }, "object": { "apiVersion": "apps/v1", "kind": "DaemonSet", @@ -123,7 +171,7 @@ object: | ### Patch -Use `JQPatch` for almost everything. Consider using `MergePatch` or `JSONPatch` if you are attempting to modify +Use `JQPatch` for almost everything. Consider using `MergePatch` or `JSONPatch` if you are attempting to modify rapidly changing object, for example `status` field with many concurrent changes (and incrementing `resourceVersion`). Be careful, when updating a `.status` field. If a `/status` subresource is enabled on a resource, @@ -140,6 +188,7 @@ More info [here][spec-and-status]. * `name` — object's name. * `jqFilter` — describes transformations to perform on an object. * `subresource` — a subresource name if subresource is to be transformed. For example, `status`. + ##### Example ```json diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index db75f57f..923a2b63 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -266,22 +266,22 @@ func (ei *resourceInformer) LoadExistedObjects() error { } func (ei *resourceInformer) OnAdd(obj interface{}) { - ei.HandleWatchEvent(obj, WatchEventAdded) + ei.HandleWatchEvent(nil, obj, WatchEventAdded) } -func (ei *resourceInformer) OnUpdate(_, newObj interface{}) { - ei.HandleWatchEvent(newObj, WatchEventModified) +func (ei *resourceInformer) OnUpdate(oldObj, newObj interface{}) { + ei.HandleWatchEvent(oldObj, newObj, WatchEventModified) } func (ei *resourceInformer) OnDelete(obj interface{}) { - ei.HandleWatchEvent(obj, WatchEventDeleted) + ei.HandleWatchEvent(nil, obj, WatchEventDeleted) } // HandleKubeEvent register object in cache. Pass object to callback if object's checksum is changed. // TODO refactor: pass KubeEvent as argument // TODO add delay to merge Added and Modified events (node added and then labels applied — one hook run on Added+Modified is enough) // func (ei *resourceInformer) HandleKubeEvent(obj *unstructured.Unstructured, objectId string, filterResult string, newChecksum string, eventType WatchEventType) { -func (ei *resourceInformer) HandleWatchEvent(object interface{}, eventType WatchEventType) { +func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, eventType WatchEventType) { // check if stop if ei.stopped { return @@ -296,6 +296,7 @@ func (ei *resourceInformer) HandleWatchEvent(object interface{}, eventType Watch object = staleObj.Obj } obj := object.(*unstructured.Unstructured) + oldObj := object.(*unstructured.Unstructured) resourceId := ResourceId(obj) @@ -308,6 +309,7 @@ func (ei *resourceInformer) HandleWatchEvent(object interface{}, eventType Watch ei.metricStorage.HistogramObserve("{PREFIX}kube_jq_filter_duration_seconds", d.Seconds(), ei.Monitor.Metadata.MetricLabels, nil) })() objFilterRes, err = ApplyFilter(ei.Monitor.JqFilter, ei.Monitor.FilterFunc, obj) + objFilterRes.OldObject = oldObj }() if err != nil { log.Errorf("%s: WATCH %s: %s", diff --git a/pkg/kube_events_manager/types/types.go b/pkg/kube_events_manager/types/types.go index 63f53b83..64878487 100644 --- a/pkg/kube_events_manager/types/types.go +++ b/pkg/kube_events_manager/types/types.go @@ -41,6 +41,7 @@ type ObjectAndFilterResult struct { RemoveObject bool } Object *unstructured.Unstructured // here is a pointer because of MarshalJSON receiver + OldObject *unstructured.Unstructured // here is a pointer because of MarshalJSON receiver and also it's nullable for some events FilterResult interface{} } From 35c9608558d6177afc566f0bf56f872329277246 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein karimi Date: Thu, 27 Jul 2023 20:41:53 +0330 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=90=9B=20fix:=20some=20typos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maksim Nabokikh --- pkg/kube_events_manager/resource_informer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 923a2b63..75a5a357 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -296,7 +296,7 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even object = staleObj.Obj } obj := object.(*unstructured.Unstructured) - oldObj := object.(*unstructured.Unstructured) + oldObj := oldObject.(*unstructured.Unstructured) resourceId := ResourceId(obj) From 7e5b91c79676db5790c5173b762833eb0758f7df Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Thu, 27 Jul 2023 21:13:58 +0330 Subject: [PATCH 03/10] =?UTF-8?q?=F0=9F=90=9B=20fix:=20nil=20check=20for?= =?UTF-8?q?=20old=20object=20and=20better=20place=20to=20add=20it.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/kube_events_manager/resource_informer.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 75a5a357..50b92daf 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -309,7 +309,6 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even ei.metricStorage.HistogramObserve("{PREFIX}kube_jq_filter_duration_seconds", d.Seconds(), ei.Monitor.Metadata.MetricLabels, nil) })() objFilterRes, err = ApplyFilter(ei.Monitor.JqFilter, ei.Monitor.FilterFunc, obj) - objFilterRes.OldObject = oldObj }() if err != nil { log.Errorf("%s: WATCH %s: %s", @@ -321,6 +320,10 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even if !ei.Monitor.KeepFullObjectsInMemory { objFilterRes.RemoveFullObject() + } else { + if oldObj != nil { + objFilterRes.OldObject = oldObj + } } // Do not fire Added or Modified if object is in cache and its checksum is equal to the newChecksum. From a63e38727144c6e30f4c55c39f9ccd170e5553c7 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Tue, 1 Aug 2023 13:09:15 +0330 Subject: [PATCH 04/10] =?UTF-8?q?=F0=9F=90=9B=20fix:=20using=20elseif=20in?= =?UTF-8?q?stead=20if=20else=20(golangci-lint)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- pkg/kube_events_manager/resource_informer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 50b92daf..f90fc009 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -320,7 +320,7 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even if !ei.Monitor.KeepFullObjectsInMemory { objFilterRes.RemoveFullObject() - } else { + } else if ei.Monitor.KeepFullObjectsInMemory { if oldObj != nil { objFilterRes.OldObject = oldObj } From b8b9fcc482f5efe76ee9d960abe81c158db4f179 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Tue, 1 Aug 2023 13:14:30 +0330 Subject: [PATCH 05/10] =?UTF-8?q?=F0=9F=90=9B=20fix:=20nil=20check=20for?= =?UTF-8?q?=20type=20casting=20in=20oldObj?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- pkg/kube_events_manager/resource_informer.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index f90fc009..12aa6a3a 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -296,7 +296,11 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even object = staleObj.Obj } obj := object.(*unstructured.Unstructured) - oldObj := oldObject.(*unstructured.Unstructured) + + var oldObj *unstructured.Unstructured = nil + if oldObject != nil { + oldObj = oldObject.(*unstructured.Unstructured) + } resourceId := ResourceId(obj) From c9c0672f5827492bc5e74f35ba233c7fb43918ea Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Thu, 3 Aug 2023 09:50:11 +0330 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=90=9B=20fix:=20remove=20nil=20assi?= =?UTF-8?q?gnment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- pkg/kube_events_manager/resource_informer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 12aa6a3a..b785ff66 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -297,7 +297,7 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even } obj := object.(*unstructured.Unstructured) - var oldObj *unstructured.Unstructured = nil + var oldObj *unstructured.Unstructured if oldObject != nil { oldObj = oldObject.(*unstructured.Unstructured) } From 570d469f9189fbac2b5a62a18cf668aad21ebf4f Mon Sep 17 00:00:00 2001 From: Maksim Nabokikh Date: Thu, 17 Aug 2023 22:52:39 +0200 Subject: [PATCH 07/10] Apply suggestions from code review --- pkg/kube_events_manager/resource_informer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index b785ff66..b3767290 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -299,7 +299,7 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even var oldObj *unstructured.Unstructured if oldObject != nil { - oldObj = oldObject.(*unstructured.Unstructured) + oldObj = oldObject.(*unstructured.Unstructured) } resourceId := ResourceId(obj) From 4d68ab21055f3e467eb12fe31fd1967486b65896 Mon Sep 17 00:00:00 2001 From: Maksim Nabokikh Date: Thu, 17 Aug 2023 22:53:09 +0200 Subject: [PATCH 08/10] Apply suggestions from code review --- pkg/kube_events_manager/resource_informer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index b3767290..2c174f51 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -300,7 +300,7 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even var oldObj *unstructured.Unstructured if oldObject != nil { oldObj = oldObject.(*unstructured.Unstructured) - } + } resourceId := ResourceId(obj) From 29a6b17d923b79ea88f787285d6c8c0302c52193 Mon Sep 17 00:00:00 2001 From: Maksim Nabokikh Date: Thu, 17 Aug 2023 22:57:14 +0200 Subject: [PATCH 09/10] Update pkg/kube_events_manager/resource_informer.go --- pkg/kube_events_manager/resource_informer.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 2c174f51..4fa17722 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -324,10 +324,9 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even if !ei.Monitor.KeepFullObjectsInMemory { objFilterRes.RemoveFullObject() - } else if ei.Monitor.KeepFullObjectsInMemory { - if oldObj != nil { - objFilterRes.OldObject = oldObj - } + } else if oldObj != nil { + objFilterRes.OldObject = oldObj + } } // Do not fire Added or Modified if object is in cache and its checksum is equal to the newChecksum. From a47cbbeef89998f9f1460bf70d5b5f462c6bb406 Mon Sep 17 00:00:00 2001 From: Maksim Nabokikh Date: Thu, 17 Aug 2023 23:02:04 +0200 Subject: [PATCH 10/10] Update pkg/kube_events_manager/resource_informer.go --- pkg/kube_events_manager/resource_informer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/kube_events_manager/resource_informer.go b/pkg/kube_events_manager/resource_informer.go index 4fa17722..e66d7a7e 100644 --- a/pkg/kube_events_manager/resource_informer.go +++ b/pkg/kube_events_manager/resource_informer.go @@ -327,7 +327,6 @@ func (ei *resourceInformer) HandleWatchEvent(oldObject, object interface{}, even } else if oldObj != nil { objFilterRes.OldObject = oldObj } - } // Do not fire Added or Modified if object is in cache and its checksum is equal to the newChecksum. // Delete is always fired.