Files
open-cluster-management/pkg/work/spoke/auth/cache/loader.go
Jian Zhu 7332a585c0 🌱 add a verify rule for golang files import order (#177)
* 🌱 add a verify rule for golang files import order

This PR uses the [gci tool](https://github.com/daixiang0/gci) to make all go files' import section with a specific order, it will organize import with group with order:
1. standard library modules
2. 3rd party modules
3. modules in OCM org, like the `open-cluster-management.io/api`
4. current project `open-cluster-management.io/ocm` modules

developers can use the `make fmt-imports` to format the import automatically and the `make verify-fmt-imports` to check for any violation.

Signed-off-by: zhujian <jiazhu@redhat.com>

* 🌱 format the go files import

Signed-off-by: zhujian <jiazhu@redhat.com>

---------

Signed-off-by: zhujian <jiazhu@redhat.com>
2023-06-12 10:23:04 -04:00

87 lines
2.9 KiB
Go

package cache
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
worklister "open-cluster-management.io/api/client/work/listers/work/v1"
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/work/helper"
"open-cluster-management.io/ocm/pkg/work/spoke/auth/store"
)
type manifestWorkExecutorCachesLoader interface {
// loadAllValuableCaches gets all manifestworks with service account executor at the current time, and then
// stores these executors and corresponding resources in the form of store.ExecutorCaches data structure.
// Note that this method only guarantees the correctness of all keys in store.ExecutorCaches, and the
// value is usually fake. so callers are recommended to only use this to know what executors and resources
// should be cached in the current state of the cluster.
loadAllValuableCaches(*store.ExecutorCaches)
}
type defaultManifestWorkExecutorCachesLoader struct {
manifestWorkLister worklister.ManifestWorkNamespaceLister
restMapper meta.RESTMapper
}
func (g *defaultManifestWorkExecutorCachesLoader) loadAllValuableCaches(retainableCache *store.ExecutorCaches) {
if retainableCache == nil {
return
}
mws, err := g.manifestWorkLister.List(labels.Everything())
if err != nil {
klog.Infof("cleanup cache, list manifestworks error: %v", err)
return
}
for _, mw := range mws {
var executor string
if mw.Spec.Executor == nil {
continue
}
if mw.Spec.Executor.Subject.Type != workapiv1.ExecutorSubjectTypeServiceAccount {
continue
}
if mw.Spec.Executor.Subject.ServiceAccount == nil {
continue
}
executor = store.ExecutorKey(
mw.Spec.Executor.Subject.ServiceAccount.Namespace, mw.Spec.Executor.Subject.ServiceAccount.Name)
for index, manifest := range mw.Spec.Workload.Manifests {
// parse the required and set resource meta
required := &unstructured.Unstructured{}
if err := required.UnmarshalJSON(manifest.Raw); err != nil {
klog.Infof("UnmarshalJSON for the manifest work %s index %d failed %v", mw.Name, index, err)
continue
}
resMeta, gvr, err := helper.BuildResourceMeta(index, required, g.restMapper)
if err != nil {
klog.Infof("Build resource meta for the manifest work %s index %d failed %v", mw.Name, index, err)
continue
}
// check if the resource to be applied should be owned by the manifest work
ownedByTheWork := helper.OwnedByTheWork(gvr, resMeta.Namespace, resMeta.Name,
mw.Spec.DeleteOption)
retainableCache.Upsert(executor, store.Dimension{
Group: resMeta.Group,
Version: resMeta.Version,
Resource: resMeta.Resource,
Namespace: resMeta.Namespace,
Name: resMeta.Name,
ExecuteAction: store.GetExecuteAction(ownedByTheWork),
},
nil, // nil means we do not know if it is allowed or not
)
}
}
}