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

Refactor to remove legacyconfig object #163

Merged
merged 6 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/orchestration/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
deps = [
"//:go_default_library",
"//cmd:go_default_library",
"//pkg/apis/orchestration/meta:go_default_library",
"//pkg/apis/orchestration/v1:go_default_library",
"//pkg/k8s:go_default_library",
"//pkg/options:go_default_library",
Expand All @@ -20,9 +21,9 @@ go_library(
"//pkg/orchestration/informer:go_default_library",
"//pkg/orchestration/updater:go_default_library",
"//pkg/orchestration/wiring:go_default_library",
"//pkg/orchestration/wiring/legacy:go_default_library",
"//pkg/orchestration/wiring/registry:go_default_library",
"//pkg/orchestration/wiring/wiringplugin:go_default_library",
"//pkg/orchestration/wiring/wiringutil/oap:go_default_library",
"//pkg/util/crash:go_default_library",
"//pkg/util/prometheus:go_default_library",
"//vendor/github.com/atlassian/ctrl:go_default_library",
Expand Down
16 changes: 4 additions & 12 deletions cmd/orchestration/app/controller_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
orchInf "github.com/atlassian/voyager/pkg/orchestration/informer"
orchUpdater "github.com/atlassian/voyager/pkg/orchestration/updater"
"github.com/atlassian/voyager/pkg/orchestration/wiring"
"github.com/atlassian/voyager/pkg/orchestration/wiring/legacy"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringplugin"
prom_util "github.com/atlassian/voyager/pkg/util/prometheus"
"github.com/pkg/errors"
Expand All @@ -30,9 +29,9 @@ import (
)

type ControllerConstructor struct {
FlagConfigFile string
GetLegacyConfigFunc func(voyager.Location) *legacy.Config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real problem, I think, is that autowiring functions at the moment are not configurable (they are all hardcoded), and the only way to pass configuration is via context and orchestration controller constructor

Copy link
Contributor

@ychen-atlassian ychen-atlassian Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about moving the plugins out of wiring/registry? We can pass them as parameters into the constructor for orchestration controller.

This matches up with what @amckague has done for the example tag generator, and it's what we sort of do for smith too. We're going to want to split the wiring out of this repo anyway.

Plugins map[voyager.ResourceType]wiringplugin.WiringPlugin
FlagConfigFile string
Plugins map[voyager.ResourceType]wiringplugin.WiringPlugin
Tags wiring.TagGenerator
}

func (cc *ControllerConstructor) AddFlags(flagset ctrl.FlagSet) {
Expand Down Expand Up @@ -88,14 +87,7 @@ func (cc *ControllerConstructor) New(config *ctrl.Config, cctx *ctrl.Context) (*
Plugins: cc.Plugins,
ClusterLocation: opts.Location.ClusterLocation(),
ClusterConfig: toClusterConfig(opts.Cluster),
TagNames: wiring.TagNames{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Seems unrelated to LegacyConfig. Could you split into multiple PRs please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment about introducing the tag generator. If the tags come from a function there's no need to make their keys come in via config.

ServiceNameTag: opts.TagNames.ServiceName,
BusinessUnitTag: opts.TagNames.BusinessUnit,
ResourceOwnerTag: opts.TagNames.ResourceOwner,
PlatformTag: opts.TagNames.Platform,
EnvironmentTypeTag: opts.TagNames.EnvironmentType,
},
GetLegacyConfigFunc: cc.GetLegacyConfigFunc,
Tags: cc.Tags,
}

// Spec check
Expand Down
53 changes: 46 additions & 7 deletions cmd/orchestration/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"flag"
"math/rand"
"os"
"strings"
"time"

"github.com/atlassian/ctrl"
ctrlApp "github.com/atlassian/ctrl/app"
"github.com/atlassian/voyager"
"github.com/atlassian/voyager/cmd"
"github.com/atlassian/voyager/pkg/orchestration/wiring/legacy"
orch_meta "github.com/atlassian/voyager/pkg/apis/orchestration/meta"
"github.com/atlassian/voyager/pkg/orchestration/wiring/registry"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringplugin"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/oap"
"github.com/atlassian/voyager/pkg/util/crash"
"k8s.io/klog"
)
Expand All @@ -23,18 +25,23 @@ const (
)

func Main() {
CustomMain(emptyLegacyConfigFunc, registry.KnownWiringPlugins)
CustomMain(registry.KnownWiringPlugins(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better 👍

exampleDeveloperRole,
exampleManagedPolicies,
exampleVPC,
exampleEnvironment,
))
}

func CustomMain(getLegacyConfigFunc func(voyager.Location) *legacy.Config, plugins map[voyager.ResourceType]wiringplugin.WiringPlugin) {
func CustomMain(plugins map[voyager.ResourceType]wiringplugin.WiringPlugin) {
rand.Seed(time.Now().UnixNano())
klog.InitFlags(nil)
cmd.RunInterruptably(func(ctx context.Context) error {
crash.InstallAPIMachineryLoggers()
controllers := []ctrl.Constructor{
&ControllerConstructor{
GetLegacyConfigFunc: getLegacyConfigFunc,
Plugins: plugins,
Plugins: plugins,
Tags: exampleTags,
},
}

Expand All @@ -48,6 +55,38 @@ func CustomMain(getLegacyConfigFunc func(voyager.Location) *legacy.Config, plugi
})
}

func emptyLegacyConfigFunc(location voyager.Location) *legacy.Config {
return &legacy.Config{}
func exampleTags(
_ voyager.ClusterLocation,
_ wiringplugin.ClusterConfig,
_ voyager.Location,
_ voyager.ServiceName,
_ orch_meta.ServiceProperties,
) map[voyager.Tag]string {
return make(map[voyager.Tag]string)
}

func exampleDeveloperRole(_ voyager.Location) []string {
return strings.Split(os.Getenv("PLUGIN_DEVELOPER_ROLE"), ",") //example
}
func exampleManagedPolicies(_ voyager.Location) []string {
return strings.Split(os.Getenv("PLUGIN_MANAGED_POLICIES"), ",")
}
func exampleVPC(location voyager.Location) *oap.VPCEnvironment {
return &oap.VPCEnvironment{
VPCID: os.Getenv("PLUGIN_VPC_ID"),
PrivateDNSZone: os.Getenv("PLUGIN_PRIVATE_DNS_ZONE"),
PrivatePaasDNSZone: os.Getenv("PLUGIN_PRIVATE_PAAS_DNS_ZONE"),
InstanceSecurityGroup: os.Getenv("PLUGIN_INSTANCE_SECURITY_GROUP"),
JumpboxSecurityGroup: os.Getenv("PLUGIN_JUMP_BOX_SECURITY_GROUP"),
SSLCertificateID: os.Getenv("PLUGIN_SSL_CERT_ID"),
Label: location.Label,
AppSubnets: strings.Split(os.Getenv("PLUGIN_APP_SUBNETS"), ","),
Zones: strings.Split(os.Getenv("PLUGIN_ZONES"), ","),
Region: location.Region,
EMRSubnet: os.Getenv("EMR_SUBNET"),
}
}

func exampleEnvironment(_ voyager.Location) string {
return os.Getenv("PLUGIN_ENVIRONMENT")
}
13 changes: 0 additions & 13 deletions cmd/orchestration/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,15 @@ package app
import (
"io/ioutil"

"github.com/atlassian/voyager"
"github.com/atlassian/voyager/pkg/options"
"github.com/pkg/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/yaml"
)

// TagNames tell us what tag to store each of these properties under.
// e.g. ServiceName = "application_name", and the service name is put into the
// application_name tag.
type TagNames struct {
ServiceName voyager.Tag `json:"serviceName"`
BusinessUnit voyager.Tag `json:"businessUnit"`
ResourceOwner voyager.Tag `json:"resourceOwner"`
Platform voyager.Tag `json:"platform"`
EnvironmentType voyager.Tag `json:"environmentType"`
}

type Options struct {
Location options.Location `json:"location"`
Cluster options.Cluster `json:"cluster"`
TagNames TagNames `json:"tagNames"`
}

func (o *Options) DefaultAndValidate() []error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/creator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ go_library(
"//pkg/apis/creator/v1:go_default_library",
"//pkg/creator/luigi:go_default_library",
"//pkg/creator/ssam:go_default_library",
"//pkg/orchestration/wiring/ec2compute/common:go_default_library",
"//pkg/orchestration/wiring/ec2compute/v2:go_default_library",
"//pkg/pagerduty:go_default_library",
"//pkg/servicecentral:go_default_library",
"//pkg/util:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions pkg/creator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
creator_v1 "github.com/atlassian/voyager/pkg/apis/creator/v1"
"github.com/atlassian/voyager/pkg/creator/luigi"
"github.com/atlassian/voyager/pkg/creator/ssam"
ec2compute_common "github.com/atlassian/voyager/pkg/orchestration/wiring/ec2compute/common"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also unrelated to LegacyConfig? separate PR please.

ec2compute_v2 "github.com/atlassian/voyager/pkg/orchestration/wiring/ec2compute/v2"
"github.com/atlassian/voyager/pkg/pagerduty"
"github.com/atlassian/voyager/pkg/servicecentral"
"github.com/atlassian/voyager/pkg/util"
Expand All @@ -33,7 +33,7 @@ const (
ServiceNameMinimumLength = 1
ServiceNameMaximumLength = 24
ServiceNameExpr = schema.ResourceNameSchemaPattern
EC2ComputeNameMaximumLength = ec2compute_common.MaximumServiceNameLength
EC2ComputeNameMaximumLength = ec2compute_v2.MaximumServiceNameLength
)

var (
Expand Down
3 changes: 2 additions & 1 deletion pkg/orchestration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ go_test(
race = "on",
deps = [
"//:go_default_library",
"//pkg/apis/orchestration/meta:go_default_library",
"//pkg/apis/orchestration/v1:go_default_library",
"//pkg/orchestration/client/fake:go_default_library",
"//pkg/orchestration/wiring:go_default_library",
"//pkg/orchestration/wiring/legacy:go_default_library",
"//pkg/orchestration/wiring/registry:go_default_library",
"//pkg/orchestration/wiring/wiringplugin:go_default_library",
"//pkg/orchestration/wiring/wiringutil/oap:go_default_library",
"//pkg/util/testutil:go_default_library",
"//vendor/github.com/atlassian/smith/pkg/apis/smith/v1:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
Expand Down
68 changes: 53 additions & 15 deletions pkg/orchestration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

smith_v1 "github.com/atlassian/smith/pkg/apis/smith/v1"
"github.com/atlassian/voyager"
orch_meta "github.com/atlassian/voyager/pkg/apis/orchestration/meta"
orch_v1 "github.com/atlassian/voyager/pkg/apis/orchestration/v1"
stateclient_fake "github.com/atlassian/voyager/pkg/orchestration/client/fake"
"github.com/atlassian/voyager/pkg/orchestration/wiring"
"github.com/atlassian/voyager/pkg/orchestration/wiring/legacy"
"github.com/atlassian/voyager/pkg/orchestration/wiring/registry"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringplugin"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/oap"
"github.com/atlassian/voyager/pkg/util/testutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
Expand All @@ -27,6 +28,10 @@ const (
fixtureBundleOutputSuffix = ".bundle.output.yaml"
fixtureStateOutputSuffix = ".state.output.yaml"
fixtureGlob = "*" + fixtureStateInputSuffix

testAccount = "testaccount"
testEnv = "testenv"
testRegion = "testregion"
)

func testHandleProcessResult(t *testing.T, filePrefix string) {
Expand Down Expand Up @@ -190,28 +195,61 @@ func TestStateResourceName(t *testing.T) {

func entanglerForTests() *wiring.Entangler {
return &wiring.Entangler{
Plugins: registry.KnownWiringPlugins,
Plugins: registry.KnownWiringPlugins(
testDeveloperRole,
testManagedPolicies,
testVPC,
testEnvironment,
),
ClusterLocation: voyager.ClusterLocation{
Account: legacy.TestAccountName,
Region: legacy.TestRegion,
EnvType: legacy.TestEnvironment,
Account: testAccount,
Region: testRegion,
EnvType: testEnv,
},
ClusterConfig: wiringplugin.ClusterConfig{
ClusterDomainName: "internal.ap-southeast-2.kitt-integration.kitt-inf.net",
KittClusterEnv: "test",
Kube2iamAccount: "test",
},
TagNames: wiring.TagNames{
ServiceNameTag: "service_name",
BusinessUnitTag: "business_unit",
ResourceOwnerTag: "resource_owner",
PlatformTag: "platform",
EnvironmentTypeTag: "environment_type",
},
GetLegacyConfigFunc: getTestLegacyConfig,
Tags: testingTags,
}
}

func getTestLegacyConfig(location voyager.Location) *legacy.Config {
return legacy.GetLegacyConfigFromMap(legacy.TestLegacyConfigs, location)
func testDeveloperRole(_ voyager.Location) []string {
return []string{"arn:aws:iam::123456789012:role/micros-server-iam-MicrosServer-ABC"}
}

func testManagedPolicies(_ voyager.Location) []string {
return []string{"arn:aws:iam::123456789012:policy/SOX-DENY-IAM-CREATE-DELETE", "arn:aws:iam::123456789012:policy/micros-iam-DefaultServicePolicy-ABC"} // example
}

func testVPC(location voyager.Location) *oap.VPCEnvironment {
return &oap.VPCEnvironment{
VPCID: "vpc-1",
PrivateDNSZone: "testregion.atl-inf.io",
PrivatePaasDNSZone: "testregion.dev.paas-inf.net",
InstanceSecurityGroup: "sg-2",
JumpboxSecurityGroup: "sg-1",
SSLCertificateID: "arn:aws:acm:testregion:123456789012:certificate/253b42fa-047c-44c2-8bac-777777777777",
Label: location.Label,
AppSubnets: []string{"subnet-1", "subnet-2"},
Zones: []string{"testregiona", "testregionb"},
Region: location.Region,
EMRSubnet: "subnet-1a",
}
}

func testEnvironment(_ voyager.Location) string {
return "microstestenv"
}

func testingTags(
_ voyager.ClusterLocation,
_ wiringplugin.ClusterConfig,
_ voyager.Location,
_ voyager.ServiceName,
_ orch_meta.ServiceProperties,
) map[voyager.Tag]string {
tags := make(map[voyager.Tag]string)
return tags
}
3 changes: 1 addition & 2 deletions pkg/orchestration/wiring/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ go_library(
"//:go_default_library",
"//pkg/apis/orchestration/meta:go_default_library",
"//pkg/apis/orchestration/v1:go_default_library",
"//pkg/orchestration/wiring/legacy:go_default_library",
"//pkg/orchestration/wiring/wiringplugin:go_default_library",
"//vendor/github.com/atlassian/smith/pkg/apis/smith/v1:go_default_library",
"//vendor/github.com/atlassian/smith/pkg/util:go_default_library",
Expand All @@ -35,10 +34,10 @@ go_test(
"//cmd/smith/config:go_default_library",
"//pkg/apis/orchestration/meta:go_default_library",
"//pkg/apis/orchestration/v1:go_default_library",
"//pkg/orchestration/wiring/legacy:go_default_library",
"//pkg/orchestration/wiring/registry:go_default_library",
"//pkg/orchestration/wiring/wiringplugin:go_default_library",
"//pkg/orchestration/wiring/wiringutil/knownshapes:go_default_library",
"//pkg/orchestration/wiring/wiringutil/oap:go_default_library",
"//pkg/util/layers:go_default_library",
"//pkg/util/testutil:go_default_library",
"//vendor/github.com/atlassian/smith/pkg/apis/smith/v1:go_default_library",
Expand Down
7 changes: 6 additions & 1 deletion pkg/orchestration/wiring/aws/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type WiringPlugin struct {

OAPResourceTypeName oap.ResourceType
generateServiceEnvironment ServiceEnvironmentGenerator

VPC func(location voyager.Location) *oap.VPCEnvironment
}

func Resource(resourceType voyager.ResourceType,
Expand All @@ -47,6 +49,7 @@ func Resource(resourceType voyager.ResourceType,
clusterServicePlanExternalID servicecatalog.PlanExternalID,
generateServiceEnvironment ServiceEnvironmentGenerator,
shapes ShapesFunc,
vpc func(voyager.Location) *oap.VPCEnvironment,
) *WiringPlugin {
wiringPlugin := &WiringPlugin{
clusterServiceClassExternalID: clusterServiceClassExternalID,
Expand All @@ -55,6 +58,7 @@ func Resource(resourceType voyager.ResourceType,
shapes: shapes,
OAPResourceTypeName: oapResourceTypeName,
generateServiceEnvironment: generateServiceEnvironment,
VPC: vpc,
}
return wiringPlugin
}
Expand Down Expand Up @@ -148,7 +152,8 @@ func (p *WiringPlugin) instanceParameters(resource *orch_v1.StateResource, conte
}

serviceName := serviceName(userServiceName, context)
environment := p.generateServiceEnvironment(oap.MakeServiceEnvironmentFromContext(context))
vpc := p.VPC(context.StateContext.Location)
environment := p.generateServiceEnvironment(oap.MakeServiceEnvironmentFromContext(context, vpc))
return instanceSpec(serviceName, resourceName, p.OAPResourceTypeName, *environment, attributes, alarms)
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/orchestration/wiring/aws/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ const (

// All osb-aws-provider resources are 'almost' the same, differing only in the service/plan names,
// what they need passed in the ServiceEnvironment.
var ResourceTypes = map[voyager.ResourceType]wiringplugin.WiringPlugin{
DynamoDB: wiringutil.StatusAdapter(Resource(DynamoDB, DynamoDBName, DynamoDBClass, DynamoDBPlan, dynamoDbServiceEnvironment, dynamoDbShapes).WireUp),
S3: wiringutil.StatusAdapter(Resource(S3, S3Name, S3Class, S3Plan, s3ServiceEnvironment, s3Shapes).WireUp),
Cfn: wiringutil.StatusAdapter(Resource(Cfn, CfnName, CfnClass, CfnPlan, CfnServiceEnvironment, cfnShapes).WireUp),
}
var (
DynamoDBPlugin = func(vpc func(voyager.Location) *oap.VPCEnvironment) wiringutil.StatusAdapter {
return wiringutil.StatusAdapter(Resource(DynamoDB, DynamoDBName, DynamoDBClass, DynamoDBPlan, dynamoDbServiceEnvironment, dynamoDbShapes, vpc).WireUp)
}

S3Plugin = func(vpc func(voyager.Location) *oap.VPCEnvironment) wiringutil.StatusAdapter {
return wiringutil.StatusAdapter(Resource(S3, S3Name, S3Class, S3Plan, s3ServiceEnvironment, s3Shapes, vpc).WireUp)
}

CfnPlugin = func(vpc func(voyager.Location) *oap.VPCEnvironment) wiringutil.StatusAdapter {
return wiringutil.StatusAdapter(Resource(Cfn, CfnName, CfnClass, CfnPlan, CfnServiceEnvironment, cfnShapes, vpc).WireUp)
}
)

func cfnShapes(resource *orch_v1.StateResource, smithResource *smith_v1.Resource, _ *wiringplugin.WiringContext) ([]wiringplugin.Shape, bool /* externalErr */, bool /* retriableErr */, error) {
templateName, external, retriable, err := oap.TemplateName(resource.Spec)
Expand Down
Loading