Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
refactor: reduce stutter in addon handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Apr 4, 2024
1 parent 788bc11 commit 4febefc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
26 changes: 13 additions & 13 deletions pkg/handlers/generic/lifecycle/ccm/aws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
)

type AWSCCMConfig struct {
type Config struct {
*options.GlobalOptions

kubernetesMinorVersionToCCMConfigMapNames map[string]string
}

func (a *AWSCCMConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
func (a *Config) AddFlags(prefix string, flags *pflag.FlagSet) {
flags.StringToStringVar(
&a.kubernetesMinorVersionToCCMConfigMapNames,
prefix+".default-aws-ccm-configmap-names",
Expand All @@ -38,22 +38,22 @@ func (a *AWSCCMConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
)
}

type AWSCCM struct {
type provider struct {
client ctrlclient.Client
config *AWSCCMConfig
config *Config
}

func New(
c ctrlclient.Client,
cfg *AWSCCMConfig,
) *AWSCCM {
return &AWSCCM{
cfg *Config,
) *provider {
return &provider{
client: c,
config: cfg,
}
}

func (a *AWSCCM) Apply(
func (p *provider) Apply(
ctx context.Context,
cluster *clusterv1.Cluster,
) error {
Expand All @@ -67,17 +67,17 @@ func (a *AWSCCM) Apply(
return fmt.Errorf("failed to parse version from cluster %w", err)
}
minorVersion := fmt.Sprintf("%d.%d", version.Major, version.Minor)
configMapForMinorVersion := a.config.kubernetesMinorVersionToCCMConfigMapNames[minorVersion]
configMapForMinorVersion := p.config.kubernetesMinorVersionToCCMConfigMapNames[minorVersion]
ccmConfigMapForMinorVersion := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: a.config.DefaultsNamespace(),
Namespace: p.config.DefaultsNamespace(),
Name: configMapForMinorVersion,
},
}
objName := ctrlclient.ObjectKeyFromObject(
ccmConfigMapForMinorVersion,
)
err = a.client.Get(ctx, objName, ccmConfigMapForMinorVersion)
err = p.client.Get(ctx, objName, ccmConfigMapForMinorVersion)
if err != nil {
log.Error(err, "failed to fetch CCM template for cluster")
return fmt.Errorf(
Expand All @@ -88,15 +88,15 @@ func (a *AWSCCM) Apply(
}

ccmConfigMap := generateCCMConfigMapForCluster(ccmConfigMapForMinorVersion, cluster)
if err = client.ServerSideApply(ctx, a.client, ccmConfigMap); err != nil {
if err = client.ServerSideApply(ctx, p.client, ccmConfigMap); err != nil {
log.Error(err, "failed to apply CCM configmap for cluster")
return fmt.Errorf(
"failed to apply AWS CCM manifests ConfigMap: %w",
err,
)
}

err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, ccmConfigMap.Name, a.client, cluster, ccmConfigMap)
err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, ccmConfigMap.Name, p.client, cluster, ccmConfigMap)
if err != nil {
return fmt.Errorf("failed to generate CCM CRS for cluster: %w", err)
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/handlers/generic/lifecycle/ccm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@ const (
variableRootName = "ccm"
)

type CCMProvider interface {
type Provider interface {
Apply(context.Context, *clusterv1.Cluster) error
}

type CCMHandler struct {
type ccmHandler struct {
client ctrlclient.Client
variableName string
variablePath []string
ProviderHandler map[string]CCMProvider
ProviderHandler map[string]Provider
}

var (
_ commonhandlers.Named = &CCMHandler{}
_ lifecycle.AfterControlPlaneInitialized = &CCMHandler{}
_ commonhandlers.Named = &ccmHandler{}
_ lifecycle.AfterControlPlaneInitialized = &ccmHandler{}
)

func New(
c ctrlclient.Client,
handlers map[string]CCMProvider,
) *CCMHandler {
return &CCMHandler{
handlers map[string]Provider,
) *ccmHandler {
return &ccmHandler{
client: c,
variableName: clusterconfig.MetaVariableName,
variablePath: []string{"addons", variableRootName},
ProviderHandler: handlers,
}
}

func (c *CCMHandler) Name() string {
func (c *ccmHandler) Name() string {
return "CCMHandler"
}

func (c *CCMHandler) AfterControlPlaneInitialized(
func (c *ccmHandler) AfterControlPlaneInitialized(
ctx context.Context,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
resp *runtimehooksv1.AfterControlPlaneInitializedResponse,
Expand Down Expand Up @@ -90,7 +90,7 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
}
infraKind := req.Cluster.Spec.InfrastructureRef.Kind
log.Info(fmt.Sprintf("finding CCM handler for %s", infraKind))
var handler CCMProvider
var handler Provider
switch {
case strings.Contains(strings.ToLower(infraKind), v1alpha1.CCMProviderAWS):
handler = c.ProviderHandler[v1alpha1.CCMProviderAWS]
Expand Down
20 changes: 10 additions & 10 deletions pkg/handlers/generic/lifecycle/csi/aws-ebs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
)

type AWSEBSConfig struct {
type Config struct {
*options.GlobalOptions
defaultAWSEBSConfigMapName string
}

func (a *AWSEBSConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
func (a *Config) AddFlags(prefix string, flags *pflag.FlagSet) {
flags.StringVar(
&a.defaultAWSEBSConfigMapName,
prefix+".aws-ebs-provider-configmap-name",
Expand All @@ -35,22 +35,22 @@ func (a *AWSEBSConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
)
}

type AWSEBS struct {
type CSI struct {
client ctrlclient.Client
config *AWSEBSConfig
config *Config
}

func New(
c ctrlclient.Client,
cfg *AWSEBSConfig,
) *AWSEBS {
return &AWSEBS{
cfg *Config,
) *CSI {
return &CSI{
client: c,
config: cfg,
}
}

func (a *AWSEBS) Apply(
func (a *CSI) Apply(
ctx context.Context,
provider v1alpha1.CSIProvider,
defaultStorageConfig *v1alpha1.DefaultStorage,
Expand All @@ -75,7 +75,7 @@ func (a *AWSEBS) Apply(
)
}

func (a *AWSEBS) createStorageClasses(ctx context.Context,
func (a *CSI) createStorageClasses(ctx context.Context,
configs []v1alpha1.StorageClassConfig,
cluster *clusterv1.Cluster,
defaultStorageConfig *v1alpha1.DefaultStorage,
Expand Down Expand Up @@ -112,7 +112,7 @@ func (a *AWSEBS) createStorageClasses(ctx context.Context,
)
}

func (a *AWSEBS) handleCRSApply(ctx context.Context,
func (a *CSI) handleCRSApply(ctx context.Context,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
) error {
awsEBSCSIConfigMap := &corev1.ConfigMap{
Expand Down
20 changes: 10 additions & 10 deletions pkg/handlers/generic/lifecycle/csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
variableRootName = "csi"
)

type CSIProvider interface {
type Provider interface {
Apply(
context.Context,
v1alpha1.CSIProvider,
Expand All @@ -31,35 +31,35 @@ type CSIProvider interface {
) error
}

type CSIHandler struct {
type Handler struct {
client ctrlclient.Client
variableName string
variablePath []string
ProviderHandler map[string]CSIProvider
ProviderHandler map[string]Provider
}

var (
_ commonhandlers.Named = &CSIHandler{}
_ lifecycle.AfterControlPlaneInitialized = &CSIHandler{}
_ commonhandlers.Named = &Handler{}
_ lifecycle.AfterControlPlaneInitialized = &Handler{}
)

func New(
c ctrlclient.Client,
handlers map[string]CSIProvider,
) *CSIHandler {
return &CSIHandler{
handlers map[string]Provider,
) *Handler {
return &Handler{
client: c,
variableName: clusterconfig.MetaVariableName,
variablePath: []string{"addons", variableRootName},
ProviderHandler: handlers,
}
}

func (c *CSIHandler) Name() string {
func (c *Handler) Name() string {
return "CSIHandler"
}

func (c *CSIHandler) AfterControlPlaneInitialized(
func (c *Handler) AfterControlPlaneInitialized(
ctx context.Context,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
resp *runtimehooksv1.AfterControlPlaneInitializedResponse,
Expand Down
20 changes: 10 additions & 10 deletions pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const (
defaultSnapshotHelmReleaseNameTemplate = "nutanix-csi-snapshot-%s"
)

type NutanixCSIConfig struct {
type Config struct {
*options.GlobalOptions
defaultValuesTemplateConfigMapName string
}

func (n *NutanixCSIConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
func (n *Config) AddFlags(prefix string, flags *pflag.FlagSet) {
flags.StringVar(
&n.defaultValuesTemplateConfigMapName,
prefix+".default-values-template-configmap-name",
Expand All @@ -48,22 +48,22 @@ func (n *NutanixCSIConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
)
}

type NutanixCSI struct {
type CSI struct {
client ctrlclient.Client
config *NutanixCSIConfig
config *Config
}

func New(
c ctrlclient.Client,
cfg *NutanixCSIConfig,
) *NutanixCSI {
return &NutanixCSI{
cfg *Config,
) *CSI {
return &CSI{
client: c,
config: cfg,
}
}

func (n *NutanixCSI) Apply(
func (n *CSI) Apply(
ctx context.Context,
provider v1alpha1.CSIProvider,
defaultStorageConfig *v1alpha1.DefaultStorage,
Expand Down Expand Up @@ -118,7 +118,7 @@ func (n *NutanixCSI) Apply(
)
}

func (n *NutanixCSI) handleHelmAddonApply(
func (n *CSI) handleHelmAddonApply(
ctx context.Context,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
) error {
Expand Down Expand Up @@ -205,7 +205,7 @@ func (n *NutanixCSI) handleHelmAddonApply(
return nil
}

func (n *NutanixCSI) createStorageClasses(ctx context.Context,
func (n *CSI) createStorageClasses(ctx context.Context,
configs []v1alpha1.StorageClassConfig,
cluster *clusterv1.Cluster,
defaultStorageConfig *v1alpha1.DefaultStorage,
Expand Down
28 changes: 14 additions & 14 deletions pkg/handlers/generic/lifecycle/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type Handlers struct {
ciliumCNIConfig *cilium.CNIConfig
nfdConfig *nfd.Config
clusterAutoscalerConfig *clusterautoscaler.Config
ebsConfig *awsebs.AWSEBSConfig
nutnaixCSIConfig *nutanixcsi.NutanixCSIConfig
awsccmConfig *awsccm.AWSCCMConfig
ebsCSIConfig *awsebs.Config
nutanixCSIConfig *nutanixcsi.Config
awsCCMConfig *awsccm.Config
}

func New(globalOptions *options.GlobalOptions) *Handlers {
Expand All @@ -38,19 +38,19 @@ func New(globalOptions *options.GlobalOptions) *Handlers {
ciliumCNIConfig: &cilium.CNIConfig{GlobalOptions: globalOptions},
nfdConfig: &nfd.Config{GlobalOptions: globalOptions},
clusterAutoscalerConfig: &clusterautoscaler.Config{GlobalOptions: globalOptions},
ebsConfig: &awsebs.AWSEBSConfig{GlobalOptions: globalOptions},
awsccmConfig: &awsccm.AWSCCMConfig{GlobalOptions: globalOptions},
nutnaixCSIConfig: &nutanixcsi.NutanixCSIConfig{GlobalOptions: globalOptions},
ebsCSIConfig: &awsebs.Config{GlobalOptions: globalOptions},
awsCCMConfig: &awsccm.Config{GlobalOptions: globalOptions},
nutanixCSIConfig: &nutanixcsi.Config{GlobalOptions: globalOptions},
}
}

func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
csiHandlers := map[string]csi.CSIProvider{
v1alpha1.CSIProviderAWSEBS: awsebs.New(mgr.GetClient(), h.ebsConfig),
v1alpha1.CSIProviderNutanix: nutanixcsi.New(mgr.GetClient(), h.nutnaixCSIConfig),
csiHandlers := map[string]csi.Provider{
v1alpha1.CSIProviderAWSEBS: awsebs.New(mgr.GetClient(), h.ebsCSIConfig),
v1alpha1.CSIProviderNutanix: nutanixcsi.New(mgr.GetClient(), h.nutanixCSIConfig),
}
ccmHandlers := map[string]ccm.CCMProvider{
v1alpha1.CCMProviderAWS: awsccm.New(mgr.GetClient(), h.awsccmConfig),
ccmHandlers := map[string]ccm.Provider{
v1alpha1.CCMProviderAWS: awsccm.New(mgr.GetClient(), h.awsCCMConfig),
}

return []handlers.Named{
Expand All @@ -69,7 +69,7 @@ func (h *Handlers) AddFlags(flagSet *pflag.FlagSet) {
h.clusterAutoscalerConfig.AddFlags("cluster-autoscaler", flagSet)
h.calicoCNIConfig.AddFlags("cni.calico", flagSet)
h.ciliumCNIConfig.AddFlags("cni.cilium", flagSet)
h.ebsConfig.AddFlags("awsebs", pflag.CommandLine)
h.awsccmConfig.AddFlags("awsccm", pflag.CommandLine)
h.nutnaixCSIConfig.AddFlags("nutanixcsi", flagSet)
h.ebsCSIConfig.AddFlags("awsebs", pflag.CommandLine)
h.awsCCMConfig.AddFlags("awsccm", pflag.CommandLine)
h.nutanixCSIConfig.AddFlags("nutanixcsi", flagSet)
}

0 comments on commit 4febefc

Please sign in to comment.