/
service.go
243 lines (199 loc) · 7.05 KB
/
service.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright © 2020 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pke
import (
"context"
"emperror.dev/errors"
"github.com/banzaicloud/pipeline/internal/cluster"
)
// +testify:mock
// Service provides an interface to PKE AWS clusters.
type Service interface {
// DeleteNodePool deletes an existing node pool.
DeleteNodePool(ctx context.Context, clusterID uint, nodePoolName string) (isDeleted bool, err error)
// UpdateCluster updates a cluster.
//
// This method accepts a partial body representation.
UpdateCluster(ctx context.Context, clusterID uint, clusterUpdate ClusterUpdate) error
// UpdateNodePool updates an existing node pool in a cluster.
//
// This method accepts a partial body representation.
UpdateNodePool(ctx context.Context, clusterID uint, nodePoolName string, nodePoolUpdate NodePoolUpdate) (string, error)
// ListNodePools lists node pools from a cluster.
ListNodePools(ctx context.Context, clusterID uint) ([]NodePool, error)
}
// ClusterUpdate describes a cluster update request.
//
// A cluster update contains a partial representation of the cluster resource,
// updating only the changed values.
type ClusterUpdate struct {
Version string `mapstructure:"version"`
}
// NodePoolUpdate describes a node pool update request.
//
// A node pool update contains a partial representation of the node pool resource,
// updating only the changed values.
type NodePoolUpdate struct {
Image string `mapstructure:"image"`
Version string `mapstructure:"version"`
Options NodePoolUpdateOptions `mapstructure:"options"`
}
type NodePoolUpdateOptions struct {
// Maximum number of extra nodes that can be created during the update.
MaxSurge int `mapstructure:"maxSurge"`
// Maximum number of nodes that can be updated simultaneously.
MaxBatchSize int `mapstructure:"maxBatchSize"`
// Maximum number of nodes that can be unavailable during the update.
MaxUnavailable int `mapstructure:"maxUnavailable"`
// Kubernetes node drain specific options.
Drain NodePoolUpdateDrainOptions `mapstructure:"drain"`
}
type NodePoolUpdateDrainOptions struct {
Timeout int `mapstructure:"timeout"`
FailOnError bool `mapstructure:"failOnError"`
PodSelector string `mapstructure:"podSelector"`
}
// NodePool encapsulates information about a cluster node pool.
type NodePool struct {
Name string `mapstructure:"name"`
Labels map[string]string `mapstructure:"labels"`
Size int `mapstructure:"size"`
Autoscaling Autoscaling `mapstructure:"autoscaling"`
VolumeSize int `mapstructure:"volumeSize"`
InstanceType string `mapstructure:"instanceType"`
Image string `mapstructure:"image"`
SpotPrice string `mapstructure:"spotPrice"`
SubnetID string `mapstructure:"subnetId"`
}
// Autoscaling describes the EC2 node pool's autoscaling settings.
type Autoscaling struct {
Enabled bool `mapstructure:"enabled"`
MinSize int `mapstructure:"minSize"`
MaxSize int `mapstructure:"maxSize"`
}
// NewService returns a new Service instance.
func NewService(
genericClusters Store,
nodePoolManager NodePoolManager,
nodePoolStore NodePoolStore,
enterprise bool,
) Service {
return service{
enterprise: enterprise,
genericClusters: genericClusters,
nodePoolManager: nodePoolManager,
nodePoolStore: nodePoolStore,
}
}
type service struct {
enterprise bool
genericClusters Store
clusterManager ClusterManager
nodePoolManager NodePoolManager
nodePoolStore NodePoolStore
}
// +testify:mock:testOnly=true
// NodePoolManager is responsible for managing node pools.
type NodePoolManager interface {
// DeleteNodePool deletes an existing node pool in a cluster.
DeleteNodePool(
ctx context.Context,
c cluster.Cluster,
existingNodePool ExistingNodePool,
shouldUpdateClusterStatus bool,
) (err error)
// UpdateNodePool updates an existing node pool in a cluster.
UpdateNodePool(
ctx context.Context,
c cluster.Cluster,
nodePoolName string,
nodePoolUpdate NodePoolUpdate,
) (string, error)
}
// ClusterManager is responsible for managing clusters.
type ClusterManager interface {
// UpdateCluster updates an existing cluster.
UpdateCluster(ctx context.Context, c cluster.Cluster, clusterUpdate ClusterUpdate) error
}
func (s service) DeleteNodePool(ctx context.Context, clusterID uint, nodePoolName string) (isDeleted bool, err error) {
c, err := s.genericClusters.GetCluster(ctx, clusterID)
if err != nil {
return false, err
}
existingNodePools, err := s.nodePoolStore.ListNodePools(ctx, c.OrganizationID, c.ID, c.Name)
if err != nil {
return false, err
}
existingNodePool, isExisting := existingNodePools[nodePoolName]
if !isExisting {
return true, nil
}
err = s.genericClusters.SetStatus(ctx, clusterID, cluster.Updating, "deleting node pool")
if err != nil {
return false, err
}
err = s.nodePoolManager.DeleteNodePool(ctx, c, existingNodePool, true)
if err != nil {
return false, err
}
return false, nil
}
func (s service) UpdateCluster(
ctx context.Context,
clusterID uint,
clusterUpdate ClusterUpdate,
) error {
c, err := s.genericClusters.GetCluster(ctx, clusterID)
if err != nil {
return err
}
err = s.genericClusters.SetStatus(ctx, clusterID, cluster.Updating, "updating cluster")
if err != nil {
return err
}
return s.clusterManager.UpdateCluster(ctx, c, clusterUpdate)
}
func (s service) UpdateNodePool(
ctx context.Context,
clusterID uint,
nodePoolName string,
nodePoolUpdate NodePoolUpdate,
) (string, error) {
c, err := s.genericClusters.GetCluster(ctx, clusterID)
if err != nil {
return "", err
}
err = s.genericClusters.SetStatus(ctx, clusterID, cluster.Updating, "updating node pool")
if err != nil {
return "", err
}
return s.nodePoolManager.UpdateNodePool(ctx, c, nodePoolName, nodePoolUpdate)
}
// ListNodePools lists node pools from a cluster.
func (s service) ListNodePools(ctx context.Context, clusterID uint) ([]NodePool, error) {
_, err := s.genericClusters.GetCluster(ctx, clusterID)
if err != nil {
return nil, errors.WrapWithDetails(err, "retrieving cluster failed", "clusterID", clusterID)
}
panic("not implemented")
}
// +testify:mock:testOnly=true
// Store provides an interface to the generic Cluster model persistence.
type Store interface {
// GetCluster returns a generic Cluster.
// Returns an error with the NotFound behavior when the cluster cannot be found.
GetCluster(ctx context.Context, id uint) (cluster.Cluster, error)
// SetStatus sets the cluster status.
SetStatus(ctx context.Context, id uint, status string, statusMessage string) error
}