forked from networkservicemesh/cloudtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
99 lines (85 loc) · 2.06 KB
/
validator.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
package k8s
import (
"context"
"fmt"
"time"
v1 "k8s.io/api/core/v1"
"github.com/pkg/errors"
"github.com/denis-tingajkin/cloudtest/pkg/config"
)
// KubernetesValidator - a validator to check periodically of cluster livenes.
type KubernetesValidator interface {
Validate() error
WaitValid(context context.Context) error
}
// ValidationFactory - factory to create validator
type ValidationFactory interface {
// CreateValidator - return intanceof of validator with config and cluster config
CreateValidator(config *config.ClusterProviderConfig, location string) (KubernetesValidator, error)
}
type k8sFactory struct {
}
type k8sValidator struct {
config *config.ClusterProviderConfig
location string
utils *Utils
}
func (v *k8sValidator) WaitValid(context context.Context) error {
for {
err := v.Validate()
if err == nil {
break
}
// Waiting a bit.
select {
case <-time.After(1 * time.Second):
case <-context.Done():
return err
}
}
return nil
}
func isNodeReady(node *v1.Node) bool {
conditions := node.Status.Conditions
for idx := range conditions {
if conditions[idx].Type == v1.NodeReady {
resultValue := conditions[idx].Status == v1.ConditionTrue
return resultValue
}
}
return false
}
func (v *k8sValidator) Validate() error {
requiedNodes := v.config.NodeCount
nodes, err := v.utils.GetNodes()
if err != nil {
return err
}
ready := 0
for idx := range nodes {
if isNodeReady(&nodes[idx]) {
ready++
}
}
if ready >= requiedNodes {
return nil
}
msg := fmt.Sprintf("Cluster doesn't have required number of nodes to be available. Required: %v Available: %v\n", requiedNodes, ready)
err = errors.Errorf(msg)
return err
}
func (*k8sFactory) CreateValidator(config *config.ClusterProviderConfig, location string) (KubernetesValidator, error) {
utils, err := NewK8sUtils(location)
if err != nil {
return nil, err
}
return &k8sValidator{
config: config,
location: location,
utils: utils,
}, nil
}
// CreateFactory - creates a validation factory.
func CreateFactory() ValidationFactory {
return &k8sFactory{}
}