-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move split from cmd to konf pkg
- Loading branch information
1 parent
577414b
commit 6bbe5b1
Showing
5 changed files
with
310 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package konf | ||
|
||
import ( | ||
k8s "k8s.io/client-go/tools/clientcmd/api/v1" | ||
) | ||
|
||
type Config struct { | ||
Id KonfID | ||
Kubeconfig k8s.Config | ||
StorePath string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package konf | ||
|
||
import ( | ||
"io" | ||
|
||
k8s "k8s.io/client-go/tools/clientcmd/api/v1" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// KonfsFromKubeconfig takes in the content of a kubeconfig and splits it into | ||
// one or multiple konfs. | ||
// | ||
// No error is being returned if the kubeconfig contains no contexts, instead | ||
// konfs is simply an empty slice | ||
func KonfsFromKubeconfig(kubeconfig io.Reader) (konfs []*Config, err error) { | ||
konfs = []*Config{} | ||
|
||
b, err := io.ReadAll(kubeconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var origConf k8s.Config | ||
err = yaml.Unmarshal(b, &origConf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// basically should be as simple as | ||
// 1. Loop through all the contexts | ||
// 2. Find the corresponding cluster for each context | ||
// 3. Find the corresponding user for each context | ||
// 4. Create a new konfigFile for each context mapped to its cluster | ||
|
||
for _, curCon := range origConf.Contexts { | ||
|
||
cluster := k8s.NamedCluster{} | ||
for _, curCl := range origConf.Clusters { | ||
if curCl.Name == curCon.Context.Cluster { | ||
cluster = curCl | ||
break | ||
} | ||
} | ||
user := k8s.NamedAuthInfo{} | ||
for _, curU := range origConf.AuthInfos { | ||
if curU.Name == curCon.Context.AuthInfo { | ||
user = curU | ||
break | ||
} | ||
} | ||
|
||
var k Config | ||
id := IDFromClusterAndContext(cluster.Name, curCon.Name) | ||
// TODO need to remove this. StorePath should only be setable by store pkg later on | ||
k.StorePath = id.StorePath() | ||
k.Id = id | ||
k.Kubeconfig.AuthInfos = append(k.Kubeconfig.AuthInfos, user) | ||
k.Kubeconfig.Clusters = append(k.Kubeconfig.Clusters, cluster) | ||
k.Kubeconfig.Contexts = append(k.Kubeconfig.Contexts, curCon) | ||
|
||
k.Kubeconfig.APIVersion = origConf.APIVersion | ||
k.Kubeconfig.Kind = origConf.Kind | ||
k.Kubeconfig.CurrentContext = curCon.Name | ||
|
||
konfs = append(konfs, &k) | ||
} | ||
|
||
return konfs, nil | ||
} |
Oops, something went wrong.