-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
addon.go
106 lines (93 loc) · 3.39 KB
/
addon.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
package v1alpha5
import (
"encoding/json"
"fmt"
"strings"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"sigs.k8s.io/yaml"
)
// Addon holds the EKS addon configuration
type Addon struct {
// +required
Name string `json:"name,omitempty"`
// +optional
Version string `json:"version,omitempty"`
// +optional
ServiceAccountRoleARN string `json:"serviceAccountRoleARN,omitempty"`
// list of ARNs of the IAM policies to attach
// +optional
AttachPolicyARNs []string `json:"attachPolicyARNs,omitempty"`
// AttachPolicy holds a policy document to attach
// +optional
AttachPolicy InlineDocument `json:"attachPolicy,omitempty"`
// ARN of the permissions' boundary to associate
// +optional
PermissionsBoundary string `json:"permissionsBoundary,omitempty"`
// WellKnownPolicies for attaching common IAM policies
WellKnownPolicies WellKnownPolicies `json:"wellKnownPolicies,omitempty"`
// The metadata to apply to the cluster to assist with categorization and organization.
// Each tag consists of a key and an optional value, both of which you define.
// +optional
Tags map[string]string `json:"tags,omitempty"`
// ResolveConflicts determines how to resolve field value conflicts for an EKS add-on
// if a value was changed from default
ResolveConflicts ekstypes.ResolveConflicts `json:"resolveConflicts,omitempty"`
// ConfigurationValues defines the set of configuration properties for add-ons.
// For now, all properties will be specified as a JSON string
// and have to respect the schema from DescribeAddonConfiguration.
// +optional
ConfigurationValues string `json:"configurationValues,omitempty"`
// Force overwrites an existing self-managed add-on with an EKS managed add-on.
// Force is intended to be used when migrating an existing self-managed add-on to an EKS managed add-on.
Force bool `json:"-"`
// +optional
Publishers []string `json:"publishers,omitempty"`
// +optional
Types []string `json:"types,omitempty"`
// +optional
Owners []string `json:"owners,omitempty"`
}
func (a Addon) CanonicalName() string {
return strings.ToLower(a.Name)
}
func (a Addon) Validate() error {
if a.Name == "" {
return fmt.Errorf("name is required")
}
if !json.Valid([]byte(a.ConfigurationValues)) {
if err := a.convertConfigurationValuesToJSON(); err != nil {
return fmt.Errorf("configurationValues: \"%s\" is not valid, supported format(s) are: JSON and YAML", a.ConfigurationValues)
}
}
return a.checkOnlyOnePolicyProviderIsSet()
}
func (a *Addon) convertConfigurationValuesToJSON() (err error) {
rawConfigurationValues := []byte(a.ConfigurationValues)
var js map[string]interface{}
if err = yaml.UnmarshalStrict(rawConfigurationValues, &js); err == nil {
var JSONConfigurationValues []byte
if JSONConfigurationValues, err = yaml.YAMLToJSONStrict(rawConfigurationValues); err == nil {
a.ConfigurationValues = string(JSONConfigurationValues)
}
}
return err
}
func (a Addon) checkOnlyOnePolicyProviderIsSet() error {
setPolicyProviders := 0
if a.AttachPolicy != nil {
setPolicyProviders++
}
if a.AttachPolicyARNs != nil && len(a.AttachPolicyARNs) > 0 {
setPolicyProviders++
}
if a.ServiceAccountRoleARN != "" {
setPolicyProviders++
}
if a.WellKnownPolicies.HasPolicy() {
setPolicyProviders++
}
if setPolicyProviders > 1 {
return fmt.Errorf("at most one of wellKnownPolicies, serviceAccountRoleARN, attachPolicyARNs and attachPolicy can be specified")
}
return nil
}