...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/policy_options.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/topologymanager

     1  /*
     2  Copyright 2022 The Kubernetes 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 topologymanager
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    25  	kubefeatures "k8s.io/kubernetes/pkg/features"
    26  )
    27  
    28  const (
    29  	PreferClosestNUMANodes string = "prefer-closest-numa-nodes"
    30  )
    31  
    32  var (
    33  	alphaOptions = sets.New[string]()
    34  	betaOptions  = sets.New[string](
    35  		PreferClosestNUMANodes,
    36  	)
    37  	stableOptions = sets.New[string]()
    38  )
    39  
    40  func CheckPolicyOptionAvailable(option string) error {
    41  	if !alphaOptions.Has(option) && !betaOptions.Has(option) && !stableOptions.Has(option) {
    42  		return fmt.Errorf("unknown Topology Manager Policy option: %q", option)
    43  	}
    44  
    45  	if alphaOptions.Has(option) && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.TopologyManagerPolicyAlphaOptions) {
    46  		return fmt.Errorf("Topology Manager Policy Alpha-level Options not enabled, but option %q provided", option)
    47  	}
    48  
    49  	if betaOptions.Has(option) && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.TopologyManagerPolicyBetaOptions) {
    50  		return fmt.Errorf("Topology Manager Policy Beta-level Options not enabled, but option %q provided", option)
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  type PolicyOptions struct {
    57  	PreferClosestNUMA bool
    58  }
    59  
    60  func NewPolicyOptions(policyOptions map[string]string) (PolicyOptions, error) {
    61  	opts := PolicyOptions{}
    62  	for name, value := range policyOptions {
    63  		if err := CheckPolicyOptionAvailable(name); err != nil {
    64  			return opts, err
    65  		}
    66  
    67  		switch name {
    68  		case PreferClosestNUMANodes:
    69  			optValue, err := strconv.ParseBool(value)
    70  			if err != nil {
    71  				return opts, fmt.Errorf("bad value for option %q: %w", name, err)
    72  			}
    73  			opts.PreferClosestNUMA = optValue
    74  		default:
    75  			// this should never be reached, we already detect unknown options,
    76  			// but we keep it as further safety.
    77  			return opts, fmt.Errorf("unsupported topologymanager option: %q (%s)", name, value)
    78  		}
    79  	}
    80  	return opts, nil
    81  }
    82  

View as plain text