Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KO-297: Fix --kubeconfig flag support #11

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $(LOCALBIN):

GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.52.2
ENVTEST_K8S_VERSION = 1.26.1
ENVTEST_K8S_VERSION = 1.27.1

.PHONY: golanci-lint
golanci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
Expand Down
4 changes: 2 additions & 2 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespaces.
It creates ServiceAccount, RoleBinding or ClusterRoleBinding as per given scope`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
params, err := configuration.NewParams(ctx, namespaces, allNamespaces, clusterScope)
params, err := configuration.NewParams(ctx, kubeconfig, namespaces, allNamespaces, clusterScope)
if err != nil {
return err
}
Expand All @@ -58,7 +58,7 @@ namespaces.
It deletes ServiceAccount, RoleBinding or ClusterRoleBinding as per given scope`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
params, err := configuration.NewParams(ctx, namespaces, allNamespaces, clusterScope)
params, err := configuration.NewParams(ctx, kubeconfig, namespaces, allNamespaces, clusterScope)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/collectinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var collectinfoCmd = &cobra.Command{
* events logs.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
params, err := configuration.NewParams(ctx, namespaces, allNamespaces, clusterScope)
params, err := configuration.NewParams(ctx, kubeconfig, namespaces, allNamespaces, clusterScope)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ var _ = Describe("Auth", func() {
testDeleteRbac([]string{namespace}, true, true)
})
})

Context("Wrong kubeconfig path", func() {
It("Should fail when wrong kubeconfig path is given", func() {
_, err := configuration.NewParams(testCtx, "wrongpath", []string{namespace},
false, false)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("wrongpath: no such file or directory"))
})
})

})

func testCreateRbac(namespaces []string, clusterScope bool) {
Expand Down
29 changes: 22 additions & 7 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeConfig "sigs.k8s.io/controller-runtime/pkg/client/config"
)
Expand All @@ -40,11 +42,13 @@ type Parameters struct {
AllNamespaces bool
}

func NewParams(ctx context.Context, namespaces []string, allNamespaces, clusterScope bool) (*Parameters, error) {
func NewParams(ctx context.Context, kubeconfigPath string, namespaces []string, allNamespaces,
clusterScope bool,
) (*Parameters, error) {
logger := InitializeConsoleLogger()
logger.Info("Initialized logger")

k8sClient, clientSet, err := createKubeClients()
k8sClient, clientSet, err := createKubeClients(kubeconfigPath)
if err != nil {
return nil, err
}
Expand All @@ -66,20 +70,31 @@ func NewParams(ctx context.Context, namespaces []string, allNamespaces, clusterS
return params, nil
}

func createKubeClients() (client.Client, *kubernetes.Clientset, error) {
func createKubeClients(kubeconfigPath string) (k8sClient client.Client, clientSet *kubernetes.Clientset, err error) {
var cfg *rest.Config

if kubeconfigPath != "" {
cfg, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, nil, err
}
} else {
cfg = runtimeConfig.GetConfigOrDie()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientcmd.BuildConfigFromFlags also has a fallback implementation here. Just check if we have to use both functions to get the config.

Copy link
Contributor Author

@abhishekdwivedi3060 abhishekdwivedi3060 Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have KUBECONFIG env fallback support.

}

scheme := runtime.NewScheme()
cfg := runtimeConfig.GetConfigOrDie()

if err := clientgoscheme.AddToScheme(scheme); err != nil {
err = clientgoscheme.AddToScheme(scheme)
if err != nil {
return nil, nil, err
}

k8sClient, err := client.New(cfg, client.Options{Scheme: scheme})
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
return nil, nil, err
}

clientSet, err := kubernetes.NewForConfig(cfg)
clientSet, err = kubernetes.NewForConfig(cfg)
if err != nil {
return nil, nil, err
}
Expand Down
Loading