# Activer le proxy
Start-Process kubectl -ArgumentList "proxy" -WindowStyle Hidden

# Créer le ServiceAccount
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard --force

# Lui donner les droits admin
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin --force

# Générer le jeton
$Token = kubectl create token dashboard-admin -n kubernetes-dashboard

# Informations du cluster
$ClusterName = kubectl config current-context
$Server = kubectl config view -o jsonpath="{.clusters[?(@.name==`"$ClusterName`")].cluster.server}"
$CACert = kubectl config view -o jsonpath="{.clusters[?(@.name==`"$ClusterName`")].cluster.certificate-authority-data}"

# Générer le kubeconfig avec token uniquement (pas de certificats)
$Kubeconfig = @"
apiVersion: v1
kind: Config
clusters:
- name: $ClusterName
  cluster:
    server: $Server
    certificate-authority-data: $CACert
contexts:
- name: $ClusterName
  context:
    cluster: $ClusterName
    user: dashboard-admin
current-context: $ClusterName
users:
- name: dashboard-admin
  user:
    token: $Token
"@

# Sauvegarder
$Kubeconfig | Out-File -FilePath "C:\DevOpsProject\scripts\dashboard.kubeconfig" -Encoding UTF8

Write-Host "✅ kubeconfig généré : C:\DevOpsProject\scripts\dashboard.kubeconfig"
Write-Host "Accédez au dashboard : http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/"
Write-Host "→ Choisissez 'Kubeconfig' et sélectionnez le fichier."   