Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
fix(ODC-125): reorganization of the tenant namespaces (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored and alexeykazakov committed Apr 10, 2019
1 parent 9aec8b8 commit 8c48499
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 1,566 deletions.
1 change: 0 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ image:https://codecov.io/gh/fabric8-services/fabric8-tenant/branch/master/graph/

All template YAML files and YAML files containing resource quotas and limits are located in link:environment/templates/[] directory.
Every template uses the same versioning system - the version is stored as a label called `version` and is equal to a short version of `SHA` of the latest commit that changes the particular file.
Both link:environment/templates/fabric8-tenant-jenkins.yml[jenkins] and link:environment/templates/fabric8-tenant-che.yml[single-tenant-che] templates contain also versions of imagines to be deployed - these versions are stored as a template parameter. In case of jenkins the parameter is `JENKINS_OPENSHIFT_VERSION` and in case of che `CHE_SERVER_VERSION`

==== In-production testing

Expand Down
2 changes: 0 additions & 2 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const (
varTogglesURL = "toggles.url"
varConsoleURL = "console.url"
varOpenshiftUseCurrentCluster = "openshift.use.current.cluster"
varTemplateJenkinsRootURL = "template.jenkins.root.url"
varTemplateRecommenderExternalName = "template.recommender.external.name"
varTemplateRecommenderAPIToken = "template.recommender.api.token"
varTemplateDomain = "template.domain"
Expand Down Expand Up @@ -392,7 +391,6 @@ func (c *Data) GetTemplateValues() (map[string]string, error) {
"CHE_KEYCLOAK_AUTH__SERVER__URL": c.GetKeycloakURL() + "/auth",
"CHE_KEYCLOAK_REALM": c.GetKeycloakRealm(),
"CHE_KEYCLOAK_CLIENT__ID": c.GetKeycloakClientID(),
"JENKINS_ROOT_URL": c.v.GetString(varTemplateJenkinsRootURL),
"CHE_MULTITENANT_SERVER": c.v.GetString(varTemplateCheMultiTenantServer),
"OSIO_TOKEN": "", // set per request
"IDENTITY_ID": "", // set per request
Expand Down
8 changes: 4 additions & 4 deletions controller/convert_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func namespaces(ns1, ns2 time.Time) []*tenant.Namespace {
CreatedAt: ns2,
UpdatedAt: ns2,
MasterURL: "http://test2.org",
Name: "test-jenkins",
Type: environment.TypeJenkins,
Name: "test",
Type: environment.TypeUser,
Version: "1.0",
State: "created",
},
Expand Down Expand Up @@ -91,8 +91,8 @@ func TestConvertTenant(t *testing.T) {
ClusterConsoleURL: strToPtr("https://console.example.com/console"),
ClusterMetricsURL: strToPtr("https://metrics.example.com"),
ClusterLoggingURL: strToPtr("https://console.example.com/console"),
Name: strToPtr("test-jenkins"),
Type: strToPtr("jenkins"),
Name: strToPtr("test"),
Type: strToPtr("user"),
Version: strToPtr("1.0"),
State: strToPtr("created"),
ClusterCapacityExhausted: boolToPtr(true),
Expand Down
14 changes: 7 additions & 7 deletions controller/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (s *TenantControllerTestSuite) TestSetupTenantOKWhenNoTenantExistsInParalle
assert.Equal(s.T(), 0, deleteCalls)
assertion.AssertTenantFromDB(s.T(), s.DB, id).
HasNsBaseName("johny").
HasNumberOfNamespaces(5)
HasNumberOfNamespaces(len(environment.DefaultEnvTypes))

}

Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *TenantControllerTestSuite) TestSetupTenantOKWhenTenantExistsInParallelF
for index, id := range tenantIDs {
assertion.AssertTenantFromDB(s.T(), s.DB, id).
HasNsBaseName(fmt.Sprintf("%djohny", index)).
HasNumberOfNamespaces(5)
HasNumberOfNamespaces(len(environment.DefaultEnvTypes))
}
}

Expand Down Expand Up @@ -305,7 +305,7 @@ func (s *TenantControllerTestSuite) TestDeleteTenantOK() {
assert.Equal(s.T(), testdoubles.ExpectedNumberOfCallsWhenClean(environment.DefaultEnvTypes...), calls)
assertion.AssertTenantFromService(t, repo, id).
Exists().
HasNumberOfNamespaces(5)
HasNumberOfNamespaces(len(environment.DefaultEnvTypes))
})

t.Run("remove namespaces and tenant", func(t *testing.T) {
Expand Down Expand Up @@ -391,7 +391,7 @@ func (s *TenantControllerTestSuite) TestDeleteTenantFailures() {
// given
defer gock.OffAll()
gock.New(test.ClusterURL).
Delete("/api/v1/namespaces/johny1-jenkins/configmaps").
Delete("/api/v1/namespaces/johny1-che/persistentvolumeclaims").
Persist().
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre"))).
Reply(500)
Expand All @@ -402,7 +402,7 @@ func (s *TenantControllerTestSuite) TestDeleteTenantFailures() {
// then
assertion.AssertTenantFromService(t, repo, id).
Exists().
HasNumberOfNamespaces(5)
HasNumberOfNamespaces(len(environment.DefaultEnvTypes))
})

t.Run("remove tenant fails when one namespace removal fails", func(t *testing.T) {
Expand Down Expand Up @@ -444,7 +444,7 @@ func (s *TenantControllerTestSuite) TestUpdateTenant() {
// then
objects := testdoubles.AllDefaultObjects(t, config)
// get and patch requests for all objects but ProjectRequest
assert.Equal(t, (len(objects)-5)*2, calls)
assert.Equal(t, (len(objects)-len(environment.DefaultEnvTypes))*2, calls)
})

s.T().Run("Failures", func(t *testing.T) {
Expand Down Expand Up @@ -473,7 +473,7 @@ func (s *TenantControllerTestSuite) TestUpdateTenant() {
// given
defer gock.OffAll()
gock.New(test.ClusterURL).
Patch("/api/v1/namespaces/johny1-jenkins/configmaps").
Patch("/api/v1/namespaces/johny1-che/serviceaccounts").
Times(2).
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre"))).
Reply(500)
Expand Down
6 changes: 3 additions & 3 deletions controller/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (s *UpdateControllerTestSuite) TestStartUpdateOk() {
})

// when
goatest.StartUpdateAccepted(t, createValidSAContext("fabric8-tenant-update"), svc, ctrl, ptr.String("http://api.cluster1/"), ptr.String("jenkins"))
goatest.StartUpdateAccepted(t, createValidSAContext("fabric8-tenant-update"), svc, ctrl, ptr.String("http://api.cluster1/"), ptr.String("user"))

// then
err := test.WaitWithTimeout(10 * time.Second).Until(func() error {
Expand All @@ -182,7 +182,7 @@ func (s *UpdateControllerTestSuite) TestStartUpdateOk() {
assert.NoError(t, err)
for _, ns := range namespaces {
assert.Equal(t, tenant.Ready.String(), ns.State.String())
if ns.MasterURL == "http://api.cluster1/" && ns.Type == environment.TypeJenkins {
if ns.MasterURL == "http://api.cluster1/" && ns.Type == environment.TypeUser {
assertion.AssertNamespace(t, ns).
HasState(tenant.Ready).
HasVersion(environment.RetrieveMappedTemplates()[ns.Type].ConstructCompleteVersion()).
Expand Down Expand Up @@ -250,7 +250,7 @@ func (s *UpdateControllerTestSuite) TestShowUpdateOk() {
versionManagers := update.RetrieveVersionManagers()
configuration.Commit = "123abc"
tf.FillDB(s.T(), s.DB, tf.AddTenants(5), tf.AddDefaultNamespaces())
tf.FillDB(s.T(), s.DB, tf.AddTenants(6), tf.AddNamespaces(environment.TypeJenkins, environment.TypeUser).Outdated())
tf.FillDB(s.T(), s.DB, tf.AddTenants(6), tf.AddNamespaces(environment.TypeChe, environment.TypeUser).Outdated())
tf.FillDB(s.T(), s.DB, tf.AddTenants(6), tf.AddDefaultNamespaces().Outdated())
tf.FillDB(s.T(), s.DB, tf.AddTenants(4), tf.AddDefaultNamespaces().MasterURL("http://api.cluster2/").Outdated())

Expand Down
2 changes: 1 addition & 1 deletion design/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var namespaceAttributes = a.Type("NamespaceAttributes", func() {
a.Attribute("cluster-capacity-exhausted", d.Boolean, "Whether cluster hosting this namespace exhausted it's capacity", func() {
})
a.Attribute("type", d.String, "The tenant namespaces", func() {
a.Enum("user", "che", "jenkins", "stage", "test", "run")
a.Enum("user", "che")
})
})

Expand Down
4 changes: 2 additions & 2 deletions design/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = a.Resource("update", func() {
a.Params(func() {
a.Param("cluster_url", d.String, "the URL of the OSO cluster the update should be limited to")
a.Param("env_type", d.String, "environment type the update should be executed for", func() {
a.Enum("user", "che", "jenkins", "stage", "run")
a.Enum("user", "che")
})
})

Expand All @@ -70,7 +70,7 @@ var _ = a.Resource("update", func() {
a.Params(func() {
a.Param("cluster_url", d.String, "the URL of the OSO cluster the number of outdated tenants should be limited to")
a.Param("env_type", d.String, "environment type the number of outdated tenants should be limited to", func() {
a.Enum("user", "che", "jenkins", "stage", "run")
a.Enum("user", "che")
})
})

Expand Down
13 changes: 1 addition & 12 deletions environment/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ var (
VersionFabric8TenantJenkinsQuotasFile string
VersionFabric8TenantCheQuotasFile string
VersionFabric8TenantDeployFile string
DefaultEnvTypes = []Type{TypeChe, TypeJenkins, TypeRun, TypeStage, TypeUser}
DefaultEnvTypes = []Type{TypeChe, TypeUser}
)

type Templates []*Template

func RetrieveMappedTemplates() map[Type]Templates {
return map[Type]Templates{
TypeRun: tmpl(deploy("run"), "fabric8-tenant-deploy.yml"),
TypeStage: tmpl(deploy("stage"), "fabric8-tenant-deploy.yml"),
TypeChe: tmplWithQuota(versions(VersionFabric8TenantCheMtFile, VersionFabric8TenantCheQuotasFile),
"fabric8-tenant-che-mt.yml", "fabric8-tenant-che-quotas.yml"),
TypeJenkins: tmplWithQuota(versions(VersionFabric8TenantJenkinsFile, VersionFabric8TenantJenkinsQuotasFile),
"fabric8-tenant-jenkins.yml", "fabric8-tenant-jenkins-quotas.yml"),
TypeUser: tmpl(versions(VersionFabric8TenantUserFile, ""), "fabric8-tenant-user.yml"),
}
}
Expand All @@ -52,13 +48,6 @@ func versions(version, quotasVersion string) map[string]string {
return map[string]string{varCommit: version, varCommitQuotas: quotasVersion}
}

func deploy(stage string) map[string]string {
return map[string]string{
varCommit: VersionFabric8TenantDeployFile,
varDeployType: stage,
}
}

func tmplWithQuota(defaultParams map[string]string, fileName string, quotasFileName string) []*Template {
tmpl := newTemplate(fileName, defaultParams, defaultParams[varCommit])
quotas := newTemplate(quotasFileName, defaultParams, defaultParams[varCommitQuotas])
Expand Down
52 changes: 9 additions & 43 deletions environment/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ objects:
var customLocationTempl = `apiVersion: v1
kind: Template
metadata:
name: fabric8-tenant-jenkins
name: fabric8-tenant-che
objects:
- apiVersion: v1
kind: ProjectRequest
Expand All @@ -36,7 +36,7 @@ objects:
test: custom-location
version: ${COMMIT}
version-quotas: ${COMMIT_QUOTAS}
name: ${USER_NAME}-jenkins`
name: ${USER_NAME}-che`

var customLocationQuotas = `apiVersion: v1
kind: List
Expand All @@ -45,11 +45,11 @@ items:
kind: LimitRange
metadata:
labels:
app: fabric8-tenant-jenkins-quotas
app: fabric8-tenant-che-quotas
provider: fabric8
version: ${COMMIT_QUOTAS}
name: resource-limits
namespace: ${USER_NAME}-jenkins`
namespace: ${USER_NAME}-che`

func TestGetAllTemplatesForAllTypes(t *testing.T) {
// given
Expand Down Expand Up @@ -77,24 +77,11 @@ func TestGetAllTemplatesForAllTypes(t *testing.T) {
assert.Equal(t, "234bcd", environment.GetLabelVersion(objects[0]))
assert.Equal(t, "zyx098", environment.GetLabel(objects[0], environment.FieldVersionQuotas))

case "jenkins":
assert.Len(t, env.Templates, 2)
assert.Contains(t, env.Templates[0].Filename, envType)
assert.Contains(t, env.Templates[1].Filename, "quotas")
assert.Equal(t, "567efg", environment.GetLabelVersion(objects[0]))
assert.Equal(t, "yxw987", environment.GetLabel(objects[0], environment.FieldVersionQuotas))

case "user":
assert.Len(t, env.Templates, 1)
assert.Contains(t, env.Templates[0].Filename, envType)
assert.Equal(t, "345cde", environment.GetLabelVersion(objects[0]))
assert.Empty(t, environment.GetLabel(objects[0], environment.FieldVersionQuotas))

default:
assert.Len(t, env.Templates, 1)
assert.Contains(t, env.Templates[0].Filename, "deploy")
assert.Equal(t, "456def", environment.GetLabelVersion(objects[0]))
assert.Empty(t, environment.GetLabel(objects[0], environment.FieldVersionQuotas))
}

for _, template := range env.Templates {
Expand Down Expand Up @@ -166,14 +153,14 @@ func TestDownloadFromGivenBlob(t *testing.T) {
// given
defer gock.OffAll()
gock.New("https://raw.githubusercontent.com").
Get("fabric8-services/fabric8-tenant/987654321/environment/templates/fabric8-tenant-deploy.yml").
Get("fabric8-services/fabric8-tenant/987654321/environment/templates/fabric8-tenant-user.yml").
Reply(200).
BodyString(defaultLocationTempl)
testdoubles.SetTemplateVersions()
service := environment.NewServiceForUserData(testdoubles.NewUserDataWithTenantConfig("", "987654321", ""))

// when
envData, err := service.GetEnvData(context.Background(), "run")
envData, err := service.GetEnvData(context.Background(), environment.TypeUser)

// then
require.NoError(t, err)
Expand All @@ -191,18 +178,18 @@ func TestDownloadFromGivenBlobLocatedInCustomLocation(t *testing.T) {
// given
defer gock.OffAll()
gock.New("http://raw.githubusercontent.com").
Get("my-services/my-tenant/987cba/any/path/fabric8-tenant-jenkins.yml").
Get("my-services/my-tenant/987cba/any/path/fabric8-tenant-che-mt.yml").
Reply(200).
BodyString(customLocationTempl)
gock.New("http://raw.githubusercontent.com").
Get("my-services/my-tenant/987cba/any/path/fabric8-tenant-jenkins-quotas.yml").
Get("my-services/my-tenant/987cba/any/path/fabric8-tenant-che-quotas.yml").
Reply(200).
BodyString(customLocationQuotas)
testdoubles.SetTemplateVersions()
service := environment.NewServiceForUserData(testdoubles.NewUserDataWithTenantConfig("http://github.com/my-services/my-tenant", "987cba", "any/path"))

// when
envData, err := service.GetEnvData(context.Background(), "jenkins")
envData, err := service.GetEnvData(context.Background(), environment.TypeChe)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -240,34 +227,13 @@ func TestConstructCompleteVersion(t *testing.T) {
// then
assert.Equal(t, "234bcd_zyx098", completeVersion)
})
t.Run("check jenkins complete version", func(t *testing.T) {
// and when
completeVersion := mappedTemplates["jenkins"].ConstructCompleteVersion()

// then
assert.Equal(t, "567efg_yxw987", completeVersion)
})
t.Run("check user complete version", func(t *testing.T) {
// and when
completeVersion := mappedTemplates["user"].ConstructCompleteVersion()

// then
assert.Equal(t, "345cde", completeVersion)
})
t.Run("check run complete version", func(t *testing.T) {
// and when
completeVersion := mappedTemplates["run"].ConstructCompleteVersion()

// then
assert.Equal(t, "456def", completeVersion)
})
t.Run("check stage complete version", func(t *testing.T) {
// and when
completeVersion := mappedTemplates["stage"].ConstructCompleteVersion()

// then
assert.Equal(t, "456def", completeVersion)
})
}

func TestCreateUsername(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions environment/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var processTemplate = `
openshift.io/description: ${PROJECT_DESCRIPTION}
openshift.io/display-name: ${PROJECT_DISPLAYNAME}
openshift.io/requester: ${PROJECT_REQUESTING_USER}
serviceaccounts.openshift.io/oauth-redirectreference.jenkins: '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"jenkins"}}'
labels:
provider: fabric8
project: fabric8-tenant-team-environments
Expand Down Expand Up @@ -131,7 +130,6 @@ objects:
metadata:
labels:
user_name: ${USER_NAME}
jenkins_openshift_version: ${JENKINS_OPENSHIFT_VERSION}
project_user: ${PROJECT_USER}
project_displayname: ${PROJECT_DISPLAYNAME}
commit: ${COMMIT}
Expand Down Expand Up @@ -218,9 +216,6 @@ func TestProcess(t *testing.T) {
assert.Contains(t, processed, vars["PROJECT_REQUESTING_USER"], "missing")
assert.Contains(t, processed, vars["PROJECT_NAME"], "missing")
})
t.Run("Verify not fiddling with values", func(t *testing.T) {
assert.Contains(t, processed, `'{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"jenkins"}}'`)
})

t.Run("Verify not escaping xml/html values", func(t *testing.T) {
assert.Contains(t, processed, `<?xml version='1.0' encoding='UTF-8'?>`)
Expand Down Expand Up @@ -263,6 +258,5 @@ func TestUseTemplateParams(t *testing.T) {
assert.Equal(t, "Aslak", environment.GetLabel(objects[0], "user_name"))
assert.Equal(t, "12345", environment.GetLabel(objects[0], "commit"))
assert.Equal(t, "Test-Project-Name", environment.GetLabel(objects[0], "project_displayname"))
assert.Equal(t, "9865421", environment.GetLabel(objects[0], "jenkins_openshift_version"))

}
Loading

0 comments on commit 8c48499

Please sign in to comment.