forked from kubernetes-sigs/cluster-api
/
validate_cluster_api_objects.go
135 lines (118 loc) · 4.59 KB
/
validate_cluster_api_objects.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
130
131
132
133
134
135
/*
Copyright 2018 The Kubernetes Authors.
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 validation
import (
"context"
"fmt"
"io"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/controller/noderefutil"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func ValidateClusterAPIObjects(ctx context.Context, w io.Writer, c client.Client, clusterName string, namespace string) error {
fmt.Fprintf(w, "Validating Cluster API objects in namespace %q\n", namespace)
cluster, err := getClusterObject(ctx, c, clusterName, namespace)
if err != nil {
return err
}
if err := validateClusterObject(w, cluster); err != nil {
return err
}
machines := &clusterv1alpha1.MachineList{}
if err := c.List(ctx, client.InNamespace(namespace), machines); err != nil {
return errors.Wrapf(err, "failed to get the machines from the apiserver in namespace %q", namespace)
}
return validateMachineObjects(ctx, w, machines, c)
}
func getClusterObject(ctx context.Context, c client.Reader, clusterName string, namespace string) (*v1alpha1.Cluster, error) {
if clusterName != "" {
cluster := &clusterv1alpha1.Cluster{}
err := c.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, cluster)
return cluster, err
}
clusters := &clusterv1alpha1.ClusterList{}
if err := c.List(ctx, &client.ListOptions{Namespace: namespace}, clusters); err != nil {
return nil, errors.Wrapf(err, "failed to get the clusters from the apiserver in namespace %q", namespace)
}
if numOfClusters := len(clusters.Items); numOfClusters == 0 {
return nil, errors.Errorf("fail: No cluster exists in namespace %q", namespace)
} else if numOfClusters > 1 {
return nil, errors.Errorf("fail: There is more than one cluster in namespace %q. Please specify --cluster-name", namespace)
}
return &clusters.Items[0], nil
}
func validateClusterObject(w io.Writer, cluster *v1alpha1.Cluster) error {
fmt.Fprintf(w, "Checking cluster object %q... ", cluster.Name)
if cluster.Status.ErrorReason != "" || cluster.Status.ErrorMessage != "" {
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\t[%v]: %s\n", cluster.Status.ErrorReason, cluster.Status.ErrorMessage)
return errors.Errorf("cluster %q failed the validation", cluster.Name)
}
fmt.Fprintf(w, "PASS\n")
return nil
}
func validateMachineObjects(ctx context.Context, w io.Writer, machines *v1alpha1.MachineList, client client.Client) error {
pass := true
for _, machine := range machines.Items {
if !validateMachineObject(ctx, w, machine, client) {
pass = false
}
}
if !pass {
return errors.Errorf("machine objects failed the validation")
}
return nil
}
func validateMachineObject(ctx context.Context, w io.Writer, machine v1alpha1.Machine, client client.Client) bool {
fmt.Fprintf(w, "Checking machine object %q... ", machine.Name)
if machine.Status.ErrorReason != nil || machine.Status.ErrorMessage != nil {
var reason common.MachineStatusError
if machine.Status.ErrorReason != nil {
reason = *machine.Status.ErrorReason
}
var message string
if machine.Status.ErrorMessage != nil {
message = *machine.Status.ErrorMessage
}
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\t[%v]: %s\n", reason, message)
return false
}
if machine.Status.NodeRef == nil {
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\tThe corresponding node is missing.\n")
return false
}
if err := validateReferredNode(ctx, machine.Status.NodeRef.Name, client); err != nil {
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\t%v\n", err)
return false
}
fmt.Fprintf(w, "PASS\n")
return true
}
func validateReferredNode(ctx context.Context, nodeName string, client client.Client) error {
node := &corev1.Node{}
if err := client.Get(ctx, types.NamespacedName{Name: nodeName}, node); err != nil {
return errors.Wrapf(err, "the corresponding node %q is not found", nodeName)
}
if !noderefutil.IsNodeReady(node) {
return errors.Errorf("the corresponding node %q is not ready", nodeName)
}
return nil
}