-
Notifications
You must be signed in to change notification settings - Fork 16
/
eks.go
77 lines (65 loc) · 2.08 KB
/
eks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright © 2021 Amiran Alavidze @airman604 and Marc Wickenden @marcwickenden
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package eks
import (
"fmt"
"github.com/4armed/kubeletmein/pkg/common"
"github.com/4armed/kubeletmein/pkg/config"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/kubicorn/kubicorn/pkg/logger"
"k8s.io/client-go/tools/clientcmd"
)
// Generate creates the kubeconfig for EKS
func Generate(c *config.Config) error {
var userData string
var err error
// get user-data
if c.MetadataFile != "" {
userDataBytes, err := common.FetchMetadataFromFile(c.MetadataFile)
if err != nil {
return err
}
userData = string(userDataBytes)
} else {
logger.Info("fetching cluster information from the metadata service")
userData, err = getUserData()
if err != nil {
return err
}
}
// These parsers should return an api.Config{} struct
logger.Info("parsing user-data")
kubeConfigData, err := ParseUserData(userData)
if err != nil {
return err
}
// Marshal to disk
err = clientcmd.WriteToFile(*kubeConfigData, c.KubeConfig)
if err != nil {
return fmt.Errorf("unable to write kubeconfig file: %v", err)
}
logger.Info("wrote kubeconfig")
logger.Info("now try: kubectl --kubeconfig %v get pods", c.KubeConfig)
return err
}
func getUserData() (string, error) {
md := ec2metadata.New(session.New())
userData, err := md.GetUserData()
if err != nil {
return "", err
}
return userData, nil
}