Unverified Commit 9b83d1b5 authored by rmweir's avatar rmweir Committed by GitHub
Browse files

Merge pull request #29933 from rmweir/cluster-name-option

Add clusterName filter to templates GET and tests
parents 4010e75d 4e166451
Showing with 136 additions and 5 deletions
+136 -5
......@@ -64,7 +64,7 @@ func (t *templateStore) extractVersionLinks(apiContext *types.APIContext, resour
if revision != "" {
versionID = fmt.Sprintf("%v-%v", resource["id"], revision)
}
if t.templateVersionForRancherVersion(apiContext, version.(map[string]interface{})["externalId"].(string)) {
if t.isTemplateVersionCompatible(apiContext.Query.Get("clusterName"), version.(map[string]interface{})["externalId"].(string)) {
r[versionString] = apiContext.URLBuilder.ResourceLinkByID(schema, versionID)
}
}
......@@ -75,7 +75,7 @@ func (t *templateStore) extractVersionLinks(apiContext *types.APIContext, resour
// templateVersionForRancherVersion indicates if a templateVersion works with the rancher server version
// In the error case it will always return true - if a template is actually invalid for that rancher version
// API validation will handle the rejection
func (t *templateStore) templateVersionForRancherVersion(apiContext *types.APIContext, externalID string) bool {
func (t *templateStore) isTemplateVersionCompatible(clusterName, externalID string) bool {
rancherVersion := settings.ServerVersion.Get()
if !catUtil.ReleaseServerVersion(rancherVersion) {
......@@ -96,5 +96,12 @@ func (t *templateStore) templateVersionForRancherVersion(apiContext *types.APICo
if err != nil {
return false
}
if clusterName != "" {
if err := t.CatalogManager.ValidateKubeVersion(template, clusterName); err != nil {
return false
}
}
return true
}
package catalog
import (
"fmt"
"strings"
"testing"
apimgmtv3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3"
"github.com/rancher/rancher/pkg/catalog/manager"
mgmtv3 "github.com/rancher/rancher/pkg/generated/norman/management.cattle.io/v3"
"github.com/rancher/rancher/pkg/generated/norman/management.cattle.io/v3/fakes"
"github.com/rancher/rancher/pkg/settings"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/version"
)
type testCase struct {
externalID string
rancherVersion string
kubernetesVersion string
result bool
}
func newTestCase(templateName, rancherVersion, kubernetesVersion string, result bool) testCase {
return testCase{
externalID: generateExternalID(templateName),
rancherVersion: rancherVersion,
kubernetesVersion: kubernetesVersion,
result: result,
}
}
func TestExtractLinks(t *testing.T) {
templateStore := setupTestTemplateStore()
// Setup test cases
var tests []testCase
tests = append(tests, newTestCase("rancher-testRancherConstraint-0.1", "2.4.0", "", true))
tests = append(tests, newTestCase("rancher-testRancherConstraint-0.1", "2.5.1", "", false))
// all test cases without rancher version should result in true, since a dev environment is assumed and checks are bypassed
tests = append(tests, newTestCase("rancher-testRancherConstraint-0.1", "", "", true))
tests = append(tests, newTestCase("rancher-testKubeVersionConstraint-0.1", "2.4.0", "1.14.0", true))
tests = append(tests, newTestCase("rancher-testKubeVersionConstraint-0.1", "2.4.0", "1.16.0", false))
tests = append(tests, newTestCase("rancher-testKubeVersionConstraint-0.1", "2.4.0", "", true))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "2.5.1", "1.14.0", false))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "2.4.9", "1.16.0", false))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "2.5.1", "1.16.0", false))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "2.4.9", "1.14.0", true))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "", "", true))
tests = append(tests, newTestCase("rancher-testBothConstraints-0.1", "", "1.16.0", true))
tests = append(tests, newTestCase("rancher-testNeitherConstraints-0.1", "", "", true))
tests = append(tests, newTestCase("rancher-testNeitherConstraints-0.1", "2.5.1", "1.16.0", true))
// The externalID of a testCase will be tested for compatibility with the given rancherVersion and cluster kubernetes
// version. The result field defines whether compatibility is expected.
for _, test := range tests {
settings.ServerVersion.Set(test.rancherVersion)
templateStore.CatalogManager = &manager.Manager{ClusterLister: newClusterListerWithVersion(test.kubernetesVersion)}
clusterName := "test"
if test.kubernetesVersion == "" {
// kubernetesVersion being empty is equivalent to not targeting a cluster
clusterName = ""
}
assert.Equal(t, test.result, templateStore.isTemplateVersionCompatible(clusterName, test.externalID), fmt.Sprintf("kubeVersion [%s] or rancherVersion [%s] unexpected compatibility with template [%s]", test.kubernetesVersion, test.rancherVersion, test.externalID))
}
}
// setupTestTemplateStore sets up test templates with different combinations of rancher
// version constraints and kubeVersionConstraints
func setupTestTemplateStore() templateStore {
catalogTemplateVersionLister := &fakes.CatalogTemplateVersionListerMock{
GetFunc: func(namespace, name string) (*mgmtv3.CatalogTemplateVersion, error) {
switch name {
case "rancher-testRancherConstraint-0.1":
return newCatalogTemplateVersion("2.4.99", ""), nil
case "rancher-testKubeVersionConstraint-0.1":
return newCatalogTemplateVersion("", "<1.15.0"), nil
case "rancher-testBothConstraints-0.1":
return newCatalogTemplateVersion("2.4.99", "<1.15.0"), nil
case "rancher-testNeitherConstraints-0.1":
return newCatalogTemplateVersion("", ""), nil
}
return nil, nil
},
}
return templateStore{
CatalogTemplateVersionLister: catalogTemplateVersionLister,
CatalogManager: &manager.Manager{},
}
}
func newClusterListerWithVersion(kubernetesVersion string) *fakes.ClusterListerMock {
return &fakes.ClusterListerMock{
GetFunc: func(namespace, name string) (*mgmtv3.Cluster, error) {
if name == "test" {
cluster := mgmtv3.Cluster{
Status: apimgmtv3.ClusterStatus{
Version: &version.Info{
GitVersion: kubernetesVersion,
},
},
}
return &cluster, nil
}
return nil, fmt.Errorf("invalid cluster: %s", name)
},
}
}
func newCatalogTemplateVersion(maxRancherVersion, kubeVersion string) *apimgmtv3.CatalogTemplateVersion {
catalogTemplateVersion := &apimgmtv3.CatalogTemplateVersion{
TemplateVersion: apimgmtv3.TemplateVersion{
Spec: apimgmtv3.TemplateVersionSpec{
KubeVersion: kubeVersion,
RancherMaxVersion: maxRancherVersion,
},
},
}
return catalogTemplateVersion
}
func generateExternalID(name string) string {
parts := strings.Split(name, "-")
return fmt.Sprintf("catalog://?catalog=%s&template=%s&version=%s", parts[0], parts[1], parts[2])
}
......@@ -26,7 +26,7 @@ import (
type Manager struct {
catalogClient v3.CatalogInterface
CatalogLister v3.CatalogLister
clusterLister v3.ClusterLister
ClusterLister v3.ClusterLister
templateClient v3.CatalogTemplateInterface
templateContentClient v3.TemplateContentInterface
templateVersionClient v3.CatalogTemplateVersionInterface
......@@ -57,7 +57,7 @@ func New(management managementv3.Interface, project projectv3.Interface) *Manage
return &Manager{
catalogClient: management.Catalogs(""),
CatalogLister: management.Catalogs("").Controller().Lister(),
clusterLister: management.Clusters("").Controller().Lister(),
ClusterLister: management.Clusters("").Controller().Lister(),
templateClient: management.CatalogTemplates(""),
templateContentClient: management.TemplateContents(""),
templateVersionClient: management.CatalogTemplateVersions(""),
......@@ -221,7 +221,7 @@ func (m *Manager) ValidateKubeVersion(template *v3.CatalogTemplateVersion, clust
return nil
}
cluster, err := m.clusterLister.Get("", clusterName)
cluster, err := m.ClusterLister.Get("", clusterName)
if err != nil {
return err
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment