github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/eviction.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 control
    18  
    19  import (
    20  	"context"
    21  
    22  	policy "k8s.io/api/policy/v1beta1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/kubernetes"
    25  )
    26  
    27  // PodEjector is used to evict Pods
    28  type PodEjector interface {
    29  	DeletePod(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error
    30  	EvictPod(ctx context.Context, eviction *policy.Eviction) error
    31  }
    32  
    33  type DummyPodEjector struct{}
    34  
    35  func (d DummyPodEjector) DeletePod(_ context.Context, _, _ string, _ metav1.DeleteOptions) error {
    36  	return nil
    37  }
    38  
    39  func (d DummyPodEjector) EvictPod(_ context.Context, _ *policy.Eviction) error {
    40  	return nil
    41  }
    42  
    43  type RealPodEjector struct {
    44  	client kubernetes.Interface
    45  }
    46  
    47  func NewRealPodEjector(client kubernetes.Interface) *RealPodEjector {
    48  	return &RealPodEjector{
    49  		client: client,
    50  	}
    51  }
    52  
    53  func (d *RealPodEjector) DeletePod(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
    54  	return d.client.CoreV1().Pods(namespace).Delete(ctx, name, opts)
    55  }
    56  
    57  func (d *RealPodEjector) EvictPod(ctx context.Context, eviction *policy.Eviction) error {
    58  	return d.client.PolicyV1beta1().Evictions(eviction.Namespace).Evict(ctx, eviction)
    59  }
    60  
    61  // RealPodEjectorWithMetric todo: implement with emitting metrics on updating
    62  type RealPodEjectorWithMetric struct {
    63  	client kubernetes.Interface
    64  	RealPodUpdater
    65  }