-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathutils.go
More file actions
129 lines (110 loc) · 2.98 KB
/
utils.go
File metadata and controls
129 lines (110 loc) · 2.98 KB
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
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
"github.com/percona/percona-server-mysql-operator/pkg/k8s"
"github.com/percona/percona-server-mysql-operator/pkg/mysql"
"github.com/percona/percona-server-mysql-operator/pkg/naming"
)
func getFQDN(svcName string) (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", errors.Wrap(err, "get hostname")
}
namespace, err := k8s.DefaultAPINamespace()
if err != nil {
return "", errors.Wrap(err, "get namespace")
}
return fmt.Sprintf("%s.%s.%s", hostname, svcName, namespace), nil
}
func getReadTimeout() (uint32, error) {
s, ok := os.LookupEnv(naming.EnvBootstrapReadTimeout)
if !ok {
return 0, nil
}
readTimeout, err := strconv.Atoi(s)
if err != nil {
return 0, errors.Wrap(err, "failed to parse BOOTSTRAP_READ_TIMEOUT")
}
if readTimeout < 0 {
return 0, errors.New("BOOTSTRAP_READ_TIMEOUT should be a positive value")
}
return uint32(readTimeout), nil
}
func getSecret(username apiv1alpha1.SystemUser) (string, error) {
path := filepath.Join(mysql.CredsMountPath, string(username))
sBytes, err := os.ReadFile(path)
if err != nil {
return "", errors.Wrapf(err, "read %s", path)
}
return strings.TrimSpace(string(sBytes)), nil
}
func getPodIP(hostname string) (string, error) {
addrs, err := net.LookupHost(hostname)
if err != nil {
return "", errors.Wrapf(err, "lookup %s", hostname)
}
log.Println("lookup", hostname, addrs)
return addrs[0], nil
}
func lookup(svcName string) (sets.Set[string], error) {
endpoints := sets.New[string]()
_, srvRecords, err := net.LookupSRV("", "", svcName)
if err != nil {
return endpoints, err
}
for _, srvRecord := range srvRecords {
// The SRV records have the pattern $HOSTNAME.$SERVICE.$.NAMESPACE.svc.$CLUSTER_DNS_SUFFIX
// We only want $HOSTNAME.$SERVICE.$NAMESPACE because in the `selectDonor` function we
// compare the list generated here with the output of the `getFQDN` function
srv := strings.Split(srvRecord.Target, ".")
ep := strings.Join(srv[:3], ".")
endpoints.Insert(ep)
}
return endpoints, nil
}
func lockExists(lockName string) (bool, error) {
return fileExists(fmt.Sprintf("/var/lib/mysql/%s.lock", lockName))
}
func fileExists(name string) (bool, error) {
_, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrap(err, "os stat")
}
return true, nil
}
func waitLockRemoval(lockName string) error {
for {
exists, err := lockExists(lockName)
if err != nil {
return err
}
time.Sleep(time.Second)
if !exists {
return nil
}
}
}
func createFile(name, content string) error {
f, err := os.Create(name)
if err != nil {
return errors.Wrapf(err, "create %s", name)
}
_, err = f.WriteString(content)
if err != nil {
return errors.Wrapf(err, "write to %s", name)
}
return nil
}