-
Notifications
You must be signed in to change notification settings - Fork 90
/
s3.go
277 lines (236 loc) · 7.6 KB
/
s3.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package s3
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
kotsadmversion "github.com/replicatedhq/kots/pkg/kotsadm/version"
"github.com/replicatedhq/kots/pkg/kurl"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type S3OpsPodOptions struct {
PodName string
Endpoint string
BucketName string
CACertData []byte
AccessKeyID string
SecretAccessKey string
Namespace string
IsOpenShift bool
RegistryConfig *kotsadmtypes.RegistryConfig
}
func GetConfig() *aws.Config {
forcePathStyle := false
if os.Getenv("S3_BUCKET_ENDPOINT") == "true" {
forcePathStyle = true
}
region := os.Getenv("S3_REGION")
if region == "" {
region = "us-east-1"
}
accessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
var creds *credentials.Credentials
if accessKeyID != "" && secretAccessKey != "" {
creds = credentials.NewStaticCredentials(accessKeyID, secretAccessKey, "")
}
s3Config := &aws.Config{
Credentials: creds,
Endpoint: aws.String(os.Getenv("S3_ENDPOINT")),
Region: aws.String(region),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(forcePathStyle),
}
return s3Config
}
// CreateS3BucketUsingAPod is helpful when trying to hit a cluster s3 service using the CLI since that could be used outside the cluster, or due to firewall restrictions
func CreateS3BucketUsingAPod(ctx context.Context, clientset kubernetes.Interface, podOptions S3OpsPodOptions) error {
command := []string{"/s3-bucket-create.sh"}
pod, err := s3BucketPod(clientset, podOptions, command)
if err != nil {
return errors.Wrap(err, "failed to get pod resource")
}
createBucketPod, err := clientset.CoreV1().Pods(podOptions.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create pod")
}
if err := k8sutil.WaitForPod(ctx, clientset, podOptions.Namespace, createBucketPod.Name, time.Minute*2); err != nil {
return errors.Wrap(err, "failed to wait for pod")
}
logs, err := k8sutil.GetPodLogs(ctx, clientset, createBucketPod, true, nil)
if err != nil {
return errors.Wrap(err, "failed to get pod logs")
}
if len(logs) == 0 {
return errors.New("no logs found")
}
type CreateBucketPodOutput struct {
Success bool `json:"success"`
}
createBucketPodOutput := CreateBucketPodOutput{}
scanner := bufio.NewScanner(bytes.NewReader(logs))
var line string
for scanner.Scan() {
line = scanner.Text()
if err := json.Unmarshal([]byte(line), &createBucketPodOutput); err != nil {
continue
}
break
}
if !createBucketPodOutput.Success {
// Last line is the error
if len(line) > 0 {
return util.ActionableError{
Message: fmt.Sprintf("failed to create S3 bucket: %s", line),
}
}
return util.ActionableError{
Message: fmt.Sprintf("failed to create bucket, please check %s pod logs for more details", createBucketPod.Name),
}
}
// only delete the pod on success
err = clientset.CoreV1().Pods(podOptions.Namespace).Delete(ctx, createBucketPod.Name, metav1.DeleteOptions{})
if err != nil {
logger.Errorf("failed to delete bucket creating pod %s: %v", createBucketPod.Name, err)
}
return nil
}
// HeadS3BucketUsingAPod is helpful when trying to hit a cluster s3 service using the CLI since that could be used outside the cluster, or due to firewall restrictions
func HeadS3BucketUsingAPod(ctx context.Context, clientset kubernetes.Interface, podOptions S3OpsPodOptions) error {
command := []string{"/s3-bucket-head.sh"}
pod, err := s3BucketPod(clientset, podOptions, command)
if err != nil {
return errors.Wrap(err, "failed to get pod resource")
}
headBucketPod, err := clientset.CoreV1().Pods(podOptions.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create pod")
}
if err := k8sutil.WaitForPod(ctx, clientset, podOptions.Namespace, headBucketPod.Name, time.Minute*2); err != nil {
return errors.Wrap(err, "failed to wait for pod")
}
logs, err := k8sutil.GetPodLogs(ctx, clientset, headBucketPod, true, nil)
if err != nil {
return errors.Wrap(err, "failed to get pod logs")
}
if len(logs) == 0 {
return errors.New("no logs found")
}
type HeadBucketPodOutput struct {
Success bool `json:"success"`
}
headBucketPodOutput := HeadBucketPodOutput{}
scanner := bufio.NewScanner(bytes.NewReader(logs))
for scanner.Scan() {
line := scanner.Text()
if err := json.Unmarshal([]byte(line), &headBucketPodOutput); err != nil {
continue
}
break
}
if !headBucketPodOutput.Success {
return errors.Errorf("failed to head bucket, please check %s pod logs for more details", headBucketPod.Name)
}
// only delete the pod on success
clientset.CoreV1().Pods(podOptions.Namespace).Delete(ctx, headBucketPod.Name, metav1.DeleteOptions{})
return nil
}
func s3BucketPod(clientset kubernetes.Interface, podOptions S3OpsPodOptions, command []string) (*corev1.Pod, error) {
var securityContext corev1.PodSecurityContext
if !podOptions.IsOpenShift {
securityContext = corev1.PodSecurityContext{
RunAsUser: util.IntPointer(1001),
FSGroup: util.IntPointer(1001),
}
}
kotsadmTag := kotsadmversion.KotsadmTag(kotsadmtypes.RegistryConfig{}) // default tag
image := fmt.Sprintf("kotsadm/kotsadm:%s", kotsadmTag)
imagePullSecrets := []corev1.LocalObjectReference{}
isKurl, err := kurl.IsKurl(clientset)
if err != nil {
return nil, errors.Wrap(err, "failed to check if cluster is kurl")
}
if !isKurl || podOptions.Namespace != metav1.NamespaceDefault {
var err error
imageRewriteFn := kotsadmversion.KotsadmImageRewriteKotsadmRegistry(podOptions.Namespace, podOptions.RegistryConfig)
image, imagePullSecrets, err = imageRewriteFn(image, false)
if err != nil {
return nil, errors.Wrap(err, "failed to rewrite image")
}
}
env := []corev1.EnvVar{
{
Name: "TMP_S3_ENDPOINT",
Value: podOptions.Endpoint,
},
{
Name: "TMP_S3_BUCKET_NAME",
Value: podOptions.BucketName,
},
{
Name: "TMP_S3_ACCESS_KEY_ID",
Value: podOptions.AccessKeyID,
},
{
Name: "TMP_S3_SECRET_ACCESS_KEY",
Value: podOptions.SecretAccessKey,
},
}
if podOptions.CACertData != nil {
env = append(env, corev1.EnvVar{
Name: "TMP_CA_CERT",
Value: string(podOptions.CACertData),
})
}
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: podOptions.PodName,
Namespace: podOptions.Namespace,
Labels: map[string]string{
"app": "kotsadm-s3-ops",
},
},
Spec: corev1.PodSpec{
SecurityContext: &securityContext,
RestartPolicy: corev1.RestartPolicyOnFailure,
ImagePullSecrets: imagePullSecrets,
Containers: []corev1.Container{
{
Image: image,
ImagePullPolicy: corev1.PullIfNotPresent,
Name: "s3-bucket",
Command: command,
Env: env,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("100Mi"),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse("50m"),
"memory": resource.MustParse("50Mi"),
},
},
},
},
},
}
return pod, nil
}