From e2140bc7098252cc8c1ffaed45904292b0c907e9 Mon Sep 17 00:00:00 2001 From: Sebastien THOMAS Date: Mon, 27 Nov 2023 16:34:27 -0500 Subject: [PATCH 1/5] cluster log diff --- go.mod | 2 +- pkg/cache/cluster.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9e4b02627..4670c219a 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-logr/logr v1.2.3 github.com/golang/mock v1.6.0 github.com/google/gnostic v0.5.7-v3refs + github.com/google/go-cmp v0.5.9 github.com/spf13/cobra v1.6.0 github.com/stretchr/testify v1.8.1 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 @@ -45,7 +46,6 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/btree v1.0.1 // indirect - github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.1.0 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/google/uuid v1.1.2 // indirect diff --git a/pkg/cache/cluster.go b/pkg/cache/cluster.go index 02ca18911..c68a0c378 100644 --- a/pkg/cache/cluster.go +++ b/pkg/cache/cluster.go @@ -10,6 +10,7 @@ import ( "time" "github.com/go-logr/logr" + "github.com/google/go-cmp/cmp" "golang.org/x/sync/semaphore" authorizationv1 "k8s.io/api/authorization/v1" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -659,6 +660,19 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo return fmt.Errorf("Failed to convert to *unstructured.Unstructured: %v", event.Object) } + if kube.IsCRD(obj) { + resourceKey := kube.GetResourceKey(obj) + crd := v1.CustomResourceDefinition{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd) + if err != nil { + c.log.Error(err, "Failed to extract CRD resources") + } + // fmt.Printf("got watchEvents for %s (%s/%s): %v \n", resourceKey, c.resources[resourceKey].ResourceVersion, crd.ResourceVersion, obj) + if diff := cmp.Diff(c.resources[resourceKey], crd); diff != "" { + fmt.Printf("watchEvents mismatch for CRD %s (-want +got):\n%s\n", resourceKey, diff) + } + + } c.processEvent(event.Type, obj) if kube.IsCRD(obj) { var resources []kube.APIResourceInfo From f9141a265395bdbc1b4c1af31f809aeaea9af72b Mon Sep 17 00:00:00 2001 From: Sebastien THOMAS Date: Mon, 27 Nov 2023 17:04:01 -0500 Subject: [PATCH 2/5] diff real resource --- pkg/cache/cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cache/cluster.go b/pkg/cache/cluster.go index c68a0c378..f317676e9 100644 --- a/pkg/cache/cluster.go +++ b/pkg/cache/cluster.go @@ -668,7 +668,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo c.log.Error(err, "Failed to extract CRD resources") } // fmt.Printf("got watchEvents for %s (%s/%s): %v \n", resourceKey, c.resources[resourceKey].ResourceVersion, crd.ResourceVersion, obj) - if diff := cmp.Diff(c.resources[resourceKey], crd); diff != "" { + if diff := cmp.Diff(c.resources[resourceKey].Resource, crd); diff != "" { fmt.Printf("watchEvents mismatch for CRD %s (-want +got):\n%s\n", resourceKey, diff) } From e626892c2d3b44736f8171f27af2000f59713890 Mon Sep 17 00:00:00 2001 From: Sebastien THOMAS Date: Mon, 27 Nov 2023 17:11:16 -0500 Subject: [PATCH 3/5] use resource object from cache --- pkg/cache/cluster.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/cache/cluster.go b/pkg/cache/cluster.go index f317676e9..079ed16d5 100644 --- a/pkg/cache/cluster.go +++ b/pkg/cache/cluster.go @@ -667,8 +667,14 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo if err != nil { c.log.Error(err, "Failed to extract CRD resources") } + + cachedCRD := v1.CustomResourceDefinition{} + err = runtime.DefaultUnstructuredConverter.FromUnstructured(c.resources[resourceKey].Resource.Object, &cachedCRD) + if err != nil { + c.log.Error(err, "Failed to extract CRD resources") + } // fmt.Printf("got watchEvents for %s (%s/%s): %v \n", resourceKey, c.resources[resourceKey].ResourceVersion, crd.ResourceVersion, obj) - if diff := cmp.Diff(c.resources[resourceKey].Resource, crd); diff != "" { + if diff := cmp.Diff(cachedCRD, crd); diff != "" { fmt.Printf("watchEvents mismatch for CRD %s (-want +got):\n%s\n", resourceKey, diff) } From 2f0d2038cc9cd0b72ca4a114c09e148a43829b02 Mon Sep 17 00:00:00 2001 From: Sebastien THOMAS Date: Mon, 27 Nov 2023 17:28:15 -0500 Subject: [PATCH 4/5] add skipCRD --- pkg/cache/cluster.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/cache/cluster.go b/pkg/cache/cluster.go index 079ed16d5..cd2260211 100644 --- a/pkg/cache/cluster.go +++ b/pkg/cache/cluster.go @@ -660,6 +660,9 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo return fmt.Errorf("Failed to convert to *unstructured.Unstructured: %v", event.Object) } + // skipCRD will be set to true if the resource is a CRD but the change is not important + skipCRD := true + if kube.IsCRD(obj) { resourceKey := kube.GetResourceKey(obj) crd := v1.CustomResourceDefinition{} @@ -673,14 +676,22 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo if err != nil { c.log.Error(err, "Failed to extract CRD resources") } + + // remove useless stuff before we diff + cachedCRD.ResourceVersion = "0" + crd.ResourceVersion = "0" + cachedCRD.ManagedFields = []metav1.ManagedFieldsEntry{} + crd.ManagedFields = []metav1.ManagedFieldsEntry{} // fmt.Printf("got watchEvents for %s (%s/%s): %v \n", resourceKey, c.resources[resourceKey].ResourceVersion, crd.ResourceVersion, obj) if diff := cmp.Diff(cachedCRD, crd); diff != "" { fmt.Printf("watchEvents mismatch for CRD %s (-want +got):\n%s\n", resourceKey, diff) + // the CRD is really different, process it + skipCRD = false } } c.processEvent(event.Type, obj) - if kube.IsCRD(obj) { + if kube.IsCRD(obj) && !skipCRD { var resources []kube.APIResourceInfo crd := v1.CustomResourceDefinition{} err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd) From 518494f162836609e713b90af702d6ff3aba52db Mon Sep 17 00:00:00 2001 From: Sebastien THOMAS Date: Mon, 27 Nov 2023 17:35:03 -0500 Subject: [PATCH 5/5] add debug --- pkg/cache/cluster.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cache/cluster.go b/pkg/cache/cluster.go index cd2260211..782fc2109 100644 --- a/pkg/cache/cluster.go +++ b/pkg/cache/cluster.go @@ -684,7 +684,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo crd.ManagedFields = []metav1.ManagedFieldsEntry{} // fmt.Printf("got watchEvents for %s (%s/%s): %v \n", resourceKey, c.resources[resourceKey].ResourceVersion, crd.ResourceVersion, obj) if diff := cmp.Diff(cachedCRD, crd); diff != "" { - fmt.Printf("watchEvents mismatch for CRD %s (-want +got):\n%s\n", resourceKey, diff) + fmt.Printf("watchEvents mismatch for CRD after cleanup %s (-want +got):\n%s\n", resourceKey, diff) // the CRD is really different, process it skipCRD = false } @@ -692,6 +692,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo } c.processEvent(event.Type, obj) if kube.IsCRD(obj) && !skipCRD { + fmt.Printf("processing CRD event\n") var resources []kube.APIResourceInfo crd := v1.CustomResourceDefinition{} err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd)