-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
204 lines (172 loc) · 4.95 KB
/
main.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
package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// Uploads a file to S3 given a bucket and object key.
// go run main.go < myfile.txt
func UploadToS3() {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-west-2"))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
// Using the Config value, create the s3 client
client := s3.NewFromConfig(cfg)
uploader := manager.NewUploader(client)
_, err = uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("backup-112664522426-us-west-2"),
Key: aws.String("renxuan/renxuan-test"),
Body: os.Stdin,
ServerSideEncryption: types.ServerSideEncryptionAwsKms,
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to upload object, %v\n", err)
os.Exit(1)
}
fmt.Printf("successfully uploaded file")
}
func DownloadFromKube() {
var newClient interface{}
client, err := GetClientToK8s()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to download object, %v\n", err)
os.Exit(1)
}
newClient = client
w, err := os.Create("coordination.fdq")
DownloadFromK8s(newClient, "renxuan/foo-storage-1/foundationdb/var/fdb/data/coordination-0.fdq", w)
}
func main() {
// UploadToS3()
DownloadFromKube()
}
// K8sClient holds a clientset and a config
type K8sClient struct {
ClientSet *kubernetes.Clientset
Config *rest.Config
}
// GetClientToK8s returns a k8sClient
func GetClientToK8s() (*K8sClient, error) {
var kubeconfig string
if kubeConfigPath := os.Getenv("KUBECONFIG"); kubeConfigPath != "" {
kubeconfig = kubeConfigPath // CI process
} else {
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") // Development environment
}
var config *rest.Config
fmt.Println("111111111")
_, err := os.Stat(kubeconfig)
if err != nil {
// In cluster configuration
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
// Out of cluster configuration
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
fmt.Println("2222222222")
clientset, err := kubernetes.NewForConfig(config)
fmt.Println("33333333333")
if err != nil {
return nil, err
}
var client = &K8sClient{ClientSet: clientset, Config: config}
fmt.Println("4444444444")
return client, nil
}
// DownloadFromK8s downloads a single file from Kubernetes
func DownloadFromK8s(iClient interface{}, path string, writer io.Writer) error {
client := *iClient.(*K8sClient)
pSplit := strings.Split(path, "/")
namespace, podName, containerName, pathToCopy := initK8sVariables(pSplit)
command := []string{"cat", pathToCopy}
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
stderr, err := Exec(client, namespace, podName, containerName, command, nil, writer)
if attempt == attempts {
if len(stderr) != 0 {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
if err != nil {
return err
}
}
if err == nil {
return nil
}
time.Sleep(time.Duration(attempt) * time.Second)
}
return nil
}
func initK8sVariables(split []string) (string, string, string, string) {
namespace := split[0]
pod := split[1]
container := split[2]
path := getAbsPath(split[3:]...)
return namespace, pod, container, path
}
func getAbsPath(path ...string) string {
return filepath.Join("/", filepath.Join(path...))
}
// Exec executes a command in a given container
func Exec(client K8sClient, namespace, podName, containerName string, command []string, stdin io.Reader, stdout io.Writer) ([]byte, error) {
clientset, config := client.ClientSet, client.Config
req := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec")
scheme := runtime.NewScheme()
if err := core_v1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("error adding to scheme: %v", err)
}
parameterCodec := runtime.NewParameterCodec(scheme)
req.VersionedParams(&core_v1.PodExecOptions{
Command: command,
Container: containerName,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: true,
TTY: false,
}, parameterCodec)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
return nil, fmt.Errorf("error while creating Executor: %v", err)
}
var stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
Stderr: &stderr,
Tty: false,
})
if err != nil {
return nil, fmt.Errorf("error in Stream: %v", err)
}
return stderr.Bytes(), nil
}