-
Notifications
You must be signed in to change notification settings - Fork 254
/
main.go
133 lines (108 loc) · 3.26 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
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"kubevirt.io/containerized-data-importer/pkg/util"
"kubevirt.io/containerized-data-importer/tests/utils"
)
const (
serviceName = "cdi-docker-registry-host"
configMapName = serviceName + "-certs"
certFile = "domain.crt"
keyFile = "domain.key"
)
func main() {
certDir := flag.String("certDir", "", "")
inFile := flag.String("inFile", "", "")
outDir := flag.String("outDir", "", "")
klog.InitFlags(nil)
flag.Parse()
ft := &formatTable{
[]string{""},
[]string{".tar"},
[]string{".tar", ".gz"},
[]string{".qcow2"},
}
if err := ft.generateFiles(*inFile, *outDir); err != nil {
klog.Fatal(errors.Wrapf(err, "generating files from %s to %s' errored: ", *inFile, *outDir))
}
if err := utils.CreateCertForTestService(util.GetNamespace(), serviceName, configMapName, *certDir, certFile, keyFile); err != nil {
klog.Fatal(errors.Wrapf(err, "populate certificate directory %s' errored: ", *certDir))
}
}
func (ft formatTable) generateFiles(inFile, outDir string) error {
klog.Info("Generating test files")
if err := os.MkdirAll(outDir, 0777); err != nil {
return err
}
if err := ft.initializeTestFiles(inFile, outDir); err != nil {
return err
}
klog.Info("File initialization completed without error")
return nil
}
type formatTable [][]string
func (ft formatTable) initializeTestFiles(inFile, outDir string) error {
sem := make(chan bool, 2)
errChan := make(chan error, len(ft))
reportError := func(err error, msg string, format ...interface{}) {
e := errors.Wrapf(err, msg, format...)
klog.Error(e)
errChan <- e
}
for _, fList := range ft {
sem <- true
go func(i, o string, f []string) {
defer func() { <-sem }()
klog.Infof("Generating file %s\n", f)
ext := strings.Join(f, "")
tmpDir := filepath.Join(o, "tmp"+ext)
if err := os.Mkdir(tmpDir, 0777); err != nil {
reportError(err, "Error creating temp dir %s", tmpDir)
return
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
reportError(err, "Error deleting tmp dir %s", tmpDir)
}
}()
klog.Infof("Mkdir %s\n", tmpDir)
p, err := utils.FormatTestData(i, tmpDir, f...)
if err != nil {
reportError(err, "Error formatting files")
return
}
if err = os.Rename(p, filepath.Join(o, filepath.Base(p))); err != nil {
reportError(err, "Error moving file %s to %s", p, o)
return
}
klog.Infof("Generated file %q\n", p)
}(inFile, outDir, fList)
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
close(errChan)
if len(errChan) > 0 {
for err := range errChan {
klog.Error(err)
}
return errors.New("Error(s) occurred during file conversion")
}
return nil
}