From 86435cca488a0a78bf534edc7a1c26bf55f8dd7f Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Fri, 21 Dec 2018 12:18:20 -0700 Subject: [PATCH] feat: Add install and refactor some tests This adds install to the action package, and then fixes up a lot of testing. Signed-off-by: Matt Butcher --- cmd/helm/install.go | 35 +++++++++++++++++++++++++++++++ pkg/action/action.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index a0b23a829..76433fed7 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -308,6 +308,41 @@ func (o *installOptions) printRelease(out io.Writer, rel *release.Release) { } } +// printRelease prints info about a release if the Debug is true. +func (o *installOptions) printRelease(out io.Writer, rel *release.Release) { + if rel == nil { + return + } + fmt.Fprintf(out, "NAME: %s\n", rel.Name) + if settings.Debug { + printRelease(out, rel) + } + if !rel.Info.LastDeployed.IsZero() { + fmt.Fprintf(out, "LAST DEPLOYED: %s\n", rel.Info.LastDeployed) + } + fmt.Fprintf(out, "NAMESPACE: %s\n", rel.Namespace) + fmt.Fprintf(out, "STATUS: %s\n", rel.Info.Status.String()) + fmt.Fprintf(out, "\n") + if len(rel.Info.Resources) > 0 { + re := regexp.MustCompile(" +") + + w := tabwriter.NewWriter(out, 0, 0, 2, ' ', tabwriter.TabIndent) + fmt.Fprintf(w, "RESOURCES:\n%s\n", re.ReplaceAllString(rel.Info.Resources, "\t")) + w.Flush() + } + if rel.Info.LastTestSuiteRun != nil { + lastRun := rel.Info.LastTestSuiteRun + fmt.Fprintf(out, "TEST SUITE:\n%s\n%s\n\n%s\n", + fmt.Sprintf("Last Started: %s", lastRun.StartedAt), + fmt.Sprintf("Last Completed: %s", lastRun.CompletedAt), + formatTestResults(lastRun.Results)) + } + + if len(rel.Info.Notes) > 0 { + fmt.Fprintf(out, "NOTES:\n%s\n", rel.Info.Notes) + } +} + // Merges source and destination map, preferring values from the source map func mergeValues(dest, src map[string]interface{}) map[string]interface{} { for k, v := range src { diff --git a/pkg/action/action.go b/pkg/action/action.go index ad417cc08..2ecbb5be2 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -46,6 +46,55 @@ type Configuration struct { KubeClient environment.KubeClient Log func(string, ...interface{}) + + Timestamper Timestamper +} + +// capabilities builds a Capabilities from discovery information. +func (c *Configuration) capabilities() (*chartutil.Capabilities, error) { + sv, err := c.Discovery.ServerVersion() + if err != nil { + return nil, err + } + vs, err := GetVersionSet(c.Discovery) + if err != nil { + return nil, errors.Wrap(err, "could not get apiVersions from Kubernetes") + } + return &chartutil.Capabilities{ + APIVersions: vs, + KubeVersion: sv, + HelmVersion: version.GetBuildInfo(), + }, nil +} + +// Now generates a timestamp +// +// If the configuration has a Timestamper on it, that will be used. +// Otherwise, this will use time.Now(). +func (c *Configuration) Now() time.Time { + if c.Timestamper != nil { + return c.Timestamper() + } + return time.Now() +} + +// GetVersionSet retrieves a set of available k8s API versions +func GetVersionSet(client discovery.ServerGroupsInterface) (chartutil.VersionSet, error) { + groups, err := client.ServerGroups() + if err != nil { + return chartutil.DefaultVersionSet, err + } + + // FIXME: The Kubernetes test fixture for cli appears to always return nil + // for calls to Discovery().ServerGroups(). So in this case, we return + // the default API list. This is also a safe value to return in any other + // odd-ball case. + if groups.Size() == 0 { + return chartutil.DefaultVersionSet, nil + } + + versions := metav1.ExtractGroupVersions(groups) + return chartutil.NewVersionSet(versions...), nil } // capabilities builds a Capabilities from discovery information.