k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/test/modules.go (about)

     1  /*
     2  Copyright 2020 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 test
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/perf-tests/clusterloader2/api"
    23  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    24  )
    25  
    26  // flattenModuleSteps computes a slice of executable steps. An executable step
    27  // is either a measurement or phase step. The method recursively flattens the
    28  // steps being part of a module while skipping the module steps itself. E.g. if
    29  // the input (after loading the modules from their files) is:
    30  //   - step 1:
    31  //   - step 2:
    32  //     module:
    33  //   - step 3:
    34  //   - step 4:
    35  //     module:
    36  //     -step 5
    37  //   - step 6
    38  //
    39  // the method will return [step1, step3, step5, step6].
    40  func flattenModuleSteps(ctx Context, unprocessedSteps []*api.Step) ([]*api.Step, error) {
    41  	var processedSteps []*api.Step
    42  	for i := range unprocessedSteps {
    43  		step := unprocessedSteps[i]
    44  		if step.IsModule() {
    45  			module, err := loadModule(ctx, &step.Module)
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  			processedModuleSteps, err := flattenModuleSteps(ctx, module.Steps)
    50  			if err != nil {
    51  				return nil, err
    52  			}
    53  			processedSteps = append(processedSteps, processedModuleSteps...)
    54  		} else {
    55  			processedSteps = append(processedSteps, step)
    56  		}
    57  	}
    58  	return processedSteps, nil
    59  }
    60  
    61  // loadModule loads the module pointed by the moduleRef.
    62  func loadModule(ctx Context, moduleRef *api.ModuleRef) (*api.Module, error) {
    63  	if moduleRef.Path == "" {
    64  		return nil, fmt.Errorf("path not set: %#v", moduleRef)
    65  	}
    66  	mapping := ctx.GetTemplateMappingCopy()
    67  	if moduleRef.Params != nil {
    68  		util.CopyMap(moduleRef.Params, mapping)
    69  	}
    70  	var module api.Module
    71  	if err := ctx.GetTemplateProvider().TemplateInto(moduleRef.Path, mapping, &module); err != nil {
    72  		return nil, fmt.Errorf("error while processing module %#v: %w", moduleRef, err)
    73  	}
    74  	return &module, nil
    75  }