github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/pod/pod_stub.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 pod
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  
    26  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    27  )
    28  
    29  type PodFetcherStub struct {
    30  	mutex   sync.Mutex
    31  	PodList []*v1.Pod
    32  }
    33  
    34  var _ PodFetcher = &PodFetcherStub{}
    35  
    36  func (p *PodFetcherStub) GetPodList(_ context.Context, podFilter func(*v1.Pod) bool) ([]*v1.Pod, error) {
    37  	p.mutex.Lock()
    38  	defer p.mutex.Unlock()
    39  	pods := make([]*v1.Pod, 0, len(p.PodList))
    40  	for _, pod := range p.PodList {
    41  		if podFilter != nil && !podFilter(pod) {
    42  			continue
    43  		}
    44  		pods = append(pods, pod)
    45  	}
    46  	return pods, nil
    47  }
    48  
    49  func (p *PodFetcherStub) GetPod(_ context.Context, podUID string) (*v1.Pod, error) {
    50  	p.mutex.Lock()
    51  	defer p.mutex.Unlock()
    52  	for _, pod := range p.PodList {
    53  		if string(pod.UID) == podUID {
    54  			return pod, nil
    55  		}
    56  	}
    57  	return nil, fmt.Errorf("failed to find pod by uid %v", podUID)
    58  }
    59  
    60  func (p *PodFetcherStub) Run(_ context.Context) {}
    61  
    62  func (p *PodFetcherStub) GetContainerID(podUID, containerName string) (string, error) {
    63  	p.mutex.Lock()
    64  	defer p.mutex.Unlock()
    65  
    66  	for _, pod := range p.PodList {
    67  		if string(pod.UID) == podUID {
    68  			return native.GetContainerID(pod, containerName)
    69  		}
    70  	}
    71  
    72  	return "", fmt.Errorf("container: %s isn't found in pod: %s statues", containerName, podUID)
    73  }
    74  
    75  func (p *PodFetcherStub) GetContainerSpec(podUID, containerName string) (*v1.Container, error) {
    76  	p.mutex.Lock()
    77  	defer p.mutex.Unlock()
    78  
    79  	for _, pod := range p.PodList {
    80  		if string(pod.UID) == podUID {
    81  			for _, c := range pod.Spec.Containers {
    82  				if c.Name == containerName {
    83  					return c.DeepCopy(), nil
    84  				}
    85  			}
    86  		}
    87  	}
    88  
    89  	return nil, fmt.Errorf("container: %s isn't found in pod: %s spec", containerName, podUID)
    90  }