github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/resourcemanager/fetcher/util/common.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 util
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/util/errors"
    21  
    22  	"github.com/kubewharf/katalyst-api/pkg/protocol/reporterplugin/v1alpha1"
    23  )
    24  
    25  type GetContentFunc func() ([]*v1alpha1.ReportContent, error)
    26  
    27  // AppendReportContent appends contents generated from multiple sources (each source
    28  // is represented as a general getter function) into one single content slice.
    29  func AppendReportContent(fn ...GetContentFunc) ([]*v1alpha1.ReportContent, error) {
    30  	var (
    31  		contents []*v1alpha1.ReportContent
    32  		errList  []error
    33  	)
    34  
    35  	for _, f := range fn {
    36  		c, err := f()
    37  		if err != nil {
    38  			errList = append(errList, err)
    39  			continue
    40  		}
    41  
    42  		contents = append(contents, c...)
    43  	}
    44  
    45  	if len(errList) > 0 {
    46  		return nil, errors.NewAggregate(errList)
    47  	}
    48  
    49  	return contents, nil
    50  }