diff --git a/Gopkg.toml b/Gopkg.toml index 52ade6e71..99ea05b9c 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -104,7 +104,6 @@ [prune] go-tests = true - unused-packages = true [[constraint]] name = "github.com/xeipuuv/gojsonschema" diff --git a/docs/chart_template_guide/builtin_objects.md b/docs/chart_template_guide/builtin_objects.md index f5af186e8..6c4322399 100644 --- a/docs/chart_template_guide/builtin_objects.md +++ b/docs/chart_template_guide/builtin_objects.md @@ -19,7 +19,7 @@ In the previous section, we use `{{.Release.Name}}` to insert the name of a rele - `Files.GetBytes` is a function for getting the contents of a file as an array of bytes instead of as a string. This is useful for things like images. - `Capabilities`: This provides information about what capabilities the Kubernetes cluster supports. - `Capabilities.APIVersions` is a set of versions. - - `Capabilities.APIVersions.Has $version` indicates whether a version (`batch/v1`) is enabled on the cluster. + - `Capabilities.APIVersions.Has $version` indicates whether a version (e.g., `batch/v1`) or resource (e.g., `apps/v1/Deployment`) is available on the cluster. - `Capabilities.Kube.Version` is the Kubernetes version. - `Capabilities.Kube` is a short form for Kubernetes version. - `Capabilities.Kube.Major` is the Kubernetes major version. diff --git a/pkg/action/action.go b/pkg/action/action.go index f69c16869..07aef4f4e 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -17,12 +17,12 @@ limitations under the License. package action import ( + "path" "regexp" "time" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/discovery" "k8s.io/client-go/rest" @@ -131,21 +131,50 @@ func (c *Configuration) releaseContent(name string, version int) (*release.Relea } // GetVersionSet retrieves a set of available k8s API versions -func GetVersionSet(client discovery.ServerGroupsInterface) (chartutil.VersionSet, error) { - groups, err := client.ServerGroups() +func GetVersionSet(client discovery.ServerResourcesInterface) (chartutil.VersionSet, error) { + groups, resources, err := client.ServerGroupsAndResources() 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 { + // for calls to Discovery().ServerGroupsAndResources(). 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 len(groups) == 0 && len(resources) == 0 { return chartutil.DefaultVersionSet, nil } - versions := metav1.ExtractGroupVersions(groups) + versionMap := make(map[string]interface{}) + versions := []string{} + + // Extract the groups + for _, g := range groups { + for _, gv := range g.Versions { + versionMap[gv.GroupVersion] = struct{}{} + } + } + + // Extract the resources + var id string + var ok bool + for _, r := range resources { + for _, rl := range r.APIResources { + + // A Kind at a GroupVersion can show up more than once. We only want + // it displayed once in the final output. + id = path.Join(r.GroupVersion, rl.Kind) + if _, ok = versionMap[id]; !ok { + versionMap[id] = struct{}{} + } + } + } + + // Convert to a form that NewVersionSet can use + for k := range versionMap { + versions = append(versions, k) + } + return chartutil.VersionSet(versions), nil } diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index cab02a9c4..afd9d4b51 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/pkg/errors" + fakeclientset "k8s.io/client-go/kubernetes/fake" "helm.sh/helm/pkg/chart" "helm.sh/helm/pkg/chartutil" @@ -187,3 +188,19 @@ type hookFailingKubeClient struct { func (h *hookFailingKubeClient) WatchUntilReady(r io.Reader, timeout time.Duration) error { return errors.New("Failed watch") } + +func TestGetVersionSet(t *testing.T) { + client := fakeclientset.NewSimpleClientset() + + vs, err := GetVersionSet(client.Discovery()) + if err != nil { + t.Error(err) + } + + if !vs.Has("v1") { + t.Errorf("Expected supported versions to at least include v1.") + } + if vs.Has("nosuchversion/v1") { + t.Error("Non-existent version is reported found.") + } +}