Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions pkg/handlers/generic/lifecycle/addons/helmaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package addons
import (
"context"
"fmt"
"time"

"github.com/go-logr/logr"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand All @@ -19,6 +21,7 @@ import (
k8sclient "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
lifecycleconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/wait"
)

var (
Expand Down Expand Up @@ -75,16 +78,21 @@ func NewHelmAddonApplier(
}
}

type valueTemplaterFunc func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error)

type waiterFunc func(ctx context.Context, client ctrlclient.Client, hcp *caaphv1.HelmChartProxy) error

type applyOptions struct {
valueTemplater func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error)
valueTemplater valueTemplaterFunc
targetCluster *clusterv1.Cluster
helmReleaseName string
waiter waiterFunc
}

type applyOption func(*applyOptions)

func (a *helmAddonApplier) WithValueTemplater(
valueTemplater func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error),
valueTemplater valueTemplaterFunc,
) *helmAddonApplier {
a.opts = append(a.opts, func(o *applyOptions) {
o.valueTemplater = valueTemplater
Expand All @@ -109,6 +117,14 @@ func (a *helmAddonApplier) WithHelmReleaseName(name string) *helmAddonApplier {
return a
}

func (a *helmAddonApplier) WithDefaultWaiter() *helmAddonApplier {
a.opts = append(a.opts, func(o *applyOptions) {
o.waiter = waitToBeReady
})

return a
}

func (a *helmAddonApplier) Apply(
ctx context.Context,
cluster *clusterv1.Cluster,
Expand Down Expand Up @@ -194,5 +210,36 @@ func (a *helmAddonApplier) Apply(
return fmt.Errorf("failed to apply HelmChartProxy %q: %w", chartProxy.Name, err)
}

if applyOpts.waiter != nil {
return applyOpts.waiter(ctx, a.client, chartProxy)
}

return nil
}

func waitToBeReady(
ctx context.Context,
client ctrlclient.Client,
hcp *caaphv1.HelmChartProxy,
) error {
if err := wait.ForObject(
ctx,
wait.ForObjectInput[*caaphv1.HelmChartProxy]{
Reader: client,
Target: hcp.DeepCopy(),
Check: func(_ context.Context, obj *caaphv1.HelmChartProxy) (bool, error) {
return conditions.IsTrue(obj, caaphv1.HelmReleaseProxiesReadyCondition), nil
},
Interval: 5 * time.Second,
Timeout: 30 * time.Second,
},
); err != nil {
return fmt.Errorf(
"failed to wait for addon %s to deploy: %w",
ctrlclient.ObjectKeyFromObject(hcp),
err,
)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ import (
kwait "k8s.io/apimachinery/pkg/util/wait"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/util/conditions"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/addons"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/wait"
)

const (
Expand Down Expand Up @@ -118,38 +115,12 @@ func (n *MetalLB) Apply(
),
n.client,
helmChartInfo,
)
).WithDefaultWaiter()

if err := addonApplier.Apply(ctx, cluster, n.config.DefaultsNamespace(), log); err != nil {
return fmt.Errorf("failed to apply MetalLB addon: %w", err)
}

hcp := &caaphv1.HelmChartProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: fmt.Sprintf(
"%s-%s",
DefaultHelmReleaseName,
cluster.Annotations[v1alpha1.ClusterUUIDAnnotationKey],
),
},
}

if err := wait.ForObject(
ctx,
wait.ForObjectInput[*caaphv1.HelmChartProxy]{
Reader: n.client,
Target: hcp.DeepCopy(),
Check: func(_ context.Context, obj *caaphv1.HelmChartProxy) (bool, error) {
return conditions.IsTrue(obj, caaphv1.HelmReleaseProxiesReadyCondition), nil
},
Interval: 5 * time.Second,
Timeout: 30 * time.Second,
},
); err != nil {
return fmt.Errorf("failed to wait for MetalLB to deploy: %w", err)
}

if slb.Configuration == nil {
// Nothing more to do.
return nil
Expand Down
Loading