argoproj / gitops-engine

Democratizing GitOps
https://pkg.go.dev/github.com/argoproj/gitops-engine?tab=subdirectories
Apache License 2.0
1.7k stars 260 forks source link

Somethingwrong with clusterCache.EnsureSynced() #408

Open biubiudu opened 2 years ago

biubiudu commented 2 years ago

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