generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
146 lines (124 loc) · 4.09 KB
/
cluster.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
package aws
import (
"context"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/giantswarm/object-storage-operator/internal/pkg/cluster"
"github.com/giantswarm/object-storage-operator/internal/pkg/flags"
)
// AWSClusterGetter implements ClusterGetter Interface
// It creates an AWSCluster object
type AWSClusterGetter struct {
Client client.Client
ManagementCluster flags.ManagementCluster
}
const (
Group = "infrastructure.cluster.x-k8s.io"
KindCluster = "AWSCluster"
VersionCluster = "v1beta2"
KindClusterIdentity = "AWSClusterRoleIdentity"
VersionClusterIdentity = "v1beta2"
)
func (c AWSClusterGetter) GetCluster(ctx context.Context) (cluster.Cluster, error) {
logger := log.FromContext(ctx)
cluster, err := c.getClusterCR(ctx)
if err != nil {
logger.Error(err, "Missing management cluster AWSCluster CR")
return nil, errors.WithStack(err)
}
clusterIdentityName, found, err := unstructured.NestedString(cluster.Object, "spec", "identityRef", "name")
if err != nil {
logger.Error(err, "Identity name is not a string")
return nil, errors.WithStack(err)
}
if !found || clusterIdentityName == "" {
logger.Info("Missing identity, skipping")
return nil, errors.New("Missing management cluster identityRef")
}
clusterIdentity, err := c.getClusterCRIdentiy(ctx, clusterIdentityName)
if err != nil {
logger.Error(err, "Missing management cluster identity AWSClusterRoleIdentity CR")
return nil, errors.WithStack(err)
}
roleArn, found, err := unstructured.NestedString(clusterIdentity.Object, "spec", "roleARN")
if err != nil {
logger.Error(err, "Role arn is not a string")
return nil, errors.WithStack(err)
}
if !found {
return nil, errors.New("Missing role arn")
}
clusterTags, found, err := unstructured.NestedStringMap(cluster.Object, "spec", "additionalTags")
if err != nil {
logger.Error(err, "Additional tags are not a map")
return nil, errors.WithStack(err)
}
if !found || len(clusterTags) == 0 {
logger.Info("No cluster tags found")
}
return AWSCluster{
Client: c.Client,
Name: c.ManagementCluster.Name,
Namespace: c.ManagementCluster.Namespace,
BaseDomain: c.ManagementCluster.BaseDomain,
Region: c.ManagementCluster.Region,
Tags: clusterTags,
Credentials: AWSCredentials{
Role: roleArn,
},
}, nil
}
func (c AWSClusterGetter) getClusterCR(ctx context.Context) (*unstructured.Unstructured, error) {
cluster := &unstructured.Unstructured{}
cluster.SetGroupVersionKind(schema.GroupVersionKind{
Group: Group,
Kind: KindCluster,
Version: VersionCluster,
})
err := c.Client.Get(ctx, c.ManagementCluster.ToObjectKey(c.ManagementCluster.Name, c.ManagementCluster.Namespace), cluster)
return cluster, errors.WithStack(err)
}
func (c AWSClusterGetter) getClusterCRIdentiy(ctx context.Context, clusterIdentityName string) (*unstructured.Unstructured, error) {
clusterIdentity := &unstructured.Unstructured{}
clusterIdentity.SetGroupVersionKind(schema.GroupVersionKind{
Group: Group,
Kind: KindClusterIdentity,
Version: VersionClusterIdentity,
})
err := c.Client.Get(ctx, c.ManagementCluster.ToObjectKey(clusterIdentityName, c.ManagementCluster.Namespace), clusterIdentity)
return clusterIdentity, errors.WithStack(err)
}
// AWSCluster implements Cluster Interface with AWS data
type AWSCluster struct {
Client client.Client
Name string
Namespace string
BaseDomain string
Region string
Tags map[string]string
Credentials AWSCredentials
}
type AWSCredentials struct {
Role string
}
func (c AWSCluster) GetName() string {
return c.Name
}
func (c AWSCluster) GetNamespace() string {
return c.Namespace
}
func (c AWSCluster) GetBaseDomain() string {
return c.BaseDomain
}
func (c AWSCluster) GetRegion() string {
return c.Region
}
func (c AWSCluster) GetTags() map[string]string {
return c.Tags
}
func (c AWSCluster) GetCredentials() cluster.Credentials {
return c.Credentials
}