You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.8 KiB
Go

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)
}