-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate cluster and machine objects
- Loading branch information
1 parent
cbc610d
commit eb250f5
Showing
5 changed files
with
434 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
|
||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common" | ||
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" | ||
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset" | ||
) | ||
|
||
func ValidateClusterAPIObjects(client *clientset.Clientset, clusterName string, namespace string) error { | ||
fmt.Printf("Validating cluster API objects in namespace %q.\n", namespace) | ||
|
||
cluster, err := getClusterObject(client, clusterName, namespace) | ||
if err != nil { | ||
return err | ||
} | ||
if err := validateClusterObject(cluster); err != nil { | ||
return err | ||
} | ||
|
||
machines, err := client.ClusterV1alpha1().Machines(namespace).List(meta_v1.ListOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get the machines from the apiserver in namespace %q: %v", namespace, err) | ||
} | ||
if err = validateMachineObjects(machines); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getClusterObject(client *clientset.Clientset, clusterName string, namespace string) (*v1alpha1.Cluster, error) { | ||
if clusterName != "" { | ||
cluster, err := client.ClusterV1alpha1().Clusters(namespace).Get(clusterName, meta_v1.GetOptions{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get the cluster %q from the apiserver in namespace %q: %v", clusterName, namespace, err) | ||
} | ||
return cluster, nil | ||
} | ||
|
||
clusters, err := client.ClusterV1alpha1().Clusters(namespace).List(meta_v1.ListOptions{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get the clusters from the apiserver in namespace %q: %v", namespace, err) | ||
} | ||
if numOfClusters := len(clusters.Items); numOfClusters == 0 { | ||
return nil, fmt.Errorf("Fail: No cluster exists in namespace %q.", namespace) | ||
} else if numOfClusters > 1 { | ||
return nil, fmt.Errorf("Fail: There are more than one cluster in namespace %q. Please specify --cluster-name.", namespace) | ||
} | ||
return &clusters.Items[0], nil | ||
} | ||
|
||
func validateClusterObject(cluster *v1alpha1.Cluster) error { | ||
fmt.Printf("Checking cluster object %q... ", cluster.Name) | ||
if cluster.Status.ErrorReason != "" || cluster.Status.ErrorMessage != "" { | ||
fmt.Printf("FAIL\n") | ||
fmt.Printf("\t[%v]: %s\n", cluster.Status.ErrorReason, cluster.Status.ErrorMessage) | ||
return fmt.Errorf("Failed to validate the cluster %q.", cluster.Name) | ||
} | ||
fmt.Printf("PASS\n") | ||
return nil | ||
} | ||
|
||
func validateMachineObjects(machines *v1alpha1.MachineList) error { | ||
pass := true | ||
for _, machine := range machines.Items { | ||
fmt.Printf("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.Printf("FAIL\n") | ||
fmt.Printf("\t[%v]: %s\n", reason, message) | ||
pass = false | ||
continue | ||
} | ||
if machine.Status.NodeRef == nil { | ||
fmt.Printf("FAIL\n") | ||
fmt.Printf("\tThe corresponding node does not exist.\n") | ||
pass = false | ||
continue | ||
} | ||
fmt.Printf("PASS\n") | ||
} | ||
if !pass { | ||
return fmt.Errorf("Failed to validate machine objects.") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.