Files
kubevela/pkg/cache/informer.go
Ai Ranthem a5606b7808
Some checks failed
CodeQL / Analyze (go) (push) Failing after 6m23s
Definition-Lint / definition-doc (push) Failing after 3m8s
E2E MultiCluster Test / detect-noop (push) Successful in 6s
E2E Test / detect-noop (push) Successful in 3s
Go / detect-noop (push) Successful in 2s
license / Check for unapproved licenses (push) Failing after 18s
Registry / publish-core-images (push) Failing after 1m4s
Unit-Test / detect-noop (push) Successful in 20s
Sync SDK / sync_sdk (push) Failing after 3m9s
Go / staticcheck (push) Successful in 6m17s
Go / check-diff (push) Failing after 19m4s
Go / check-core-image-build (push) Failing after 5m44s
Go / check-cli-image-build (push) Failing after 3m31s
Unit-Test / unit-tests (push) Failing after 13m54s
Go / lint (push) Failing after 1h53m27s
Scorecards supply-chain security / Scorecards analysis (push) Failing after 1m27s
E2E MultiCluster Test / e2e-multi-cluster-tests (v1.29) (push) Has been cancelled
E2E Test / e2e-tests (v1.29) (push) Has been cancelled
Go / check-windows (push) Has been cancelled
Chore: (deps): Update k8s to 1.29 (#6654)
* chore: update k8s to 1.29

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: unit test

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: e2e

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint and e2e test

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* test(e2e): increase timeout

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix e2e and scripts

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* make reviewable

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* rollback a unnecessary ut change

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* update go.mod to import merged workflow

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

---------

Signed-off-by: phantomnat <w.nattadej@gmail.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Co-authored-by: phantomnat <w.nattadej@gmail.com>
2025-01-03 07:54:42 +08:00

271 lines
8.3 KiB
Go

/*
Copyright 2023 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cache
import (
"context"
"sync"
"time"
"github.com/kubevela/pkg/util/k8s"
"github.com/kubevela/pkg/util/runtime"
"github.com/kubevela/pkg/util/singleton"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
utilfeature "k8s.io/apiserver/pkg/util/feature"
kcache "k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/features"
"github.com/oam-dev/kubevela/pkg/utils/apply"
)
var (
// ApplicationRevisionDefinitionCachePruneDuration the prune duration for application revision definition cache
ApplicationRevisionDefinitionCachePruneDuration = time.Hour
)
// ObjectCacheEntry entry for object cache
type ObjectCacheEntry[T any] struct {
ptr *T
refs sets.Set[string]
lastAccessed time.Time
}
// ObjectCache cache for objects
type ObjectCache[T any] struct {
mu sync.RWMutex
objects map[string]*ObjectCacheEntry[T]
}
// NewObjectCache create an object cache
func NewObjectCache[T any]() *ObjectCache[T] {
return &ObjectCache[T]{
objects: map[string]*ObjectCacheEntry[T]{},
}
}
// Get retrieve the cache entry
func (in *ObjectCache[T]) Get(hash string) *T {
in.mu.RLock()
defer in.mu.RUnlock()
if entry, found := in.objects[hash]; found {
return entry.ptr
}
return nil
}
// Add insert cache entry with ref, return the ptr of the entry
func (in *ObjectCache[T]) Add(hash string, obj *T, ref string) *T {
in.mu.Lock()
defer in.mu.Unlock()
if entry, found := in.objects[hash]; found {
entry.refs.Insert(ref)
entry.lastAccessed = time.Now()
return entry.ptr
}
in.objects[hash] = &ObjectCacheEntry[T]{
ptr: obj,
refs: sets.New[string](ref),
lastAccessed: time.Now(),
}
return obj
}
// DeleteRef delete ref for an obj
func (in *ObjectCache[T]) DeleteRef(hash string, ref string) {
in.mu.Lock()
defer in.mu.Unlock()
if entry, found := in.objects[hash]; found {
entry.refs.Delete(ref)
if entry.refs.Len() == 0 {
delete(in.objects, hash)
}
}
}
// Remap relocate the object ptr with given ref
func (in *ObjectCache[T]) Remap(m map[string]*T, ref string) {
for key, o := range m {
if hash, err := apply.ComputeSpecHash(o); err == nil {
m[key] = in.Add(hash, o, ref)
}
}
}
// Unmap drop all the hash object from the map
func (in *ObjectCache[T]) Unmap(m map[string]*T, ref string) {
for _, o := range m {
if hash, err := apply.ComputeSpecHash(o); err == nil {
in.DeleteRef(hash, ref)
}
}
}
// Size get the size of the cache
func (in *ObjectCache[T]) Size() int {
in.mu.RLock()
defer in.mu.RUnlock()
return len(in.objects)
}
// Prune remove outdated cache, return the pruned count
func (in *ObjectCache[T]) Prune(outdated time.Duration) int {
in.mu.Lock()
defer in.mu.Unlock()
cnt := 0
for key, entry := range in.objects {
if time.Now().After(entry.lastAccessed.Add(outdated)) {
delete(in.objects, key)
cnt++
}
}
return cnt
}
// DefinitionCache cache for definitions
type DefinitionCache struct {
ComponentDefinitionCache *ObjectCache[v1beta1.ComponentDefinition]
TraitDefinitionCache *ObjectCache[v1beta1.TraitDefinition]
WorkflowStepDefinitionCache *ObjectCache[v1beta1.WorkflowStepDefinition]
}
// NewDefinitionCache create DefinitionCache
func NewDefinitionCache() *DefinitionCache {
return &DefinitionCache{
ComponentDefinitionCache: NewObjectCache[v1beta1.ComponentDefinition](),
TraitDefinitionCache: NewObjectCache[v1beta1.TraitDefinition](),
WorkflowStepDefinitionCache: NewObjectCache[v1beta1.WorkflowStepDefinition](),
}
}
// RemapRevision remap all definitions in the given revision
func (in *DefinitionCache) RemapRevision(rev *v1beta1.ApplicationRevision) {
ref := client.ObjectKeyFromObject(rev).String()
in.ComponentDefinitionCache.Remap(rev.Spec.ComponentDefinitions, ref)
in.TraitDefinitionCache.Remap(rev.Spec.TraitDefinitions, ref)
in.WorkflowStepDefinitionCache.Remap(rev.Spec.WorkflowStepDefinitions, ref)
}
// UnmapRevision unmap definitions from the provided revision by the given ref
func (in *DefinitionCache) UnmapRevision(rev *v1beta1.ApplicationRevision) {
ref := client.ObjectKeyFromObject(rev).String()
in.ComponentDefinitionCache.Unmap(rev.Spec.ComponentDefinitions, ref)
in.TraitDefinitionCache.Unmap(rev.Spec.TraitDefinitions, ref)
in.WorkflowStepDefinitionCache.Unmap(rev.Spec.WorkflowStepDefinitions, ref)
}
// Start clear cache every duration
func (in *DefinitionCache) Start(ctx context.Context, store cache.Cache, duration time.Duration) {
informer := runtime.Must(store.GetInformer(ctx, &v1beta1.ApplicationRevision{}))
_, err := informer.AddEventHandler(kcache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if rev, ok := obj.(*v1beta1.ApplicationRevision); ok {
in.RemapRevision(rev)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if rev, ok := oldObj.(*v1beta1.ApplicationRevision); ok {
in.UnmapRevision(rev)
}
if rev, ok := newObj.(*v1beta1.ApplicationRevision); ok {
in.RemapRevision(rev)
}
},
DeleteFunc: func(obj interface{}) {
if rev, ok := obj.(*v1beta1.ApplicationRevision); ok {
in.UnmapRevision(rev)
}
},
})
if err != nil {
klog.ErrorS(err, "failed to add event handler for definition cache")
}
for {
select {
case <-ctx.Done():
return
default:
t0 := time.Now()
compDefPruned := in.ComponentDefinitionCache.Prune(duration)
traitDefPruned := in.TraitDefinitionCache.Prune(duration)
wsDefPruned := in.WorkflowStepDefinitionCache.Prune(duration)
klog.Infof("DefinitionCache prune finished. ComponentDefinition: %d(-%d), TraitDefinition: %d(-%d), WorkflowStepDefinition: %d(-%d). Time cost: %d ms.",
in.ComponentDefinitionCache.Size(), compDefPruned,
in.TraitDefinitionCache.Size(), traitDefPruned,
in.WorkflowStepDefinitionCache.Size(), wsDefPruned,
time.Since(t0).Microseconds())
time.Sleep(duration)
}
}
}
// DefaultDefinitionCache the default definition cache
var DefaultDefinitionCache = singleton.NewSingleton(NewDefinitionCache)
func filterUnnecessaryField(o client.Object) {
_ = k8s.DeleteAnnotation(o, "kubectl.kubernetes.io/last-applied-configuration")
o.SetManagedFields(nil)
}
func wrapTransformFunc[T client.Object](fn func(T)) kcache.TransformFunc {
return func(i interface{}) (interface{}, error) {
if o, ok := i.(T); ok {
filterUnnecessaryField(o)
fn(o)
return o, nil
}
return i, nil
}
}
// AddInformerTransformFuncToCacheOption add informer transform func to cache option
// This will filter out the unnecessary fields for cached objects and use definition cache
// to reduce the duplicated storage of same definitions
func AddInformerTransformFuncToCacheOption(opts *cache.Options) {
if utilfeature.DefaultMutableFeatureGate.Enabled(features.InformerCacheFilterUnnecessaryFields) {
if opts.ByObject == nil {
opts.ByObject = map[client.Object]cache.ByObject{}
}
opts.ByObject[&v1beta1.ApplicationRevision{}] = cache.ByObject{
Transform: wrapTransformFunc(func(rev *v1beta1.ApplicationRevision) {
if utilfeature.DefaultMutableFeatureGate.Enabled(features.SharedDefinitionStorageForApplicationRevision) {
DefaultDefinitionCache.Get().RemapRevision(rev)
}
}),
}
opts.ByObject[&v1beta1.Application{}] = cache.ByObject{
Transform: wrapTransformFunc(func(app *v1beta1.Application) {}),
}
opts.ByObject[&v1beta1.ResourceTracker{}] = cache.ByObject{
Transform: wrapTransformFunc(func(rt *v1beta1.ResourceTracker) {}),
}
}
}
// NewResourcesToDisableCache get ClientDisableCacheFor for building controller
func NewResourcesToDisableCache() []client.Object {
var objs []client.Object
if utilfeature.DefaultMutableFeatureGate.Enabled(features.DisableWorkflowContextConfigMapCache) {
objs = append(objs, &corev1.ConfigMap{})
}
return objs
}