Skip to content

Commit 7bd9120

Browse files
authored
Merge pull request kubesphere#4734 from iawia002/validate-cluster
Check if the cluster is the same when updating kubeconfig
2 parents 5af3f14 + 146cff3 commit 7bd9120

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pkg/kapis/cluster/v1alpha1/handler.go

+14
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ func (h *handler) updateKubeConfig(request *restful.Request, response *restful.R
311311
api.HandleBadRequest(response, request, fmt.Errorf("failed to validate member cluster configuration, err: %v", err))
312312
}
313313

314+
// Check if the cluster is the same
315+
kubeSystem, err := clientSet.CoreV1().Namespaces().Get(context.TODO(), metav1.NamespaceSystem, metav1.GetOptions{})
316+
if err != nil {
317+
api.HandleBadRequest(response, request, err)
318+
return
319+
}
320+
if kubeSystem.UID != cluster.Status.UID {
321+
api.HandleBadRequest(
322+
response, request, fmt.Errorf(
323+
"this kubeconfig corresponds to a different cluster than the previous one, you need to make sure that kubeconfig is not from another cluster",
324+
))
325+
return
326+
}
327+
314328
cluster.Spec.Connection.KubeConfig = req.KubeConfig
315329
if _, err = h.ksclient.ClusterV1alpha1().Clusters().Update(context.TODO(), cluster, metav1.UpdateOptions{}); err != nil {
316330
api.HandleBadRequest(response, request, err)

0 commit comments

Comments
 (0)