github.com/ironcore-dev/gardener-extension-provider-ironcore@v0.3.2-0.20240314231816-8336447fb9a0/pkg/cmd/config.go (about)

     1  // SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  
     9  	healthcheckconfig "github.com/gardener/gardener/extensions/pkg/apis/config"
    10  	"github.com/spf13/pflag"
    11  
    12  	"github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/config"
    13  	configloader "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/config/loader"
    14  )
    15  
    16  // ConfigOptions are command line options that can be set for config.ControllerConfiguration.
    17  type ConfigOptions struct {
    18  	// Kubeconfig is the path to a kubeconfig.
    19  	ConfigFilePath string
    20  
    21  	config *Config
    22  }
    23  
    24  // Config is a completed controller configuration.
    25  type Config struct {
    26  	// Config is the controller configuration.
    27  	Config *config.ControllerConfiguration
    28  }
    29  
    30  func (c *ConfigOptions) buildConfig() (*config.ControllerConfiguration, error) {
    31  	if len(c.ConfigFilePath) == 0 {
    32  		return nil, fmt.Errorf("config file path not set")
    33  	}
    34  	return configloader.LoadFromFile(c.ConfigFilePath)
    35  }
    36  
    37  // Complete implements RESTCompleter.Complete.
    38  func (c *ConfigOptions) Complete() error {
    39  	config, err := c.buildConfig()
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	c.config = &Config{config}
    45  	return nil
    46  }
    47  
    48  // Completed returns the completed Config. Only call this if `Complete` was successful.
    49  func (c *ConfigOptions) Completed() *Config {
    50  	return c.config
    51  }
    52  
    53  // AddFlags implements Flagger.AddFlags.
    54  func (c *ConfigOptions) AddFlags(fs *pflag.FlagSet) {
    55  	fs.StringVar(&c.ConfigFilePath, "config-file", "", "path to the controller manager configuration file")
    56  }
    57  
    58  // Apply sets the values of this Config in the given config.ControllerConfiguration.
    59  func (c *Config) Apply(cfg *config.ControllerConfiguration) {
    60  	*cfg = *c.Config
    61  }
    62  
    63  // ApplyETCDStorage sets the given etcd storage configuration to that of this Config.
    64  func (c *Config) ApplyETCDStorage(etcdStorage *config.ETCDStorage) {
    65  	*etcdStorage = c.Config.ETCD.Storage
    66  }
    67  
    68  // ApplyETCDBackup sets the given etcd backup configuration to that of this Config.
    69  func (c *Config) ApplyETCDBackup(etcdBackup *config.ETCDBackup) {
    70  	*etcdBackup = c.Config.ETCD.Backup
    71  }
    72  
    73  // Options initializes empty config.ControllerConfiguration, applies the set values and returns it.
    74  func (c *Config) Options() config.ControllerConfiguration {
    75  	var cfg config.ControllerConfiguration
    76  	c.Apply(&cfg)
    77  	return cfg
    78  }
    79  
    80  // ApplyHealthCheckConfig applies the HealthCheckConfig to the config
    81  func (c *Config) ApplyHealthCheckConfig(config *healthcheckconfig.HealthCheckConfig) {
    82  	if c.Config.HealthCheckConfig != nil {
    83  		*config = *c.Config.HealthCheckConfig
    84  	}
    85  }
    86  
    87  // ApplyBastionConfig applies the BastionConfig to the config
    88  func (c *Config) ApplyBastionConfig(config *config.BastionConfig) {
    89  	if c.Config.BastionConfig != nil {
    90  		*config = *c.Config.BastionConfig
    91  	}
    92  }
    93  
    94  // ApplyBackupBucketConfig applies the BackupBucketConfig to the config
    95  func (c *Config) ApplyBackupbucketConfig(config *config.BackupBucketConfig) {
    96  	if c.Config.BackupBucketConfig != nil {
    97  		*config = *c.Config.BackupBucketConfig
    98  	}
    99  }