github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/config/config.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 flagutil
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  
    23  	"sigs.k8s.io/prow/pkg/config"
    24  	"sigs.k8s.io/prow/pkg/flagutil"
    25  )
    26  
    27  const (
    28  	defaultConfigPathFlagName = "config-path"
    29  	defaultJobConfigPathFlag  = "job-config-path"
    30  )
    31  
    32  type ConfigOptions struct {
    33  	ConfigPath    string
    34  	JobConfigPath string
    35  	// ConfigPathFlagName allows to override the flag name for the prow config. Defaults
    36  	// to 'config-path'.
    37  	ConfigPathFlagName string
    38  	// JobConfigPathFlagName allows to override the flag name for the job config. Defaults
    39  	// to 'job-config-path'.
    40  	JobConfigPathFlagName                 string
    41  	SupplementalProwConfigDirs            flagutil.Strings
    42  	SupplementalProwConfigsFileNameSuffix string
    43  	// Inrepoconfig related flags
    44  	InRepoConfigCacheSize    int
    45  	InRepoConfigCacheDirBase string
    46  	// Moonraker is the centralized Inrepconfig Caching Service. Using this flag
    47  	// overrides the use of the local InRepoConfigCache.
    48  	MoonrakerAddress string
    49  }
    50  
    51  func (o *ConfigOptions) AddFlags(fs *flag.FlagSet) {
    52  	if o.ConfigPathFlagName == "" {
    53  		o.ConfigPathFlagName = defaultConfigPathFlagName
    54  	}
    55  	if o.JobConfigPathFlagName == "" {
    56  		o.JobConfigPathFlagName = defaultJobConfigPathFlag
    57  	}
    58  	fs.StringVar(&o.ConfigPath, o.ConfigPathFlagName, o.ConfigPath, "Path to the prowconfig")
    59  	fs.StringVar(&o.JobConfigPath, o.JobConfigPathFlagName, o.JobConfigPath, "Path to a file or a directory from which to load job configuration. ProwJob specs will be generated by merging any presubmits, postsubmits, periodics and presets found in these files. If the path is to a directory, it wil be searched recursively.")
    60  	fs.Var(&o.SupplementalProwConfigDirs, "supplemental-prow-config-dir", "An additional directory from which to load prow configs. Can be used for config sharding but only supports a subset of the config. The flag can be passed multiple times.")
    61  	fs.StringVar(&o.SupplementalProwConfigsFileNameSuffix, "supplemental-prow-configs-filename", "_prowconfig.yaml", "Suffix for additional prow configs. Only files with this name will be considered. Deprecated and mutually exclusive with --supplemental-prow-configs-filename-suffix")
    62  	fs.StringVar(&o.SupplementalProwConfigsFileNameSuffix, "supplemental-prow-configs-filename-suffix", "_prowconfig.yaml", "Suffix for additional prow configs. Only files with this name will be considered")
    63  	fs.IntVar(&o.InRepoConfigCacheSize, "in-repo-config-cache-size", 200, "Cache size for ProwYAMLs read from in-repo configs.")
    64  	fs.StringVar(&o.InRepoConfigCacheDirBase, "cache-dir-base", "", "Directory where the repo cache should be mounted.")
    65  	fs.StringVar(&o.MoonrakerAddress, "moonraker-address", "", "full HTTP address (domain and port) of moonraker service")
    66  }
    67  
    68  func (o *ConfigOptions) Validate(_ bool) error {
    69  	if o.ConfigPath == "" {
    70  		return fmt.Errorf("--%s is mandatory", o.ConfigPathFlagName)
    71  	}
    72  	return nil
    73  }
    74  
    75  func (o *ConfigOptions) ValidateConfigOptional() error {
    76  	if o.JobConfigPath != "" && o.ConfigPath == "" {
    77  		return fmt.Errorf("if --%s is given, --%s must be given as well", o.JobConfigPathFlagName, o.ConfigPathFlagName)
    78  	}
    79  	return nil
    80  }
    81  
    82  func (o *ConfigOptions) ConfigAgent(reuse ...*config.Agent) (*config.Agent, error) {
    83  	var ca *config.Agent
    84  	if n := len(reuse); n > 1 {
    85  		return nil, fmt.Errorf("got more than one (%d) config agents to re-use", n)
    86  	} else if n == 1 {
    87  		ca = reuse[0]
    88  	} else {
    89  		ca = &config.Agent{}
    90  	}
    91  	return o.ConfigAgentWithAdditionals(ca, nil)
    92  }
    93  
    94  func (o *ConfigOptions) ConfigAgentWithAdditionals(ca *config.Agent, additionals []func(*config.Config) error) (*config.Agent, error) {
    95  	return ca, ca.Start(o.ConfigPath, o.JobConfigPath, o.SupplementalProwConfigDirs.Strings(), o.SupplementalProwConfigsFileNameSuffix, additionals...)
    96  }