@@ -6,11 +6,13 @@ package addons
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "time"
9
10
10
11
"github.com/go-logr/logr"
11
12
"github.com/spf13/pflag"
12
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
14
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
15
+ "sigs.k8s.io/cluster-api/util/conditions"
14
16
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
15
17
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
16
18
@@ -19,6 +21,7 @@ import (
19
21
k8sclient "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
20
22
lifecycleconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
21
23
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"
22
25
)
23
26
24
27
var (
@@ -75,16 +78,21 @@ func NewHelmAddonApplier(
75
78
}
76
79
}
77
80
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
+
78
85
type applyOptions struct {
79
- valueTemplater func ( cluster * clusterv1. Cluster , valuesTemplate string ) ( string , error )
86
+ valueTemplater valueTemplaterFunc
80
87
targetCluster * clusterv1.Cluster
81
88
helmReleaseName string
89
+ waiter waiterFunc
82
90
}
83
91
84
92
type applyOption func (* applyOptions )
85
93
86
94
func (a * helmAddonApplier ) WithValueTemplater (
87
- valueTemplater func ( cluster * clusterv1. Cluster , valuesTemplate string ) ( string , error ) ,
95
+ valueTemplater valueTemplaterFunc ,
88
96
) * helmAddonApplier {
89
97
a .opts = append (a .opts , func (o * applyOptions ) {
90
98
o .valueTemplater = valueTemplater
@@ -109,6 +117,14 @@ func (a *helmAddonApplier) WithHelmReleaseName(name string) *helmAddonApplier {
109
117
return a
110
118
}
111
119
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
+
112
128
func (a * helmAddonApplier ) Apply (
113
129
ctx context.Context ,
114
130
cluster * clusterv1.Cluster ,
@@ -194,5 +210,32 @@ func (a *helmAddonApplier) Apply(
194
210
return fmt .Errorf ("failed to apply HelmChartProxy %q: %w" , chartProxy .Name , err )
195
211
}
196
212
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
+
197
240
return nil
198
241
}
0 commit comments