github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/plugin/resource/reclaimed_resources.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 resource
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/client-go/tools/events"
    25  
    26  	"github.com/kubewharf/katalyst-core/pkg/agent/evictionmanager/plugin"
    27  	"github.com/kubewharf/katalyst-core/pkg/client"
    28  	"github.com/kubewharf/katalyst-core/pkg/config"
    29  	"github.com/kubewharf/katalyst-core/pkg/metaserver"
    30  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    31  	"github.com/kubewharf/katalyst-core/pkg/util/process"
    32  )
    33  
    34  const ReclaimedResourcesEvictionPluginName = "reclaimed-resource-pressure-eviction-plugin"
    35  
    36  type reclaimedResourcesPlugin struct {
    37  	*process.StopControl
    38  	*ResourcesEvictionPlugin
    39  }
    40  
    41  func NewReclaimedResourcesEvictionPlugin(_ *client.GenericClientSet, _ events.EventRecorder,
    42  	metaServer *metaserver.MetaServer, emitter metrics.MetricEmitter, conf *config.Configuration,
    43  ) plugin.EvictionPlugin {
    44  	reclaimedResourcesGetter := func(ctx context.Context) (v1.ResourceList, error) {
    45  		cnr, err := metaServer.GetCNR(ctx)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  
    50  		allocatable := make(v1.ResourceList)
    51  		if cnr != nil && cnr.Status.Resources.Allocatable != nil {
    52  			allocatable = *cnr.Status.Resources.Allocatable
    53  		}
    54  		return allocatable, nil
    55  	}
    56  
    57  	reclaimedThresholdGetter := func(resourceName v1.ResourceName) *float64 {
    58  		if threshold, ok := conf.GetDynamicConfiguration().EvictionThreshold[resourceName]; !ok {
    59  			return nil
    60  		} else {
    61  			return &threshold
    62  		}
    63  	}
    64  
    65  	deletionGracePeriodGetter := func() int64 {
    66  		return conf.GetDynamicConfiguration().ReclaimedResourcesEvictionConfiguration.DeletionGracePeriod
    67  	}
    68  	thresholdMetToleranceDurationGetter := func() int64 {
    69  		return conf.GetDynamicConfiguration().ThresholdMetToleranceDuration
    70  	}
    71  
    72  	p := NewResourcesEvictionPlugin(
    73  		ReclaimedResourcesEvictionPluginName,
    74  		metaServer,
    75  		emitter,
    76  		reclaimedResourcesGetter,
    77  		reclaimedThresholdGetter,
    78  		deletionGracePeriodGetter,
    79  		thresholdMetToleranceDurationGetter,
    80  		conf.SkipZeroQuantityResourceNames,
    81  		conf.CheckReclaimedQoSForPod,
    82  	)
    83  
    84  	return &reclaimedResourcesPlugin{
    85  		StopControl:             process.NewStopControl(time.Time{}),
    86  		ResourcesEvictionPlugin: p,
    87  	}
    88  }