Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit b2bad19

Browse files
committed
add APIServerEndpoint validation for JoinConfiguration
1 parent 1699fe8 commit b2bad19

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
8585
Complete(r)
8686
}
8787

88-
// Reconcile TODO
88+
// Reconcile handles KubeadmConfig events
8989
func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr error) {
9090
ctx := context.Background()
9191
log := r.Log.WithValues("kubeadmconfig", req.NamespacedName)
@@ -380,7 +380,7 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o handler.MapObject) [
380380
return result
381381
}
382382

383-
// reconcileDiscovery ensure that config.JoinConfiguration.Discovery is properly set for the joining node.
383+
// reconcileDiscovery ensures that config.JoinConfiguration.Discovery is properly set for the joining node.
384384
// The implementation func respect user provided discovery configurations, but in case some of them are missing, a valid BootstrapToken object
385385
// is automatically injected into config.JoinConfiguration.Discovery.
386386
// This allows to simplify configuration UX, by providing the option to delegate to CABPK the configuration of kubeadm join discovery.
@@ -398,8 +398,8 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
398398
}
399399

400400
// if BootstrapToken already contains an APIServerEndpoint, respect it; otherwise inject the APIServerEndpoint endpoint defined in cluster status
401-
//TODO(fp) might be we want to validate user provided APIServerEndpoint and warn/error if it doesn't match the api endpoint defined at cluster level
402401
apiServerEndpoint := config.Spec.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint
402+
needValidateAPIEndpoints := true
403403
if apiServerEndpoint == "" {
404404
if len(cluster.Status.APIEndpoints) == 0 {
405405
return errors.Wrap(&capierrors.RequeueAfterError{RequeueAfter: 10 * time.Second}, "Waiting for Cluster Controller to set cluster.Status.APIEndpoints")
@@ -409,6 +409,20 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
409409
apiServerEndpoint = fmt.Sprintf("%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port)
410410
config.Spec.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint = apiServerEndpoint
411411
log.Info("Altering JoinConfiguration.Discovery.BootstrapToken", "APIServerEndpoint", apiServerEndpoint)
412+
needValidateAPIEndpoints = false
413+
}
414+
// validate user provided APIServerEndpoint matches the api endpoint defined at cluster level
415+
if needValidateAPIEndpoints {
416+
found := false
417+
for _, APIEndpoint := range cluster.Status.APIEndpoints {
418+
if apiServerEndpoint == fmt.Sprintf("%s:%d", APIEndpoint.Host, APIEndpoint.Port) {
419+
found = true
420+
break
421+
}
422+
}
423+
if !found {
424+
return errors.Errorf("the provided APIServerEndpoint %v does not match any api endpoint defined at cluster %v", apiServerEndpoint, cluster.Status.APIEndpoints)
425+
}
412426
}
413427

414428
// if BootstrapToken already contains a token, respect it; otherwise create a new bootstrap token for the node to join

0 commit comments

Comments
 (0)