/
clustervalidator.go
104 lines (93 loc) 路 4.12 KB
/
clustervalidator.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
package framework
import (
"context"
"fmt"
"time"
machinerytypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/controller/clientutil"
"github.com/aws/eks-anywhere/pkg/retrier"
clusterf "github.com/aws/eks-anywhere/test/framework/cluster"
"github.com/aws/eks-anywhere/test/framework/cluster/validations"
)
func validationsForExpectedObjects() []clusterf.StateValidation {
mediumRetier := retrier.New(10 * time.Minute)
longRetier := retrier.New(30 * time.Minute)
return []clusterf.StateValidation{
clusterf.RetriableStateValidation(mediumRetier, validations.ValidateEKSAObjects),
clusterf.RetriableStateValidation(longRetier, validations.ValidateControlPlaneNodes),
clusterf.RetriableStateValidation(longRetier, validations.ValidateWorkerNodes),
clusterf.RetriableStateValidation(mediumRetier, validations.ValidateCilium),
// This should be checked last as the Cluster should only be ready after all the other validations pass.
clusterf.RetriableStateValidation(mediumRetier, validations.ValidateClusterReady),
}
}
func validationsForClusterDoesNotExist() []clusterf.StateValidation {
return []clusterf.StateValidation{
clusterf.RetriableStateValidation(retrier.NewWithMaxRetries(120, 5*time.Second), validations.ValidateClusterDoesNotExist),
}
}
func (e *ClusterE2ETest) buildClusterStateValidationConfig(ctx context.Context) {
managementClusterClient, err := buildClusterClient(e.managementKubeconfigFilePath())
if err != nil {
e.T.Fatalf("failed to create management cluster client: %s", err)
}
clusterClient := managementClusterClient
if e.managementKubeconfigFilePath() != e.KubeconfigFilePath() {
clusterClient, err = buildClusterClient(e.KubeconfigFilePath())
}
if err != nil {
e.T.Fatalf("failed to create cluster client: %s", err)
}
spec, err := buildClusterSpec(ctx, managementClusterClient, e.ClusterConfig)
if err != nil {
e.T.Fatalf("failed to build cluster spec with kubeconfig %s: %v", e.KubeconfigFilePath(), err)
}
e.clusterStateValidationConfig = &clusterf.StateValidationConfig{
ClusterClient: clusterClient,
ManagementClusterClient: managementClusterClient,
ClusterSpec: spec,
}
}
func newClusterStateValidator(config *clusterf.StateValidationConfig) *clusterf.StateValidator {
return clusterf.NewStateValidator(*config)
}
func buildClusterClient(kubeconfigFileName string) (client.Client, error) {
var clusterClient client.Client
// Adding the retry logic here because the connection to the client does not always
// succedd on the first try due to connection failure after the kubeconfig becomes
// available in the cluster.
err := retrier.Retry(12, 5*time.Second, func() error {
c, err := kubernetes.NewRuntimeClientFromFileName(kubeconfigFileName)
if err != nil {
return fmt.Errorf("failed to build cluster client: %v", err)
}
clusterClient = c
return nil
})
return clusterClient, err
}
func buildClusterSpec(ctx context.Context, client client.Client, config *cluster.Config) (*cluster.Spec, error) {
clusterConfig := config.DeepCopy()
// The cluster config built by the test does not have certain defaults like the bundle reference,
// so fetch that information from the cluster if missing. This is needed inorder to build the cluster spec.
if clusterConfig.Cluster.Spec.BundlesRef == nil {
clus := &v1alpha1.Cluster{}
key := machinerytypes.NamespacedName{Namespace: clusterConfig.Cluster.Namespace, Name: clusterConfig.Cluster.Name}
if err := client.Get(ctx, key, clus); err != nil {
return nil, fmt.Errorf("failed to get cluster to build spec: %s", err)
}
clusterConfig.Cluster.Spec.BundlesRef = clus.Spec.BundlesRef
if clusterConfig.Cluster.Spec.BundlesRef == nil {
clusterConfig.Cluster.Spec.EksaVersion = clus.Spec.EksaVersion
}
}
spec, err := cluster.BuildSpecFromConfig(ctx, clientutil.NewKubeClient(client), clusterConfig)
if err != nil {
return nil, fmt.Errorf("failed to build cluster spec from config: %s", err)
}
return spec, nil
}