Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTheEvilOne committed Sep 20, 2021
1 parent 6d12908 commit f5b9ce4
Show file tree
Hide file tree
Showing 35 changed files with 380 additions and 127 deletions.
1 change: 1 addition & 0 deletions cmd/gardener-extension-provider-hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/controller/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/controlplane/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
81 changes: 59 additions & 22 deletions pkg/controller/controlplane/valuesprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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{}{
Expand All @@ -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,
Expand All @@ -455,20 +483,29 @@ 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,
},
}

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")
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/healthcheck/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
42 changes: 34 additions & 8 deletions pkg/controller/infrastructure/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/infrastructure/actuator_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/infrastructure/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f5b9ce4

Please sign in to comment.