github.com/openshift/installer@v1.4.17/pkg/asset/kubeconfig/kubeconfig.go (about) 1 package kubeconfig 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/pkg/errors" 8 clientcmd "k8s.io/client-go/tools/clientcmd/api/v1" 9 "sigs.k8s.io/yaml" 10 11 "github.com/openshift/installer/pkg/asset" 12 "github.com/openshift/installer/pkg/asset/tls" 13 "github.com/openshift/installer/pkg/types" 14 ) 15 16 type kubeconfig struct { 17 Config *clientcmd.Config 18 File *asset.File 19 } 20 21 // generate generates the kubeconfig. 22 func (k *kubeconfig) generate( 23 ca tls.CertInterface, 24 clientCertKey tls.CertKeyInterface, 25 apiURL string, 26 cluster string, 27 userName string, 28 kubeconfigPath string, 29 ) error { 30 k.Config = &clientcmd.Config{ 31 Clusters: []clientcmd.NamedCluster{ 32 { 33 Name: cluster, 34 Cluster: clientcmd.Cluster{ 35 Server: apiURL, 36 CertificateAuthorityData: ca.Cert(), 37 }, 38 }, 39 }, 40 AuthInfos: []clientcmd.NamedAuthInfo{ 41 { 42 Name: userName, 43 AuthInfo: clientcmd.AuthInfo{ 44 ClientCertificateData: clientCertKey.Cert(), 45 ClientKeyData: clientCertKey.Key(), 46 }, 47 }, 48 }, 49 Contexts: []clientcmd.NamedContext{ 50 { 51 Name: userName, 52 Context: clientcmd.Context{ 53 Cluster: cluster, 54 AuthInfo: userName, 55 }, 56 }, 57 }, 58 CurrentContext: userName, 59 } 60 61 data, err := yaml.Marshal(k.Config) 62 if err != nil { 63 return errors.Wrap(err, "failed to Marshal kubeconfig") 64 } 65 66 k.File = &asset.File{ 67 Filename: kubeconfigPath, 68 Data: data, 69 } 70 71 return nil 72 } 73 74 // Files returns the files generated by the asset. 75 func (k *kubeconfig) Files() []*asset.File { 76 if k.File != nil { 77 return []*asset.File{k.File} 78 } 79 return []*asset.File{} 80 } 81 82 // load returns the kubeconfig from disk. 83 func (k *kubeconfig) load(f asset.FileFetcher, name string) (found bool, err error) { 84 file, err := f.FetchByName(name) 85 if err != nil { 86 if os.IsNotExist(err) { 87 return false, nil 88 } 89 return false, err 90 } 91 92 config := &clientcmd.Config{} 93 if err := yaml.Unmarshal(file.Data, config); err != nil { 94 return false, errors.Wrapf(err, "failed to unmarshal %s", name) 95 } 96 97 k.File, k.Config = file, config 98 return true, nil 99 } 100 101 func getExtAPIServerURL(ic *types.InstallConfig) string { 102 return fmt.Sprintf("https://api.%s:6443", ic.ClusterDomain()) 103 } 104 105 func getIntAPIServerURL(ic *types.InstallConfig) string { 106 return fmt.Sprintf("https://api-int.%s:6443", ic.ClusterDomain()) 107 } 108 109 func getLoopbackAPIServerURL(ic *types.InstallConfig) string { 110 return "https://localhost:6443" 111 }