Skip to content

Commit 4824f44

Browse files
committed
feat: support custom readiness check
1 parent a41032c commit 4824f44

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

providers/cluster-inventory-api/provider.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ type Options struct {
5656
// NewCluster is a function that creates a new cluster from a rest.Config.
5757
// The cluster will be started by the provider.
5858
NewCluster func(ctx context.Context, clp *clusterinventoryv1alpha1.ClusterProfile, cfg *rest.Config, opts ...cluster.Option) (cluster.Cluster, error)
59+
60+
// IsReady is a function that determines if a cluster is ready.
61+
// If not provided, a default readiness check is used which checks for the
62+
// ControlPlaneHealthy condition on the ClusterProfile.
63+
IsReady func(ctx context.Context, clp *clusterinventoryv1alpha1.ClusterProfile) bool
5964
}
6065

6166
type index struct {
@@ -86,6 +91,12 @@ func setDefaults(opts *Options) {
8691
return cluster.New(cfg, opts...)
8792
}
8893
}
94+
if opts.IsReady == nil {
95+
opts.IsReady = func(ctx context.Context, clp *clusterinventoryv1alpha1.ClusterProfile) bool {
96+
controlPlaneHealthyCondition := meta.FindStatusCondition(clp.Status.Conditions, clusterinventoryv1alpha1.ClusterConditionControlPlaneHealthy)
97+
return controlPlaneHealthyCondition != nil && controlPlaneHealthyCondition.Status == metav1.ConditionTrue
98+
}
99+
}
89100
}
90101

91102
// New creates a new Cluster Inventory API cluster Provider.
@@ -194,8 +205,7 @@ func (p *Provider) Reconcile(ctx context.Context, req reconcile.Request) (reconc
194205
}
195206

196207
// ready?
197-
controlPlaneHealthyCondition := meta.FindStatusCondition(clp.Status.Conditions, clusterinventoryv1alpha1.ClusterConditionControlPlaneHealthy)
198-
if controlPlaneHealthyCondition == nil || controlPlaneHealthyCondition.Status != metav1.ConditionTrue {
208+
if !p.opts.IsReady(ctx, clp) {
199209
log.Info("ClusterProfile is not healthy yet, requeuing")
200210
return reconcile.Result{RequeueAfter: time.Second * 10}, nil
201211
}

providers/cluster-inventory-api/provider_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,76 @@ var _ = Describe("Provider Cluster Inventory API", Ordered, func() {
471471
shutDownClusters()
472472
})
473473
})
474+
475+
Context("Custom readiness check", Ordered, func() {
476+
const consumerName = "hub"
477+
var sa1TokenMember string
478+
479+
BeforeAll(func() {
480+
ctx, cancel = context.WithCancel(context.Background())
481+
g, _ = errgroup.WithContext(ctx)
482+
483+
createClusters()
484+
485+
By("Setting up the Provider", func() {
486+
var err error
487+
provider, err = New(Options{
488+
KubeconfigStrategyOption: kubeconfigstrategy.Option{
489+
Secret: &kubeconfigstrategy.SecretStrategyOption{
490+
ConsumerName: consumerName,
491+
},
492+
},
493+
IsReady: func(ctx context.Context, clp *clusterinventoryv1alpha1.ClusterProfile) bool {
494+
return true
495+
},
496+
})
497+
Expect(err).NotTo(HaveOccurred())
498+
Expect(provider).NotTo(BeNil())
499+
})
500+
501+
setupAndStartControllers()
502+
503+
By("Setting up the ClusterProfile for member clusters", func() {
504+
profileMember = &clusterinventoryv1alpha1.ClusterProfile{
505+
ObjectMeta: metav1.ObjectMeta{
506+
Name: "member",
507+
Namespace: "default",
508+
},
509+
Spec: clusterinventoryv1alpha1.ClusterProfileSpec{
510+
DisplayName: "member",
511+
ClusterManager: clusterinventoryv1alpha1.ClusterManager{
512+
Name: "test",
513+
},
514+
},
515+
}
516+
Expect(cliHub.Create(ctx, profileMember)).To(Succeed())
517+
518+
sa1TokenMember = mustCreateAdminSAAndToken(ctx, cliMember, "sa1", "default")
519+
mustCreateOrUpdateKubeConfigSecretFromTokenSecret(
520+
ctx, cliHub, cfgMember,
521+
consumerName,
522+
*profileMember,
523+
sa1TokenMember,
524+
)
525+
})
526+
527+
createObjects()
528+
})
529+
530+
assertBasicControllerBehavior()
531+
assertClusterIndexBehavior()
532+
533+
AfterAll(func() {
534+
By("Stopping the provider, cluster, manager, and controller", func() {
535+
cancel()
536+
})
537+
By("Waiting for the error group to finish", func() {
538+
err := g.Wait()
539+
Expect(err).NotTo(HaveOccurred())
540+
})
541+
shutDownClusters()
542+
})
543+
})
474544
})
475545

476546
func ignoreCanceled(err error) error {

0 commit comments

Comments
 (0)