Skip to content

Commit 5dc7cbb

Browse files
committed
refactor: new waiter functionality in helmAddonApplier
1 parent 8e2b307 commit 5dc7cbb

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

pkg/handlers/generic/lifecycle/addons/helmaddon.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package addons
66
import (
77
"context"
88
"fmt"
9+
"time"
910

1011
"github.com/go-logr/logr"
1112
"github.com/spf13/pflag"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
15+
"sigs.k8s.io/cluster-api/util/conditions"
1416
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1517
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1618

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

2427
var (
@@ -75,16 +78,21 @@ func NewHelmAddonApplier(
7578
}
7679
}
7780

81+
type valueTemplaterFunc func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error)
82+
83+
type waiterFunc func(ctx context.Context, client ctrlclient.Client, hcp *caaphv1.HelmChartProxy) error
84+
7885
type applyOptions struct {
79-
valueTemplater func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error)
86+
valueTemplater valueTemplaterFunc
8087
targetCluster *clusterv1.Cluster
8188
helmReleaseName string
89+
waiter waiterFunc
8290
}
8391

8492
type applyOption func(*applyOptions)
8593

8694
func (a *helmAddonApplier) WithValueTemplater(
87-
valueTemplater func(cluster *clusterv1.Cluster, valuesTemplate string) (string, error),
95+
valueTemplater valueTemplaterFunc,
8896
) *helmAddonApplier {
8997
a.opts = append(a.opts, func(o *applyOptions) {
9098
o.valueTemplater = valueTemplater
@@ -109,6 +117,14 @@ func (a *helmAddonApplier) WithHelmReleaseName(name string) *helmAddonApplier {
109117
return a
110118
}
111119

120+
func (a *helmAddonApplier) WithDefaultWaiter() *helmAddonApplier {
121+
a.opts = append(a.opts, func(o *applyOptions) {
122+
o.waiter = waitToBeReady
123+
})
124+
125+
return a
126+
}
127+
112128
func (a *helmAddonApplier) Apply(
113129
ctx context.Context,
114130
cluster *clusterv1.Cluster,
@@ -194,5 +210,32 @@ func (a *helmAddonApplier) Apply(
194210
return fmt.Errorf("failed to apply HelmChartProxy %q: %w", chartProxy.Name, err)
195211
}
196212

213+
if applyOpts.waiter != nil {
214+
return applyOpts.waiter(ctx, a.client, chartProxy)
215+
}
216+
217+
return nil
218+
}
219+
220+
func waitToBeReady(
221+
ctx context.Context,
222+
client ctrlclient.Client,
223+
hcp *caaphv1.HelmChartProxy,
224+
) error {
225+
if err := wait.ForObject(
226+
ctx,
227+
wait.ForObjectInput[*caaphv1.HelmChartProxy]{
228+
Reader: client,
229+
Target: hcp.DeepCopy(),
230+
Check: func(_ context.Context, obj *caaphv1.HelmChartProxy) (bool, error) {
231+
return conditions.IsTrue(obj, caaphv1.HelmReleaseProxiesReadyCondition), nil
232+
},
233+
Interval: 5 * time.Second,
234+
Timeout: 30 * time.Second,
235+
},
236+
); err != nil {
237+
return fmt.Errorf("failed to wait for MetalLB to deploy: %w", err)
238+
}
239+
197240
return nil
198241
}

pkg/handlers/generic/lifecycle/serviceloadbalancer/metallb/handler.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ import (
1414
kwait "k8s.io/apimachinery/pkg/util/wait"
1515
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1616
"sigs.k8s.io/cluster-api/controllers/remote"
17-
"sigs.k8s.io/cluster-api/util/conditions"
1817
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1918

20-
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
2119
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
2220
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
2321
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/addons"
2422
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
2523
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
2624
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
27-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/wait"
2825
)
2926

3027
const (
@@ -118,38 +115,12 @@ func (n *MetalLB) Apply(
118115
),
119116
n.client,
120117
helmChartInfo,
121-
)
118+
).WithDefaultWaiter()
122119

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

127-
hcp := &caaphv1.HelmChartProxy{
128-
ObjectMeta: metav1.ObjectMeta{
129-
Namespace: cluster.Namespace,
130-
Name: fmt.Sprintf(
131-
"%s-%s",
132-
DefaultHelmReleaseName,
133-
cluster.Annotations[v1alpha1.ClusterUUIDAnnotationKey],
134-
),
135-
},
136-
}
137-
138-
if err := wait.ForObject(
139-
ctx,
140-
wait.ForObjectInput[*caaphv1.HelmChartProxy]{
141-
Reader: n.client,
142-
Target: hcp.DeepCopy(),
143-
Check: func(_ context.Context, obj *caaphv1.HelmChartProxy) (bool, error) {
144-
return conditions.IsTrue(obj, caaphv1.HelmReleaseProxiesReadyCondition), nil
145-
},
146-
Interval: 5 * time.Second,
147-
Timeout: 30 * time.Second,
148-
},
149-
); err != nil {
150-
return fmt.Errorf("failed to wait for MetalLB to deploy: %w", err)
151-
}
152-
153124
if slb.Configuration == nil {
154125
// Nothing more to do.
155126
return nil

0 commit comments

Comments
 (0)