github.com/intel/goresctrl@v0.5.0/pkg/rdt/kubernetes.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 rdt
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/intel/goresctrl/pkg/kubernetes"
    22  )
    23  
    24  const (
    25  	// RdtContainerAnnotation is the CRI level container annotation for setting
    26  	// the RDT class (CLOS) of a container
    27  	RdtContainerAnnotation = "io.kubernetes.cri.rdt-class"
    28  
    29  	// RdtPodAnnotation is a Pod annotation for setting the RDT class (CLOS) of
    30  	// all containers of the pod
    31  	RdtPodAnnotation = "rdt.resources.beta.kubernetes.io/pod"
    32  
    33  	// RdtPodAnnotationContainerPrefix is prefix for per-container Pod annotation
    34  	// for setting the RDT class (CLOS) of one container of the pod
    35  	RdtPodAnnotationContainerPrefix = "rdt.resources.beta.kubernetes.io/container."
    36  )
    37  
    38  // ContainerClassFromAnnotations determines the effective RDT class of a
    39  // container from the Pod annotations and CRI level container annotations of a
    40  // container. Verifies that the class exists in goresctrl configuration and that
    41  // it is allowed to be used.
    42  func ContainerClassFromAnnotations(containerName string, containerAnnotations, podAnnotations map[string]string) (string, error) {
    43  	clsName, clsOrigin := kubernetes.ContainerClassFromAnnotations(
    44  		RdtContainerAnnotation, RdtPodAnnotation, RdtPodAnnotationContainerPrefix,
    45  		containerName, containerAnnotations, podAnnotations)
    46  
    47  	if clsOrigin != kubernetes.ClassOriginNotFound {
    48  		if rdt == nil {
    49  			return "", fmt.Errorf("RDT not initialized, class %q not available", clsName)
    50  		}
    51  
    52  		// Verify validity of class name
    53  		if !IsQualifiedClassName(clsName) {
    54  			return "", fmt.Errorf("unqualified RDT class name %q", clsName)
    55  		}
    56  
    57  		// If RDT has been initialized we check that the class exists
    58  		if _, ok := rdt.getClass(clsName); !ok {
    59  			return "", fmt.Errorf("RDT class %q does not exist in configuration", clsName)
    60  		}
    61  
    62  		// If classes have been configured by goresctrl
    63  		if clsConf, ok := rdt.conf.Classes[unaliasClassName(clsName)]; ok {
    64  			// Check that the class is allowed
    65  			if clsOrigin == kubernetes.ClassOriginPodAnnotation && clsConf.Kubernetes.DenyPodAnnotation {
    66  				return "", fmt.Errorf("RDT class %q not allowed from Pod annotations", clsName)
    67  			} else if clsOrigin == kubernetes.ClassOriginContainerAnnotation && clsConf.Kubernetes.DenyContainerAnnotation {
    68  				return "", fmt.Errorf("RDT class %q not allowed from Container annotation", clsName)
    69  			}
    70  		}
    71  	}
    72  
    73  	return clsName, nil
    74  }