Skip to content

Commit

Permalink
fix: fix kubeconfig functions
Browse files Browse the repository at this point in the history
Co-authored-by: Guilhem Lettron <[email protected]>
  • Loading branch information
aamoyel and guilhem committed Nov 8, 2024
1 parent cc5ed02 commit b8e4ae2
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions pkg/store/kubeconfig_store_capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func (s *CapiStore) getCapiClient() (client.Client, error) {
&clientcmd.ConfigOverrides{})

restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("unable to create rest config: %v", err)
}

k8sclient, err := client.New(restConfig, client.Options{
Scheme: scheme,
Expand All @@ -111,6 +114,15 @@ func (s *CapiStore) StartSearch(channel chan SearchResult) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

// initialize CAPI client
if err := s.InitializeCapiStore(); err != nil {
channel <- SearchResult{
KubeconfigPath: "",
Error: err,
}
return
}

// list clusters
clusters := &clusterv1beta1.ClusterList{}
err := s.Client.List(ctx, clusters)
Expand All @@ -123,24 +135,33 @@ func (s *CapiStore) StartSearch(channel chan SearchResult) {
}

for _, cluster := range clusters.Items {
s.Logger.Debug("CAPI: found cluster", "name", cluster.Name, "namespace", cluster.Namespace)

channel <- SearchResult{
KubeconfigPath: fmt.Sprintf("%s/%s", cluster.Namespace, cluster.Name),
KubeconfigPath: fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name),
Error: nil,
Tags: map[string]string{
"namespace": cluster.Namespace,
"name": cluster.Name,
},
}
}
}

// GetKubeconfigForPath returns the kubeconfig for the path
func (s *CapiStore) GetKubeconfigForPath(path string, _ map[string]string) ([]byte, error) {
func (s *CapiStore) GetKubeconfigForPath(path string, tags map[string]string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

s.Logger.Debug("CAPI: GetKubeconfigForPath", "path", path)

obj := client.ObjectKey{
Namespace: path,
Name: path,
Namespace: tags["namespace"],
Name: tags["name"],
}
dataBytes, err := utilkubeconfig.FromSecret(ctx, s.Client, obj)
if err != nil {
s.Logger.Debug("CAPI: GetKubeconfigForPath", "error", err)
return nil, err
}
return dataBytes, nil
Expand Down

0 comments on commit b8e4ae2

Please sign in to comment.