cli cmd for merging one or more kubeconfig files in to ~/.kube/config

main
Jason Fowler 6 months ago committed by Jason Fowler
commit 6a6b9bbed8

@ -0,0 +1,9 @@
.PHONY: build install
build:
@go build -o ckc ./...
@echo "✅ ckc built"
install:
@sudo mv ./ckc /usr/local/bin/ckc
@echo "✅ ckc installed"

@ -0,0 +1,10 @@
# combine-kube-config - ckc
## Build and Install
`make && make install`
## Usage
`ckc <file1>...<fileN>`
`ckc ~/Downloads/staging ~/Downloads/loadtest`
will add both staging and loadtest to the ~/.kube/config file

@ -0,0 +1,5 @@
module ckc
go 1.19
require gopkg.in/yaml.v2 v2.4.0

@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

@ -0,0 +1,171 @@
package main
import (
"fmt"
"os"
"path"
"gopkg.in/yaml.v2"
)
type kubeConfig struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
CurrentContext string `yaml:"current-context"`
Clusters []cluster `yaml:"clusters"`
Users []user `yaml:"users"`
Contexts []context `yaml:"contexts"`
Preferences preferences `yaml:"Preferences,omitempty"`
}
type cluster struct {
Name string `yaml:"name"`
Cluster clusterDef `yaml:"cluster"`
}
type clusterDef struct {
Server string `yaml:"server"`
ProxyURL string `yaml:"proxy-url,omitempty"`
CertificateAuthorityData string `yaml:"certificate-authority-data"`
InsecureSkipTLSVerify bool `yaml:"insecure-skip-tls-verify"`
}
type user struct {
Name string `yaml:"name"`
User userDef `yaml:"user"`
}
type userDef struct {
ClientCertificateData string `yaml:"client-certificate-data"`
ClientKeyData string `yaml:"client-key-data"`
}
type context struct {
Name string `yaml:"name"`
Context contextDef `yaml:"context"`
}
type contextDef struct {
Cluster string `yaml:"cluster"`
User string `yaml:"user"`
Name string `yaml:"name,omitempty"`
}
type preferences struct {
Colors bool `yaml:"colors,omitempty"`
}
var args []string
func init() {
if len(os.Args) == 1 {
fmt.Println("No source files")
os.Exit(1)
}
args = os.Args[1:]
}
func main() {
var err error
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
home, err = os.Getwd()
if err != nil {
panic(err)
}
}
kubeDir := ".kube"
kubeConfigFile := path.Join(home, kubeDir, "config")
contents, err := os.ReadFile(kubeConfigFile)
if err != nil {
panic(err)
}
var config kubeConfig
err = yaml.Unmarshal(contents, &config)
if err != nil {
panic(err)
}
var newConf kubeConfig
for i := range args {
contents, err = os.ReadFile(args[i])
if err != nil {
fmt.Printf("⛔ %s\n", err)
continue
}
err = yaml.Unmarshal(contents, &newConf)
if err != nil {
fmt.Printf("⛔ %s\n", err)
continue
}
for i := range newConf.Clusters {
found := false
for j := range config.Clusters {
if newConf.Clusters[i].Name == config.Clusters[j].Name {
fmt.Printf("⚠️ cluster %s from %s already exists\n", config.Clusters[j].Name, args[i])
found = true
break
}
}
if !found {
config.Clusters = append(config.Clusters, newConf.Clusters[i])
}
}
for i := range newConf.Users {
found := false
for j := range config.Users {
if newConf.Users[i].Name == config.Users[j].Name {
found = true
fmt.Printf("⚠️ user %s from %s already exists\n", config.Users[j].Name, args[i])
break
}
}
if !found {
config.Users = append(config.Users, newConf.Users[i])
}
}
for i := range newConf.Contexts {
found := false
for j := range config.Contexts {
if newConf.Contexts[i].Name == config.Contexts[j].Name {
found = true
fmt.Printf("⚠️ context %s from %s already exists\n", config.Contexts[j].Name, args[i])
break
}
}
if !found {
config.Contexts = append(config.Contexts, newConf.Contexts[i])
}
}
if newConf.CurrentContext != "" && config.CurrentContext != newConf.CurrentContext {
config.CurrentContext = newConf.CurrentContext
}
if config.Preferences.Colors != newConf.Preferences.Colors {
config.Preferences.Colors = newConf.Preferences.Colors
}
config.APIVersion = newConf.APIVersion
config.Kind = newConf.Kind
}
result, err := yaml.Marshal(config)
if err != nil {
fmt.Printf("⛔ %s\n", err)
os.Exit(1)
}
err = os.WriteFile(kubeConfigFile, result, 0644)
if err != nil {
fmt.Printf("⛔ %s\n", err)
os.Exit(1)
}
fmt.Printf("✅ %s updated\n", kubeConfigFile)
}
Loading…
Cancel
Save