Skip to content

Commit

Permalink
commands/.../test,internal/util: use clientcmd's kubeconfig getter (#717
Browse files Browse the repository at this point in the history
)

* commands/.../test,internal/util: use clientcmd's kubeconfig getter
  • Loading branch information
AlexNPavel authored Nov 9, 2018
1 parent eace682 commit fa72147
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions commands/operator-sdk/cmd/test/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package cmdtest
import (
"bytes"
"fmt"
"os"
"strings"
"time"

k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/test"

Expand All @@ -29,7 +29,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

type testClusterConfig struct {
Expand All @@ -48,13 +47,8 @@ func NewTestClusterCmd() *cobra.Command {
Short: "Run End-To-End tests using image with embedded test binary",
RunE: testClusterFunc,
}
defaultKubeConfig := ""
homedir, ok := os.LookupEnv("HOME")
if ok {
defaultKubeConfig = homedir + "/.kube/config"
}
testCmd.Flags().StringVar(&tcConfig.namespace, "namespace", "default", "Namespace to run tests in")
testCmd.Flags().StringVar(&tcConfig.kubeconfig, "kubeconfig", defaultKubeConfig, "Kubeconfig path")
testCmd.Flags().StringVar(&tcConfig.namespace, "namespace", "", "Namespace to run tests in")
testCmd.Flags().StringVar(&tcConfig.kubeconfig, "kubeconfig", "", "Kubeconfig path")
testCmd.Flags().StringVar(&tcConfig.imagePullPolicy, "image-pull-policy", "Always", "Set test pod image pull policy. Allowed values: Always, Never")
testCmd.Flags().StringVar(&tcConfig.serviceAccount, "service-account", "default", "Service account to run tests on")
testCmd.Flags().IntVar(&tcConfig.pendingTimeout, "pending-timeout", 60, "Timeout in seconds for testing pod to stay in pending state (default 60s)")
Expand Down Expand Up @@ -98,10 +92,13 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
}},
},
}
kubeconfig, err := clientcmd.BuildConfigFromFlags("", tcConfig.kubeconfig)
kubeconfig, defaultNamespace, err := k8sInternal.GetKubeconfigAndNamespace(tcConfig.kubeconfig)
if err != nil {
return fmt.Errorf("failed to get kubeconfig: %v", err)
}
if tcConfig.namespace == "" {
tcConfig.namespace = defaultNamespace
}
kubeclient, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
return fmt.Errorf("failed to create kubeclient: %v", err)
Expand Down
7 changes: 1 addition & 6 deletions commands/operator-sdk/cmd/test/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func NewTestLocalCmd() *cobra.Command {
Short: "Run End-To-End tests locally",
Run: testLocalFunc,
}
defaultKubeConfig := ""
homedir, ok := os.LookupEnv("HOME")
if ok {
defaultKubeConfig = homedir + "/.kube/config"
}
testCmd.Flags().StringVar(&tlConfig.kubeconfig, "kubeconfig", defaultKubeConfig, "Kubeconfig path")
testCmd.Flags().StringVar(&tlConfig.kubeconfig, "kubeconfig", "", "Kubeconfig path")
testCmd.Flags().StringVar(&tlConfig.globalManPath, "global-manifest", "", "Path to manifest for Global resources (e.g. CRD manifests)")
testCmd.Flags().StringVar(&tlConfig.namespacedManPath, "namespaced-manifest", "", "Path to manifest for per-test, namespaced resources (e.g. RBAC and Operator manifest)")
testCmd.Flags().StringVar(&tlConfig.goTestFlags, "go-test-flags", "", "Additional flags to pass to go test")
Expand Down
53 changes: 53 additions & 0 deletions internal/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018 The Operator-SDK 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 k8sutil

import (
"fmt"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// GetKubeconfigAndNamespace returns the *rest.Config and default namespace defined in the
// kubeconfig at the specified path. If no path is provided, returns the default *rest.Config
// and namespace
func GetKubeconfigAndNamespace(configPath string) (*rest.Config, string, error) {
var clientConfig clientcmd.ClientConfig
var apiConfig *clientcmdapi.Config
var err error
if configPath != "" {
apiConfig, err = clientcmd.LoadFromFile(configPath)
if err != nil {
return nil, "", fmt.Errorf("failed to load user provided kubeconfig: %v", err)
}
} else {
apiConfig, err = clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return nil, "", fmt.Errorf("failed to get kubeconfig: %v", err)
}
}
clientConfig = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{})
kubeconfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, "", err
}
namespace, _, err := clientConfig.Namespace()
if err != nil {
return nil, "", err
}
return kubeconfig, namespace, nil
}
5 changes: 3 additions & 2 deletions pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"sync"
"time"

k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"

extscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand All @@ -31,7 +33,6 @@ import (
cgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ func setup(kubeconfigPath, namespacedManPath *string) error {
kubeconfig, err = rest.InClusterConfig()
*singleNamespace = true
} else {
kubeconfig, err = clientcmd.BuildConfigFromFlags("", *kubeconfigPath)
kubeconfig, _, err = k8sInternal.GetKubeconfigAndNamespace(*kubeconfigPath)
}
if err != nil {
return fmt.Errorf("failed to build the kubeconfig: %v", err)
Expand Down

0 comments on commit fa72147

Please sign in to comment.