/
init.go
90 lines (70 loc) · 2.48 KB
/
init.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
78
79
80
81
82
83
84
85
86
87
88
89
90
package v1
import (
"path/filepath"
"strings"
apiv1 "github.com/liferaft/kubekit/api/kubekit/v1"
"github.com/liferaft/kubekit/pkg/kluster"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const defaultClusterConfigFormat = "yaml"
// Init creates a configuration file for the given kind (`cluster` or `template`)
func (s *KubeKitService) Init(ctx context.Context, in *apiv1.InitRequest) (*apiv1.InitResponse, error) {
if err := s.checkAPIVersion(in.Api); err != nil {
return nil, err
}
if _, err := kluster.ValidClusterName(in.ClusterName); err != nil {
return nil, err
}
if in.Platform == apiv1.PlatformName_UNKNOWN {
return nil, grpc.Errorf(codes.Internal, "the platform name is required")
}
if s.dry {
return &apiv1.InitResponse{
Api: apiVersion,
Kind: in.Kind,
Name: in.ClusterName,
}, nil
}
var cluster *kluster.Kluster
var initResponse *apiv1.InitResponse
platformName := strings.ToLower(in.Platform.String())
createClusterConfig := func() error {
s.ui.Log.Debugf("initializing cluster %q configuration", in.ClusterName)
var err error
cluster, err = kluster.CreateCluster(in.ClusterName, platformName, s.clustersPath, defaultClusterConfigFormat, in.Variables, s.ui)
if err != nil {
return grpc.Errorf(codes.Internal, "failed to initialize the cluster %s. %s", in.ClusterName, err)
}
initResponse = &apiv1.InitResponse{
Api: apiVersion,
Kind: cluster.Kind,
Name: cluster.Name,
}
return nil
}
// If this is a cluster for the following platforms, do not process the
// credentials. They do not have them
switch platformName {
case "vra", "raw", "stacki":
return initResponse, createClusterConfig()
}
s.ui.Log.Debugf("initializing cluster %q credentials", in.ClusterName)
credentials := kluster.NewCredentials(in.ClusterName, platformName, "")
if err := credentials.AssignFromMap(in.Credentials); err != nil {
return nil, err
}
if !credentials.Complete() {
return nil, grpc.Errorf(codes.Internal, "credentials for the platform %q are incomplete or missing in the given variables", platformName)
}
if err := createClusterConfig(); err != nil {
return nil, err
}
credentialsPath := filepath.Join(filepath.Dir(cluster.Path()), ".credentials")
credentials.SetPath(credentialsPath)
if err := credentials.Write(); err != nil {
return nil, grpc.Errorf(codes.Internal, "the cluster configuration was created but failed to write the credentials files. %s", err)
}
return initResponse, nil
}