From f5b9ce437c5952b0b85560f248569ed8fe5de58e Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Mon, 20 Sep 2021 17:08:29 +0200 Subject: [PATCH] Code cleanup --- .../main.go | 1 + pkg/cmd/controller/cmd.go | 3 + pkg/cmd/controller/config.go | 22 +++++ pkg/controller/controlplane/registration.go | 7 ++ pkg/controller/controlplane/valuesprovider.go | 81 ++++++++++++++----- pkg/controller/healthcheck/registration.go | 7 ++ pkg/controller/infrastructure/actuator.go | 42 ++++++++-- .../infrastructure/actuator_delete.go | 6 ++ .../infrastructure/actuator_reconcile.go | 6 ++ .../infrastructure/ensurer/networks.go | 14 ++++ .../infrastructure/ensurer/ssh_public_key.go | 12 +++ pkg/controller/infrastructure/registration.go | 7 ++ pkg/controller/worker/actuator.go | 13 +++ .../worker/machine_controller_manager.go | 11 +++ pkg/controller/worker/machine_dependencies.go | 8 ++ pkg/controller/worker/machine_images.go | 12 ++- pkg/controller/worker/machines.go | 24 +++++- pkg/controller/worker/registration.go | 7 ++ pkg/hcloud/apis/config/install/install.go | 7 ++ pkg/hcloud/apis/config/loader/loader.go | 7 ++ pkg/hcloud/apis/config/v1alpha1/defaults.go | 26 ------ pkg/hcloud/apis/config/v1alpha1/register.go | 8 ++ pkg/hcloud/apis/register.go | 29 ++++--- pkg/hcloud/apis/types_cloudprofile.go | 2 + pkg/hcloud/apis/types_controlplane.go | 2 +- pkg/hcloud/apis/types_infrastructure.go | 3 + pkg/hcloud/apis/utils.go | 6 ++ pkg/hcloud/apis/v1alpha1/defaults.go | 23 ------ pkg/hcloud/apis/v1alpha1/register.go | 25 ++++-- .../apis/v1alpha1/types_cloudprofile.go | 2 + .../apis/v1alpha1/types_controlplane.go | 2 +- .../apis/v1alpha1/types_infrastructure.go | 3 + pkg/hcloud/credentials.go | 70 +++++++++------- pkg/hcloud/types.go | 6 +- pkg/webhook/controlplane/registration.go | 3 + 35 files changed, 380 insertions(+), 127 deletions(-) delete mode 100644 pkg/hcloud/apis/config/v1alpha1/defaults.go delete mode 100644 pkg/hcloud/apis/v1alpha1/defaults.go diff --git a/cmd/gardener-extension-provider-hcloud/main.go b/cmd/gardener-extension-provider-hcloud/main.go index 9412c8592..93b81f195 100644 --- a/cmd/gardener-extension-provider-hcloud/main.go +++ b/cmd/gardener-extension-provider-hcloud/main.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) +// main is the executable entry point. func main() { runtimelog.SetLogger(log.ZapLogger(false)) cmdDefinition := controller.NewControllerManagerCommand(signals.SetupSignalHandler()) diff --git a/pkg/cmd/controller/cmd.go b/pkg/cmd/controller/cmd.go index f4a861c79..81941a4ea 100644 --- a/pkg/cmd/controller/cmd.go +++ b/pkg/cmd/controller/cmd.go @@ -42,6 +42,9 @@ import ( ) // NewControllerManagerCommand creates a new command for running a HCloud provider controller. +// +// PARAMETERS +// ctx context.Context Execution context func NewControllerManagerCommand(ctx context.Context) *cobra.Command { restOpts := &cmd.RESTOptions{} diff --git a/pkg/cmd/controller/config.go b/pkg/cmd/controller/config.go index 669b127f3..96f293310 100644 --- a/pkg/cmd/controller/config.go +++ b/pkg/cmd/controller/config.go @@ -40,6 +40,7 @@ type Config struct { Config *config.ControllerConfiguration } +// buildConfig loads the controller configuration from the configured file. func (c *ConfigOptions) buildConfig() (*config.ControllerConfiguration, error) { if len(c.ConfigFilePath) == 0 { return nil, fmt.Errorf("config file path not set") @@ -64,31 +65,49 @@ func (c *ConfigOptions) Completed() *Config { } // AddFlags implements Flagger.AddFlags. +// +// PARAMETERS +// fs *pflag.FlagSet Flags to recognize func (c *ConfigOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&c.ConfigFilePath, "config-file", "", "path to the controller manager configuration file") } // Apply sets the values of this Config in the given config.ControllerConfiguration. +// +// PARAMETERS +// cfg *config.ControllerConfiguration Pointer to the configuration to set func (c *Config) Apply(cfg *config.ControllerConfiguration) { *cfg = *c.Config } // ApplyETCDStorage sets the given etcd storage configuration to that of this Config. +// +// PARAMETERS +// etcdStorage *config.ETCDStorage Pointer to the etcd storage configuration to set func (c *Config) ApplyETCDStorage(etcdStorage *config.ETCDStorage) { *etcdStorage = *c.Config.ETCD.Storage } // ApplyGardenId sets the gardenId. +// +// PARAMETERS +// gardenId *string Pointer to the gardenId to set func (c *Config) ApplyGardenId(gardenId *string) { *gardenId = c.Config.GardenId } // ApplyHealthProbeBindAddress sets the healthProbeBindAddress. +// +// PARAMETERS +// healthProbeBindAddress *string Pointer to the healthProbeBindAddress to set func (c *Config) ApplyHealthProbeBindAddress(healthProbeBindAddress *string) { *healthProbeBindAddress = c.Config.HealthProbeBindAddress } // ApplyMetricsBindAddress sets the metricsBindAddress. +// +// PARAMETERS +// metricsBindAddress *string Pointer to the metricsBindAddress to set func (c *Config) ApplyMetricsBindAddress(metricsBindAddress *string) { *metricsBindAddress = c.Config.MetricsBindAddress } @@ -101,6 +120,9 @@ func (c *Config) Options() config.ControllerConfiguration { } // ApplyHealthCheckConfig applies the HealthCheckConfig to the config +// +// PARAMETERS +// config *healthcheckconfig.HealthCheckConfig Pointer to the HealthCheckConfig to set func (c *Config) ApplyHealthCheckConfig(config *healthcheckconfig.HealthCheckConfig) { if c.Config.HealthCheckConfig != nil { *config = *c.Config.HealthCheckConfig diff --git a/pkg/controller/controlplane/registration.go b/pkg/controller/controlplane/registration.go index 0687181c4..85b3c0306 100644 --- a/pkg/controller/controlplane/registration.go +++ b/pkg/controller/controlplane/registration.go @@ -48,6 +48,10 @@ type AddOptions struct { // AddToManagerWithOptions adds a controller with the given Options to the given manager. // The opts.Reconciler is being set with a newly instantiated actuator. +// +// PARAMETERS +// mgr manager.Manager Control plane controller manager instance +// opts AddOptions Options to add func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { return controlplane.Add(mgr, controlplane.AddArgs{ Actuator: genericactuator.NewActuator( @@ -75,6 +79,9 @@ func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { } // AddToManager adds a controller with the default Options. +// +// PARAMETERS +// mgr manager.Manager Control plane controller manager instance func AddToManager(mgr manager.Manager) error { return AddToManagerWithOptions(mgr, DefaultAddOptions) } diff --git a/pkg/controller/controlplane/valuesprovider.go b/pkg/controller/controlplane/valuesprovider.go index e87af5fd8..6d4739e86 100644 --- a/pkg/controller/controlplane/valuesprovider.go +++ b/pkg/controller/controlplane/valuesprovider.go @@ -236,6 +236,10 @@ var storageClassChart = &chart.Chart{ } // NewValuesProvider creates a new ValuesProvider for the generic actuator. +// +// PARAMETERS +// logger logr.Logger Logger instance +// gardenID string Garden ID func NewValuesProvider(logger logr.Logger, gardenID string) genericactuator.ValuesProvider { return &valuesProvider{ logger: logger.WithName("hcloud-values-provider"), @@ -252,6 +256,11 @@ type valuesProvider struct { } // GetConfigChartValues returns the values for the config chart applied by the generic actuator. +// +// PARAMETERS +// ctx context.Context Execution context +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct func (vp *valuesProvider) GetConfigChartValues( ctx context.Context, cp *extensionsv1alpha1.ControlPlane, @@ -273,6 +282,13 @@ func (vp *valuesProvider) GetConfigChartValues( } // GetControlPlaneChartValues returns the values for the control plane chart applied by the generic actuator. +// +// PARAMETERS +// ctx context.Context Execution context +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct +// checksums map[string]string Checksums +// scaledDown bool True if scaled down func (vp *valuesProvider) GetControlPlaneChartValues( ctx context.Context, cp *extensionsv1alpha1.ControlPlane, @@ -302,6 +318,12 @@ func (vp *valuesProvider) GetControlPlaneChartValues( } // GetControlPlaneShootChartValues returns the values for the control plane shoot chart applied by the generic actuator. +// +// PARAMETERS +// ctx context.Context Execution context +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct +// checksums map[string]string Checksums func (vp *valuesProvider) GetControlPlaneShootChartValues( ctx context.Context, cp *extensionsv1alpha1.ControlPlane, @@ -319,6 +341,11 @@ func (vp *valuesProvider) GetControlPlaneShootChartValues( } // GetStorageClassesChartValues returns the values for the shoot storageclasses chart applied by the generic actuator. +// +// PARAMETERS +// _ context.Context Execution context +// _ *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct func (vp *valuesProvider) GetStorageClassesChartValues( _ context.Context, _ *extensionsv1alpha1.ControlPlane, @@ -338,26 +365,13 @@ func (vp *valuesProvider) GetStorageClassesChartValues( }, nil } -func splitServerNameAndPort(host string) (name string, port int, err error) { - parts := strings.Split(host, ":") - - if len(parts) == 1 { - name = host - port = 443 - } else if len(parts) == 2 { - name = parts[0] - port, err = strconv.Atoi(parts[1]) - if err != nil { - return "", 0, errors.Wrapf(err, "invalid port for hcloud host: host=%s,port=%s", host, parts[1]) - } - } else { - return "", 0, fmt.Errorf("invalid hcloud host: %s (too many parts %v)", host, parts) - } - - return -} - // getConfigChartValues collects and returns the configuration chart values. +// +// PARAMETERS +// cpConfig *apis.ControlPlaneConfig Control plane config struct +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct +// credentials *hcloud.Credentials Credentials instance func (vp *valuesProvider) getConfigChartValues( cpConfig *apis.ControlPlaneConfig, cp *extensionsv1alpha1.ControlPlane, @@ -373,7 +387,7 @@ func (vp *valuesProvider) getConfigChartValues( // Collect config chart values values := map[string]interface{}{ - "token": credentials.HcloudCCM().Token, + "token": credentials.CCM().Token, "region": region, "zone": zone, } @@ -382,6 +396,15 @@ func (vp *valuesProvider) getConfigChartValues( } // getControlPlaneChartValues collects and returns the control plane chart values. +// +// PARAMETERS +// cpConfig *apis.ControlPlaneConfig Control plane config struct +// infraStatus *apis.InfrastructureStatus Infrastructure status struct +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct +// credentials *hcloud.Credentials Credentials instance +// checksums map[string]string Checksums +// scaledDown bool True if scaled down func (vp *valuesProvider) getControlPlaneChartValues( cpConfig *apis.ControlPlaneConfig, infraStatus *apis.InfrastructureStatus, @@ -418,7 +441,7 @@ func (vp *valuesProvider) getControlPlaneChartValues( "replicas": extensionscontroller.GetControlPlaneReplicas(cluster, scaledDown, 1), "kubernetesVersion": cluster.Shoot.Spec.Kubernetes.Version, "clusterID": csiClusterID, - "token": credentials.HcloudCSI().Token, + "token": credentials.CSI().Token, "csiRegion": region, // "resizerEnabled": csiResizerEnabled, "podAnnotations": map[string]interface{}{ @@ -445,6 +468,11 @@ func (vp *valuesProvider) getControlPlaneChartValues( } // getControlPlaneShootChartValues collects and returns the control plane shoot chart values. +// +// PARAMETERS +// cp *extensionsv1alpha1.ControlPlane Control plane struct +// cluster *extensionscontroller.Cluster Cluster struct +// credentials *hcloud.Credentials Credentials instance func (vp *valuesProvider) getControlPlaneShootChartValues( cp *extensionsv1alpha1.ControlPlane, cluster *extensionscontroller.Cluster, @@ -455,7 +483,7 @@ func (vp *valuesProvider) getControlPlaneShootChartValues( "csi-hcloud": map[string]interface{}{ // "serverName": serverName, "clusterID": csiClusterID, - "token": credentials.HcloudCSI().Token, + "token": credentials.CSI().Token, "kubernetesVersion": cluster.Shoot.Spec.Kubernetes.Version, }, } @@ -463,12 +491,21 @@ func (vp *valuesProvider) getControlPlaneShootChartValues( return values, nil } +// calcClusterIDs returns the cluster ID and CSI cluster ID. +// +// PARAMETERS +// cp *extensionsv1alpha1.ControlPlane Control plane struct func (vp *valuesProvider) calcClusterIDs(cp *extensionsv1alpha1.ControlPlane) (clusterID string, csiClusterID string) { clusterID = cp.Namespace + "-" + vp.gardenID csiClusterID = shortenID(clusterID, 63) return } +// shortenID returns a shortened ID with the given size. +// +// PARAMETERS +// id string ID +// maxlen int Maximum length func shortenID(id string, maxlen int) string { if maxlen < 16 { panic("maxlen < 16 for shortenID") diff --git a/pkg/controller/healthcheck/registration.go b/pkg/controller/healthcheck/registration.go index 8ce151731..425f7c631 100644 --- a/pkg/controller/healthcheck/registration.go +++ b/pkg/controller/healthcheck/registration.go @@ -49,6 +49,10 @@ var ( // RegisterHealthChecks registers health checks for each extension resource // HealthChecks are grouped by extension (e.g worker), extension.type (e.g hcloud) and Health Check Type (e.g SystemComponentsHealthy) +// +// PARAMETERS +// mgr manager.Manager Health check controller manager instance +// opts healthcheck.DefaultAddArgs Options to add func RegisterHealthChecks(mgr manager.Manager, opts healthcheck.DefaultAddArgs) error { err := healthcheck.DefaultRegistration( hcloud.Type, @@ -101,6 +105,9 @@ func RegisterHealthChecks(mgr manager.Manager, opts healthcheck.DefaultAddArgs) } // AddToManager adds a controller with the default Options. +// +// PARAMETERS +// mgr manager.Manager Health check controller manager instance func AddToManager(mgr manager.Manager) error { return RegisterHealthChecks(mgr, DefaultAddOptions) } diff --git a/pkg/controller/infrastructure/actuator.go b/pkg/controller/infrastructure/actuator.go index aa1328a7b..50df15855 100644 --- a/pkg/controller/infrastructure/actuator.go +++ b/pkg/controller/infrastructure/actuator.go @@ -76,7 +76,7 @@ func (a *actuator) getActuatorConfig(ctx context.Context, infra *extensionsv1alp return nil, err } - token := credentials.HcloudCCM().Token + token := credentials.CCM().Token config := &actuatorConfig{ cloudProfileConfig: cloudProfileConfig, @@ -87,26 +87,52 @@ func (a *actuator) getActuatorConfig(ctx context.Context, infra *extensionsv1alp return config, nil } -// Restore implements infrastructure.Actuator.Delete -func (a *actuator) Delete(ctx context.Context, config *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { - return a.delete(ctx, config, cluster) +// Delete implements infrastructure.Actuator.Delete +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct +func (a *actuator) Delete(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { + return a.delete(ctx, infra, cluster) } -// Restore implements infrastructure.Actuator.Migrate +// Migrate implements infrastructure.Actuator.Migrate +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct func (a *actuator) Migrate(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { return nil } -// Restore implements infrastructure.Actuator.Reconcile -func (a *actuator) Reconcile(ctx context.Context, config *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { - return a.reconcile(ctx, config, cluster) +// Reconcile implements infrastructure.Actuator.Reconcile +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct +func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { + return a.reconcile(ctx, infra, cluster) } // Restore implements infrastructure.Actuator.Restore +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct func (a *actuator) Restore(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error { return nil } +// updateProviderStatus updates the infrastructure provider status. +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// infraStatus *v1alpha1.InfrastructureStatus Infrastructure status to be applied func (a *actuator) updateProviderStatus(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, infraStatus *v1alpha1.InfrastructureStatus) error { if nil == infraStatus { return nil diff --git a/pkg/controller/infrastructure/actuator_delete.go b/pkg/controller/infrastructure/actuator_delete.go index 2582c68c6..bcf800265 100644 --- a/pkg/controller/infrastructure/actuator_delete.go +++ b/pkg/controller/infrastructure/actuator_delete.go @@ -27,6 +27,12 @@ import ( extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" ) +// delete deletes the infrastructure config. +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct func (a *actuator) delete(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) error { actuatorConfig, err := a.getActuatorConfig(ctx, infra, cluster) // the shoot never reached the state of having a ProviderConfig assigned diff --git a/pkg/controller/infrastructure/actuator_reconcile.go b/pkg/controller/infrastructure/actuator_reconcile.go index e00fb3a11..918f24d00 100644 --- a/pkg/controller/infrastructure/actuator_reconcile.go +++ b/pkg/controller/infrastructure/actuator_reconcile.go @@ -30,6 +30,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// reconcile reconciles the infrastructure config. +// +// PARAMETERS +// ctx context.Context Execution context +// infra *extensionsv1alpha1.Infrastructure Infrastructure struct +// cluster *extensionscontroller.Cluster Cluster struct func (a *actuator) reconcile(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) error { actuatorConfig, err := a.getActuatorConfig(ctx, infra, cluster) if err != nil { diff --git a/pkg/controller/infrastructure/ensurer/networks.go b/pkg/controller/infrastructure/ensurer/networks.go index 7bca4bc4a..0b874aa23 100644 --- a/pkg/controller/infrastructure/ensurer/networks.go +++ b/pkg/controller/infrastructure/ensurer/networks.go @@ -26,6 +26,13 @@ import ( "github.com/hetznercloud/hcloud-go/hcloud" ) +// EnsureNetworks verifies the network resources requested are available. +// +// PARAMETERS +// ctx context.Context Execution context +// client *hcloud.Client HCloud client +// namespace string Shoot namespace +// networks *apis.Networks Networks struct func EnsureNetworks(ctx context.Context, client *hcloud.Client, namespace string, networks *apis.Networks) (int, error) { if "" != networks.Workers { name := fmt.Sprintf("%s-workers", namespace) @@ -62,6 +69,13 @@ func EnsureNetworks(ctx context.Context, client *hcloud.Client, namespace string return -1, nil } +// EnsureNetworksDeleted removes any previously created network resources. +// +// PARAMETERS +// ctx context.Context Execution context +// client *hcloud.Client HCloud client +// namespace string Shoot namespace +// networks *apis.NetworkIDs Network IDs struct func EnsureNetworksDeleted(ctx context.Context, client *hcloud.Client, namespace string, networks *apis.NetworkIDs) error { if "" != networks.Workers { name := fmt.Sprintf("%s-workers", namespace) diff --git a/pkg/controller/infrastructure/ensurer/ssh_public_key.go b/pkg/controller/infrastructure/ensurer/ssh_public_key.go index 4b24bdbca..f606baed9 100644 --- a/pkg/controller/infrastructure/ensurer/ssh_public_key.go +++ b/pkg/controller/infrastructure/ensurer/ssh_public_key.go @@ -25,6 +25,12 @@ import ( "github.com/hetznercloud/hcloud-go/hcloud" ) +// EnsureSSHPublicKey verifies that the SSH public key resource requested is available. +// +// PARAMETERS +// ctx context.Context Execution context +// client *hcloud.Client HCloud client +// publicKey []byte SSH public key func EnsureSSHPublicKey(ctx context.Context, client *hcloud.Client, publicKey []byte) (string, error) { if len(publicKey) == 0 { return "", fmt.Errorf("SSH public key given is empty") @@ -56,6 +62,12 @@ func EnsureSSHPublicKey(ctx context.Context, client *hcloud.Client, publicKey [] return fingerprint, nil } +// EnsureSSHPublicKeyDeleted removes any previously created SSH public key resource identified by the given fingerprint. +// +// PARAMETERS +// ctx context.Context Execution context +// client *hcloud.Client HCloud client +// fingerprint string SSH fingerprint func EnsureSSHPublicKeyDeleted(ctx context.Context, client *hcloud.Client, fingerprint string) error { sshKey, _, err := client.SSHKey.GetByFingerprint(ctx, fingerprint) if nil != err { diff --git a/pkg/controller/infrastructure/registration.go b/pkg/controller/infrastructure/registration.go index d2599ee2f..b12bb6d05 100644 --- a/pkg/controller/infrastructure/registration.go +++ b/pkg/controller/infrastructure/registration.go @@ -41,6 +41,10 @@ type AddOptions struct { // AddToManagerWithOptions adds a controller with the given Options to the given manager. // The opts.Reconciler is being set with a newly instantiated actuator. +// +// PARAMETERS +// mgr manager.Manager Infrastructure controller manager instance +// opts AddOptions Options to add func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { return infrastructure.Add(mgr, infrastructure.AddArgs{ Actuator: NewActuator(opts.GardenId), @@ -51,6 +55,9 @@ func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { } // AddToManager adds a controller with the default Options. +// +// PARAMETERS +// mgr manager.Manager Infrastructure controller manager instance func AddToManager(mgr manager.Manager) error { return AddToManagerWithOptions(mgr, DefaultAddOptions) } diff --git a/pkg/controller/worker/actuator.go b/pkg/controller/worker/actuator.go index 97ea7a1f4..ebf653272 100644 --- a/pkg/controller/worker/actuator.go +++ b/pkg/controller/worker/actuator.go @@ -58,6 +58,12 @@ func NewActuator() worker.Actuator { ) } +// WorkerDelegate returns the WorkerDelegate instance for the given worker and cluster struct. +// +// PARAMETERS +// ctx context.Context Execution context +// worker *extensionsv1alpha1.Worker Worker struct +// cluster *extensionscontroller.Cluster Cluster struct func (d *delegateFactory) WorkerDelegate(ctx context.Context, worker *extensionsv1alpha1.Worker, cluster *extensionscontroller.Cluster) (genericactuator.WorkerDelegate, error) { clientset, err := kubernetes.NewForConfig(d.RESTConfig()) if err != nil { @@ -100,6 +106,13 @@ type workerDelegate struct { } // NewWorkerDelegate creates a new context for a worker reconciliation. +// +// PARAMETERS +// clientContext common.ClientContext Client context +// seedChartApplier gardener.ChartApplier Chart applier instance +// serverVersion string Kubernetes version +// worker *extensionsv1alpha1.Worker Worker struct +// cluster *extensionscontroller.Cluster Cluster struct func NewWorkerDelegate( clientContext common.ClientContext, diff --git a/pkg/controller/worker/machine_controller_manager.go b/pkg/controller/worker/machine_controller_manager.go index c555dadc8..88a4fa4fa 100644 --- a/pkg/controller/worker/machine_controller_manager.go +++ b/pkg/controller/worker/machine_controller_manager.go @@ -58,6 +58,10 @@ var ( } ) +// GetMachineControllerManagerChartValues returns chart values relevant for the MCM instance. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) GetMachineControllerManagerChartValues(ctx context.Context) (map[string]interface{}, error) { namespace := &corev1.Namespace{} if err := w.Client().Get(ctx, kutil.Key(w.worker.Namespace), namespace); err != nil { @@ -75,6 +79,10 @@ func (w *workerDelegate) GetMachineControllerManagerChartValues(ctx context.Cont }, nil } +// GetMachineControllerManagerShootChartValues returns chart values relevant for the MCM shoot instance. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) GetMachineControllerManagerShootChartValues(ctx context.Context) (map[string]interface{}, error) { return map[string]interface{}{ "providerName": hcloud.Name, @@ -83,6 +91,9 @@ func (w *workerDelegate) GetMachineControllerManagerShootChartValues(ctx context // GetMachineControllerManagerCloudCredentials should return the IaaS credentials // with the secret keys used by the machine-controller-manager. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) GetMachineControllerManagerCloudCredentials(ctx context.Context) (map[string][]byte, error) { return w.generateMachineClassSecretData(ctx) } diff --git a/pkg/controller/worker/machine_dependencies.go b/pkg/controller/worker/machine_dependencies.go index 50c9c7a39..69c95632f 100644 --- a/pkg/controller/worker/machine_dependencies.go +++ b/pkg/controller/worker/machine_dependencies.go @@ -19,10 +19,18 @@ package worker import "context" +// DeployMachineDependencies should deploy dependencies for the worker node machines. +// +// PARAMETERS +// _ context.Context Execution context func (w *workerDelegate) DeployMachineDependencies(_ context.Context) error { return nil } +// CleanupMachineDependencies should clean up dependencies previously deployed for the worker node machines. +// +// PARAMETERS +// _ context.Context Execution context func (w *workerDelegate) CleanupMachineDependencies(ctx context.Context) error { return nil } diff --git a/pkg/controller/worker/machine_images.go b/pkg/controller/worker/machine_images.go index 0f00b2668..93f79df26 100644 --- a/pkg/controller/worker/machine_images.go +++ b/pkg/controller/worker/machine_images.go @@ -32,7 +32,12 @@ import ( "k8s.io/client-go/util/retry" ) -// findMachineImageName searches for the given machine image name and value. +// findMachineImageName returns the image name for the given name and version values. +// +// PARAMETERS +// ctx context.Context Execution context +// name string Machine image name +// version string Machine image version func (w *workerDelegate) findMachineImageName(ctx context.Context, name, version string) (string, error) { machineImage, err := transcoder.DecodeMachineImageNameFromCloudProfile(w.cloudProfileConfig, name, version) if err == nil { @@ -49,7 +54,7 @@ func (w *workerDelegate) findMachineImageName(ctx context.Context, name, version return "", err } - client := apis.GetClientForToken(string(credentials.HcloudMCM().Token)) + client := apis.GetClientForToken(string(credentials.MCM().Token)) opts := hcloudclient.ImageListOpts{ Type: []hcloudclient.ImageType{"system"}, @@ -73,6 +78,9 @@ func (w *workerDelegate) findMachineImageName(ctx context.Context, name, version } // UpdateMachineImagesStatus adds machineImages to the `WorkerStatus` resource. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) UpdateMachineImagesStatus(ctx context.Context) error { if w.machineImages == nil { if err := w.generateMachineConfig(ctx); err != nil { diff --git a/pkg/controller/worker/machines.go b/pkg/controller/worker/machines.go index a092b6936..46e08e4e4 100644 --- a/pkg/controller/worker/machines.go +++ b/pkg/controller/worker/machines.go @@ -54,6 +54,9 @@ func (w *workerDelegate) MachineClassList() client.ObjectList { } // DeployMachineClasses generates and creates the HCloud specific machine classes. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) DeployMachineClasses(ctx context.Context) error { if w.machineClasses == nil { if err := w.generateMachineConfig(ctx); err != nil { @@ -65,6 +68,9 @@ func (w *workerDelegate) DeployMachineClasses(ctx context.Context) error { } // GenerateMachineDeployments generates the configuration for the desired machine deployments. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) GenerateMachineDeployments(ctx context.Context) (worker.MachineDeployments, error) { if w.machineDeployments == nil { if err := w.generateMachineConfig(ctx); err != nil { @@ -74,10 +80,18 @@ func (w *workerDelegate) GenerateMachineDeployments(ctx context.Context) (worker return w.machineDeployments, nil } +// getSecretData returns the secret referenced by the WorkerDelegate instance's spec. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) getSecretData(ctx context.Context) (*corev1.Secret, error) { return extensionscontroller.GetSecretByReference(ctx, w.Client(), &w.worker.Spec.SecretRef) } +// generateMachineClassSecretData returns the machine class relevant secret values. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) generateMachineClassSecretData(ctx context.Context) (map[string][]byte, error) { secret, err := w.getSecretData(ctx) if err != nil { @@ -90,10 +104,14 @@ func (w *workerDelegate) generateMachineClassSecretData(ctx context.Context) (ma } return map[string][]byte{ - hcloud.HcloudToken: []byte(credentials.HcloudMCM().Token), + hcloud.HcloudToken: []byte(credentials.MCM().Token), }, nil } +// generateMachineConfig generates the machine config of the WorkerDelegate instance's spec. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) generateMachineConfig(ctx context.Context) error { var ( machineDeployments = worker.MachineDeployments{} @@ -206,6 +224,10 @@ type machineValues struct { MachineTypeOptions *apis.MachineTypeOptions } +// extractMachineValues extracts the relevant machine values from the cloud profile spec. +// +// PARAMETERS +// ctx context.Context Execution context func (w *workerDelegate) extractMachineValues(machineTypeName string) (*machineValues, error) { var machineType *corev1beta1.MachineType for _, mt := range w.cluster.CloudProfile.Spec.MachineTypes { diff --git a/pkg/controller/worker/registration.go b/pkg/controller/worker/registration.go index f89776e6c..35dab2c1c 100644 --- a/pkg/controller/worker/registration.go +++ b/pkg/controller/worker/registration.go @@ -41,6 +41,10 @@ type AddOptions struct { // AddToManagerWithOptions adds a controller with the given Options to the given manager. // The opts.Reconciler is being set with a newly instantiated actuator. +// +// PARAMETERS +// mgr manager.Manager Worker controller manager instance +// opts AddOptions Options to add func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { scheme := mgr.GetScheme() if err := apiextensionsscheme.AddToScheme(scheme); err != nil { @@ -59,6 +63,9 @@ func AddToManagerWithOptions(mgr manager.Manager, opts AddOptions) error { } // AddToManager adds a controller with the default Options. +// +// PARAMETERS +// mgr manager.Manager Worker controller manager instance func AddToManager(mgr manager.Manager) error { return AddToManagerWithOptions(mgr, DefaultAddOptions) } diff --git a/pkg/hcloud/apis/config/install/install.go b/pkg/hcloud/apis/config/install/install.go index ae899a154..169d2cfbd 100644 --- a/pkg/hcloud/apis/config/install/install.go +++ b/pkg/hcloud/apis/config/install/install.go @@ -36,11 +36,18 @@ var ( AddToScheme = schemeBuilder.AddToScheme ) +// setVersionPriority is used to set priority of the scheme to the latest one. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to set version in. func setVersionPriority(scheme *runtime.Scheme) error { return scheme.SetVersionPriority(v1alpha1.SchemeGroupVersion) } // Install installs all APIs in the scheme. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to install into. func Install(scheme *runtime.Scheme) { utilruntime.Must(AddToScheme(scheme)) } diff --git a/pkg/hcloud/apis/config/loader/loader.go b/pkg/hcloud/apis/config/loader/loader.go index 90184085a..f6f3f7b8c 100644 --- a/pkg/hcloud/apis/config/loader/loader.go +++ b/pkg/hcloud/apis/config/loader/loader.go @@ -34,6 +34,7 @@ var ( scheme *runtime.Scheme ) +// init is called by Go once. func init() { scheme = runtime.NewScheme() install.Install(scheme) @@ -48,6 +49,9 @@ func init() { } // LoadFromFile takes a filename and de-serializes the contents into ControllerConfiguration object. +// +// PARAMETERS +// filename string File path and name to load func LoadFromFile(filename string) (*config.ControllerConfiguration, error) { bytes, err := ioutil.ReadFile(filename) if err != nil { @@ -59,6 +63,9 @@ func LoadFromFile(filename string) (*config.ControllerConfiguration, error) { // Load takes a byte slice and de-serializes the contents into ControllerConfiguration object. // Encapsulates de-serialization without assuming the source is a file. +// +// PARAMETERS +// data []byte Data to decode and interprete func Load(data []byte) (*config.ControllerConfiguration, error) { cfg := &config.ControllerConfiguration{} diff --git a/pkg/hcloud/apis/config/v1alpha1/defaults.go b/pkg/hcloud/apis/config/v1alpha1/defaults.go deleted file mode 100644 index 01baa39a4..000000000 --- a/pkg/hcloud/apis/config/v1alpha1/defaults.go +++ /dev/null @@ -1,26 +0,0 @@ -/* -Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package v1alpha1 provides hcloud.provider.extensions.config.gardener.cloud/v1alpha1 -package v1alpha1 - -import ( - "k8s.io/apimachinery/pkg/runtime" -) - -func addDefaultingFuncs(scheme *runtime.Scheme) error { - return RegisterDefaults(scheme) -} diff --git a/pkg/hcloud/apis/config/v1alpha1/register.go b/pkg/hcloud/apis/config/v1alpha1/register.go index e060239e5..884e8d2fc 100644 --- a/pkg/hcloud/apis/config/v1alpha1/register.go +++ b/pkg/hcloud/apis/config/v1alpha1/register.go @@ -48,6 +48,14 @@ func init() { localSchemeBuilder.Register(addDefaultingFuncs, addKnownTypes) } +// addDefaultingFuncs sets defaults in the scheme given. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to set defaults in. +func addDefaultingFuncs(scheme *runtime.Scheme) error { + return RegisterDefaults(scheme) +} + // Adds the list of known types to api.Scheme. func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, diff --git a/pkg/hcloud/apis/register.go b/pkg/hcloud/apis/register.go index 67ac299dc..633a115d6 100644 --- a/pkg/hcloud/apis/register.go +++ b/pkg/hcloud/apis/register.go @@ -28,16 +28,6 @@ const GroupName = "hcloud.provider.extensions.gardener.cloud" // SchemeGroupVersion is group version used to register these objects var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal} -// Kind takes an unqualified kind and returns a Group qualified GroupKind -func Kind(kind string) schema.GroupKind { - return SchemeGroupVersion.WithKind(kind).GroupKind() -} - -// Resource takes an unqualified resource and returns a Group qualified GroupResource -func Resource(resource string) schema.GroupResource { - return SchemeGroupVersion.WithResource(resource).GroupResource() -} - var ( // SchemeBuilder used to register the Shoot resource. SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) @@ -46,6 +36,9 @@ var ( ) // Adds the list of known types to api.Scheme. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to set known types in. func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, &CloudProfileConfig{}, @@ -56,3 +49,19 @@ func addKnownTypes(scheme *runtime.Scheme) error { ) return nil } + +// Kind takes an unqualified kind and returns a Group qualified GroupKind +// +// PARAMETERS +// kind string Unqualified kind +func Kind(kind string) schema.GroupKind { + return SchemeGroupVersion.WithKind(kind).GroupKind() +} + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +// +// PARAMETERS +// resource string Unqualified resource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} diff --git a/pkg/hcloud/apis/types_cloudprofile.go b/pkg/hcloud/apis/types_cloudprofile.go index 009222b26..9b815ada1 100644 --- a/pkg/hcloud/apis/types_cloudprofile.go +++ b/pkg/hcloud/apis/types_cloudprofile.go @@ -65,7 +65,9 @@ type MachineImages struct { type MachineImageVersion struct { // Version is the version of the image. Version string + // ImageName is the Hetzner Cloud image name if not matching name + "-" + version. + // +optional ImageName string `json:"imageName,omitempty"` } diff --git a/pkg/hcloud/apis/types_controlplane.go b/pkg/hcloud/apis/types_controlplane.go index a03e920a1..b9687a61c 100644 --- a/pkg/hcloud/apis/types_controlplane.go +++ b/pkg/hcloud/apis/types_controlplane.go @@ -28,7 +28,7 @@ type ControlPlaneConfig struct { metav1.TypeMeta // Zone is the HCloud zone. - Zone string `json:"zone,omitempty"` + Zone string `json:"zone"` // CloudControllerManager contains configuration settings for the cloud-controller-manager. CloudControllerManager *CloudControllerManagerConfig diff --git a/pkg/hcloud/apis/types_infrastructure.go b/pkg/hcloud/apis/types_infrastructure.go index a910de895..a4482156a 100644 --- a/pkg/hcloud/apis/types_infrastructure.go +++ b/pkg/hcloud/apis/types_infrastructure.go @@ -26,9 +26,12 @@ import ( // InfrastructureConfig infrastructure configuration resource type InfrastructureConfig struct { metav1.TypeMeta `json:",inline"` + // FloatingPoolName contains the FloatingPoolName name in which LoadBalancer FIPs should be created. + // +optional FloatingPoolName string `json:"floatingPoolName,omitempty"` // Networks is the HCloud specific network configuration + // +optional Networks *Networks `json:"networks,omitempty"` } diff --git a/pkg/hcloud/apis/utils.go b/pkg/hcloud/apis/utils.go index 1af27df45..4619b0237 100644 --- a/pkg/hcloud/apis/utils.go +++ b/pkg/hcloud/apis/utils.go @@ -24,12 +24,18 @@ import ( ) // GetRegionFromZone returns the region for a given zone string +// +// PARAMETERS +// zone string Zone func GetRegionFromZone(zone string) string { zoneData := strings.SplitN(zone, "-", 2) return zoneData[0] } // GetSSHFingerprint returns the calculated fingerprint for an SSH public key. +// +// PARAMETERS +// publicKey []byte SSH public key func GetSSHFingerprint(publicKey []byte) (string, error) { publicKeyData := strings.SplitN(string(publicKey), " ", 3) if len(publicKeyData) < 2 { diff --git a/pkg/hcloud/apis/v1alpha1/defaults.go b/pkg/hcloud/apis/v1alpha1/defaults.go deleted file mode 100644 index 8b665d270..000000000 --- a/pkg/hcloud/apis/v1alpha1/defaults.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package v1alpha1 - -import ( - "k8s.io/apimachinery/pkg/runtime" -) - -func addDefaultingFuncs(scheme *runtime.Scheme) error { - return RegisterDefaults(scheme) -} diff --git a/pkg/hcloud/apis/v1alpha1/register.go b/pkg/hcloud/apis/v1alpha1/register.go index c6833842f..d2bacff30 100644 --- a/pkg/hcloud/apis/v1alpha1/register.go +++ b/pkg/hcloud/apis/v1alpha1/register.go @@ -25,11 +25,6 @@ const GroupName = "hcloud.provider.extensions.gardener.cloud" // SchemeGroupVersion is group version used to register these objects var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha1"} -// Resource takes an unqualified resource and returns a Group qualified GroupResource -func Resource(resource string) schema.GroupResource { - return SchemeGroupVersion.WithResource(resource).GroupResource() -} - var ( // SchemeBuilder used to register the Shoot resource. SchemeBuilder runtime.SchemeBuilder @@ -38,6 +33,7 @@ var ( AddToScheme = localSchemeBuilder.AddToScheme ) +// init is called by Go once. func init() { // We only register manually written functions here. The registration of the // generated functions takes place in the generated files. The separation @@ -45,7 +41,18 @@ func init() { localSchemeBuilder.Register(addDefaultingFuncs, addKnownTypes) } +// addDefaultingFuncs sets defaults in the scheme given. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to set defaults in. +func addDefaultingFuncs(scheme *runtime.Scheme) error { + return RegisterDefaults(scheme) +} + // Adds the list of known types to api.Scheme. +// +// PARAMETERS +// scheme *runtime.Scheme Kubernetes scheme to set known types in. func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, &CloudProfileConfig{}, @@ -56,3 +63,11 @@ func addKnownTypes(scheme *runtime.Scheme) error { ) return nil } + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +// +// PARAMETERS +// resource string Unqualified resource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} diff --git a/pkg/hcloud/apis/v1alpha1/types_cloudprofile.go b/pkg/hcloud/apis/v1alpha1/types_cloudprofile.go index 485bec9a9..18b705f45 100644 --- a/pkg/hcloud/apis/v1alpha1/types_cloudprofile.go +++ b/pkg/hcloud/apis/v1alpha1/types_cloudprofile.go @@ -68,7 +68,9 @@ type MachineImages struct { type MachineImageVersion struct { // Version is the version of the image. Version string `json:"version"` + // ImageName is the Hetzner Cloud image name if not matching name + "-" + version. + // +optional ImageName string `json:"imageName,omitempty"` } diff --git a/pkg/hcloud/apis/v1alpha1/types_controlplane.go b/pkg/hcloud/apis/v1alpha1/types_controlplane.go index a29995932..9caa9f597 100644 --- a/pkg/hcloud/apis/v1alpha1/types_controlplane.go +++ b/pkg/hcloud/apis/v1alpha1/types_controlplane.go @@ -27,7 +27,7 @@ type ControlPlaneConfig struct { metav1.TypeMeta `json:",inline"` // Zone is the HCloud zone. - Zone string `json:"zone,omitempty"` + Zone string `json:"zone"` // CloudControllerManager contains configuration settings for the cloud-controller-manager. // +optional diff --git a/pkg/hcloud/apis/v1alpha1/types_infrastructure.go b/pkg/hcloud/apis/v1alpha1/types_infrastructure.go index 554d8353e..98d59f2c5 100644 --- a/pkg/hcloud/apis/v1alpha1/types_infrastructure.go +++ b/pkg/hcloud/apis/v1alpha1/types_infrastructure.go @@ -27,9 +27,12 @@ import ( // InfrastructureConfig infrastructure configuration resource type InfrastructureConfig struct { metav1.TypeMeta `json:",inline"` + // FloatingPoolName contains the FloatingPoolName name in which LoadBalancer FIPs should be created. + // +optional FloatingPoolName string `json:"floatingPoolName,omitempty"` // Networks is the HCloud specific network configuration + // +optional Networks *Networks `json:"networks,omitempty"` } diff --git a/pkg/hcloud/credentials.go b/pkg/hcloud/credentials.go index b9d8b1420..d11df2484 100644 --- a/pkg/hcloud/credentials.go +++ b/pkg/hcloud/credentials.go @@ -32,34 +32,42 @@ type Token struct { // Credentials contains the necessary HCloud credential information. type Credentials struct { - hcloud *Token - hcloudMCM *Token - hcloudCCM *Token - hcloudCSI *Token + commonToken *Token + ccmToken *Token + csiToken *Token + mcmToken *Token } -func (c *Credentials) HcloudMCM() Token { - if c.hcloudMCM != nil { - return *c.hcloudMCM +// CCM returns the token used for the Cloud Controller Manager. +func (c *Credentials) CCM() Token { + if c.ccmToken != nil { + return *c.ccmToken } - return *c.hcloud + return *c.commonToken } -func (c *Credentials) HcloudCCM() Token { - if c.hcloudCCM != nil { - return *c.hcloudCCM +// CSI returns the token used for the Container Storage Interface driver. +func (c *Credentials) CSI() Token { + if c.csiToken != nil { + return *c.csiToken } - return *c.hcloud + return *c.commonToken } -func (c *Credentials) HcloudCSI() Token { - if c.hcloudCSI != nil { - return *c.hcloudCSI +// MCM returns the token used for the Machine Controller Manager. +func (c *Credentials) MCM() Token { + if c.mcmToken != nil { + return *c.mcmToken } - return *c.hcloud + return *c.commonToken } // GetCredentials computes for a given context and infrastructure the corresponding credentials object. +// +// PARAMETERS +// ctx context.Context Execution context +// c client.Client Controller client +// secretRef corev1.SecretReference Secret reference to read credentials from func GetCredentials(ctx context.Context, c client.Client, secretRef corev1.SecretReference) (*Credentials, error) { secret, err := extensionscontroller.GetSecretByReference(ctx, c, &secretRef) if err != nil { @@ -68,6 +76,11 @@ func GetCredentials(ctx context.Context, c client.Client, secretRef corev1.Secre return ExtractCredentials(secret) } +// extractToken returns the token with the given key from the secret. +// +// PARAMETERS +// secret *corev1.Secret Secret to get token from +// tokenKey string Token key func extractToken(secret *corev1.Secret, tokenKey string) (*Token, error) { token, ok := secret.Data[tokenKey] if !ok { @@ -78,30 +91,33 @@ func extractToken(secret *corev1.Secret, tokenKey string) (*Token, error) { } // ExtractCredentials generates a credentials object for a given provider secret. +// +// PARAMETERS +// secret *corev1.Secret Secret to extract tokens from func ExtractCredentials(secret *corev1.Secret) (*Credentials, error) { if secret.Data == nil { return nil, fmt.Errorf("secret does not contain any data") } - hcloud, hcloudErr := extractToken(secret, HcloudToken) + commonToken, hcloudErr := extractToken(secret, HcloudToken) - mcm, err := extractToken(secret, HcloudTokenMCM) + ccmToken, err := extractToken(secret, HcloudTokenCCM) if err != nil && hcloudErr != nil { - return nil, fmt.Errorf("Need either common or machine controller manager specific Hcloud account credentials: %s, %s", hcloudErr, err) + return nil, fmt.Errorf("Need either common or cloud controller manager specific Hcloud account credentials: %s, %s", hcloudErr, err) } - ccm, err := extractToken(secret, HcloudTokenCCM) + csiToken, err := extractToken(secret, HcloudTokenCSI) if err != nil && hcloudErr != nil { - return nil, fmt.Errorf("Need either common or cloud controller manager specific Hcloud account credentials: %s, %s", hcloudErr, err) + return nil, fmt.Errorf("Need either common or container storage interface driver specific Hcloud account credentials: %s, %s", hcloudErr, err) } - csi, err := extractToken(secret, HcloudTokenCSI) + mcmToken, err := extractToken(secret, HcloudTokenMCM) if err != nil && hcloudErr != nil { - return nil, fmt.Errorf("Need either common or cloud controller manager specific Hcloud account credentials: %s, %s", hcloudErr, err) + return nil, fmt.Errorf("Need either common or machine controller manager specific Hcloud account credentials: %s, %s", hcloudErr, err) } return &Credentials{ - hcloud: hcloud, - hcloudMCM: mcm, - hcloudCCM: ccm, - hcloudCSI: csi, + commonToken: commonToken, + ccmToken: ccmToken, + csiToken: csiToken, + mcmToken: mcmToken, }, nil } diff --git a/pkg/hcloud/types.go b/pkg/hcloud/types.go index 370442d1e..f3b3eb72d 100644 --- a/pkg/hcloud/types.go +++ b/pkg/hcloud/types.go @@ -49,10 +49,14 @@ const ( // LivenessProbeImageName is the name of the liveness-probe image. LivenessProbeImageName = "liveness-probe" + // Common HCloud credentials token HcloudToken = "hcloudToken" - HcloudTokenMCM = "hcloudTokenMCM" + // Cloud Controller Manager HCloud credentials token HcloudTokenCCM = "hcloudTokenCCM" + // Container Storage Interface driver HCloud credentials token HcloudTokenCSI = "hcloudTokenCSI" + // Machine Controller Manager HCloud credentials token + HcloudTokenMCM = "hcloudTokenMCM" // CloudProviderConfig is the name of the configmap containing the cloud provider config. CloudProviderConfig = "cloud-provider-config" diff --git a/pkg/webhook/controlplane/registration.go b/pkg/webhook/controlplane/registration.go index 68cf6c5bf..be938ca14 100644 --- a/pkg/webhook/controlplane/registration.go +++ b/pkg/webhook/controlplane/registration.go @@ -36,6 +36,9 @@ import ( var logger = log.Log.WithName("hcloud-controlplane-webhook") // AddToManager creates a webhook and adds it to the manager. +// +// PARAMETERS +// mgr manager.Manager Webhook control plane controller manager instance func AddToManager(mgr manager.Manager) (*extensionswebhook.Webhook, error) { logger.Info("Adding webhook to manager") fciCodec := oscutils.NewFileContentInlineCodec()