-
Notifications
You must be signed in to change notification settings - Fork 22
/
current.go
146 lines (117 loc) · 3.57 KB
/
current.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 s3bucket
import (
"context"
"fmt"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/giantswarm/microerror"
"golang.org/x/sync/errgroup"
"github.com/giantswarm/aws-operator/service/controller/legacy/v26/controllercontext"
"github.com/giantswarm/aws-operator/service/controller/legacy/v26/key"
)
func (r *Resource) GetCurrentState(ctx context.Context, obj interface{}) (interface{}, error) {
customObject, err := key.ToCustomObject(obj)
if err != nil {
return nil, microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
bucketStateNames := []string{
key.TargetLogBucketName(customObject),
key.BucketName(customObject, cc.Status.TenantCluster.AWSAccountID),
}
var currentBucketState []BucketState
{
r.logger.LogCtx(ctx, "level", "debug", "message", "finding the S3 buckets")
g := &errgroup.Group{}
m := sync.Mutex{}
for _, inputBucketName := range bucketStateNames {
bucketName := inputBucketName
g.Go(func() error {
inputBucket := BucketState{
Name: bucketName,
}
// TODO this check should not be done here. Here we only fetch the
// current state. We have to make a request anyway so fetching what we
// want and handling the not found errors as usual should be the way to
// go.
//
//
// https://github.com/giantswarm/giantswarm/issues/5246
//
isCreated, err := r.isBucketCreated(ctx, bucketName)
if err != nil {
return microerror.Mask(err)
}
if !isCreated {
return nil
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("finding the S3 bucket %#q", bucketName))
lc, err := r.getLoggingConfiguration(ctx, bucketName)
if err != nil {
return microerror.Mask(err)
}
m.Lock()
inputBucket.IsLoggingBucket = isLoggingBucket(bucketName, lc)
inputBucket.IsLoggingEnabled = isLoggingEnabled(lc)
currentBucketState = append(currentBucketState, inputBucket)
m.Unlock()
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("found the S3 bucket %#q", bucketName))
return nil
})
}
err := g.Wait()
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", "found the S3 buckets")
}
return currentBucketState, nil
}
func (r *Resource) isBucketCreated(ctx context.Context, name string) (bool, error) {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return false, microerror.Mask(err)
}
headInput := &s3.HeadBucketInput{
Bucket: aws.String(name),
}
_, err = cc.Client.TenantCluster.AWS.S3.HeadBucket(headInput)
if IsBucketNotFound(err) {
return false, nil
} else if err != nil {
return false, microerror.Mask(err)
}
return true, nil
}
func (r *Resource) getLoggingConfiguration(ctx context.Context, name string) (*s3.GetBucketLoggingOutput, error) {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
bucketLoggingInput := &s3.GetBucketLoggingInput{
Bucket: aws.String(name),
}
bucketLoggingOutput, err := cc.Client.TenantCluster.AWS.S3.GetBucketLogging(bucketLoggingInput)
if err != nil {
return bucketLoggingOutput, microerror.Mask(err)
}
return bucketLoggingOutput, nil
}
func isLoggingEnabled(lc *s3.GetBucketLoggingOutput) bool {
if lc.LoggingEnabled != nil {
return true
}
return false
}
func isLoggingBucket(name string, lc *s3.GetBucketLoggingOutput) bool {
if lc.LoggingEnabled != nil {
if *lc.LoggingEnabled.TargetBucket == name {
return true
}
}
return false
}