forked from kubevirt/containerized-data-importer
/
clone-source.go
150 lines (116 loc) · 3.4 KB
/
clone-source.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
package main
import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"flag"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/klog"
"kubevirt.io/containerized-data-importer/pkg/common"
prometheusutil "kubevirt.io/containerized-data-importer/pkg/util/prometheus"
)
var (
contentType string
uploadBytes uint64
)
func init() {
flag.StringVar(&contentType, "content_type", "", "archive|kubevirt")
flag.Uint64Var(&uploadBytes, "upload_bytes", 0, "approx number of bytes in input")
klog.InitFlags(nil)
}
func getEnvVarOrDie(name string) string {
value := os.Getenv(name)
if value == "" {
klog.Fatalf("Error geting env var %s", name)
}
return value
}
func createHTTPClient(clientKey, clientCert, serverCert []byte) *http.Client {
clientKeyPair, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
klog.Fatalf("Error %s creating client keypair", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(serverCert)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientKeyPair},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
return client
}
func startPrometheus() {
certsDirectory, err := ioutil.TempDir("", "certsdir")
if err != nil {
klog.Fatalf("Error %s creating temp dir", err)
}
prometheusutil.StartPrometheusEndpoint(certsDirectory)
}
func createProgressReader(readCloser io.ReadCloser, ownerUID string, totalBytes uint64) io.ReadCloser {
progress := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "clone_progress",
Help: "The clone progress in percentage",
},
[]string{"ownerUID"},
)
prometheus.MustRegister(progress)
promReader := prometheusutil.NewProgressReader(readCloser, totalBytes, progress, ownerUID)
promReader.StartTimedUpdate()
return promReader
}
func pipeToGzip(reader io.ReadCloser) io.ReadCloser {
pr, pw := io.Pipe()
gzw := gzip.NewWriter(pw)
go func() {
n, err := io.Copy(gzw, reader)
if err != nil {
klog.Fatalf("Error %s piping to gzip", err)
}
gzw.Close()
pw.Close()
klog.Infof("Wrote %d bytes\n", n)
}()
return pr
}
func main() {
flag.Parse()
defer klog.Flush()
klog.Infof("content_type is %q\n", contentType)
klog.Infof("upload_bytes is %d", uploadBytes)
ownerUID := getEnvVarOrDie(common.OwnerUID)
clientKey := []byte(getEnvVarOrDie("CLIENT_KEY"))
clientCert := []byte(getEnvVarOrDie("CLIENT_CERT"))
serverCert := []byte(getEnvVarOrDie("SERVER_CA_CERT"))
url := getEnvVarOrDie("UPLOAD_URL")
klog.V(1).Infoln("Starting cloner target")
reader := pipeToGzip(createProgressReader(os.Stdin, ownerUID, uploadBytes))
startPrometheus()
client := createHTTPClient(clientKey, clientCert, serverCert)
req, _ := http.NewRequest("POST", url, reader)
if contentType != "" {
req.Header.Set("x-cdi-content-type", contentType)
klog.Infof("Set header to %s", contentType)
}
response, err := client.Do(req)
if err != nil {
klog.Fatalf("Error %s POSTing to %s", err, url)
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
klog.Fatalf("Unexpected status code %d", response.StatusCode)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, response.Body)
if err != nil {
klog.Fatalf("Error %s copying response body", err)
}
klog.V(1).Infof("Response body:\n%s", buf.String())
klog.V(1).Infoln("clone complete")
}