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

Commit 1041da9

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

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 20 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,7 +398,6 @@ 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
403402
if apiServerEndpoint == "" {
404403
if len(cluster.Status.APIEndpoints) == 0 {
@@ -410,6 +409,10 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
410409
config.Spec.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint = apiServerEndpoint
411410
log.Info("Altering JoinConfiguration.Discovery.BootstrapToken", "APIServerEndpoint", apiServerEndpoint)
412411
}
412+
// validate user provided APIServerEndpoint matches the api endpoint defined at cluster level
413+
if err := validateAPIEndpoints(apiServerEndpoint, cluster.Status.APIEndpoints); err != nil {
414+
log.Error(err, "found invalid APIServerEndpoint configuration")
415+
}
413416

414417
// if BootstrapToken already contains a token, respect it; otherwise create a new bootstrap token for the node to join
415418
if config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token == "" {
@@ -562,3 +565,17 @@ func (r *KubeadmConfigReconciler) createClusterCertificates(ctx context.Context,
562565

563566
return certificates, nil
564567
}
568+
569+
func validateAPIEndpoints(endpoint string, endpoints []clusterv1.APIEndpoint) error {
570+
found := false
571+
for _, v := range endpoints {
572+
if endpoint == fmt.Sprintf("%s:%d", v.Host, v.Port) {
573+
found = true
574+
break
575+
}
576+
}
577+
if !found {
578+
return errors.Errorf("provided APIServerEndpoint %v does not match any api endpoint defined at cluster %v", endpoint, endpoints)
579+
}
580+
return nil
581+
}

0 commit comments

Comments
 (0)