@@ -6,20 +6,25 @@ package metallb
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"
13
+ corev1 "k8s.io/api/core/v1"
12
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
+ kwait "k8s.io/apimachinery/pkg/util/wait"
13
16
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14
17
"sigs.k8s.io/cluster-api/controllers/remote"
15
18
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
16
19
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
17
20
18
21
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
22
+ "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
19
23
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
20
24
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
21
25
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
22
26
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"
23
28
)
24
29
25
30
const (
@@ -70,6 +75,7 @@ func New(
70
75
71
76
func (n * MetalLB ) Apply (
72
77
ctx context.Context ,
78
+ slb v1alpha1.ServiceLoadBalancer ,
73
79
cluster * clusterv1.Cluster ,
74
80
log logr.Logger ,
75
81
) error {
@@ -152,5 +158,77 @@ func (n *MetalLB) Apply(
152
158
return fmt .Errorf ("failed to apply MetalLB installation HelmChartProxy: %w" , err )
153
159
}
154
160
161
+ if err := wait .ForObject (
162
+ ctx ,
163
+ wait.ForObjectInput [* caaphv1.HelmChartProxy ]{
164
+ Reader : n .client ,
165
+ Target : hcp .DeepCopy (),
166
+ Check : func (_ context.Context , obj * caaphv1.HelmChartProxy ) (bool , error ) {
167
+ for _ , c := range obj .GetConditions () {
168
+ if c .Type == caaphv1 .HelmReleaseProxiesReadyCondition && c .Status == corev1 .ConditionTrue {
169
+ return true , nil
170
+ }
171
+ }
172
+ return false , nil
173
+ },
174
+ Interval : 5 * time .Second ,
175
+ Timeout : 10 * time .Second ,
176
+ },
177
+ ); err != nil {
178
+ return fmt .Errorf ("failed to wait for MetalLB to deploy: %w" , err )
179
+ }
180
+
181
+ log .Info (
182
+ fmt .Sprintf ("Applying MetalLB configuration to cluster %s" ,
183
+ ctrlclient .ObjectKeyFromObject (cluster ),
184
+ ),
185
+ )
186
+
187
+ cos , err := configurationObjects (& configurationInput {
188
+ name : defaultHelmReleaseName ,
189
+ namespace : defaultHelmReleaseNamespace ,
190
+ addressRanges : slb .Configuration .AddressRanges ,
191
+ })
192
+ if err != nil {
193
+ return fmt .Errorf ("failed to generate MetalLB configuration: %w" , err )
194
+ }
195
+
196
+ var applyErr error
197
+ if waitErr := kwait .PollUntilContextTimeout (
198
+ ctx ,
199
+ 5 * time .Second ,
200
+ 10 * time .Second ,
201
+ true ,
202
+ func (ctx context.Context ) (done bool , err error ) {
203
+ for i := range cos {
204
+ o := & cos [i ]
205
+ if err = client .ServerSideApply (
206
+ ctx ,
207
+ remoteClient ,
208
+ o ,
209
+ & ctrlclient.PatchOptions {
210
+ Raw : & metav1.PatchOptions {
211
+ FieldValidation : metav1 .FieldValidationStrict ,
212
+ },
213
+ },
214
+ ); err != nil {
215
+ applyErr = fmt .Errorf (
216
+ "failed to apply MetalLB configuration %s %s: %w" ,
217
+ o .GetKind (),
218
+ ctrlclient .ObjectKeyFromObject (o ),
219
+ err ,
220
+ )
221
+ return false , nil
222
+ }
223
+ }
224
+ return true , nil
225
+ },
226
+ ); waitErr != nil {
227
+ if applyErr != nil {
228
+ return fmt .Errorf ("%w: last apply error: %w" , waitErr , applyErr )
229
+ }
230
+ return fmt .Errorf ("%w: failed to apply MetalLB configuration" , waitErr )
231
+ }
232
+
155
233
return nil
156
234
}
0 commit comments