Skip to content

Commit

Permalink
refactor: new waiter functionality in helmAddonApplier (#988)
Browse files Browse the repository at this point in the history
**What problem does this PR solve?**:
This PR is a small refactor to pull existing functionality into the
common addon applier. I wanted similar functionality in another handler
to wait for the HCP to be ready.

**Which issue(s) this PR fixes**:
Fixes #

**How Has This Been Tested?**:
<!--
Please describe the tests that you ran to verify your changes.
Provide output from the tests and any manual steps needed to replicate
the tests.
-->

**Special notes for your reviewer**:
<!--
Use this to provide any additional information to the reviewers.
This may include:
- Best way to review the PR.
- Where the author wants the most review attention on.
- etc.
-->

---------

Co-authored-by: Jimmi Dyson <[email protected]>
  • Loading branch information
dkoshkin and jimmidyson authored Dec 5, 2024
1 parent 8e2b307 commit 8c40639
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
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

0 comments on commit 8c40639

Please sign in to comment.