Use the kubernetes Interface

This simplifies testing
This commit is contained in:
Ian Howell 2019-05-02 11:37:20 -05:00
parent d8f3a2377a
commit c29ad8a23f
6 changed files with 33 additions and 20 deletions
cmd
root.go
testdata/TestVersionGoldenOutput
version.go
go.sum
internal/test
pkg/kube

@ -6,6 +6,7 @@ import (
"os"
"github.com/ian-howell/airshipadm/pkg/environment"
"github.com/ian-howell/airshipadm/pkg/kube"
"github.com/spf13/cobra"
)
@ -13,26 +14,32 @@ var settings environment.AirshipADMSettings
// NewRootCmd creates the root `airshipadm` command. All other commands are
// subcommands branching from this one
func NewRootCmd(out io.Writer) *cobra.Command {
func NewRootCmd(out io.Writer, client *kube.Client) *cobra.Command {
rootCmd := &cobra.Command{
Use: "airshipadm",
Short: "airshipadm is a unified entrypoint to various airship components",
}
rootCmd.SetOutput(out)
rootCmd.AddCommand(NewVersionCommand(out))
rootCmd.PersistentFlags().StringVar(&settings.KubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
rootCmd.AddCommand(NewVersionCommand(out, client))
// Compound commands
rootCmd.AddCommand(NewWorkflowCommand())
rootCmd.PersistentFlags().StringVar(&settings.KubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
return rootCmd
}
// Execute runs the base airshipadm command
func Execute(out io.Writer) {
rootCmd := NewRootCmd(out)
// TODO(howell): Remove this panic
client, err := kube.NewForConfig(settings.KubeConfigFilePath)
if err != nil {
panic(err.Error())
}
rootCmd := NewRootCmd(out, client)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)

@ -1,5 +1,5 @@
airshipadm: v0.1.0
golang : go1.12
kubernetes: v1.12.6
kubernetes: v0.0.0-master+$Format:%h$
helm : TODO
argo : TODO

@ -20,7 +20,7 @@ This includes the following tools, in order:
`
// NewVersionCommand prints out the versions of airshipadm and its underlying tools
func NewVersionCommand(out io.Writer) *cobra.Command {
func NewVersionCommand(out io.Writer, client *kube.Client) *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Show the version number of airshipadm and its underlying tools",
@ -28,7 +28,7 @@ func NewVersionCommand(out io.Writer) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(out, "%-10s: %s\n", "airshipadm", airshipadmVersion())
fmt.Fprintf(out, "%-10s: %s\n", "golang", runtime.Version())
fmt.Fprintf(out, "%-10s: %s\n", "kubernetes", kubeVersion(settings.KubeConfigFilePath))
fmt.Fprintf(out, "%-10s: %s\n", "kubernetes", kubeVersion(client))
fmt.Fprintf(out, "%-10s: %s\n", "helm", helmVersion())
fmt.Fprintf(out, "%-10s: %s\n", "argo", argoVersion())
},
@ -41,17 +41,13 @@ func airshipadmVersion() string {
return "v0.1.0"
}
func kubeVersion(configPath string) string {
clientset, err := kube.NewForConfig(configPath)
func kubeVersion(client *kube.Client) string {
version, err := client.Discovery().ServerVersion()
// TODO(howell): Remove this panic
if err != nil {
panic(err.Error())
}
v, err := clientset.Discovery().ServerVersion()
if err != nil {
panic(err.Error())
}
return v.String()
return version.String()
}
func helmVersion() string {

2
go.sum

@ -11,6 +11,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebda/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550 h1:mV9jbLoSW/8m4VK16ZkHTozJa8sesK5u5kTMFysTYac=
github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU=
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
@ -256,6 +257,7 @@ k8s.io/client-go v0.0.0-20190419212732-59781b88d0fa h1:rGhw4l1IV9MkdyckDd9mJPwn2
k8s.io/client-go v0.0.0-20190419212732-59781b88d0fa/go.mod h1:K6+rEBz3fBrn9/LZpYRJd01Ili9dg3XuNmNPS9b94lc=
k8s.io/klog v0.3.0 h1:0VPpR+sizsiivjIfIAQH/rl8tan6jvWkS7lU+0di3lE=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30 h1:TRb4wNWoBVrH9plmkp2q86FIDppkbrEXdXlxU3a3BMI=
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
k8s.io/utils v0.0.0-20190221042446-c2654d5206da h1:ElyM7RPonbKnQqOcw7dG2IK5uvQQn3b/WPHqD5mBvP4=
k8s.io/utils v0.0.0-20190221042446-c2654d5206da/go.mod h1:8k8uAuAQ0rXslZKaEWd0c3oVhZz7sSzSiPnVZayjIX0=

@ -10,6 +10,8 @@ import (
"testing"
"github.com/ian-howell/airshipadm/cmd"
"github.com/ian-howell/airshipadm/pkg/kube"
"k8s.io/client-go/kubernetes/fake"
)
// UpdateGolden writes out the golden files with the latest values, rather than failing the test.
@ -41,7 +43,8 @@ func RunCmdTests(t *testing.T, tests []CmdTest) {
func executeCmd(t *testing.T, command string) []byte {
var actual bytes.Buffer
rootCmd := cmd.NewRootCmd(&actual)
client := &kube.Client{Interface: fake.NewSimpleClientset()}
rootCmd := cmd.NewRootCmd(&actual, client)
// TODO(howell): switch to shellwords (or similar)
args := strings.Fields(command)

@ -1,4 +1,4 @@
package kubernetes
package kube
import (
"os"
@ -8,8 +8,13 @@ import (
"k8s.io/client-go/tools/clientcmd"
)
// Client is a device which communicates with the Kubernetes API
type Client struct {
kubernetes.Interface
}
// NewForConfig creates a kubernetes client using the config at $HOME/.kube/config
func NewForConfig(kubeconfigFilepath string) (kubernetes.Interface, error) {
func NewForConfig(kubeconfigFilepath string) (*Client, error) {
if kubeconfigFilepath == "" {
home, err := os.UserHomeDir()
if err != nil {
@ -29,5 +34,5 @@ func NewForConfig(kubeconfigFilepath string) (kubernetes.Interface, error) {
if err != nil {
panic(err.Error())
}
return clientset, nil
return &Client{clientset}, nil
}