1package main 2 3import ( 4 "encoding/base64" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "time" 9 10 yaml "gopkg.in/yaml.v2" 11 12 "k8s.io/client-go/kubernetes" 13 "k8s.io/client-go/rest" 14) 15 16type BootstrapConfig struct { 17 Name string 18 Server string 19 CA string // CA is expected to be base64 encoded PEM file 20} 21 22var bootstrapConfigs []BootstrapConfig 23 24func CreateBootstrapClients() ([]kubernetes.Interface, error) { 25 var bootstrapClients []kubernetes.Interface 26 27 bootstrapYaml, err := ioutil.ReadFile("./bootstrap.yaml") 28 if err != nil { 29 panic(err) 30 } 31 32 err = yaml.Unmarshal(bootstrapYaml, &bootstrapConfigs) 33 if err != nil { 34 panic(err) 35 } 36 37 for _, cluster := range bootstrapConfigs { 38 // CA is base64 encoded, so we decode 39 caReader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(cluster.CA)) 40 caBytes, _ := ioutil.ReadAll(caReader) 41 42 var restConfig *rest.Config 43 restConfig = &rest.Config{ 44 Host: cluster.Server, 45 TLSClientConfig: rest.TLSClientConfig{ 46 CAData: caBytes, 47 }, 48 } 49 50 previousWrappedTransport := restConfig.WrapTransport 51 restConfig.WrapTransport = func(rt http.RoundTripper) http.RoundTripper { 52 if previousWrappedTransport != nil { 53 rt = previousWrappedTransport(rt) 54 } 55 return &TokenRoundtripper{ 56 TokenProvider: NewHeptioProvider(cluster.Name), 57 RoundTripper: rt, 58 } 59 } 60 61 restConfig.Timeout = time.Second * 5 62 63 client, err := kubernetes.NewForConfig(restConfig) 64 if err != nil { 65 return nil, err 66 } 67 bootstrapClients = append(bootstrapClients, client) 68 } 69 return bootstrapClients, nil 70} 71