@@ -6,11 +6,13 @@ package addons
66import (
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
2427var (
@@ -75,16 +78,20 @@ func NewHelmAddonApplier(
7578 }
7679}
7780
81+ type valueTemplaterFunc func (cluster * clusterv1.Cluster , valuesTemplate string ) (string , error )
82+ type waiterFunc func (ctx context.Context , client ctrlclient.Client , hcp * caaphv1.HelmChartProxy ) error
83+
7884type applyOptions struct {
79- valueTemplater func ( cluster * clusterv1. Cluster , valuesTemplate string ) ( string , error )
85+ valueTemplater valueTemplaterFunc
8086 targetCluster * clusterv1.Cluster
8187 helmReleaseName string
88+ waiter waiterFunc
8289}
8390
8491type applyOption func (* applyOptions )
8592
8693func (a * helmAddonApplier ) WithValueTemplater (
87- valueTemplater func ( cluster * clusterv1. Cluster , valuesTemplate string ) ( string , error ) ,
94+ valueTemplater valueTemplaterFunc ,
8895) * helmAddonApplier {
8996 a .opts = append (a .opts , func (o * applyOptions ) {
9097 o .valueTemplater = valueTemplater
@@ -109,6 +116,14 @@ func (a *helmAddonApplier) WithHelmReleaseName(name string) *helmAddonApplier {
109116 return a
110117}
111118
119+ func (a * helmAddonApplier ) WithDefaultWaiter () * helmAddonApplier {
120+ a .opts = append (a .opts , func (o * applyOptions ) {
121+ o .waiter = waitToBeReady
122+ })
123+
124+ return a
125+ }
126+
112127func (a * helmAddonApplier ) Apply (
113128 ctx context.Context ,
114129 cluster * clusterv1.Cluster ,
@@ -194,5 +209,32 @@ func (a *helmAddonApplier) Apply(
194209 return fmt .Errorf ("failed to apply HelmChartProxy %q: %w" , chartProxy .Name , err )
195210 }
196211
212+ if applyOpts .waiter != nil {
213+ return applyOpts .waiter (ctx , a .client , chartProxy )
214+ }
215+
216+ return nil
217+ }
218+
219+ func waitToBeReady (
220+ ctx context.Context ,
221+ client ctrlclient.Client ,
222+ hcp * caaphv1.HelmChartProxy ,
223+ ) error {
224+ if err := wait .ForObject (
225+ ctx ,
226+ wait.ForObjectInput [* caaphv1.HelmChartProxy ]{
227+ Reader : client ,
228+ Target : hcp .DeepCopy (),
229+ Check : func (_ context.Context , obj * caaphv1.HelmChartProxy ) (bool , error ) {
230+ return conditions .IsTrue (obj , caaphv1 .HelmReleaseProxiesReadyCondition ), nil
231+ },
232+ Interval : 5 * time .Second ,
233+ Timeout : 30 * time .Second ,
234+ },
235+ ); err != nil {
236+ return fmt .Errorf ("failed to wait for MetalLB to deploy: %w" , err )
237+ }
238+
197239 return nil
198240}
0 commit comments