github.com/intel/goresctrl@v0.5.0/pkg/kubernetes/annotations.go (about)

     1  /*
     2  Copyright 2021 Intel Corporation
     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 kubernetes
    18  
    19  // ClassOrigin type indicates the source of container's class
    20  // information: whether it is found from CRI level container
    21  // annotations, Kubernetes' pod annotations, or it has not been found
    22  // at all.
    23  type ClassOrigin int
    24  
    25  const (
    26  	ClassOriginNotFound ClassOrigin = iota
    27  	ClassOriginContainerAnnotation
    28  	ClassOriginPodAnnotation
    29  )
    30  
    31  func (c ClassOrigin) String() string {
    32  	switch c {
    33  	case ClassOriginNotFound:
    34  		return "<not found>"
    35  	case ClassOriginContainerAnnotation:
    36  		return "container annotations"
    37  	case ClassOriginPodAnnotation:
    38  		return "pod annotations"
    39  	default:
    40  		return "<unknown>"
    41  	}
    42  }
    43  
    44  // ContainerClassFromAnnotations determines the effective class of a
    45  // container from the Pod annotations and CRI level container
    46  // annotations of a container.
    47  func ContainerClassFromAnnotations(containerAnnotation, podAnnotation, podAnnotationContainerPrefix string, containerName string, containerAnnotations, podAnnotations map[string]string) (string, ClassOrigin) {
    48  	if clsName, ok := containerAnnotations[containerAnnotation]; ok {
    49  		return clsName, ClassOriginContainerAnnotation
    50  	}
    51  	if clsName, ok := podAnnotations[podAnnotationContainerPrefix+containerName]; ok {
    52  		return clsName, ClassOriginPodAnnotation
    53  	}
    54  	if clsName, ok := podAnnotations[podAnnotation]; ok {
    55  		return clsName, ClassOriginPodAnnotation
    56  	}
    57  	return "", ClassOriginNotFound
    58  }