github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/resource-recommend/processor/percentile/process_util.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 percentile
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/controller/resource-recommend/processor/percentile/task"
    25  	"github.com/kubewharf/katalyst-core/pkg/util/resource-recommend/log"
    26  	processortypes "github.com/kubewharf/katalyst-core/pkg/util/resource-recommend/types/processor"
    27  )
    28  
    29  func NewContext() context.Context {
    30  	return log.SetKeysAndValues(log.InitContext(context.Background()), "processor", ProcessorName)
    31  }
    32  
    33  func (p *Processor) getTaskForProcessKey(processKey *processortypes.ProcessKey) (*task.HistogramTask, error) {
    34  	if processKey == nil {
    35  		return nil, errors.Errorf("ProcessKey is nil")
    36  	}
    37  	if processKey.Metric == nil {
    38  		return nil, errors.Errorf("Metric is nil")
    39  	}
    40  	if tasks, ok := p.ResourceRecommendTaskIDsMap[processKey.ResourceRecommendNamespacedName]; !ok {
    41  		return nil, errors.Errorf("not found percentile process task ID for ResourceRecommend(%s)",
    42  			processKey.ResourceRecommendNamespacedName)
    43  	} else {
    44  		if taskID, exist := (*tasks)[*processKey.Metric]; exist {
    45  			return p.getTaskForTaskID(taskID)
    46  		}
    47  		return nil, errors.Errorf("not found process task ID for container(%s) in ResourceRecommend(%s)",
    48  			processKey.ContainerName, processKey.ResourceRecommendNamespacedName)
    49  	}
    50  }
    51  
    52  func (p *Processor) getTaskForTaskID(taskID processortypes.TaskID) (*task.HistogramTask, error) {
    53  	data, found := p.AggregateTasks.Load(taskID)
    54  	if !found {
    55  		return nil, errors.New("process task not found")
    56  	}
    57  	t, ok := data.(*task.HistogramTask)
    58  	if !ok {
    59  		return nil, errors.New("process task type illegal")
    60  	}
    61  	return t, nil
    62  }