Skip to content

Commit

Permalink
Sonobuoy existing preflight check (#345)
Browse files Browse the repository at this point in the history
Signed-off-by: liz <liz@heptio.com>
  • Loading branch information
liztio authored Mar 8, 2018
1 parent 25d62bf commit e46f80b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/sonobuoy/app/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func e2es(cmd *cobra.Command, args []string) {
}

if !e2eflags.skipPreflight {
if errs := sonobuoy.PreflightChecks(); len(errs) > 0 {
if errs := sonobuoy.PreflightChecks(&client.PreflightConfig{e2eflags.namespace}); len(errs) > 0 {
errlog.LogError(errors.New("Preflight checks failed"))
for _, err := range errs {
errlog.LogError(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sonobuoy/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func submitSonobuoyRun(cmd *cobra.Command, args []string) {
}

if !runflags.skipPreflight {
if errs := sbc.PreflightChecks(); len(errs) > 0 {
if errs := sbc.PreflightChecks(&ops.PreflightConfig{runflags.namespace}); len(errs) > 0 {
errlog.LogError(errors.New("Preflight checks failed"))
for _, err := range errs {
errlog.LogError(err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ type RetrieveConfig struct {
Namespace string
}

// PreflightConfig are the options passed to PreflightChecks.
type PreflightConfig struct {
Namespace string
}

// SonobuoyClient is a high-level interface to Sonobuoy operations.
type SonobuoyClient struct {
RestConfig *rest.Config
Expand Down Expand Up @@ -129,5 +134,5 @@ type Interface interface {
// Delete removes a sonobuoy run, namespace, and all associated resources.
Delete(cfg *DeleteConfig) error
// PreflightChecks runs a number of preflight checks to confirm the environment is good for Sonobuoy
PreflightChecks() []error
PreflightChecks(cfg *PreflightConfig) []error
}
27 changes: 22 additions & 5 deletions pkg/client/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import (
version "github.com/hashicorp/go-version"
"github.com/heptio/sonobuoy/pkg/buildinfo"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

var preflightChecks = []func(kubernetes.Interface) error{
var preflightChecks = []func(kubernetes.Interface, *PreflightConfig) error{
preflightDNSCheck,
preflightVersionCheck,
preflightExistingSonobuoy,
}

// PreflightChecks runs all preflight checks in order, returning the first error encountered.
func (c *SonobuoyClient) PreflightChecks() []error {
func (c *SonobuoyClient) PreflightChecks(cfg *PreflightConfig) []error {
client, err := c.Client()
if err != nil {
return []error{err}
Expand All @@ -41,7 +43,7 @@ func (c *SonobuoyClient) PreflightChecks() []error {
errors := []error{}

for _, check := range preflightChecks {
if err := check(client); err != nil {
if err := check(client, cfg); err != nil {
errors = append(errors, err)
}
}
Expand All @@ -54,7 +56,7 @@ const (
kubeDNSLabelValue = "kube-dns"
)

func preflightDNSCheck(client kubernetes.Interface) error {
func preflightDNSCheck(client kubernetes.Interface, cfg *PreflightConfig) error {
selector := metav1.AddLabelToSelector(&metav1.LabelSelector{}, kubeDNSLabelKey, kubeDNSLabelValue)

obj, err := client.CoreV1().Pods(kubeSystemNamespace).List(
Expand All @@ -76,7 +78,7 @@ var (
maximumKubeVersion = version.Must(version.NewVersion(buildinfo.MaximumKubeVersion))
)

func preflightVersionCheck(client kubernetes.Interface) error {
func preflightVersionCheck(client kubernetes.Interface, cfg *PreflightConfig) error {
versionInfo, err := client.Discovery().ServerVersion()
if err != nil {
return errors.Wrap(err, "failed to retrieve server version")
Expand All @@ -97,3 +99,18 @@ func preflightVersionCheck(client kubernetes.Interface) error {

return nil
}

func preflightExistingSonobuoy(client kubernetes.Interface, cfg *PreflightConfig) error {
_, err := client.CoreV1().Pods(cfg.Namespace).Get("sonobuoy", metav1.GetOptions{})
switch {
// Pod doesn't exist: great!
case apierrors.IsNotFound(err):
return nil
case err != nil:
return errors.Wrap(err, "error checking for Sonobuoy pod")
// No error: pod exists
case err == nil:
return errors.New("sonobuoy run already exists in this namespace")
}
return nil
}

0 comments on commit e46f80b

Please sign in to comment.