This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
tenants.go
207 lines (185 loc) · 6.89 KB
/
tenants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package controller
import (
"github.com/fabric8-services/fabric8-tenant/metric"
"reflect"
"fmt"
commonauth "github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/errors"
"github.com/fabric8-services/fabric8-common/log"
"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"
"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/tenant"
"github.com/goadesign/goa"
errs "github.com/pkg/errors"
)
var SERVICE_ACCOUNTS = []string{"fabric8-jenkins-idler", "rh-che"}
// TenantsController implements the tenants resource.
type TenantsController struct {
*goa.Controller
tenantService tenant.Service
clusterService cluster.Service
authClientService auth.Service
config *configuration.Data
}
// NewTenantsController creates a tenants controller.
func NewTenantsController(
service *goa.Service,
tenantService tenant.Service,
clusterService cluster.Service,
authClientService auth.Service,
config *configuration.Data) *TenantsController {
return &TenantsController{
Controller: service.NewController("TenantsController"),
tenantService: tenantService,
clusterService: clusterService,
authClientService: authClientService,
config: config,
}
}
// Show runs the show action.
func (c *TenantsController) Show(ctx *app.ShowTenantsContext) error {
if !commonauth.IsSpecificServiceAccount(ctx, SERVICE_ACCOUNTS...) {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError("Wrong token"))
}
// get tenant from DB
tenantID := ctx.TenantID
tenantRepository := c.tenantService.NewTenantRepository(tenantID)
tenant, err := tenantRepository.GetTenant()
if err != nil {
serviceAccountName, _ := commonauth.ExtractServiceAccountName(ctx)
log.Error(ctx, map[string]interface{}{
"tenant_id": tenantID,
"error_type": reflect.TypeOf(err),
"caller": serviceAccountName,
}, "error while looking-up tenant record")
return jsonapi.JSONErrorResponse(ctx, err)
}
// gets tenant's namespaces
namespaces, err := tenantRepository.GetNamespaces()
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenantID,
}, "retrieval of existing namespaces from DB failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
result := &app.TenantSingle{Data: convertTenant(ctx, tenant, namespaces, c.clusterService.GetCluster)}
return ctx.OK(result)
}
// Search runs the search action.
func (c *TenantsController) Search(ctx *app.SearchTenantsContext) error {
if !commonauth.IsSpecificServiceAccount(ctx, SERVICE_ACCOUNTS...) {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError("Wrong token"))
}
// find tenant in DB
tenant, err := c.tenantService.LookupTenantByClusterAndNamespace(ctx.MasterURL, ctx.Namespace)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"clusterURL": ctx.MasterURL,
"namespace": ctx.Namespace,
}, "lookup for a tenant entity failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
tenantRepository := c.tenantService.NewTenantRepository(tenant.ID)
// gets tenant's namespaces
namespaces, err := tenantRepository.GetNamespaces()
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenant.ID,
}, "retrieval of existing namespaces from DB failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
result := app.TenantList{
Data: []*app.Tenant{
convertTenant(ctx, tenant, namespaces, c.clusterService.GetCluster),
},
// skipping the paging links for now
Meta: &app.TenantListMeta{
TotalCount: 1,
},
}
return ctx.OK(&result)
}
// Delete runs the `delete` action to deprovision a user
func (c *TenantsController) Delete(ctx *app.DeleteTenantsContext) error {
if !commonauth.IsSpecificServiceAccount(ctx, "fabric8-auth") {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError("Wrong token"))
}
// find tenant in DB
tenantID := ctx.TenantID
tenantRepository := c.tenantService.NewTenantRepository(tenantID)
tenant, err := tenantRepository.GetTenant()
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenant,
}, "retrieval of tenant entity from DB failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
nsBaseName := tenant.NsBaseName
if nsBaseName == "" {
nsBaseName = environment.RetrieveUserName(tenant.OSUsername)
}
// gets tenant's namespaces
namespaces, err := tenantRepository.GetNamespaces()
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenantID,
}, "retrieval of existing namespaces from DB failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
// map target cluster for every environment type
clusterMapping, err := GetClusterMapping(ctx, c.clusterService, namespaces)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewInternalError(ctx, err))
}
// create openshift service
// we don't need token as DELETE uses cluster token
context := openshift.NewServiceContext(ctx, c.config, clusterMapping, tenant.OSUsername, nsBaseName, openshift.TokenResolver())
service := openshift.NewService(context, c.tenantService.NewTenantRepository(tenantID), environment.NewService())
// perform delete method on the list of existing namespaces
err = service.Delete(environment.DefaultEnvTypes, namespaces, openshift.DeleteOpts().EnableSelfHealing().RemoveFromCluster())
if err != nil {
metric.RecordDeletedTenant(false)
namespaces, getErr := tenantRepository.GetNamespaces()
if getErr != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenantID,
}, "retrieval of existing namespaces from DB after the removal attempt failed")
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, err.Error()))
}
params := namespacesToParams(namespaces)
params["err"] = err
log.Error(ctx, params, "deletion of namespaces failed")
return jsonapi.JSONErrorResponse(ctx, err)
}
// the tenant should have been deleted - check it
if tenantRepository.Exists() {
metric.RecordDeletedTenant(false)
log.Error(ctx, map[string]interface{}{
"err": err,
"tenantID": tenantID,
}, "deletion of the tenant failed - it still exists")
return jsonapi.JSONErrorResponse(ctx, fmt.Errorf("unable to delete tenant %s", tenantID))
}
metric.RecordDeletedTenant(true)
log.Info(ctx, map[string]interface{}{"tenant_id": tenantID}, "tenant deleted")
return ctx.NoContent()
}
func namespacesToParams(namespaces []*tenant.Namespace) map[string]interface{} {
params := make(map[string]interface{})
for idx, ns := range namespaces {
key := fmt.Sprintf("namespace#%d", idx)
params[key] = fmt.Sprintf("%+v", *ns)
}
return params
}