/
creator.go
66 lines (59 loc) · 2.01 KB
/
creator.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
package accessentry
import (
"context"
"crypto/sha1"
"encoding/base32"
"errors"
"fmt"
"strings"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
)
// CreatorInterface creates access entries.
//
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate -o fakes/fake_creator.go . CreatorInterface
type CreatorInterface interface {
// Create creates access entries.
Create(ctx context.Context, accessEntries []api.AccessEntry) error
// CreateTasks creates a TaskTree for creating access entries.
CreateTasks(ctx context.Context, accessEntries []api.AccessEntry) *tasks.TaskTree
}
// A Creator creates access entries.
type Creator struct {
ClusterName string
StackCreator StackCreator
}
// Create creates the specified access entries.
func (m *Creator) Create(ctx context.Context, accessEntries []api.AccessEntry) error {
taskTree := m.CreateTasks(ctx, accessEntries)
if errs := taskTree.DoAllSync(); len(errs) > 0 {
var allErrs []string
for _, err := range errs {
allErrs = append(allErrs, err.Error())
}
return errors.New(strings.Join(allErrs, "\n"))
}
return nil
}
// CreateTasks creates a TaskTree for creating access entries.
func (m *Creator) CreateTasks(ctx context.Context, accessEntries []api.AccessEntry) *tasks.TaskTree {
taskTree := &tasks.TaskTree{
Parallel: true,
}
for _, ae := range accessEntries {
taskTree.Append(&accessEntryTask{
ctx: ctx,
info: fmt.Sprintf("create access entry for principal ARN %s", ae.PrincipalARN),
clusterName: m.ClusterName,
accessEntry: ae,
stackCreator: m.StackCreator,
})
}
return taskTree
}
// MakeStackName creates a stack name for the specified access entry.
func MakeStackName(clusterName string, accessEntry api.AccessEntry) string {
s := sha1.Sum([]byte(accessEntry.PrincipalARN.String()))
return fmt.Sprintf("eksctl-%s-accessentry-%s", clusterName, base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(s[:]))
}