Expose the resource types in addition to the api group/version in templates

Ported #5842 to Helm v3

Signed-off-by: Matt Farina <matt@mattfarina.com>
pull/5860/head
Matt Farina 5 years ago
parent a6fefdd9a3
commit 64496cdc07
No known key found for this signature in database
GPG Key ID: 9436E80BFBA46909

@ -104,7 +104,6 @@
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/xeipuuv/gojsonschema"

@ -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.

@ -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
}

@ -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.")
}
}

Loading…
Cancel
Save