Skip to content

Commit

Permalink
Merge pull request #7 from roberthbailey/unit-tests
Browse files Browse the repository at this point in the history
Update clusterctl validation to use the dynamic client
  • Loading branch information
Phillip Wittrock committed Sep 26, 2018
2 parents a46811c + 576e85d commit 4eeba4c
Show file tree
Hide file tree
Showing 51 changed files with 330 additions and 1,338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
)

// This is a convienience method to prevent the need of importing both this version of clientcmd and the client-go version
// This is a convenience method to prevent the need of importing both this version of clientcmd and the client-go version
func NewConfigOverrides() clientcmd.ConfigOverrides {
return clientcmd.ConfigOverrides{}
}
Expand All @@ -43,15 +43,6 @@ func NewCoreClientSetForDefaultSearchPath(kubeconfigPath string, overrides clien
return kubernetes.NewForConfig(config)
}

// NewCoreClientSetForKubeconfig creates a core kubernetes clientset for the given kubeconfig string.
func NewCoreClientSetForKubeconfig(kubeconfig string) (*kubernetes.Clientset, error) {
config, err := newRestConfigForKubeconfig(kubeconfig)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}

// NewClusterApiClientForDefaultSearchPath creates a Cluster API clientset. If the kubeconfigPath is specified then the configuration is loaded from that path.
// Otherwise the default kubeconfig search path is used.
// The overrides parameter is used to select a specific context of the config, for example, select the context with a given cluster name or namespace.
Expand All @@ -63,15 +54,6 @@ func NewClusterApiClientForDefaultSearchPath(kubeconfigPath string, overrides cl
return clientset.NewForConfig(config)
}

// NewClusterApiClientForKubeconfig creates a Cluster API clientset for the given kubeconfig string.
func NewClusterApiClientForKubeconfig(kubeconfig string) (*clientset.Clientset, error) {
config, err := newRestConfigForKubeconfig(kubeconfig)
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}

// NewClientsForDefaultSearchpath creates both a core kubernetes clientset and a cluster-api clientset. If the kubeconfigPath
// is specified then the configuration is loaded from that path. Otherwise the default kubeconfig search path is used.
// The overrides parameter is used to select a specific context of the config, for example, select the context with a given cluster name or namespace.
Expand Down
40 changes: 0 additions & 40 deletions cmd/clusterctl/clusterdeployer/BUILD.bazel

This file was deleted.

14 changes: 0 additions & 14 deletions cmd/clusterctl/clusterdeployer/bootstrap/existing/BUILD.bazel

This file was deleted.

15 changes: 0 additions & 15 deletions cmd/clusterctl/clusterdeployer/bootstrap/minikube/BUILD.bazel

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/clusterctl/clusterdeployer/clientfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package clusterdeployer

import (
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/cluster-api/pkg/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/clientcmd"
)

type clientFactory struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/clusterdeployer/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tcmd "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/clientcmd"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
"sigs.k8s.io/cluster-api/pkg/clientcmd"
"sigs.k8s.io/cluster-api/pkg/util"

"github.com/golang/glog"
Expand Down
40 changes: 0 additions & 40 deletions cmd/clusterctl/cmd/BUILD.bazel

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/clusterctl/cmd/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (

"k8s.io/api/core/v1"
tcmd "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/clusterdeployer"
"sigs.k8s.io/cluster-api/cmd/clusterctl/clusterdeployer/bootstrap/minikube"
"sigs.k8s.io/cluster-api/cmd/clusterctl/providercomponents"
"sigs.k8s.io/cluster-api/pkg/clientcmd"

"github.com/golang/glog"
"github.com/spf13/cobra"
Expand Down
28 changes: 18 additions & 10 deletions cmd/clusterctl/cmd/validate_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package cmd
import (
"fmt"
"os"
"sigs.k8s.io/cluster-api/pkg/apis"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/spf13/cobra"

tcmd "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/cluster-api/cmd/clusterctl/validation"
"sigs.k8s.io/cluster-api/pkg/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

type ValidateClusterOptions struct {
Kubeconfig string
KubeconfigOverrides tcmd.ConfigOverrides
}

Expand All @@ -47,25 +50,30 @@ var validateClusterCmd = &cobra.Command{
}

func init() {
validateClusterCmd.Flags().StringVarP(
&vco.Kubeconfig, "kubeconfig", "", "",
"The file path of the kubeconfig file for the cluster to validate.. If not specified, $KUBECONFIG environment variable or ${HOME}/.kube/config is used.")
// BindContextFlags will bind the flags cluster, namespace, and user
tcmd.BindContextFlags(&vco.KubeconfigOverrides.Context, validateClusterCmd.Flags(), tcmd.RecommendedContextOverrideFlags(""))
validateCmd.AddCommand(validateClusterCmd)
}

func RunValidateCluster() error {
clusterApiClient, err := clientcmd.NewClusterApiClientForDefaultSearchPath(vco.Kubeconfig, vco.KubeconfigOverrides)
cfg, err := config.GetConfig()
if err != nil {
return fmt.Errorf("failed to create cluster API client: %v", err)
return fmt.Errorf("failed to create client configuration: %v", err)
}
k8sClient, err := clientcmd.NewCoreClientSetForDefaultSearchPath(vco.Kubeconfig, vco.KubeconfigOverrides)
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
return fmt.Errorf("failed to create kubernetes client: %v", err)
return fmt.Errorf("failed to create manager: %v", err)
}
// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
return fmt.Errorf("failed to add APIs to manager: %v", err)
}

if err = validation.ValidateClusterAPIObjects(os.Stdout, clusterApiClient, k8sClient, vco.KubeconfigOverrides.Context.Cluster, vco.KubeconfigOverrides.Context.Namespace); err != nil {
c, err := client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()})
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
if err = validation.ValidateClusterAPIObjects(os.Stdout, c, vco.KubeconfigOverrides.Context.Cluster, vco.KubeconfigOverrides.Context.Namespace); err != nil {
return err
}

Expand Down
29 changes: 0 additions & 29 deletions cmd/clusterctl/providercomponents/BUILD.bazel

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/clusterctl/validation/BUILD.bazel

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Validating Cluster API objects in namespace "validate-cluster-api-object-output"
Validating Cluster API objects in namespace "validate-cluster-objects-errors"
Checking cluster object "test-cluster"... FAIL
[CreateError]: Failed to create cluster
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Validating Cluster API objects in namespace "validate-cluster-api-object-output"
Validating Cluster API objects in namespace "validate-machine-objects-errors"
Checking cluster object "test-cluster"... PASS
Checking machine object "test-machine1"... FAIL
[CreateError]: Failed to create machine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Validating Cluster API objects in namespace "validate-cluster-api-object-output"
Validating Cluster API objects in namespace "validate-machine-objects-node-ref-errors"
Checking cluster object "test-cluster"... PASS
Checking machine object "test-machine1"... FAIL
The corresponding node "test-node-not-ready" is not ready.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Validating Cluster API objects in namespace "validate-cluster-api-object-output"
Validating Cluster API objects in namespace "validate-cluster-objects"
Checking cluster object "test-cluster"... PASS
Checking machine object "test-machine1"... PASS
Checking machine object "test-machine2"... PASS
Loading

0 comments on commit 4eeba4c

Please sign in to comment.