gitops-engine icon indicating copy to clipboard operation
gitops-engine copied to clipboard

Somethingwrong with clusterCache.EnsureSynced()

Open biubiudu opened this issue 3 years ago • 0 comments

config := &rest.Config{}  
clusterCache := cache.NewClusterCache(config,
		// cache default namespace only
		cache.SetNamespaces([]string{"default", "kube-system"}),
		// configure custom logic to cache resources manifest and additional metadata
		cache.SetPopulateResourceInfoHandler(func(un *unstructured.Unstructured, isRoot bool) (info interface{}, cacheManifest bool) {
			// if resource belongs to 'extensions' group then mark if with 'deprecated' label
			if un.GroupVersionKind().Group == "extensions" {
				info = []string{"deprecated"}
			}
			_, ok := un.GetLabels()["acme.io/my-label"]
			// cache whole manifest if resource has label
			cacheManifest = ok
			return
		}),
	)
	// Ensure cluster is synced before using it
	if err := clusterCache.EnsureSynced(); err != nil {
		panic(err)
	}
	// Iterate default namespace resources tree
	for _, root := range clusterCache.FindResources("default", cache.TopLevelResource) {
		clusterCache.IterateHierarchy(root.ResourceKey(), func(resource *cache.Resource, _ map[kube.ResourceKey]*cache.Resource) bool {
			fmt.Printf("name:%s, resource: %s, \n\n", resource.Ref.Name, resource.Ref.String())
			return true
		})
	} 

**When i use the example code, it will report an error**

I0507 17:59:03.375877   49921 cluster.go:688]  "msg"="Start syncing cluster"  
panic: SchemaError(dev.oam.core.v1beta1.ApplicationRevision.spec.referredObjects): array should have exactly one sub-item 

biubiudu avatar May 07 '22 10:05 biubiudu