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

feat: Add support for in-cluster auth #237

Merged
merged 4 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 14 additions & 7 deletions cmd/kubent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"

Expand All @@ -14,9 +15,11 @@ import (
"github.com/rs/zerolog"
)

const FIXTURES_DIR = "../../fixtures"

func TestInitCollectors(t *testing.T) {
testConfig := config.Config{
Filenames: []string{"../../fixtures/deployment-v1beta1.yaml"},
Filenames: []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")},
Cluster: false,
Helm2: false,
Helm3: false,
Expand All @@ -33,7 +36,8 @@ func TestInitCollectors(t *testing.T) {
}

func TestGetCollectors(t *testing.T) {
fileCollector, err := collector.NewFileCollector(&collector.FileOpts{Filenames: []string{"../../fixtures/deployment-v1beta1.yaml"}})
fileCollector, err := collector.NewFileCollector(
&collector.FileOpts{Filenames: []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}})

if err != nil {
t.Errorf("Failed to create File collector with error: %s", err)
Expand All @@ -51,7 +55,8 @@ func TestGetCollectors(t *testing.T) {

func TestStoreCollector(t *testing.T) {
collectors := []collector.Collector{}
fileCollector, err := collector.NewFileCollector(&collector.FileOpts{Filenames: []string{"../../fixtures/deployment-v1beta1.yaml"}})
fileCollector, err := collector.NewFileCollector(
&collector.FileOpts{Filenames: []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}})

if err != nil {
t.Errorf("Failed to create File collector with error: %s", err)
Expand All @@ -66,7 +71,8 @@ func TestStoreCollector(t *testing.T) {

func TestStoreCollectorMultiple(t *testing.T) {
collectors := []collector.Collector{}
fileCollector, err := collector.NewFileCollector(&collector.FileOpts{Filenames: []string{"../../fixtures/deployment-v1beta1.yaml"}})
fileCollector, err := collector.NewFileCollector(
&collector.FileOpts{Filenames: []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}})

if err != nil {
t.Errorf("Failed to create File collector with error: %s", err)
Expand Down Expand Up @@ -100,9 +106,9 @@ func TestMainExitCodes(t *testing.T) {
}{
{"success", []string{"-c=false", "--helm2=false", "--helm3=false"}, 0},
{"errorBadFlag", []string{"-c=not-boolean"}, 2},
{"successFound", []string{"-c=false", "--helm2=false", "--helm3=false", "-f=../../fixtures/deployment-v1beta1.yaml"}, 0},
{"successFound", []string{"-c=false", "--helm2=false", "--helm3=false", "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0},
{"exitErrorFlagNone", []string{"-c=false", "--helm2=false", "--helm3=false", "-e"}, 0},
{"exitErrorFlagFound", []string{"-c=false", "--helm2=false", "--helm3=false", "-e", "-f=../../fixtures/deployment-v1beta1.yaml"}, 200},
{"exitErrorFlagFound", []string{"-c=false", "--helm2=false", "--helm3=false", "-e", "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 200},
}

if os.Getenv("TEST_EXIT_CODE") == "1" {
Expand Down Expand Up @@ -160,7 +166,8 @@ func TestGetServerVersionNone(t *testing.T) {
func TestGetServerVersionNotSupported(t *testing.T) {
collectors := []collector.Collector{}

fileCollector, err := collector.NewFileCollector(&collector.FileOpts{Filenames: []string{"../../fixtures/deployment-v1beta1.yaml"}})
fileCollector, err := collector.NewFileCollector(
&collector.FileOpts{Filenames: []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}})
if err != nil {
t.Errorf("Failed to create File collector with error: %s", err)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/collector/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package collector

import (
"encoding/json"
"io/ioutil"
"path/filepath"
"strings"
"testing"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -15,14 +16,13 @@ import (

func TestNewClusterCollectorBadPath(t *testing.T) {
testOpts := ClusterOpts{Kubeconfig: "bad path"}
result, funcErr := NewClusterCollector(&testOpts, []string{})
_, err := NewClusterCollector(&testOpts, []string{})

if funcErr.Error() != "invalid configuration: no configuration has been provided" {
out, err := json.Marshal(result)
if !strings.Contains(err.Error(), "no configuration has been provided") {
if err != nil {
t.Errorf("Should have errored with invalid configuration error instead got: %s", string(out))
t.Errorf("Should have errored with invalid configuration error instead got: %s", err)
} else {
t.Errorf("Should have errored instead got: %s", funcErr)
t.Errorf("Should have failed but succeeded")
}
}
}
Expand All @@ -32,7 +32,7 @@ func TestNewClusterCollectorValidEmptyCollector(t *testing.T) {
clientset := fake.NewSimpleDynamicClient(scheme)
discoveryClient := discoveryFake.FakeDiscovery{}
testOpts := ClusterOpts{
Kubeconfig: "../../fixtures/kube.config",
Kubeconfig: filepath.Join(FIXTURES_DIR, "kube.config"),
ClientSet: clientset,
DiscoveryClient: &discoveryClient,
}
Expand Down Expand Up @@ -74,10 +74,10 @@ func TestClusterCollectorGetFake(t *testing.T) {
expected int // number of manifests
}{
{"empty", []string{}, 0},
{"withoutAnnotation", []string{"../../fixtures/fake-deployment-v1beta1-no-annotation.yaml"}, 0},
{"one", []string{"../../fixtures/fake-deployment-v1beta1-with-annotation.yaml"}, 1},
{"multiple", []string{"../../fixtures/fake-deployment-v1beta1-with-annotation.yaml", "../../fixtures/fake-ingress-v1beta1-with-annotation.yaml"}, 2},
{"mixed", []string{"../../fixtures/fake-deployment-v1beta1-no-annotation.yaml", "../../fixtures/fake-ingress-v1beta1-with-annotation.yaml"}, 1},
{"withoutAnnotation", []string{"fake-deployment-v1beta1-no-annotation.yaml"}, 0},
{"one", []string{"fake-deployment-v1beta1-with-annotation.yaml"}, 1},
{"multiple", []string{"fake-deployment-v1beta1-with-annotation.yaml", "fake-ingress-v1beta1-with-annotation.yaml"}, 2},
{"mixed", []string{"fake-deployment-v1beta1-no-annotation.yaml", "fake-ingress-v1beta1-with-annotation.yaml"}, 1},
}

for _, tc := range testCases {
Expand All @@ -88,7 +88,7 @@ func TestClusterCollectorGetFake(t *testing.T) {
for _, f := range tc.input {
obj := &unstructured.Unstructured{}

input, err := ioutil.ReadFile(f)
input, err := ioutil.ReadFile(filepath.Join(FIXTURES_DIR, f))
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
_, _, err = dec.Decode(input, nil, obj)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/collector/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -28,10 +29,10 @@ func TestFileCollectorGet(t *testing.T) {
input []string // file list
expected []string // kinds of objects
}{
{"yaml", []string{"../../fixtures/deployment-v1beta1.yaml"}, []string{"Deployment"}},
{"yamlMulti", []string{"../../fixtures/deployment-v1beta1-and-ingress-v1beta1.yaml"}, []string{"Deployment", "Ingress"}},
{"json", []string{"../../fixtures/deployment-v1beta1.json"}, []string{"Deployment"}},
{"mixed", []string{"../../fixtures/deployment-v1beta1.json", "../../fixtures/deployment-v1beta1.yaml"}, []string{"Deployment", "Deployment"}},
{"yaml", []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, []string{"Deployment"}},
{"yamlMulti", []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1-and-ingress-v1beta1.yaml")}, []string{"Deployment", "Ingress"}},
{"json", []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.json")}, []string{"Deployment"}},
{"mixed", []string{filepath.Join(FIXTURES_DIR, "deployment-v1beta1.json"), filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, []string{"Deployment", "Deployment"}},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestFileCollectorGet(t *testing.T) {
}

func TestFileCollectorGetUnknown(t *testing.T) {
input := []string{"../../fixtures/meow.txt"}
input := []string{filepath.Join(FIXTURES_DIR, "meow.txt")}

c, err := NewFileCollector(
&FileOpts{Filenames: input},
Expand All @@ -79,7 +80,7 @@ func TestFileCollectorGetUnknown(t *testing.T) {
}

func TestFileCollectorGetNonExistent(t *testing.T) {
input := []string{"../../fixtures/does-not-exist"}
input := []string{"does-not-exist"}
expected := "failed to read"

c, err := NewFileCollector(
Expand All @@ -101,7 +102,7 @@ func TestFileCollectorGetNonExistent(t *testing.T) {

func TestFileCollectorGetStdin(t *testing.T) {
input := []string{"-"}
inputFilename := "../../fixtures/deployment-v1beta1.yaml"
inputFilename := filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")
expected := 1

c, err := NewFileCollector(
Expand Down
50 changes: 34 additions & 16 deletions pkg/collector/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,48 @@ func newKubeCollector(kubeconfig string, kubecontext string, discoveryClient dis
if discoveryClient != nil {
col.discoveryClient = discoveryClient
} else {
pathOptions := clientcmd.NewDefaultPathOptions()
if kubeconfig != "" {
pathOptions.GlobalFile = kubeconfig
var err error
stepanstipl marked this conversation as resolved.
Show resolved Hide resolved
if col.restConfig, err = newClientRestConfig(kubeconfig, kubecontext, rest.InClusterConfig); err != nil {
return nil, fmt.Errorf("failed to assemble client config: %w", err)
}

config, err := pathOptions.GetStartingConfig()

configOverrides := clientcmd.ConfigOverrides{}
if kubecontext != "" {
configOverrides.CurrentContext = kubecontext
if col.discoveryClient, err = discovery.NewDiscoveryClientForConfig(col.restConfig); err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
}
}

clientConfig := clientcmd.NewDefaultClientConfig(*config, &configOverrides)
col.restConfig, err = clientConfig.ClientConfig()
if err != nil {
return nil, err
}
return col, nil
}

if col.discoveryClient, err = discovery.NewDiscoveryClientForConfig(col.restConfig); err != nil {
return nil, err
func newClientRestConfig(kubeconfig string, kubecontext string, inClusterFn func() (*rest.Config, error)) (*rest.Config, error) {
if kubeconfig == "" {
if restConfig, err := inClusterFn(); err == nil {
return restConfig, nil
}
}

return col, nil
pathOptions := clientcmd.NewDefaultPathOptions()
if kubeconfig != "" {
pathOptions.GlobalFile = kubeconfig
}

config, err := pathOptions.GetStartingConfig()
if err != nil {
return nil, err
}

configOverrides := clientcmd.ConfigOverrides{}
if kubecontext != "" {
configOverrides.CurrentContext = kubecontext
}

clientConfig := clientcmd.NewDefaultClientConfig(*config, &configOverrides)
restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}

return restConfig, nil
}

func (c *kubeCollector) GetServerVersion() (*judge.Version, error) {
Expand Down
Loading