Skip to content

Commit

Permalink
Add Kubernetes cluster validation (kubernetes-sigs#679)
Browse files Browse the repository at this point in the history
* Implement validate kube cluster objects

* Add validate kube cluster in validate cluster command

* Pass namespace and add more conditions to fail validate pods

* Add header to validate pods file

* Refactor validate pods to write test easier

* Add unit test for validate pods

* Remove cluster name from validate pods in validate command handler

* Update bazel file for cluster validation

* Refactor to collect pod failures and fail at the end

* Remove unecessary prints and pass kube-system with metav1

* Print all pod failures when found

* Cleanup and remove unnecessary test

* Add test to cover looping functionality with N pods

* Add validate components into the validation workflow
  • Loading branch information
girikuncoro authored and k8s-ci-robot committed Feb 27, 2019
1 parent b0b14bc commit fab4c07
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cmd/clusterctl/cmd/validate_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tcmd "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/validation"
"sigs.k8s.io/cluster-api/pkg/apis"
Expand Down Expand Up @@ -77,7 +78,9 @@ func RunValidateCluster() error {
if err := validation.ValidateClusterAPIObjects(context.TODO(), os.Stdout, c, vco.KubeconfigOverrides.Context.Cluster, vco.KubeconfigOverrides.Context.Namespace); err != nil {
return err
}
if err := validation.ValidatePods(context.TODO(), os.Stdout, c, metav1.NamespaceSystem); err != nil {
return err
}

// TODO(wangzhen127): Also validate the cluster in addition to the cluster API objects. https://github.com/kubernetes-sigs/cluster-api/issues/168
return nil
}
8 changes: 7 additions & 1 deletion cmd/clusterctl/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["validate_cluster_api_objects.go"],
srcs = [
"validate_cluster_api_objects.go",
"validate_pods.go",
],
importpath = "sigs.k8s.io/cluster-api/cmd/clusterctl/validation",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/cluster/common:go_default_library",
"//pkg/apis/cluster/v1alpha1:go_default_library",
"//pkg/controller/noderefutil:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/golang.org/x/net/context:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
Expand All @@ -21,6 +25,7 @@ go_test(
srcs = [
"validate_cluster_api_objects_suite_test.go",
"validate_cluster_api_objects_test.go",
"validate_pods_test.go",
],
data = glob(["testdata/**"]),
embed = [":go_default_library"],
Expand All @@ -29,6 +34,7 @@ go_test(
"//pkg/apis/cluster/common:go_default_library",
"//pkg/apis/cluster/v1alpha1:go_default_library",
"//pkg/apis/cluster/v1alpha1/testutil:go_default_library",
"//vendor/golang.org/x/net/context:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
Expand Down
146 changes: 146 additions & 0 deletions cmd/clusterctl/validation/validate_pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright 2019 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"
"strings"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type validationError struct {
name string
message string
}

func ValidatePods(ctx context.Context, w io.Writer, c client.Client, namespace string) error {
fmt.Fprintf(w, "Validating pods in namespace %q\n", namespace)

pods, err := getPods(ctx, c, namespace)
if err != nil {
return err
}
if err := validatePods(w, pods, namespace); err != nil {
return err
}

components, err := getComponents(ctx, c)
if err != nil {
return err
}
return validateComponents(w, components)
}

func getPods(ctx context.Context, c client.Client, namespace string) (*corev1.PodList, error) {
pods := &corev1.PodList{}
if err := c.List(ctx, client.InNamespace(namespace), pods); err != nil {
return nil, fmt.Errorf("failed to get pods in namespace %q: %v", namespace, err)
}
return pods, nil
}

func validatePods(w io.Writer, pods *corev1.PodList, namespace string) error {
if len(pods.Items) == 0 {
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\tpods in namespace %q not exist.\n", namespace)
return fmt.Errorf("pods in namespace %q not exist", namespace)
}

var failures []*validationError
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodSucceeded {
continue
}

if pod.Status.Phase == corev1.PodPending ||
pod.Status.Phase == corev1.PodFailed ||
pod.Status.Phase == corev1.PodUnknown {
failures = append(failures, &validationError{
name: fmt.Sprintf("%q/%q", pod.Namespace, pod.Name),
message: fmt.Sprintf("Pod %q in namespace %q is %s.", pod.Name, pod.Namespace, pod.Status.Phase),
})
continue
}

var notready []string
for _, container := range pod.Status.ContainerStatuses {
if !container.Ready {
notready = append(notready, container.Name)
}
}
if len(notready) != 0 {
failures = append(failures, &validationError{
name: fmt.Sprintf("%q/%q", pod.Namespace, pod.Name),
message: fmt.Sprintf("Pod %q in namespace %q is not ready (%s).", pod.Name, pod.Namespace, strings.Join(notready, ",")),
})
}
}

if len(failures) != 0 {
fmt.Fprintf(w, "FAIL\n")
for _, failure := range failures {
fmt.Fprintf(w, "\t[%v]: %s\n", failure.name, failure.message)
}
return fmt.Errorf("pod failures in namespace %q found", namespace)
}

fmt.Fprintf(w, "PASS\n")
return nil
}

func getComponents(ctx context.Context, c client.Client) (*corev1.ComponentStatusList, error) {
components := &corev1.ComponentStatusList{}
if err := c.List(ctx, &client.ListOptions{}, components); err != nil {
return nil, err
}
return components, nil
}

func validateComponents(w io.Writer, components *corev1.ComponentStatusList) error {
if len(components.Items) == 0 {
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\tcomponents not exist.\n")
return fmt.Errorf("components not exist")
}

var failures []*validationError
for _, component := range components.Items {
for _, condition := range component.Conditions {
if condition.Status != corev1.ConditionTrue {
failures = append(failures, &validationError{
name: fmt.Sprintf("%q", component.Name),
message: fmt.Sprintf("Component %q is not healthy", component.Name),
})
}
}
}

if len(failures) != 0 {
fmt.Fprintf(w, "FAIL\n")
for _, failure := range failures {
fmt.Fprintf(w, "\t[%v]: %s\n", failure.name, failure.message)
}
return fmt.Errorf("component failures found")
}

fmt.Fprintf(w, "PASS\n")
return nil
}
Loading

0 comments on commit fab4c07

Please sign in to comment.