github.com/oam-dev/kubevela@v1.9.11/pkg/cache/optimize.go (about)

     1  /*
     2  Copyright 2023 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kubevela/pkg/controller/sharding"
    23  	"github.com/kubevela/pkg/util/k8s"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    26  	"k8s.io/client-go/rest"
    27  	"sigs.k8s.io/controller-runtime/pkg/cache"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	"github.com/oam-dev/kubevela/pkg/features"
    32  	"github.com/oam-dev/kubevela/pkg/oam"
    33  )
    34  
    35  // AppIndex identify the index for resourcetracker to accelerate cache retrieval
    36  const AppIndex = "app"
    37  
    38  var (
    39  	// OptimizeListOp optimize ResourceTracker & ApplicationRevision list op by adding index
    40  	// used in controller optimization (informer index). Client side should not use it.
    41  	OptimizeListOp = false
    42  )
    43  
    44  // BuildCache if optimize-list-op enabled, ResourceTracker and ApplicationRevision will be cached by
    45  // application namespace & name
    46  func BuildCache(ctx context.Context, scheme *runtime.Scheme, shardingObjects ...client.Object) cache.NewCacheFunc {
    47  	opts := cache.Options{Scheme: scheme}
    48  	AddInformerTransformFuncToCacheOption(&opts)
    49  	fn := sharding.BuildCacheWithOptions(opts, shardingObjects...)
    50  	return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
    51  		c, err := fn(config, opts)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  		if OptimizeListOp {
    56  			if err = c.IndexField(ctx, &v1beta1.ResourceTracker{}, AppIndex, func(obj client.Object) []string {
    57  				return []string{k8s.GetLabel(obj, oam.LabelAppNamespace) + "/" + k8s.GetLabel(obj, oam.LabelAppName)}
    58  			}); err != nil {
    59  				return nil, err
    60  			}
    61  			if err = c.IndexField(ctx, &v1beta1.ApplicationRevision{}, AppIndex, func(obj client.Object) []string {
    62  				return []string{obj.GetNamespace() + "/" + k8s.GetLabel(obj, oam.LabelAppName)}
    63  			}); err != nil {
    64  				return nil, err
    65  			}
    66  		}
    67  		if utilfeature.DefaultMutableFeatureGate.Enabled(features.SharedDefinitionStorageForApplicationRevision) {
    68  			go DefaultDefinitionCache.Get().Start(ctx, c, ApplicationRevisionDefinitionCachePruneDuration)
    69  		}
    70  		return c, nil
    71  	}
    72  }