-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathutil.go
129 lines (99 loc) · 3.11 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
/*
Copyright 2021 RadonDB.
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 sidecar
import (
"io"
"os"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)
var (
log = logf.Log.WithName("sidecar")
// MysqlServerIDOffset represents the offset with which all server ids are shifted from 0
mysqlServerIDOffset = 100
// mysqlConfigPath is the mysql configs path.
mysqlConfigPath = utils.MysqlConfVolumeMountPath
// xenonConfigPath is the xenon meta path.
xenonConfigPath = utils.XenonMetaVolumeMountPath
// clientConfPath is the client.cnf path.
clientConfPath = utils.ConfClientPath
// extraConfPath is the mysql extra configs path.
extraConfPath = utils.MysqlConfVolumeMountPath + "/conf.d"
// mysqlCMPath is the mounted configmap.
mysqlCMPath = utils.MysqlCMVolumeMountPath
// xenonCMPath is the mounted configmap.
xenonCMPath = utils.XenonCMVolumeMountPath
// dataPath is the mysql data path.
dataPath = utils.DataVolumeMountPath
// scriptsPath is the scripts path used for xenon.
scriptsPath = utils.ScriptsVolumeMountPath
// sysPath is the linux kernel path used for install tokudb.
sysPath = utils.SysVolumeMountPath
// xenonPath is the xenon configs path.
xenonPath = utils.XenonConfVolumeMountPath
// initFilePath is the init files path for mysql.
initFilePath = utils.InitFileVolumeMountPath
// xtrabackupCommand is the backup tool file name.
xtrabackupCommand = "xtrabackup"
// xcloudCommand is the upload tool file name.
xcloudCommand = "xbcloud"
// clone restore init data directory.
backupInitDirectory = "initbackup"
)
// copyFile the src file to dst.
// nolint: gosec
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer func() {
if err1 := in.Close(); err1 != nil {
log.Error(err1, "failed to close source file", "src_file", src)
}
}()
out, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
if err1 := out.Close(); err1 != nil {
log.Error(err1, "failed to close destination file", "dest_file", dst)
}
}()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return nil
}
// getEnvValue get environment variable by the key.
func getEnvValue(key string) string {
value := os.Getenv(key)
if len(value) == 0 {
log.Info("environment is not set", "key", key)
}
return value
}
// checkIfPathExists check if the path exists.
func checkIfPathExists(path string) (bool, error) {
f, err := os.Open(path)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
log.Error(err, "failed to open file", "file", path)
return false, err
}
err = f.Close()
return true, err
}