Skip to content

Commit f604b8a

Browse files
authored
fix bugs around kube config path flags (#497)
Signed-off-by: Caleb Boylan <[email protected]>
1 parent a42f353 commit f604b8a

File tree

9 files changed

+23
-60
lines changed

9 files changed

+23
-60
lines changed

pkg/cmd/get/clusters.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ func populateClusterList() ([]idpTypes.Cluster, error) {
6363
return nil, err
6464
}
6565

66-
kubeConfig, err := helpers.GetKubeConfig()
66+
kubeConfig, err := util.GetKubeConfig()
6767
if err != nil {
6868
return nil, err
6969
}
7070

71-
// TODO: Check if we need it or not like also if the new code handle the kubeconfig path passed as parameter
72-
_, err = helpers.GetKubeClient(kubeConfig)
71+
_, err = util.GetKubeClient(kubeConfig)
7372
if err != nil {
7473
return nil, err
7574
}
7675

77-
config, err := helpers.LoadKubeConfig()
76+
config, err := util.LoadKubeConfig()
7877
if err != nil {
7978
//logger.Error(err, "failed to load the kube config.")
8079
return nil, err

pkg/cmd/get/packages.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import (
44
"context"
55
"fmt"
66
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
7-
"github.com/cnoe-io/idpbuilder/pkg/build"
8-
"github.com/cnoe-io/idpbuilder/pkg/k8s"
97
"github.com/cnoe-io/idpbuilder/pkg/printer"
108
"github.com/cnoe-io/idpbuilder/pkg/printer/types"
119
"github.com/cnoe-io/idpbuilder/pkg/util"
1210
"github.com/spf13/cobra"
1311
"io"
14-
"k8s.io/client-go/util/homedir"
1512
"os"
16-
"path/filepath"
1713
"sigs.k8s.io/controller-runtime/pkg/client"
1814
"strconv"
1915
)
@@ -29,23 +25,13 @@ var PackagesCmd = &cobra.Command{
2925
func getPackagesE(cmd *cobra.Command, args []string) error {
3026
ctx, ctxCancel := context.WithCancel(cmd.Context())
3127
defer ctxCancel()
32-
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
3328

34-
opts := build.NewBuildOptions{
35-
KubeConfigPath: kubeConfigPath,
36-
Scheme: k8s.GetScheme(),
37-
CancelFunc: ctxCancel,
38-
TemplateData: v1alpha1.BuildCustomizationSpec{},
39-
}
40-
41-
b := build.NewBuild(opts)
42-
43-
kubeConfig, err := b.GetKubeConfig()
29+
kubeConfig, err := util.GetKubeConfig()
4430
if err != nil {
4531
return fmt.Errorf("getting kube config: %w", err)
4632
}
4733

48-
kubeClient, err := b.GetKubeClient(kubeConfig)
34+
kubeClient, err := util.GetKubeClient(kubeConfig)
4935
if err != nil {
5036
return fmt.Errorf("getting kube client: %w", err)
5137
}

pkg/cmd/get/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package get
22

33
import (
44
"fmt"
5-
"github.com/cnoe-io/idpbuilder/pkg/cmd/helpers"
5+
"github.com/cnoe-io/idpbuilder/pkg/util"
66
"github.com/spf13/cobra"
77
)
88

@@ -24,7 +24,7 @@ func init() {
2424
GetCmd.AddCommand(PackagesCmd)
2525
GetCmd.PersistentFlags().StringSliceVarP(&packages, "packages", "p", []string{}, "names of packages.")
2626
GetCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "table", "Output format: table (default if not specified), json or yaml.")
27-
GetCmd.PersistentFlags().StringVarP(&helpers.KubeConfigPath, "kubeconfig", "", "", "kube config file Path.")
27+
GetCmd.PersistentFlags().StringVarP(&util.KubeConfigPath, "kubeconfig", "", "", "kube config file Path.")
2828
}
2929

3030
func exportE(cmd *cobra.Command, args []string) error {

pkg/cmd/get/secrets.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"fmt"
66
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
7-
"github.com/cnoe-io/idpbuilder/pkg/build"
8-
"github.com/cnoe-io/idpbuilder/pkg/k8s"
97
"github.com/cnoe-io/idpbuilder/pkg/printer"
108
"github.com/cnoe-io/idpbuilder/pkg/printer/types"
119
"github.com/cnoe-io/idpbuilder/pkg/util"
@@ -15,9 +13,7 @@ import (
1513
"k8s.io/apimachinery/pkg/api/errors"
1614
"k8s.io/apimachinery/pkg/labels"
1715
"k8s.io/apimachinery/pkg/selection"
18-
"k8s.io/client-go/util/homedir"
1916
"os"
20-
"path/filepath"
2117
"sigs.k8s.io/controller-runtime/pkg/client"
2218
)
2319

@@ -46,22 +42,13 @@ var (
4642
func getSecretsE(cmd *cobra.Command, args []string) error {
4743
ctx, ctxCancel := context.WithCancel(cmd.Context())
4844
defer ctxCancel()
49-
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
5045

51-
opts := build.NewBuildOptions{
52-
KubeConfigPath: kubeConfigPath,
53-
Scheme: k8s.GetScheme(),
54-
CancelFunc: ctxCancel,
55-
}
56-
57-
b := build.NewBuild(opts)
58-
59-
kubeConfig, err := b.GetKubeConfig()
46+
kubeConfig, err := util.GetKubeConfig()
6047
if err != nil {
6148
return fmt.Errorf("getting kube config: %w", err)
6249
}
6350

64-
kubeClient, err := b.GetKubeClient(kubeConfig)
51+
kubeClient, err := util.GetKubeClient(kubeConfig)
6552
if err != nil {
6653
return fmt.Errorf("getting kube client: %w", err)
6754
}

pkg/k8s/client.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,9 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1010
"k8s.io/apimachinery/pkg/types"
11-
"k8s.io/client-go/tools/clientcmd"
12-
"k8s.io/client-go/util/homedir"
13-
"path/filepath"
1411
"sigs.k8s.io/controller-runtime/pkg/client"
1512
)
1613

17-
func GetKubeClient() (client.Client, error) {
18-
conf, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
19-
if err != nil {
20-
return nil, err
21-
}
22-
return client.New(conf, client.Options{Scheme: GetScheme()})
23-
}
24-
2514
func EnsureObject(ctx context.Context, kubeClient client.Client, obj client.Object, namespace string) error {
2615
curObj := &unstructured.Unstructured{}
2716
curObj.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())

pkg/util/argocd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package util
33
import (
44
"context"
55
"fmt"
6-
"github.com/cnoe-io/idpbuilder/pkg/util/idp"
76
corev1 "k8s.io/api/core/v1"
87
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
98
)
@@ -16,7 +15,7 @@ const (
1615
)
1716

1817
func ArgocdBaseUrl(ctx context.Context) (string, error) {
19-
idpConfig, err := idp.GetConfig(ctx)
18+
idpConfig, err := GetConfig(ctx)
2019
if err != nil {
2120
return "", fmt.Errorf("error fetching idp config: %s", err)
2221
}

pkg/util/gitea.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"fmt"
88
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
9-
"github.com/cnoe-io/idpbuilder/pkg/util/idp"
109
corev1 "k8s.io/api/core/v1"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -110,7 +109,7 @@ func GetGiteaToken(ctx context.Context, baseUrl, username, password string) (str
110109
}
111110

112111
func GiteaBaseUrl(ctx context.Context) (string, error) {
113-
idpConfig, err := idp.GetConfig(ctx)
112+
idpConfig, err := GetConfig(ctx)
114113
if err != nil {
115114
return "", fmt.Errorf("error fetching idp config: %s", err)
116115
}

pkg/util/idp/idp.go renamed to pkg/util/idp.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
package idp
1+
package util
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
6-
"github.com/cnoe-io/idpbuilder/pkg/k8s"
77
"sigs.k8s.io/controller-runtime/pkg/client"
88
)
99

1010
func GetConfig(ctx context.Context) (v1alpha1.BuildCustomizationSpec, error) {
1111
b := v1alpha1.BuildCustomizationSpec{}
1212

13-
kubeClient, err := k8s.GetKubeClient()
13+
kubeConfig, err := GetKubeConfig()
1414
if err != nil {
15-
return b, err
15+
return b, fmt.Errorf("getting kube config: %w", err)
16+
}
17+
18+
kubeClient, err := GetKubeClient(kubeConfig)
19+
if err != nil {
20+
return b, fmt.Errorf("getting kube client: %w", err)
1621
}
1722

1823
list, err := getLocalBuild(ctx, kubeClient)

pkg/cmd/helpers/k8s.go renamed to pkg/util/k8s.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package helpers
1+
package util
22

33
import (
44
"fmt"
5-
"k8s.io/apimachinery/pkg/runtime"
5+
"github.com/cnoe-io/idpbuilder/pkg/k8s"
66
"k8s.io/client-go/rest"
77
"k8s.io/client-go/tools/clientcmd"
88
"k8s.io/client-go/tools/clientcmd/api"
@@ -13,7 +13,6 @@ import (
1313

1414
var (
1515
KubeConfigPath string
16-
scheme *runtime.Scheme
1716
)
1817

1918
func GetKubeConfigPath() string {
@@ -42,7 +41,7 @@ func GetKubeConfig() (*rest.Config, error) {
4241
}
4342

4443
func GetKubeClient(kubeConfig *rest.Config) (client.Client, error) {
45-
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme})
44+
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: k8s.GetScheme()})
4645
if err != nil {
4746
return nil, fmt.Errorf("Error creating kubernetes client: %w", err)
4847
}

0 commit comments

Comments
 (0)