Skip to content

Commit fa72147

Browse files
authored
commands/.../test,internal/util: use clientcmd's kubeconfig getter (#717)
* commands/.../test,internal/util: use clientcmd's kubeconfig getter
1 parent eace682 commit fa72147

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commands/operator-sdk/cmd/test/cluster.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ package cmdtest
1717
import (
1818
"bytes"
1919
"fmt"
20-
"os"
2120
"strings"
2221
"time"
2322

23+
k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2424
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2525
"github.com/operator-framework/operator-sdk/pkg/test"
2626

@@ -29,7 +29,6 @@ import (
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/util/wait"
3131
"k8s.io/client-go/kubernetes"
32-
"k8s.io/client-go/tools/clientcmd"
3332
)
3433

3534
type testClusterConfig struct {
@@ -48,13 +47,8 @@ func NewTestClusterCmd() *cobra.Command {
4847
Short: "Run End-To-End tests using image with embedded test binary",
4948
RunE: testClusterFunc,
5049
}
51-
defaultKubeConfig := ""
52-
homedir, ok := os.LookupEnv("HOME")
53-
if ok {
54-
defaultKubeConfig = homedir + "/.kube/config"
55-
}
56-
testCmd.Flags().StringVar(&tcConfig.namespace, "namespace", "default", "Namespace to run tests in")
57-
testCmd.Flags().StringVar(&tcConfig.kubeconfig, "kubeconfig", defaultKubeConfig, "Kubeconfig path")
50+
testCmd.Flags().StringVar(&tcConfig.namespace, "namespace", "", "Namespace to run tests in")
51+
testCmd.Flags().StringVar(&tcConfig.kubeconfig, "kubeconfig", "", "Kubeconfig path")
5852
testCmd.Flags().StringVar(&tcConfig.imagePullPolicy, "image-pull-policy", "Always", "Set test pod image pull policy. Allowed values: Always, Never")
5953
testCmd.Flags().StringVar(&tcConfig.serviceAccount, "service-account", "default", "Service account to run tests on")
6054
testCmd.Flags().IntVar(&tcConfig.pendingTimeout, "pending-timeout", 60, "Timeout in seconds for testing pod to stay in pending state (default 60s)")
@@ -98,10 +92,13 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
9892
}},
9993
},
10094
}
101-
kubeconfig, err := clientcmd.BuildConfigFromFlags("", tcConfig.kubeconfig)
95+
kubeconfig, defaultNamespace, err := k8sInternal.GetKubeconfigAndNamespace(tcConfig.kubeconfig)
10296
if err != nil {
10397
return fmt.Errorf("failed to get kubeconfig: %v", err)
10498
}
99+
if tcConfig.namespace == "" {
100+
tcConfig.namespace = defaultNamespace
101+
}
105102
kubeclient, err := kubernetes.NewForConfig(kubeconfig)
106103
if err != nil {
107104
return fmt.Errorf("failed to create kubeclient: %v", err)

commands/operator-sdk/cmd/test/local.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ func NewTestLocalCmd() *cobra.Command {
4949
Short: "Run End-To-End tests locally",
5050
Run: testLocalFunc,
5151
}
52-
defaultKubeConfig := ""
53-
homedir, ok := os.LookupEnv("HOME")
54-
if ok {
55-
defaultKubeConfig = homedir + "/.kube/config"
56-
}
57-
testCmd.Flags().StringVar(&tlConfig.kubeconfig, "kubeconfig", defaultKubeConfig, "Kubeconfig path")
52+
testCmd.Flags().StringVar(&tlConfig.kubeconfig, "kubeconfig", "", "Kubeconfig path")
5853
testCmd.Flags().StringVar(&tlConfig.globalManPath, "global-manifest", "", "Path to manifest for Global resources (e.g. CRD manifests)")
5954
testCmd.Flags().StringVar(&tlConfig.namespacedManPath, "namespaced-manifest", "", "Path to manifest for per-test, namespaced resources (e.g. RBAC and Operator manifest)")
6055
testCmd.Flags().StringVar(&tlConfig.goTestFlags, "go-test-flags", "", "Additional flags to pass to go test")

internal/util/k8sutil/k8sutil.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package k8sutil
16+
17+
import (
18+
"fmt"
19+
20+
"k8s.io/client-go/rest"
21+
"k8s.io/client-go/tools/clientcmd"
22+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
23+
)
24+
25+
// GetKubeconfigAndNamespace returns the *rest.Config and default namespace defined in the
26+
// kubeconfig at the specified path. If no path is provided, returns the default *rest.Config
27+
// and namespace
28+
func GetKubeconfigAndNamespace(configPath string) (*rest.Config, string, error) {
29+
var clientConfig clientcmd.ClientConfig
30+
var apiConfig *clientcmdapi.Config
31+
var err error
32+
if configPath != "" {
33+
apiConfig, err = clientcmd.LoadFromFile(configPath)
34+
if err != nil {
35+
return nil, "", fmt.Errorf("failed to load user provided kubeconfig: %v", err)
36+
}
37+
} else {
38+
apiConfig, err = clientcmd.NewDefaultClientConfigLoadingRules().Load()
39+
if err != nil {
40+
return nil, "", fmt.Errorf("failed to get kubeconfig: %v", err)
41+
}
42+
}
43+
clientConfig = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{})
44+
kubeconfig, err := clientConfig.ClientConfig()
45+
if err != nil {
46+
return nil, "", err
47+
}
48+
namespace, _, err := clientConfig.Namespace()
49+
if err != nil {
50+
return nil, "", err
51+
}
52+
return kubeconfig, namespace, nil
53+
}

pkg/test/framework.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"sync"
2323
"time"
2424

25+
k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
26+
2527
extscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
2628
"k8s.io/apimachinery/pkg/runtime"
2729
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -31,7 +33,6 @@ import (
3133
cgoscheme "k8s.io/client-go/kubernetes/scheme"
3234
"k8s.io/client-go/rest"
3335
"k8s.io/client-go/restmapper"
34-
"k8s.io/client-go/tools/clientcmd"
3536
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
3637
)
3738

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

0 commit comments

Comments
 (0)