Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
pkg/k8sutil: add new NewClientset function
Browse files Browse the repository at this point in the history
This commit adds NewClientset exported function, which allows creating
Kubernetes Clientset from content of kubeconfig file, which allows to
create it from sources differant than file on local file system.

This is required for being able to execute 'lokoctl health' without
having kubeconfig file present in local assets directory, as it can be
pulled from other places, e.g. Terraform state.

See #608 for more details.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Jun 23, 2020
1 parent d5a5c0c commit 813b32a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/k8sutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package k8sutil

import (
"fmt"

"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -35,3 +37,19 @@ func NewClientsetFromFile(kubeconfigPath string) (*kubernetes.Clientset, error)

return apiclientset, nil
}

// NewClientset creates new Kubernetes Client set object from given kubeconfig
// file content.
func NewClientset(data []byte) (*kubernetes.Clientset, error) {
c, err := clientcmd.NewClientConfigFromBytes(data)
if err != nil {
return nil, fmt.Errorf("creating client config failed: %w", err)
}

restConfig, err := c.ClientConfig()
if err != nil {
return nil, fmt.Errorf("converting client config to rest client config failed: %w", err)
}

return kubernetes.NewForConfig(restConfig)
}
54 changes: 54 additions & 0 deletions pkg/k8sutil/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 The Lokomotive 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_test

import (
"testing"

"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const (
validKubeconfig = `
apiVersion: v1
kind: Config
clusters:
- name: admin
cluster:
server: https://nonexistent:6443
users:
- name: admin
user:
token: "foo.bar"
current-context: admin
contexts:
- name: admin
context:
cluster: admin
user: admin
`
)

func TestNewClientset(t *testing.T) {
if _, err := k8sutil.NewClientset([]byte(validKubeconfig)); err != nil {
t.Fatalf("Creating clientset from valid kubeconfig should succeed, got: %v", err)
}
}

func TestNewClientsetInvalidKubeconfig(t *testing.T) {
if _, err := k8sutil.NewClientset([]byte("foo")); err == nil {
t.Fatalf("Creating clientset from invalid kubeconfig should fail")
}
}

0 comments on commit 813b32a

Please sign in to comment.