Skip to content

Commit

Permalink
Update code for new dynamic client
Browse files Browse the repository at this point in the history
* Fixes code consistency across commands
* Adds new helper that glues together discovery and REST mapping

Signed-off-by: Chuck Ha <[email protected]>
  • Loading branch information
Chuck Ha authored and chuckha committed Jul 3, 2018
1 parent 6d7180b commit 4f61a01
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 130 deletions.
17 changes: 17 additions & 0 deletions cmd/sonobuoy/app/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app

import (
"github.com/heptio/sonobuoy/pkg/client"
sonodynamic "github.com/heptio/sonobuoy/pkg/dynamic"

"github.com/pkg/errors"
"k8s.io/client-go/rest"
)

func getSonobuoyClient(cfg *rest.Config) (*client.SonobuoyClient, error) {
skc, err := sonodynamic.NewAPIHelperFromRESTConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "couldn't get sonobuoy api helper")
}
return client.NewSonobuoyClient(cfg, skc)
}
2 changes: 1 addition & 1 deletion cmd/sonobuoy/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func deleteSonobuoyRun(cmd *cobra.Command, args []string) {
os.Exit(1)
}

sbc, err := client.NewSonobuoyClient(cfg)
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
Expand Down
11 changes: 5 additions & 6 deletions cmd/sonobuoy/app/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@ func e2es(cmd *cobra.Command, args []string) {
}
defer gzr.Close()

var restConfig *rest.Config
var cfg *rest.Config
// If we are doing a rerun, only then, we need kubeconfig
if e2eflags.rerun {
restConfig, err = e2eflags.kubecfg.Get()
cfg, err = e2eflags.kubecfg.Get()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get REST client"))
os.Exit(1)
}
}

sonobuoy, err := client.NewSonobuoyClient(restConfig)
sonobuoy, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
Expand All @@ -105,7 +104,7 @@ func e2es(cmd *cobra.Command, args []string) {
return
}

cfg, err := e2eflags.Config()
runCfg, err := e2eflags.Config()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't make a Run config"))
os.Exit(1)
Expand All @@ -125,7 +124,7 @@ func e2es(cmd *cobra.Command, args []string) {
}

fmt.Printf("Rerunning %d tests:\n", len(testCases))
if err := sonobuoy.Run(cfg); err != nil {
if err := sonobuoy.Run(runCfg); err != nil {
errlog.LogError(errors.Wrap(err, "error attempting to rerun failed tests"))
os.Exit(1)
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/sonobuoy/app/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ func genManifest(cmd *cobra.Command, args []string) {
errlog.LogError(err)
os.Exit(1)
}
// Passing in `nil` and no `kubeconfig` because it is not required by the method
// for generating any manifests
sbc, err := client.NewSonobuoyClient(nil)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
}

// Generate does not require any client configuration
sbc := &client.SonobuoyClient{}

bytes, err := sbc.GenerateManifest(cfg)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/sonobuoy/app/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func init() {
}

func getLogs(cmd *cobra.Command, args []string) {
restConfig, err := logsKubecfg.Get()
cfg, err := logsKubecfg.Get()
if err != nil {
errlog.LogError(fmt.Errorf("failed to get rest config: %v", err))
errlog.LogError(errors.Wrap(err, "failed to get rest config"))
os.Exit(1)
}
sbc, err := client.NewSonobuoyClient(restConfig)
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
Expand Down
7 changes: 3 additions & 4 deletions cmd/sonobuoy/app/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package app

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -59,12 +58,12 @@ func retrieveResults(cmd *cobra.Command, args []string) {
outDir = args[0]
}

restConfig, err := rcvFlags.kubecfg.Get()
cfg, err := rcvFlags.kubecfg.Get()
if err != nil {
errlog.LogError(fmt.Errorf("failed to get kubernetes client: %v", err))
errlog.LogError(errors.Wrap(err, "failed to get kubernetes client"))
os.Exit(1)
}
sbc, err := client.NewSonobuoyClient(restConfig)
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
Expand Down
21 changes: 10 additions & 11 deletions cmd/sonobuoy/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

ops "github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/errlog"
)

Expand All @@ -44,12 +44,12 @@ func RunFlagSet(cfg *runFlags) *pflag.FlagSet {
return runset
}

func (r *runFlags) Config() (*ops.RunConfig, error) {
func (r *runFlags) Config() (*client.RunConfig, error) {
gencfg, err := r.genFlags.Config()
if err != nil {
return nil, err
}
return &ops.RunConfig{
return &client.RunConfig{
GenConfig: *gencfg,
}, nil
}
Expand All @@ -67,26 +67,25 @@ func init() {
}

func submitSonobuoyRun(cmd *cobra.Command, args []string) {
restConfig, err := runflags.kubecfg.Get()
cfg, err := runflags.kubecfg.Get()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get REST client"))
os.Exit(1)
}

cfg, err := runflags.Config()
runCfg, err := runflags.Config()
if err != nil {
errlog.LogError(errors.Wrap(err, "could not retrieve E2E config"))
os.Exit(1)
}

sbc, err := ops.NewSonobuoyClient(restConfig)
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
}

plugins := make([]string, len(cfg.Config.PluginSelections))
for i, plugin := range cfg.Config.PluginSelections {
plugins := make([]string, len(runCfg.Config.PluginSelections))
for i, plugin := range runCfg.Config.PluginSelections {
plugins[i] = plugin.Name
}

Expand All @@ -95,7 +94,7 @@ func submitSonobuoyRun(cmd *cobra.Command, args []string) {
}

if !runflags.skipPreflight {
if errs := sbc.PreflightChecks(&ops.PreflightConfig{Namespace: runflags.namespace}); len(errs) > 0 {
if errs := sbc.PreflightChecks(&client.PreflightConfig{Namespace: runflags.namespace}); len(errs) > 0 {
errlog.LogError(errors.New("Preflight checks failed"))
for _, err := range errs {
errlog.LogError(err)
Expand All @@ -104,7 +103,7 @@ func submitSonobuoyRun(cmd *cobra.Command, args []string) {
}
}

if err := sbc.Run(cfg); err != nil {
if err := sbc.Run(runCfg); err != nil {
errlog.LogError(errors.Wrap(err, "error attempting to run sonobuoy"))
os.Exit(1)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/sonobuoy/app/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

ops "github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/errlog"
"github.com/heptio/sonobuoy/pkg/plugin/aggregation"
)
Expand Down Expand Up @@ -59,12 +58,12 @@ func init() {
// TODO (timothysc) summarize and aggregate daemonset-plugins by status done (24) running (24)
// also --show-all
func getStatus(cmd *cobra.Command, args []string) {
config, err := statusFlags.kubecfg.Get()
cfg, err := statusFlags.kubecfg.Get()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get kubernetes config"))
os.Exit(1)
}
sbc, err := ops.NewSonobuoyClient(config)
sbc, err := getSonobuoyClient(cfg)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
Expand Down
9 changes: 8 additions & 1 deletion pkg/client/example_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client_test
import (
"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/config"
"github.com/heptio/sonobuoy/pkg/dynamic"
"k8s.io/client-go/rest"
)

Expand All @@ -27,8 +28,14 @@ var cfg *rest.Config

// Example shows how to create a client and run Sonobuoy.
func Example() {
// Get an APIHelper with default implementations from client-go.
apiHelper, err := dynamic.NewAPIHelperFromRESTConfig(cfg)
if err != nil {
panic(err)
}

// client.NewSonobuoyClient returns a struct that implements the client.Interface.
sonobuoy, err := client.NewSonobuoyClient(cfg)
sonobuoy, err := client.NewSonobuoyClient(cfg, apiHelper)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestGenerateManifest(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
sbc, err := client.NewSonobuoyClient(nil)
sbc, err := client.NewSonobuoyClient(nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/heptio/sonobuoy/pkg/config"
"github.com/heptio/sonobuoy/pkg/plugin/aggregation"
"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
Expand Down Expand Up @@ -78,19 +78,27 @@ type PreflightConfig struct {
Namespace string
}

// SonobuoyKubeAPIClient is the interface Sonobuoy uses to communicate with a kube-apiserver.
type SonobuoyKubeAPIClient interface {
CreateObject(*unstructured.Unstructured) (*unstructured.Unstructured, error)
Name(*unstructured.Unstructured) (string, error)
Namespace(*unstructured.Unstructured) (string, error)
ResourceVersion(*unstructured.Unstructured) (string, error)
}

// SonobuoyClient is a high-level interface to Sonobuoy operations.
type SonobuoyClient struct {
RestConfig *rest.Config
client kubernetes.Interface
dynamicClient dynamic.ClientPool
dynamicClient SonobuoyKubeAPIClient
}

// NewSonobuoyClient creates a new SonobuoyClient
func NewSonobuoyClient(restConfig *rest.Config) (*SonobuoyClient, error) {
func NewSonobuoyClient(restConfig *rest.Config, skc SonobuoyKubeAPIClient) (*SonobuoyClient, error) {
sc := &SonobuoyClient{
RestConfig: restConfig,
client: nil,
dynamicClient: nil,
dynamicClient: skc,
}
return sc, nil
}
Expand All @@ -107,14 +115,6 @@ func (s *SonobuoyClient) Client() (kubernetes.Interface, error) {
return s.client, nil
}

// DynamicClientPool creates or retrieves an existing dynamic client from the SonobuoyClient's RESTConfig.
func (s *SonobuoyClient) DynamicClientPool() dynamic.ClientPool {
if s.dynamicClient == nil {
s.dynamicClient = dynamic.NewDynamicClientPool(s.RestConfig)
}
return s.dynamicClient
}

// Make sure SonobuoyClient implements the interface
var _ Interface = &SonobuoyClient{}

Expand Down
Loading

0 comments on commit 4f61a01

Please sign in to comment.