Skip to content

Commit 0f8a382

Browse files
committed
Allow configuring the backoff time.
Round out the test coverage somewhat.
1 parent 1ced6d4 commit 0f8a382

6 files changed

+235
-23
lines changed

api/v1alpha1/clusterbootstrapconfig_types.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
)
2525

26-
const defaultWaitDuration = time.Duration(0)
26+
const defaultWaitDuration = time.Second * 60
2727

2828
const BootstrappedAnnotation = "capi.weave.works/bootstrapped"
2929

@@ -43,9 +43,17 @@ type JobTemplate struct {
4343

4444
// ClusterBootstrapConfigSpec defines the desired state of ClusterBootstrapConfig
4545
type ClusterBootstrapConfigSpec struct {
46-
ClusterSelector metav1.LabelSelector `json:"clusterSelector"`
47-
Template JobTemplate `json:"jobTemplate"`
48-
RequireClusterReady bool `json:"requireClusterReady"`
46+
ClusterSelector metav1.LabelSelector `json:"clusterSelector"`
47+
Template JobTemplate `json:"jobTemplate"`
48+
49+
// Wait for the remote cluster to be "ready" before creating the jobs.
50+
// Defaults to false.
51+
//+kubebuilder:default:false
52+
RequireClusterReady bool `json:"requireClusterReady"`
53+
// When checking for readiness, this is the time to wait before
54+
// checking again.
55+
//+kubebuilder:default:60s
56+
ClusterReadinessBackoff *metav1.Duration `json:"clusterReadinessBackoff,omitempty"`
4957
}
5058

5159
// ClusterBootstrapConfigStatus defines the observed state of ClusterBootstrapConfig
@@ -64,6 +72,15 @@ type ClusterBootstrapConfig struct {
6472
Status ClusterBootstrapConfigStatus `json:"status,omitempty"`
6573
}
6674

75+
// ClusterReadinessRequeue returns the configured ClusterReadinessBackoff or a default
76+
// value if not configured.
77+
func (c ClusterBootstrapConfig) ClusterReadinessRequeue() time.Duration {
78+
if v := c.Spec.ClusterReadinessBackoff; v != nil {
79+
return v.Duration
80+
}
81+
return defaultWaitDuration
82+
}
83+
6784
//+kubebuilder:object:root=true
6885

6986
// ClusterBootstrapConfigList contains a list of ClusterBootstrapConfig
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
func TestControl(t *testing.T) {
27+
cfg := ClusterBootstrapConfig{}
28+
29+
if v := cfg.ClusterReadinessRequeue(); v != defaultWaitDuration {
30+
t.Fatalf("ClusterReadinessRequeue() got %v, want %v", v, defaultWaitDuration)
31+
}
32+
33+
want := time.Second * 20
34+
cfg.Spec.ClusterReadinessBackoff = &metav1.Duration{Duration: want}
35+
if v := cfg.ClusterReadinessRequeue(); v != want {
36+
t.Fatalf("ClusterReadinessRequeue() got %v, want %v", v, want)
37+
}
38+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/capi.weave.works_clusterbootstrapconfigs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ spec:
3737
spec:
3838
description: ClusterBootstrapConfigSpec defines the desired state of ClusterBootstrapConfig
3939
properties:
40+
clusterReadinessBackoff:
41+
description: When checking for readiness, this is the time to wait
42+
before checking again.
43+
type: string
4044
clusterSelector:
4145
description: A label selector is a label query over a set of resources.
4246
The result of matchLabels and matchExpressions are ANDed. An empty
@@ -6890,6 +6894,8 @@ spec:
68906894
- spec
68916895
type: object
68926896
requireClusterReady:
6897+
description: Wait for the remote cluster to be "ready" before creating
6898+
the jobs. Defaults to false.
68936899
type: boolean
68946900
required:
68956901
- clusterSelector

controllers/clusterbootstrapconfig_controller.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"time"
2423

2524
corev1 "k8s.io/api/core/v1"
2625
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -47,17 +46,16 @@ type ClusterBootstrapConfigReconciler struct {
4746
configParser func(b []byte) (client.Client, error)
4847
}
4948

49+
// NewClusterBootstrapConfigReconcielr creates and returns a configured
50+
// reconciler ready for use.
5051
func NewClusterBootstrapConfigReconciler(c client.Client, s *runtime.Scheme) *ClusterBootstrapConfigReconciler {
5152
return &ClusterBootstrapConfigReconciler{
5253
Client: c,
5354
Scheme: s,
54-
configParser: bytesToKubeConfig,
55+
configParser: kubeConfigBytesToClient,
5556
}
5657
}
5758

58-
// TODO: make this configurable on the Spec
59-
var requeueAfterTime = time.Second * 32
60-
6159
//+kubebuilder:rbac:groups=capi.weave.works,resources=clusterbootstrapconfigs,verbs=get;list;watch;create;update;patch;delete
6260
//+kubebuilder:rbac:groups=capi.weave.works,resources=clusterbootstrapconfigs/status,verbs=get;update;patch
6361
//+kubebuilder:rbac:groups=capi.weave.works,resources=clusterbootstrapconfigs/finalizers,verbs=update
@@ -91,7 +89,7 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
9189
if err != nil {
9290
if apierrors.IsNotFound(err) {
9391
logger.Info("waiting for cluster access secret to be available")
94-
return ctrl.Result{RequeueAfter: requeueAfterTime}, nil
92+
return ctrl.Result{RequeueAfter: clusterBootstrapConfig.ClusterReadinessRequeue()}, nil
9593
}
9694

9795
return ctrl.Result{}, fmt.Errorf("failed to create client for cluster %s: %w", clusterName, err)
@@ -104,7 +102,7 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
104102
if !ready {
105103
logger.Info("waiting for control plane to be ready", "cluster", clusterName)
106104

107-
return ctrl.Result{RequeueAfter: requeueAfterTime}, nil
105+
return ctrl.Result{RequeueAfter: clusterBootstrapConfig.ClusterReadinessRequeue()}, nil
108106
}
109107
}
110108
if err := bootstrapClusterWithConfig(ctx, logger, r.Client, c, &clusterBootstrapConfig); err != nil {
@@ -249,7 +247,7 @@ func (r *ClusterBootstrapConfigReconciler) getKubeConfig(ctx context.Context, cl
249247
return kubeConfig, nil
250248
}
251249

252-
func bytesToKubeConfig(b []byte) (client.Client, error) {
250+
func kubeConfigBytesToClient(b []byte) (client.Client, error) {
253251
restConfig, err := clientcmd.RESTConfigFromKubeConfig(b)
254252
if err != nil {
255253
return nil, fmt.Errorf("failed to parse KubeConfig from secret: %w", err)

0 commit comments

Comments
 (0)