-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce the --use flag when creating a cluster
- Loading branch information
Showing
5 changed files
with
266 additions
and
2 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
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,164 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/base64" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v2" | ||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
// DefaultKubeconfigFile is the absolute default location of the Kubernetes Config File | ||
var DefaultKubeconfigFile = filepath.Join(homedir.HomeDir(), ".kube", "config") | ||
|
||
// CertificateAuthorityPath stores the absolute path of Certificate Authority Cert and | ||
// user's Certificate and Key | ||
type CertificateAuthorityPath struct { | ||
CertificateAuthorityPath, ClientKeyPath, ClientCertPath string | ||
} | ||
|
||
// KubeConfig stores the Cluster and User information read from | ||
// Kubernetes configuration file | ||
type KubeConfig struct { | ||
Clusters []Clusters `yaml:"clusters"` | ||
Users []Users `yaml:"users"` | ||
} | ||
|
||
// Clusters stores an item of a slice of clusters stored in a | ||
// Kubernetes configuration file | ||
type Clusters struct { | ||
Name string `yaml:"name"` | ||
Cluster struct { | ||
CertAuthData string `yaml:"certificate-authority-data"` | ||
Server string `yaml:"server"` | ||
} | ||
} | ||
|
||
// Users is an item of a slice of users stored in a | ||
// Kubernetes configuration file | ||
type Users struct { | ||
Name string | ||
User struct { | ||
ClientCertData string `yaml:"client-certificate-data"` | ||
ClientKeyData string `yaml:"client-key-data"` | ||
} | ||
} | ||
|
||
// CertificateAuthorityData is used to return both clusters and users information | ||
type CertificateAuthorityData struct { | ||
Clusters map[string]Cluster | ||
Users map[string]User | ||
} | ||
|
||
// Cluster is an entry containing one entry with cluster information | ||
// CertAuthorityData holds a base64 representation of the CA Certificate | ||
type Cluster struct { | ||
CertAuthorityData string | ||
Server string | ||
} | ||
|
||
// User is an entry containing the user of the cluster. It contains the base64 representation | ||
// of client certificate and key | ||
type User struct { | ||
ClientCertificateData string | ||
ClientKeyData string | ||
} | ||
|
||
// NewKubeConfig provides is used to store the CA information read from | ||
// the kubeconfig file read created | ||
func NewKubeConfig() *KubeConfig { | ||
return &KubeConfig{} | ||
} | ||
|
||
// CertDataPath reads the base64 data, stores PKI files into $HOME/.kube/<cluster_name> | ||
// and returns the absolute path of where these files are | ||
func (k *KubeConfig) CertDataPath(cad *CertificateAuthorityData) (*CertificateAuthorityPath, error) { | ||
cdp := CertificateAuthorityPath{} | ||
|
||
pkiDir := filepath.Join(os.Getenv("HOME"), ".kube", k.Clusters[0].Name) | ||
|
||
if err := os.MkdirAll(pkiDir, 0750); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, value := range k.Clusters { | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(value.Cluster.CertAuthData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = ioutil.WriteFile(path.Join(pkiDir, "ca.crt"), decoded, 0640); err != nil { | ||
return nil, err | ||
} | ||
|
||
cdp.CertificateAuthorityPath = path.Join(pkiDir, "ca.crt") | ||
|
||
} | ||
|
||
for _, value := range k.Users { | ||
|
||
cdp.ClientCertPath = path.Join(pkiDir, "client-cert.crt") | ||
decoded, err := base64.StdEncoding.DecodeString(value.User.ClientCertData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = ioutil.WriteFile(cdp.ClientCertPath, decoded, 0640); err != nil { | ||
return nil, err | ||
} | ||
|
||
cdp.ClientKeyPath = path.Join(pkiDir, "client.key") | ||
decoded, err = base64.StdEncoding.DecodeString(value.User.ClientKeyData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = ioutil.WriteFile(cdp.ClientKeyPath, decoded, 0400); err != nil { | ||
return nil, err | ||
} | ||
|
||
} | ||
|
||
return &cdp, nil | ||
} | ||
|
||
// CertData reads the base 64 certificate info from kubeconfig and return a | ||
// a CertificateAuthorityData | ||
func (k *KubeConfig) CertData(kubeconfigPath string) (*CertificateAuthorityData, error) { | ||
|
||
kconfigContent, err := ioutil.ReadFile(kubeconfigPath) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(kconfigContent, &k) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
caData := CertificateAuthorityData{} | ||
|
||
for _, c := range k.Clusters { | ||
caData.Clusters = map[string]Cluster{ | ||
c.Name: Cluster{ | ||
CertAuthorityData: c.Cluster.CertAuthData, | ||
Server: c.Cluster.Server, | ||
}, | ||
} | ||
} | ||
|
||
for _, u := range k.Users { | ||
caData.Users = map[string]User{ | ||
u.Name: User{ | ||
ClientCertificateData: u.User.ClientCertData, | ||
ClientKeyData: u.User.ClientKeyData, | ||
}, | ||
} | ||
} | ||
return &caData, nil | ||
} |