forked from kyma-project/hydroform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision.go
187 lines (156 loc) · 6.32 KB
/
provision.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package provision
import (
"errors"
"path/filepath"
"runtime"
"strings"
"github.com/kyma-incubator/hydroform/provision/action"
"github.com/kyma-incubator/hydroform/provision/internal/azure"
"github.com/kyma-incubator/hydroform/provision/internal/gardener"
"github.com/kyma-incubator/hydroform/provision/internal/kind"
"github.com/kyma-incubator/hydroform/provision/internal/gcp"
"github.com/kyma-incubator/hydroform/provision/internal/operator"
"github.com/kyma-incubator/hydroform/provision/types"
)
const provisioningOperator = operator.TerraformOperator
// Provisioner is the Hydroform interface that groups Provision, Status, Credentials, and Deprovision functions used to create and manage a cluster.
type Provisioner interface {
Provision(cluster *types.Cluster, provider *types.Provider) (*types.Cluster, error)
Status(cluster *types.Cluster, provider *types.Provider) (*types.ClusterStatus, error)
Credentials(cluster *types.Cluster, provider *types.Provider) ([]byte, error)
Deprovision(cluster *types.Cluster, provider *types.Provider) error
}
// Provision creates a new cluster for a given provider based on specific cluster and provider parameters. It returns a cluster object enriched with information from the provider, such as the IP address or the connection endpoint. This object is necessary for the other operations, such as retrieving the cluster status or deprovisioning the cluster. If the cluster cannot be created, the function returns an error.
func Provision(cluster *types.Cluster, provider *types.Provider, ops ...types.Option) (*types.Cluster, error) {
var err error
var cl *types.Cluster
if err = action.Before(); err != nil {
return cl, err
}
if runtime.GOOS == "windows" {
provider.CredentialsFilePath = updateWindowsPath(provider.CredentialsFilePath)
}
switch provider.Type {
case types.GCP:
cl, err = newGCPProvisioner(provisioningOperator, ops...).Provision(cluster, provider)
case types.Gardener:
cl, err = newGardenerProvisioner(provisioningOperator, ops...).Provision(cluster, provider)
case types.AWS:
err = errors.New("aws not supported yet")
case types.Azure:
cl, err = newAzureProvisioner(provisioningOperator, ops...).Provision(cluster, provider)
case types.Kind:
cl, err = newKindProvisioner(provisioningOperator, ops...).Provision(cluster, provider)
default:
err = errors.New("unknown provider")
}
if err != nil {
return cl, err
}
return cl, action.After()
}
// Status returns the cluster status for a given provider, or an error if providing the status is not possible. The possible status values are defined in the ClusterStatus type.
func Status(cluster *types.Cluster, provider *types.Provider, ops ...types.Option) (*types.ClusterStatus, error) {
var err error
var cs *types.ClusterStatus
if err = action.Before(); err != nil {
return cs, err
}
if runtime.GOOS == "windows" {
provider.CredentialsFilePath = updateWindowsPath(provider.CredentialsFilePath)
}
switch provider.Type {
case types.GCP:
cs, err = newGCPProvisioner(provisioningOperator, ops...).Status(cluster, provider)
case types.Gardener:
cs, err = newGardenerProvisioner(provisioningOperator, ops...).Status(cluster, provider)
case types.AWS:
err = errors.New("aws not supported yet")
case types.Azure:
cs, err = newAzureProvisioner(provisioningOperator, ops...).Status(cluster, provider)
case types.Kind:
cs, err = newKindProvisioner(provisioningOperator, ops...).Status(cluster, provider)
default:
err = errors.New("unknown provider")
}
if err != nil {
return cs, err
}
return cs, action.After()
}
// Credentials returns the kubeconfig for a specific cluster as a byte array.
func Credentials(cluster *types.Cluster, provider *types.Provider, ops ...types.Option) ([]byte, error) {
var err error
var cr []byte
if err = action.Before(); err != nil {
return cr, err
}
if runtime.GOOS == "windows" {
provider.CredentialsFilePath = updateWindowsPath(provider.CredentialsFilePath)
}
switch provider.Type {
case types.GCP:
cr, err = newGCPProvisioner(provisioningOperator, ops...).Credentials(cluster, provider)
case types.Gardener:
cr, err = newGardenerProvisioner(provisioningOperator, ops...).Credentials(cluster, provider)
case types.AWS:
err = errors.New("aws not supported yet")
case types.Azure:
cr, err = newAzureProvisioner(provisioningOperator, ops...).Credentials(cluster, provider)
case types.Kind:
cr, err = newKindProvisioner(provisioningOperator, ops...).Credentials(cluster, provider)
default:
err = errors.New("unknown provider")
}
if err != nil {
return cr, err
}
return cr, action.After()
}
// Deprovision removes an existing cluster along or returns an error if removing the cluster is not possible.
func Deprovision(cluster *types.Cluster, provider *types.Provider, ops ...types.Option) error {
var err error
if err = action.Before(); err != nil {
return err
}
if runtime.GOOS == "windows" {
provider.CredentialsFilePath = updateWindowsPath(provider.CredentialsFilePath)
}
switch provider.Type {
case types.GCP:
err = newGCPProvisioner(provisioningOperator, ops...).Deprovision(cluster, provider)
case types.Gardener:
err = newGardenerProvisioner(provisioningOperator, ops...).Deprovision(cluster, provider)
case types.AWS:
err = errors.New("aws not supported yet")
case types.Azure:
err = newAzureProvisioner(provisioningOperator, ops...).Deprovision(cluster, provider)
case types.Kind:
err = newKindProvisioner(provisioningOperator, ops...).Deprovision(cluster, provider)
default:
err = errors.New("unknown provider")
}
if err != nil {
return err
}
return action.After()
}
func newGCPProvisioner(operatorType operator.Type, ops ...types.Option) Provisioner {
return gcp.New(operatorType, ops...)
}
func newGardenerProvisioner(operatorType operator.Type, ops ...types.Option) Provisioner {
return gardener.New(operatorType, ops...)
}
func newAWSProvisioner(operatorType operator.Type, ops ...types.Option) Provisioner {
return nil
}
func newAzureProvisioner(operatorType operator.Type, ops ...types.Option) Provisioner {
return azure.New(operatorType, ops...)
}
func newKindProvisioner(operatorType operator.Type, ops ...types.Option) Provisioner {
return kind.New(operatorType, ops...)
}
func updateWindowsPath(windowsPath string) string {
cleanWindowsPath := filepath.Clean(windowsPath)
return strings.Replace(cleanWindowsPath, `\`, `\\`, -1)
}