-
Notifications
You must be signed in to change notification settings - Fork 264
/
util.go
267 lines (240 loc) · 6.99 KB
/
util.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
package util
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog"
"kubevirt.io/containerized-data-importer/pkg/common"
)
// CountingReader is a reader that keeps track of how much has been read
type CountingReader struct {
Reader io.ReadCloser
Current uint64
Done bool
}
// RandAlphaNum provides an implementation to generate a random alpha numeric string of the specified length
func RandAlphaNum(n int) string {
rand.Seed(time.Now().UnixNano())
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, n)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}
// GetNamespace returns the namespace the pod is executing in
func GetNamespace() string {
return getNamespace("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
}
func getNamespace(path string) string {
if data, err := ioutil.ReadFile(path); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns
}
}
return "cdi"
}
// ParseEnvVar provides a wrapper to attempt to fetch the specified env var
func ParseEnvVar(envVarName string, decode bool) (string, error) {
value := os.Getenv(envVarName)
if decode {
v, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return "", errors.Errorf("error decoding environment variable %q", envVarName)
}
value = fmt.Sprintf("%s", v)
}
return value, nil
}
// Read reads bytes from the stream and updates the prometheus clone_progress metric according to the progress.
func (r *CountingReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
r.Current += uint64(n)
r.Done = err == io.EOF
return n, err
}
// Close closes the stream
func (r *CountingReader) Close() error {
return r.Reader.Close()
}
// GetAvailableSpaceByVolumeMode calls another method based on the volumeMode parameter to get the amount of
// available space at the path specified.
func GetAvailableSpaceByVolumeMode(volumeMode v1.PersistentVolumeMode) int64 {
if volumeMode == v1.PersistentVolumeBlock {
return GetAvailableSpaceBlock(common.ImporterWriteBlockPath)
}
return GetAvailableSpace(common.ImporterVolumePath)
}
// GetAvailableSpace gets the amount of available space at the path specified.
func GetAvailableSpace(path string) int64 {
var stat syscall.Statfs_t
err := syscall.Statfs(path, &stat)
if err != nil {
return int64(-1)
}
return int64(stat.Bavail) * int64(stat.Bsize)
}
// GetAvailableSpaceBlock gets the amount of available space at the block device path specified.
func GetAvailableSpaceBlock(deviceName string) int64 {
cmd := exec.Command("/usr/bin/lsblk", "-n", "-b", "-o", "SIZE", deviceName)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return int64(-1)
}
i, err := strconv.ParseInt(strings.TrimSpace(string(out.Bytes())), 10, 64)
if err != nil {
return int64(-1)
}
return i
}
// MinQuantity calculates the minimum of two quantities.
func MinQuantity(availableSpace, imageSize *resource.Quantity) resource.Quantity {
if imageSize.Cmp(*availableSpace) == 1 {
return *availableSpace
}
return *imageSize
}
// StreamDataToFile provides a function to stream the specified io.Reader to the specified local file
func StreamDataToFile(r io.Reader, fileName string) error {
var outFile *os.File
var err error
if GetAvailableSpaceBlock(fileName) < 0 {
// Attempt to create the file with name filePath. If it exists, fail.
outFile, err = os.OpenFile(fileName, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
} else {
outFile, err = os.OpenFile(fileName, os.O_EXCL|os.O_WRONLY, os.ModePerm)
}
if err != nil {
return errors.Wrapf(err, "could not open file %q", fileName)
}
defer outFile.Close()
klog.V(1).Infof("Writing data...\n")
if _, err = io.Copy(outFile, r); err != nil {
klog.Errorf("Unable to write file from dataReader: %v\n", err)
os.Remove(outFile.Name())
return errors.Wrapf(err, "unable to write to file")
}
err = outFile.Sync()
return err
}
// UnArchiveTar unarchives a tar file and streams its files
// using the specified io.Reader to the specified destination.
func UnArchiveTar(reader io.Reader, destDir string, arg ...string) error {
klog.V(1).Infof("begin untar to %s...\n", destDir)
var tarOptions string
var args = arg
if len(arg) > 0 {
tarOptions = arg[0]
args = arg[1:]
}
options := fmt.Sprintf("-%s%s", tarOptions, "xvC")
untar := exec.Command("/usr/bin/tar", options, destDir, strings.Join(args, ""))
untar.Stdin = reader
var errBuf bytes.Buffer
untar.Stderr = &errBuf
err := untar.Start()
if err != nil {
return err
}
err = untar.Wait()
if err != nil {
klog.V(3).Infof("%s\n", string(errBuf.Bytes()))
klog.Errorf("%s\n", err.Error())
return err
}
return nil
}
// UnArchiveLocalTar unarchives a local tar file to the specified destination.
func UnArchiveLocalTar(filePath, destDir string, arg ...string) error {
file, err := os.Open(filePath)
if err != nil {
return errors.Wrap(err, "could not open tar file")
}
fileReader := bufio.NewReader(file)
return UnArchiveTar(fileReader, destDir, arg...)
}
// CopyFile copies a file from one location to another.
func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
// WriteTerminationMessage writes the passed in message to the default termination message file
func WriteTerminationMessage(message string) error {
return WriteTerminationMessageToFile(common.PodTerminationMessageFile, message)
}
// WriteTerminationMessageToFile writes the passed in message to the passed in message file
func WriteTerminationMessageToFile(file, message string) error {
// Only write the first line of the message.
scanner := bufio.NewScanner(strings.NewReader(message))
if scanner.Scan() {
err := ioutil.WriteFile(file, []byte(scanner.Text()), os.ModeAppend)
if err != nil {
return errors.Wrap(err, "could not create termination message file")
}
}
return nil
}
// CopyDir copies a dir from one location to another.
func CopyDir(source string, dest string) (err error) {
// get properties of source dir
sourceinfo, err := os.Stat(source)
if err != nil {
return err
}
// create dest dir
err = os.MkdirAll(dest, sourceinfo.Mode())
if err != nil {
return err
}
directory, _ := os.Open(source)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
src := filepath.Join(source, obj.Name())
dst := filepath.Join(dest, obj.Name())
if obj.IsDir() {
// create sub-directories - recursively
err = CopyDir(src, dst)
if err != nil {
fmt.Println(err)
}
} else {
// perform copy
err = CopyFile(src, dst)
if err != nil {
fmt.Println(err)
}
}
}
return
}