github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/cnc/cnc.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 cnc
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  	"time"
    24  
    25  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/klog/v2"
    27  
    28  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    29  	configv1alpha1 "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned/typed/config/v1alpha1"
    30  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    31  	"github.com/kubewharf/katalyst-core/pkg/config/agent/metaserver"
    32  )
    33  
    34  type CNCFetcher interface {
    35  	// GetCNC returns latest cnc metadata no deep-copy.
    36  	GetCNC(ctx context.Context) (*v1alpha1.CustomNodeConfig, error)
    37  }
    38  
    39  type cachedCNCFetcher struct {
    40  	sync.Mutex
    41  	cnc          *v1alpha1.CustomNodeConfig
    42  	lastSyncTime time.Time
    43  
    44  	baseConf *global.BaseConfiguration
    45  	cncConf  *metaserver.CNCConfiguration
    46  	client   configv1alpha1.CustomNodeConfigInterface
    47  }
    48  
    49  func NewCachedCNCFetcher(baseConf *global.BaseConfiguration, cncConf *metaserver.CNCConfiguration, client configv1alpha1.CustomNodeConfigInterface) CNCFetcher {
    50  	return &cachedCNCFetcher{
    51  		baseConf: baseConf,
    52  		cncConf:  cncConf,
    53  		client:   client,
    54  	}
    55  }
    56  
    57  func (c *cachedCNCFetcher) GetCNC(ctx context.Context) (*v1alpha1.CustomNodeConfig, error) {
    58  	c.Lock()
    59  	defer c.Unlock()
    60  
    61  	now := time.Now()
    62  	if c.lastSyncTime.Add(c.cncConf.CustomNodeConfigCacheTTL).Before(now) {
    63  		c.syncCNC(ctx)
    64  		c.lastSyncTime = now
    65  	}
    66  
    67  	if c.cnc != nil {
    68  		return c.cnc, nil
    69  	}
    70  
    71  	return nil, fmt.Errorf("cannot get cnc from cache and remote")
    72  }
    73  
    74  func (c *cachedCNCFetcher) syncCNC(ctx context.Context) {
    75  	klog.Info("[cnc] sync cnc from remote")
    76  	cnc, err := c.client.Get(ctx, c.baseConf.NodeName, v1.GetOptions{ResourceVersion: "0"})
    77  	if err != nil {
    78  		klog.Errorf("syncCNC failed: %v", err)
    79  		return
    80  	}
    81  
    82  	c.cnc = cnc
    83  }