forked from fabric8-services/fabric8-tenant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(fabric8-services#678): refactor core functionality to be more …
…extensible and replace oc commands (fabric8-services#747)
- Loading branch information
1 parent
663fa75
commit 9c2ea27
Showing
61 changed files
with
6,683 additions
and
2,483 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"github.com/fabric8-services/fabric8-common/convert/ptr" | ||
"github.com/fabric8-services/fabric8-tenant/app" | ||
"github.com/fabric8-services/fabric8-tenant/cluster" | ||
"github.com/fabric8-services/fabric8-tenant/sentry" | ||
"github.com/fabric8-services/fabric8-tenant/tenant" | ||
) | ||
|
||
type NamespaceFilter func(namespace tenant.Namespace) bool | ||
type ClusterResolver func(ctx context.Context, target string) (cluster.Cluster, error) | ||
|
||
func convertTenant(ctx context.Context, tenant *tenant.Tenant, namespaces []*tenant.Namespace, resolveCluster ClusterResolver) *app.Tenant { | ||
nsAttributes := make([]*app.NamespaceAttributes, 0) | ||
|
||
for _, ns := range namespaces { | ||
|
||
nsCluster, err := resolveCluster(ctx, ns.MasterURL) | ||
if err != nil { | ||
sentry.LogError(ctx, map[string]interface{}{ | ||
"err": err, | ||
"cluster_url": ns.MasterURL, | ||
}, err, "unable to resolve nsCluster") | ||
nsCluster = cluster.Cluster{} | ||
} | ||
nsAttributes = append(nsAttributes, &app.NamespaceAttributes{ | ||
CreatedAt: &ns.CreatedAt, | ||
UpdatedAt: &ns.UpdatedAt, | ||
ClusterURL: &ns.MasterURL, | ||
ClusterAppDomain: &nsCluster.AppDNS, | ||
ClusterConsoleURL: &nsCluster.ConsoleURL, | ||
ClusterMetricsURL: &nsCluster.MetricsURL, | ||
ClusterLoggingURL: &nsCluster.LoggingURL, | ||
Name: &ns.Name, | ||
Type: ptr.String(ns.Type.String()), | ||
Version: &ns.Version, | ||
State: ptr.String(ns.State.String()), | ||
ClusterCapacityExhausted: &nsCluster.CapacityExhausted, | ||
}) | ||
} | ||
|
||
return &app.Tenant{ | ||
ID: &tenant.ID, | ||
Type: "tenants", | ||
Attributes: &app.TenantAttributes{ | ||
CreatedAt: &tenant.CreatedAt, | ||
Email: &tenant.Email, | ||
Profile: &tenant.Profile, | ||
Namespaces: nsAttributes, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/fabric8-services/fabric8-tenant/app" | ||
"github.com/fabric8-services/fabric8-tenant/cluster" | ||
"github.com/fabric8-services/fabric8-tenant/tenant" | ||
|
||
"github.com/fabric8-services/fabric8-tenant/environment" | ||
"github.com/satori/go.uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var resolveCluster = func(ctx context.Context, target string) (cluster.Cluster, error) { | ||
return cluster.Cluster{ | ||
AppDNS: "apps.example.com", | ||
ConsoleURL: "https://console.example.com/console", | ||
MetricsURL: "https://metrics.example.com", | ||
LoggingURL: "https://console.example.com/console", // not a typo; logging and console are on the same host | ||
CapacityExhausted: true, | ||
}, nil | ||
} | ||
|
||
func namespaces(ns1, ns2 time.Time) []*tenant.Namespace { | ||
return []*tenant.Namespace{ | ||
{ | ||
CreatedAt: ns1, | ||
UpdatedAt: ns1, | ||
MasterURL: "http://test1.org", | ||
Name: "test-che", | ||
Type: environment.TypeChe, | ||
Version: "1.0", | ||
State: "created", | ||
}, | ||
{ | ||
CreatedAt: ns2, | ||
UpdatedAt: ns2, | ||
MasterURL: "http://test2.org", | ||
Name: "test-jenkins", | ||
Type: environment.TypeJenkins, | ||
Version: "1.0", | ||
State: "created", | ||
}, | ||
} | ||
} | ||
|
||
func TestConvertTenant(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
tenantCreated := time.Now() | ||
tenantID := uuid.NewV4() | ||
tenant := &tenant.Tenant{ | ||
ID: tenantID, | ||
Email: "[email protected]", | ||
Profile: "Q X", | ||
CreatedAt: tenantCreated, | ||
} | ||
|
||
ns1Time, ns2Time := time.Now(), time.Now() | ||
namespaces := namespaces(ns1Time, ns2Time) | ||
want := &app.Tenant{ | ||
ID: &tenantID, | ||
Type: "tenants", | ||
Attributes: &app.TenantAttributes{ | ||
CreatedAt: &tenantCreated, | ||
Email: strToPtr("[email protected]"), | ||
Profile: strToPtr("Q X"), | ||
Namespaces: []*app.NamespaceAttributes{ | ||
{ | ||
CreatedAt: &ns1Time, | ||
UpdatedAt: &ns1Time, | ||
ClusterURL: strToPtr("http://test1.org"), | ||
ClusterAppDomain: strToPtr("apps.example.com"), | ||
ClusterConsoleURL: strToPtr("https://console.example.com/console"), | ||
ClusterMetricsURL: strToPtr("https://metrics.example.com"), | ||
ClusterLoggingURL: strToPtr("https://console.example.com/console"), | ||
Name: strToPtr("test-che"), | ||
Type: strToPtr("che"), | ||
Version: strToPtr("1.0"), | ||
State: strToPtr("created"), | ||
ClusterCapacityExhausted: boolToPtr(true), | ||
}, | ||
{ | ||
CreatedAt: &ns2Time, | ||
UpdatedAt: &ns2Time, | ||
ClusterURL: strToPtr("http://test2.org"), | ||
ClusterAppDomain: strToPtr("apps.example.com"), | ||
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"), | ||
Version: strToPtr("1.0"), | ||
State: strToPtr("created"), | ||
ClusterCapacityExhausted: boolToPtr(true), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
got := convertTenant(ctx, tenant, namespaces, resolveCluster) | ||
require.Equal(t, want, got) | ||
} | ||
|
||
func strToPtr(s string) *string { | ||
return &s | ||
} | ||
|
||
func boolToPtr(b bool) *bool { | ||
return &b | ||
} |
Oops, something went wrong.