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

Commit

Permalink
feat: simplify template management (#634)
Browse files Browse the repository at this point in the history
The location of the templates is changed from the external projects f8-tenant-[che|jenkins|team] to the directory environment/templates/ inside of this fabric8-tenant project. The files (templates) located in the directory and their names correspond to namespaces they are used for. All objects (contained in the templates) have been moved from the original ones (downloaded from Maven central) just with small cosmetic changes - the result and the number of the objects are same.
  • Loading branch information
MatousJobanek authored Oct 4, 2018
1 parent bdb29d0 commit eb5a8a4
Show file tree
Hide file tree
Showing 45 changed files with 3,437 additions and 1,354 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ client/
swagger/
tool/
migration/sqlbindata.go
template/bindata.go
environment/generated/
auth/client

# Ignore artifacts directory
Expand Down
4 changes: 1 addition & 3 deletions .ike-prow/test-keeper.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
skip_validation_for:
- 'JENKINS_VERSION'
- 'CHE_VERSION'
- 'TEAM_VERSION'
- 'environment/templates/*'
1 change: 0 additions & 1 deletion CHE_VERSION

This file was deleted.

1 change: 0 additions & 1 deletion EXPOSCONTROLLER_VERSION

This file was deleted.

8 changes: 8 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ ignored = [
name = "github.com/jstemmer/go-junit-report"
revision = "master"

[[constraint]]
name = "gopkg.in/h2non/gock.v1"
revision = "v1.0.10"

[[constraint]]
name = "github.com/wadey/gocovmerge"
revision = "master"
Expand Down
1 change: 0 additions & 1 deletion JENKINS_VERSION

This file was deleted.

19 changes: 7 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
PROJECT_NAME=fabric8-tenant
PACKAGE_NAME := github.com/fabric8-services/fabric8-tenant
CHE_VERSION=$(shell cat CHE_VERSION)
JENKINS_VERSION=$(shell cat JENKINS_VERSION)
TEAM_VERSION=$(shell cat TEAM_VERSION)
EXPOSCONTROLLER_VERSION=$(shell cat EXPOSCONTROLLER_VERSION)
CUR_DIR=$(shell pwd)
TMP_PATH=$(CUR_DIR)/tmp
INSTALL_PREFIX=$(CUR_DIR)/bin
Expand Down Expand Up @@ -143,14 +139,13 @@ migration/sqlbindata.go: $(GO_BINDATA_BIN) $(wildcard migration/sql-files/*.sql)
-nocompress \
migration/sql-files

template/bindata.go: $(GO_BINDATA_BIN) $(wildcard template/*.yml)
CHE_VERSION=$(CHE_VERSION) JENKINS_VERSION=$(JENKINS_VERSION) TEAM_VERSION=$(TEAM_VERSION) EXPOSCONTROLLER_VERSION=$(EXPOSCONTROLLER_VERSION) go generate template/generate.go
environment/generated/templates.go: $(GO_BINDATA_BIN) $(wildcard environment/templates/*.yml)
$(GO_BINDATA_BIN) \
-o template/bindata.go \
-pkg template \
-prefix '' \
-o environment/generated/templates.go \
-pkg templates \
-prefix 'environment/templates' \
-nocompress \
template
environment/templates

# install dep (see https://golang.github.io/dep/docs/installation.html)
$(DEP_BIN):
Expand Down Expand Up @@ -193,7 +188,7 @@ clean-generated:
-rm -rf ./client
-rm -rf ./swagger/
-rm -f ./migration/sqlbindata.go
-rm -f ./template/bindata.go
-rm -f ./environment/generated/templates.go
-rm -rf ./auth/client

CLEAN_TARGETS += clean-vendor
Expand Down Expand Up @@ -224,7 +219,7 @@ migrate-database: $(BINARY_SERVER_BIN)

.PHONY: generate
## Generate GOA sources. Only necessary after clean of if changed `design` folder.
generate: app/controllers.go migration/sqlbindata.go template/bindata.go
generate: app/controllers.go migration/sqlbindata.go environment/generated/templates.go

.PHONY: dev
dev: prebuild-check deps generate $(FRESH_BIN)
Expand Down
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ When a PR is made on one of the above YAML repos you can test in production in y
* http://openshift.io/ and login
* go to your Profile page (click the top right drop down and select `Profile`)
* add `/_tenant` to your browsers address bar - so you will be looking at a URL like https://openshift.io/my-user-name/_tenant
* enter the PR version number (it should be reported in a comment on the PR) along with the maven repo that the PR has been pushed to
* enter the `sha` of the latest commit in your PR as a templates repository blob
* click `Update Profile`
* now https://github.com/openshiftio/openshift.io/wiki/FAQ#how-do-i-update-my-tenant-[Update Tenant] - you're tenant should now be running the PR

Expand Down
1 change: 0 additions & 1 deletion TEAM_VERSION

This file was deleted.

24 changes: 0 additions & 24 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ const (
varKeycloakURL = "keycloak.url"
varTogglesURL = "toggles.url"
varConsoleURL = "console.url"
varOpenshiftCheVersion = "openshift.che.version"
varOpenshiftJenkinsVersion = "openshift.jenkins.version"
varOpenshiftTeamVersion = "openshift.team.version"
varOpenshiftTemplateDir = "openshift.template.dir"
varOpenshiftUseCurrentCluster = "openshift.use.current.cluster"
varTemplateJenkinsRootURL = "template.jenkins.root.url"
varTemplateRecommenderExternalName = "template.recommender.external.name"
Expand Down Expand Up @@ -309,26 +305,6 @@ func (c *Data) GetTogglesURL() string {
return c.v.GetString(varTogglesURL)
}

// GetOpenshiftTeamVersion returns the team version of YAML files used to provision tenant team namespaces and roles
func (c *Data) GetOpenshiftTeamVersion() string {
return c.v.GetString(varOpenshiftTeamVersion)
}

// GetOpenshiftCheVersion returns the team version of YAML files used to provision tenant che
func (c *Data) GetOpenshiftCheVersion() string {
return c.v.GetString(varOpenshiftCheVersion)
}

// GetOpenshiftJenkinsVersion returns the team version of YAML files used to provision tenant jenkins
func (c *Data) GetOpenshiftJenkinsVersion() string {
return c.v.GetString(varOpenshiftJenkinsVersion)
}

// GetOpenshiftTemplateDir returns the directory containing the local team YAML files
func (c *Data) GetOpenshiftTemplateDir() string {
return c.v.GetString(varOpenshiftTemplateDir)
}

// UseOpenshiftCurrentCluster returns if we should use the current cluster to provision tenant service
func (c *Data) UseOpenshiftCurrentCluster() bool {
return c.v.GetBool(varOpenshiftUseCurrentCluster)
Expand Down
79 changes: 38 additions & 41 deletions controller/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/fabric8-services/fabric8-tenant/app"
"github.com/fabric8-services/fabric8-tenant/auth"
"github.com/fabric8-services/fabric8-tenant/cluster"
"github.com/fabric8-services/fabric8-tenant/configuration"
env "github.com/fabric8-services/fabric8-tenant/environment"
"github.com/fabric8-services/fabric8-tenant/jsonapi"
"github.com/fabric8-services/fabric8-tenant/openshift"
"github.com/fabric8-services/fabric8-tenant/sentry"
Expand All @@ -25,11 +27,10 @@ import (
// TenantController implements the status resource.
type TenantController struct {
*goa.Controller
tenantService tenant.Service
clusterService cluster.Service
authClientService *auth.Service
defaultOpenshiftConfig openshift.Config
templateVars map[string]string
tenantService tenant.Service
clusterService cluster.Service
authClientService *auth.Service
config *configuration.Data
}

// NewTenantController creates a status controller.
Expand All @@ -38,16 +39,14 @@ func NewTenantController(
tenantService tenant.Service,
clusterService cluster.Service,
authClientService *auth.Service,
defaultOpenshiftConfig openshift.Config,
templateVars map[string]string) *TenantController {
config *configuration.Data) *TenantController {

return &TenantController{
Controller: service.NewController("TenantController"),
tenantService: tenantService,
clusterService: clusterService,
authClientService: authClientService,
defaultOpenshiftConfig: defaultOpenshiftConfig,
templateVars: templateVars,
Controller: service.NewController("TenantController"),
tenantService: tenantService,
clusterService: clusterService,
authClientService: authClientService,
config: config,
}
}

Expand Down Expand Up @@ -85,7 +84,7 @@ func (c *TenantController) Setup(ctx *app.SetupTenantContext) error {
}

// create openshift config
openshiftConfig := openshift.NewConfig(c.defaultOpenshiftConfig, user.UserData, cluster.User, cluster.Token, cluster.APIURL)
openshiftConfig := openshift.NewConfig(c.config, user.UserData, cluster.User, cluster.Token, cluster.APIURL, Commit)
tenant := &tenant.Tenant{
ID: ttoken.Subject(),
Email: ttoken.Email(),
Expand All @@ -107,8 +106,7 @@ func (c *TenantController) Setup(ctx *app.SetupTenantContext) error {
openshiftConfig,
InitTenant(ctx, openshiftConfig.MasterURL, c.tenantService, t, user.OpenshiftUsername),
user.OpenshiftUsername,
user.OpenshiftUserToken,
c.templateVars)
user.OpenshiftUserToken)

if err != nil {
sentry.LogError(ctx, map[string]interface{}{
Expand Down Expand Up @@ -154,7 +152,7 @@ func (c *TenantController) Update(ctx *app.UpdateTenantContext) error {
}

// create openshift config
openshiftConfig := openshift.NewConfig(c.defaultOpenshiftConfig, user.UserData, cluster.User, cluster.Token, cluster.APIURL)
openshiftConfig := openshift.NewConfig(c.config, user.UserData, cluster.User, cluster.Token, cluster.APIURL, Commit)

// update tenant config
tenant.OSUsername = user.OpenshiftUsername
Expand All @@ -173,8 +171,7 @@ func (c *TenantController) Update(ctx *app.UpdateTenantContext) error {
ctx,
openshiftConfig,
InitTenant(ctx, openshiftConfig.MasterURL, c.tenantService, t, user.OpenshiftUsername),
user.OpenshiftUsername,
c.templateVars)
user.OpenshiftUsername)

if err != nil {
sentry.LogError(ctx, map[string]interface{}{
Expand Down Expand Up @@ -217,9 +214,9 @@ func (c *TenantController) Clean(ctx *app.CleanTenantContext) error {
}

// create openshift config
openshiftConfig := openshift.NewConfig(c.defaultOpenshiftConfig, user.UserData, cluster.User, cluster.Token, cluster.APIURL)
openshiftConfig := openshift.NewConfig(c.config, user.UserData, cluster.User, cluster.Token, cluster.APIURL, Commit)

err = openshift.CleanTenant(ctx, openshiftConfig, user.OpenshiftUsername, c.templateVars, removeFromCluster)
err = openshift.CleanTenant(ctx, openshiftConfig, user.OpenshiftUsername, removeFromCluster)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
Expand Down Expand Up @@ -259,40 +256,40 @@ func (c *TenantController) Show(ctx *app.ShowTenantContext) error {
func InitTenant(ctx context.Context, masterURL string, service tenant.Service, currentTenant *tenant.Tenant, openshiftUsername string) openshift.Callback {
var maxResourceQuotaStatusCheck int32 = 50 // technically a global retry count across all ResourceQuota on all Tenant Namespaces
var currentResourceQuotaStatusCheck int32 // default is 0
username := openshift.CreateName(openshiftUsername)
username := env.RetrieveUserName(openshiftUsername)
return func(statusCode int, method string, request, response map[interface{}]interface{}) (string, map[interface{}]interface{}) {
log.Info(ctx, map[string]interface{}{
"status": statusCode,
"method": method,
"cluster_url": masterURL,
"namespace": openshift.GetNamespace(request),
"name": openshift.GetName(request),
"kind": openshift.GetKind(request),
"namespace": env.GetNamespace(request),
"name": env.GetName(request),
"kind": env.GetKind(request),
"request": yamlString(request),
"response": yamlString(response),
}, "resource requested")
if statusCode == http.StatusConflict {
if openshift.GetKind(request) == openshift.ValKindNamespace {
if env.GetKind(request) == env.ValKindNamespace {
return "", nil
}
if openshift.GetKind(request) == openshift.ValKindProjectRequest {
if env.GetKind(request) == env.ValKindProjectRequest {
return "", nil
}
if openshift.GetKind(request) == openshift.ValKindPersistenceVolumeClaim {
if env.GetKind(request) == env.ValKindPersistenceVolumeClaim {
return "", nil
}
if openshift.GetKind(request) == openshift.ValKindServiceAccount {
if env.GetKind(request) == env.ValKindServiceAccount {
return "", nil
}
return "DELETE", request
} else if statusCode == http.StatusCreated {
if openshift.GetKind(request) == openshift.ValKindProjectRequest {
name := openshift.GetName(request)
if env.GetKind(request) == env.ValKindProjectRequest {
name := env.GetName(request)
service.SaveNamespace(&tenant.Namespace{
TenantID: currentTenant.ID,
Name: name,
State: "created",
Version: openshift.GetLabelVersion(request),
Version: env.GetLabelVersion(request),
Type: tenant.GetNamespaceType(name, username),
MasterURL: masterURL,
})
Expand All @@ -301,17 +298,17 @@ func InitTenant(ctx context.Context, masterURL string, service tenant.Service, c
// Should loop on a Check if allowed type of call instead
time.Sleep(time.Second * 5)

} else if openshift.GetKind(request) == openshift.ValKindNamespace {
name := openshift.GetName(request)
} else if env.GetKind(request) == env.ValKindNamespace {
name := env.GetName(request)
service.SaveNamespace(&tenant.Namespace{
TenantID: currentTenant.ID,
Name: name,
State: "created",
Version: openshift.GetLabelVersion(request),
Version: env.GetLabelVersion(request),
Type: tenant.GetNamespaceType(name, username),
MasterURL: masterURL,
})
} else if openshift.GetKind(request) == openshift.ValKindResourceQuota {
} else if env.GetKind(request) == env.ValKindResourceQuota {
// trigger a check status loop
time.Sleep(time.Millisecond * 50)
return "GET", response
Expand All @@ -321,9 +318,9 @@ func InitTenant(ctx context.Context, masterURL string, service tenant.Service, c
if method == "DELETE" {
return "POST", request
} else if method == "GET" {
if openshift.GetKind(request) == openshift.ValKindResourceQuota {
if env.GetKind(request) == env.ValKindResourceQuota {

if openshift.HasValidStatus(response) || atomic.LoadInt32(&currentResourceQuotaStatusCheck) >= maxResourceQuotaStatusCheck {
if env.HasValidStatus(response) || atomic.LoadInt32(&currentResourceQuotaStatusCheck) >= maxResourceQuotaStatusCheck {
return "", nil
}
atomic.AddInt32(&currentResourceQuotaStatusCheck, 1)
Expand All @@ -336,10 +333,10 @@ func InitTenant(ctx context.Context, masterURL string, service tenant.Service, c
log.Info(ctx, map[string]interface{}{
"status": statusCode,
"method": method,
"namespace": openshift.GetNamespace(request),
"namespace": env.GetNamespace(request),
"cluster_url": masterURL,
"name": openshift.GetName(request),
"kind": openshift.GetKind(request),
"name": env.GetName(request),
"kind": env.GetKind(request),
"request": yamlString(request),
"response": yamlString(response),
}, "unhandled resource response")
Expand Down
21 changes: 9 additions & 12 deletions controller/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ var SERVICE_ACCOUNTS = []string{"fabric8-jenkins-idler", "rh-che"}
// TenantsController implements the tenants resource.
type TenantsController struct {
*goa.Controller
tenantService tenant.Service
openshiftService openshift.Service
clusterService cluster.Service
authClientService *auth.Service
defaultOpenshiftConfig openshift.Config
tenantService tenant.Service
openshiftService openshift.Service
clusterService cluster.Service
authClientService *auth.Service
}

// NewTenantsController creates a tenants controller.
Expand All @@ -33,15 +32,13 @@ func NewTenantsController(service *goa.Service,
clusterService cluster.Service,
authClientService *auth.Service,
openshiftService openshift.Service,
defaultOpenshiftConfig openshift.Config,
) *TenantsController {
return &TenantsController{
Controller: service.NewController("TenantsController"),
tenantService: tenantService,
clusterService: clusterService,
openshiftService: openshiftService,
authClientService: authClientService,
defaultOpenshiftConfig: defaultOpenshiftConfig,
Controller: service.NewController("TenantsController"),
tenantService: tenantService,
clusterService: clusterService,
openshiftService: openshiftService,
authClientService: authClientService,
}
}

Expand Down
3 changes: 1 addition & 2 deletions controller/tenants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ func (s *TenantsControllerTestSuite) newTestTenantsController(filename string) (
tenantService := tenant.NewDBService(s.DB)

openshiftService := openshift.NewService(configuration.WithRoundTripper(r))
defaultOpenshiftConfig := openshift.Config{}
svc := goa.New("Tenants-service")
ctrl := controller.NewTenantsController(svc, tenantService, clusterService, authService, openshiftService, defaultOpenshiftConfig)
ctrl := controller.NewTenantsController(svc, tenantService, clusterService, authService, openshiftService)
return svc, ctrl, nil
}
Loading

0 comments on commit eb5a8a4

Please sign in to comment.