github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/container.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 native
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  )
    25  
    26  const (
    27  	defaultDockerContainerIDPrefix     = "docker://"
    28  	defaultContainerdContainerIDPrefix = "containerd://"
    29  )
    30  
    31  const (
    32  	ContainerMetricPortName      = "metrics"
    33  	ContainerMetricStorePortName = "store"
    34  )
    35  
    36  // CheckContainerNotRunning returns whether the given container is not-runnin
    37  func CheckContainerNotRunning(pod *v1.Pod, containerName string) (bool, error) {
    38  	cstatus, err := findContainerStatusByName(&pod.Status, containerName)
    39  	if err != nil {
    40  		return false, fmt.Errorf("container status not found in pod status, err: %v", err)
    41  	}
    42  
    43  	return containerNotRunning([]v1.ContainerStatus{cstatus}), nil
    44  }
    45  
    46  func findContainerStatusByName(status *v1.PodStatus, name string) (v1.ContainerStatus, error) {
    47  	for _, containerStatus := range append(status.InitContainerStatuses, status.ContainerStatuses...) {
    48  		if containerStatus.Name == name {
    49  			return containerStatus, nil
    50  		}
    51  	}
    52  	return v1.ContainerStatus{}, fmt.Errorf("unable to find status for container with name %v in pod status (it may not be running)", name)
    53  }
    54  
    55  // containerNotRunning returns whether the given containers are all not-running, ie.
    56  // if anyone falls to not-running state, returns false
    57  func containerNotRunning(statuses []v1.ContainerStatus) bool {
    58  	for _, status := range statuses {
    59  		if status.State.Terminated == nil && status.State.Waiting == nil {
    60  			return false
    61  		}
    62  	}
    63  	return true
    64  }
    65  
    66  // TrimContainerIDPrefix is used to parse the specific containerID
    67  // out of the whole containerID info
    68  func TrimContainerIDPrefix(id string) string {
    69  	return strings.TrimPrefix(strings.TrimPrefix(id, defaultDockerContainerIDPrefix), defaultContainerdContainerIDPrefix)
    70  }
    71  
    72  // ParseHostPortsForContainer gets host port from container spec
    73  func ParseHostPortsForContainer(container *v1.Container, portName string) (int32, bool) {
    74  	for _, port := range container.Ports {
    75  		if port.Name == portName && port.HostPort > 0 {
    76  			return port.HostPort, true
    77  		}
    78  	}
    79  	return 0, false
    80  }