/
utils.go
65 lines (55 loc) · 1.58 KB
/
utils.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
package controller
import (
"errors"
"fmt"
"net"
"time"
apiv1 "k8s.io/api/core/v1"
"github.com/amadeusitgroup/redis-operator/pkg/config"
"github.com/amadeusitgroup/redis-operator/pkg/redis"
)
// NewRedisAdmin builds and returns new redis.Admin from the list of pods
func NewRedisAdmin(pods []*apiv1.Pod, cfg *config.Redis) (redis.AdminInterface, error) {
nodesAddrs := []string{}
for _, pod := range pods {
redisPort := redis.DefaultRedisPort
for _, container := range pod.Spec.Containers {
if container.Name == "redis-node" {
for _, port := range container.Ports {
if port.Name == "redis" {
redisPort = fmt.Sprintf("%d", port.ContainerPort)
}
}
}
}
nodesAddrs = append(nodesAddrs, net.JoinHostPort(pod.Status.PodIP, redisPort))
}
adminConfig := redis.AdminOptions{
ConnectionTimeout: time.Duration(cfg.DialTimeout) * time.Millisecond,
RenameCommandsFile: cfg.GetRenameCommandsFile(),
}
return redis.NewAdmin(nodesAddrs, &adminConfig), nil
}
// IsPodReady check if pod is in ready condition, return the error message otherwise
func IsPodReady(pod *apiv1.Pod) (bool, error) {
if pod == nil {
return false, errors.New("No Pod")
}
// get ready condition
var readycondition apiv1.PodCondition
found := false
for _, cond := range pod.Status.Conditions {
if cond.Type == apiv1.PodReady {
readycondition = cond
found = true
break
}
}
if !found {
return false, errors.New("Cound't find ready condition")
}
if readycondition.Status != apiv1.ConditionTrue {
return false, errors.New(readycondition.Message)
}
return true, nil
}