Skip to content

Commit

Permalink
Reverting changes
Browse files Browse the repository at this point in the history
Signed-off-by: PrimalPimmy <Prashant20.pm@gmail.com>
  • Loading branch information
PrimalPimmy committed Jul 24, 2024
1 parent 27b5ad6 commit 00b4c1f
Showing 1 changed file with 20 additions and 59 deletions.
79 changes: 20 additions & 59 deletions controllers/pkg/reconcilers/spire-bootstrap/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package spirebootstrap
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
Expand All @@ -39,14 +38,12 @@ import (
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/yaml"
)

func init() {
Expand Down Expand Up @@ -371,61 +368,25 @@ func (r *reconciler) createKubeconfigConfigMap(ctx context.Context, clientset *k
}
caCert := configMap.Data["ca.crt"]

// Create kubeconfig content
// Create the clientcmdapi.Config struct
config := clientcmdapi.NewConfig()

// Add cluster
cluster := clientcmdapi.NewCluster()
cluster.CertificateAuthorityData = []byte(base64.StdEncoding.EncodeToString([]byte(caCert)))
cluster.Server = clientset.RESTClient().Get().URL().String()
config.Clusters[clustername] = cluster

// Add context
context := clientcmdapi.NewContext()
context.Cluster = clustername
context.Namespace = "spire"
context.AuthInfo = "spire-kubeconfig"
config.Contexts["spire-kubeconfig@"+clustername] = context

// Set current context
config.CurrentContext = "spire-kubeconfig@" + clustername

// Add user
authInfo := clientcmdapi.NewAuthInfo()
authInfo.Token = token
config.AuthInfos["spire-kubeconfig"] = authInfo

// Marshal to YAML
configJsonData, err := json.Marshal(config)
if err != nil {
return nil, fmt.Errorf("error marshaling config to JSON: %v", err)
}
fmt.Println("JSONNN", configJsonData)

yamlKubeconfig, err := yaml.JSONToYAML(configJsonData)
if err != nil {
return nil, fmt.Errorf("error marshaling config to YAML: %v", err)
}
// kubeconfig := strings.TrimSpace(fmt.Sprintf(`
// apiVersion: v1
// kind: Config
// clusters:
// - cluster:
// certificate-authority-data: %s
// server: %s
// name: regional
// contexts:
// - context:
// cluster: %s
// namespace: spire
// user: spire-kubeconfig
// current-context: spire-kubeconfig@regional
// users:
// - name: spire-kubeconfig
// user:
// token: %s
// `, base64.StdEncoding.EncodeToString([]byte(caCert)), clientset.RESTClient().Get().URL().String(), clustername, token))
kubeconfig := fmt.Sprintf(`
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: %s
server: %s
name: regional
contexts:
- context:
cluster: %s
namespace: spire
user: spire-kubeconfig
current-context: spire-kubeconfig@regional
users:
- name: spire-kubeconfig
user:
token: %s
`, base64.StdEncoding.EncodeToString([]byte(caCert)), clientset.RESTClient().Get().URL().String(), clustername, token)

// Generate a unique key for the new kubeconfig
newConfigKey := fmt.Sprintf("kubeconfig-%s", clustername)
Expand All @@ -434,7 +395,7 @@ func (r *reconciler) createKubeconfigConfigMap(ctx context.Context, clientset *k
if restrictedKC.Data == nil {
restrictedKC.Data = make(map[string]string)
}
restrictedKC.Data[newConfigKey] = string(yamlKubeconfig)
restrictedKC.Data[newConfigKey] = string(kubeconfig)

// _, err = clientset.CoreV1().ConfigMaps("spire").Create(context.TODO(), kubeconfigCM, metav1.CreateOptions{})
err = r.Update(ctx, restrictedKC)
Expand Down

0 comments on commit 00b4c1f

Please sign in to comment.