@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"fmt"
23
23
24
+ "github.com/fluxcd/pkg/runtime/conditions"
24
25
gitopsv1alpha1 "github.com/weaveworks/cluster-controller/api/v1alpha1"
25
26
corev1 "k8s.io/api/core/v1"
26
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -62,6 +63,7 @@ func NewClusterBootstrapConfigReconciler(c client.Client, s *runtime.Scheme) *Cl
62
63
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
63
64
//+kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters,verbs=get;list;watch;update;patch
64
65
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
66
+ // +kubebuilder:rbac:groups="gitops.weave.works",resources=gitopsclusters,verbs=get;watch;list;patch
65
67
66
68
// Reconcile is part of the main kubernetes reconciliation loop which aims to
67
69
// move the current state of the cluster closer to the desired state.
@@ -76,15 +78,15 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
76
78
}
77
79
logger .Info ("cluster bootstrap config loaded" , "name" , clusterBootstrapConfig .ObjectMeta .Name )
78
80
79
- clusters , err := r .getClustersBySelector (ctx , req .Namespace , clusterBootstrapConfig .Spec . ClusterSelector )
81
+ clusters , err := r .getClustersBySelector (ctx , req .Namespace , clusterBootstrapConfig .Spec )
80
82
if err != nil {
81
83
return ctrl.Result {}, fmt .Errorf ("failed to getClustersBySelector for bootstrap config %s: %w" , req , err )
82
84
}
83
85
logger .Info ("identified clusters for reconciliation" , "clusterCount" , len (clusters ))
84
86
85
- for _ , c := range clusters {
87
+ for _ , cluster := range clusters {
86
88
if clusterBootstrapConfig .Spec .RequireClusterReady {
87
- clusterName := types.NamespacedName {Name : c .GetName (), Namespace : c .GetNamespace ()}
89
+ clusterName := types.NamespacedName {Name : cluster .GetName (), Namespace : cluster .GetNamespace ()}
88
90
clusterClient , err := r .clientForCluster (ctx , clusterName )
89
91
if err != nil {
90
92
if apierrors .IsNotFound (err ) {
@@ -105,7 +107,7 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
105
107
return ctrl.Result {RequeueAfter : clusterBootstrapConfig .ClusterReadinessRequeue ()}, nil
106
108
}
107
109
}
108
- if err := bootstrapClusterWithConfig (ctx , logger , r .Client , c , & clusterBootstrapConfig ); err != nil {
110
+ if err := bootstrapClusterWithConfig (ctx , logger , r .Client , cluster , & clusterBootstrapConfig ); err != nil {
109
111
return ctrl.Result {}, fmt .Errorf ("failed to bootstrap cluster config: %w" , err )
110
112
}
111
113
@@ -119,8 +121,8 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
119
121
if err != nil {
120
122
return ctrl.Result {}, fmt .Errorf ("failed to create a patch to update the cluster annotations: %w" , err )
121
123
}
122
- if err := r .Client .Patch (ctx , c , client .RawPatch (types .MergePatchType , mergePatch )); err != nil {
123
- return ctrl.Result {}, fmt .Errorf ("failed to annotate cluster %s/%s as bootstrapped: %w" , c .ObjectMeta .Name , c .ObjectMeta .Namespace , err )
124
+ if err := r .Client .Patch (ctx , cluster , client .RawPatch (types .MergePatchType , mergePatch )); err != nil {
125
+ return ctrl.Result {}, fmt .Errorf ("failed to annotate cluster %s/%s as bootstrapped: %w" , cluster .ObjectMeta .Name , cluster .ObjectMeta .Namespace , err )
124
126
}
125
127
}
126
128
return ctrl.Result {}, nil
@@ -137,9 +139,9 @@ func (r *ClusterBootstrapConfigReconciler) SetupWithManager(mgr ctrl.Manager) er
137
139
Complete (r )
138
140
}
139
141
140
- func (r * ClusterBootstrapConfigReconciler ) getClustersBySelector (ctx context.Context , ns string , ls metav1. LabelSelector ) ([]* gitopsv1alpha1.GitopsCluster , error ) {
142
+ func (r * ClusterBootstrapConfigReconciler ) getClustersBySelector (ctx context.Context , ns string , spec capiv1alpha1. ClusterBootstrapConfigSpec ) ([]* gitopsv1alpha1.GitopsCluster , error ) {
141
143
logger := ctrl .LoggerFrom (ctx )
142
- selector , err := metav1 .LabelSelectorAsSelector (& ls )
144
+ selector , err := metav1 .LabelSelectorAsSelector (& spec . ClusterSelector )
143
145
if err != nil {
144
146
return nil , fmt .Errorf ("unable to convert selector: %w" , err )
145
147
}
@@ -156,23 +158,24 @@ func (r *ClusterBootstrapConfigReconciler) getClustersBySelector(ctx context.Con
156
158
logger .Info ("identified clusters with selector" , "selector" , selector , "count" , len (clusterList .Items ))
157
159
clusters := []* gitopsv1alpha1.GitopsCluster {}
158
160
for i := range clusterList .Items {
159
- c := & clusterList .Items [i ]
161
+ cluster := & clusterList .Items [i ]
160
162
161
- clusterFound := false
162
- for _ , condition := range c .Status .Conditions {
163
- if condition .Type == "Ready" && condition .Status == metav1 .ConditionTrue {
164
- clusterFound = true
165
- }
166
- }
167
- if ! clusterFound {
168
- logger .Info ("cluster discarded - not provisioned" , "phase" , c .Status )
163
+ if ! conditions .IsReady (cluster ) && ! spec .RequireClusterProvisioned {
164
+ logger .Info ("cluster discarded - not ready" , "phase" , cluster .Status )
169
165
continue
170
166
}
171
- if metav1 .HasAnnotation (c .ObjectMeta , capiv1alpha1 .BootstrappedAnnotation ) {
167
+ if spec .RequireClusterProvisioned {
168
+ if ! isProvisioned (cluster ) {
169
+ logger .Info ("waiting for cluster to be provisioned" , "cluster" , cluster .Name )
170
+ continue
171
+ }
172
+ }
173
+
174
+ if metav1 .HasAnnotation (cluster .ObjectMeta , capiv1alpha1 .BootstrappedAnnotation ) {
172
175
continue
173
176
}
174
- if c .DeletionTimestamp .IsZero () {
175
- clusters = append (clusters , c )
177
+ if cluster .DeletionTimestamp .IsZero () {
178
+ clusters = append (clusters , cluster )
176
179
}
177
180
}
178
181
return clusters , nil
@@ -270,3 +273,7 @@ func kubeConfigBytesToClient(b []byte) (client.Client, error) {
270
273
}
271
274
return client , nil
272
275
}
276
+
277
+ func isProvisioned (from conditions.Getter ) bool {
278
+ return conditions .IsTrue (from , gitopsv1alpha1 .ClusterProvisionedCondition )
279
+ }
0 commit comments