github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/plugins/plugins.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/flagutil"
    24  	"sigs.k8s.io/prow/pkg/plugins"
    25  )
    26  
    27  type PluginOptions struct {
    28  	PluginConfigPath                         string
    29  	PluginConfigPathDefault                  string
    30  	SupplementalPluginsConfigDirs            flagutil.Strings
    31  	SupplementalPluginsConfigsFileNameSuffix string
    32  	CheckUnknownPlugins                      bool
    33  	SkipResolveConfigUpdater                 bool
    34  }
    35  
    36  func (o *PluginOptions) AddFlags(fs *flag.FlagSet) {
    37  	fs.StringVar(&o.PluginConfigPath, "plugin-config", o.PluginConfigPathDefault, "Path to plugin config file.")
    38  	fs.Var(&o.SupplementalPluginsConfigDirs, "supplemental-plugin-config-dir", "An additional directory from which to load plugin configs. Can be used for config sharding but only supports a subset of the config. The flag can be passed multiple times.")
    39  	fs.StringVar(&o.SupplementalPluginsConfigsFileNameSuffix, "supplemental-plugin-configs-filename-suffix", "_pluginconfig.yaml", "Suffix for additional plugin configs. Only files with this name will be considered")
    40  }
    41  
    42  func (o *PluginOptions) Validate(_ bool) error {
    43  	return nil
    44  }
    45  
    46  func (o *PluginOptions) PluginAgent() (*plugins.ConfigAgent, error) {
    47  	pluginAgent := &plugins.ConfigAgent{}
    48  	if err := pluginAgent.Start(o.PluginConfigPath, o.SupplementalPluginsConfigDirs.Strings(), o.SupplementalPluginsConfigsFileNameSuffix, o.CheckUnknownPlugins, o.SkipResolveConfigUpdater); err != nil {
    49  		return nil, fmt.Errorf("failed to start plugins agent: %w", err)
    50  	}
    51  
    52  	return pluginAgent, nil
    53  }