Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ refactor: cluster scope and service refactor for future EKS support #1810

Merged
merged 3 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 30 additions & 3 deletions pkg/cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,62 @@ type Session interface {
Session() awsclient.ConfigProvider
}

// ScopeUsage is used to indicate which controller is using a scope
type ScopeUsage interface {
// ControllerName returns the name of the controller that created the scope
ControllerName() string
}
randomvariable marked this conversation as resolved.
Show resolved Hide resolved

// ClusterObject represents a AWS cluster object
type ClusterObject interface {
conditions.Setter
}

// ClusterScoper is the interface for a cluster scopde
// ClusterScoper is the interface for a cluster scope
type ClusterScoper interface {
logr.Logger
Session
ScopeUsage

// Name returns the cluster name.
Name() string
// Namespace returns the cluster namespace.
Namespace() string
// Region returns the cluster region.
Region() string

// InfraCluster returns the AWS infrastructure cluster object.
InfraCluster() ClusterObject

// Network returns the cluster network object.
Network() *infrav1.Network
// VPC returns the cluster VPC.
VPC() *infrav1.VPCSpec
// Subnets returns the cluster subnets.
Subnets() infrav1.Subnets
// SetSubnets updates the clusters subnets.
SetSubnets(subnets infrav1.Subnets)
// CNIIngressRules returns the CNI spec ingress rules.
CNIIngressRules() infrav1.CNIIngressRules
// SecurityGroups returns the cluster security groups as a map, it creates the map if empty.
SecurityGroups() map[infrav1.SecurityGroupRole]infrav1.SecurityGroup
// ListOptionsLabelSelector returns a ListOptions with a label selector for clusterName.
ListOptionsLabelSelector() client.ListOption
PatchObject() error
Close() error
// APIServerPort returns the port to use when communicating with the API server.
APIServerPort() int32
// AdditionalTags returns any tags that you would like to attach to AWS resources. The returned value will never be nil.
AdditionalTags() infrav1.Tags
// SetFailureDomain sets the infrastructure provider failure domain key to the spec given as input.
SetFailureDomain(id string, spec clusterv1.FailureDomainSpec)
// Bastion returns the bastion details for the cluster.
Bastion() *infrav1.Bastion
// SetBastionInstance sets the bastion instance in the status of the cluster.
SetBastionInstance(instance *infrav1.Instance)
// SSHKeyName returns the SSH key name to use for instances.
SSHKeyName() *string

// PatchObject persists the cluster configuration and status.
PatchObject() error
// Close closes the current scope persisting the cluster configuration and status.
Close() error
randomvariable marked this conversation as resolved.
Show resolved Hide resolved
}
21 changes: 13 additions & 8 deletions pkg/cloud/scope/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,49 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud"
awsmetrics "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/metrics"
"sigs.k8s.io/cluster-api-provider-aws/pkg/record"
"sigs.k8s.io/cluster-api-provider-aws/version"
)

// NewEC2Client creates a new EC2 API client for a given session
func NewEC2Client(session cloud.Session, target runtime.Object) ec2iface.EC2API {
func NewEC2Client(scopeUser cloud.ScopeUsage, session cloud.Session, target runtime.Object) ec2iface.EC2API {
ec2Client := ec2.New(session.Session())
ec2Client.Handlers.Build.PushFrontNamed(getUserAgentHandler())
ec2Client.Handlers.CompleteAttempt.PushFront(awsmetrics.CaptureRequestMetrics(scopeUser.ControllerName()))
ec2Client.Handlers.Complete.PushBack(recordAWSPermissionsIssue(target))

return ec2Client
}

// NewELBClient creates a new ELB API client for a given session
func NewELBClient(session cloud.Session, target runtime.Object) elbiface.ELBAPI {
func NewELBClient(scopeUser cloud.ScopeUsage, session cloud.Session, target runtime.Object) elbiface.ELBAPI {
elbClient := elb.New(session.Session())
elbClient.Handlers.Build.PushFrontNamed(getUserAgentHandler())
elbClient.Handlers.CompleteAttempt.PushFront(awsmetrics.CaptureRequestMetrics(scopeUser.ControllerName()))
elbClient.Handlers.Complete.PushBack(recordAWSPermissionsIssue(target))

return elbClient
}

// NewResourgeTaggingClient creates a new Resource Tagging API client for a given session
func NewResourgeTaggingClient(session cloud.Session, target runtime.Object) resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI {
func NewResourgeTaggingClient(scopeUser cloud.ScopeUsage, session cloud.Session, target runtime.Object) resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI {
resourceTagging := resourcegroupstaggingapi.New(session.Session())
resourceTagging.Handlers.Build.PushFrontNamed(getUserAgentHandler())
resourceTagging.Handlers.CompleteAttempt.PushFront(awsmetrics.CaptureRequestMetrics(scopeUser.ControllerName()))
resourceTagging.Handlers.Complete.PushBack(recordAWSPermissionsIssue(target))

return resourceTagging
}

// NewSecretsManagerClient creates a new Secrets API client for a given session
func NewSecretsManagerClient(session cloud.Session, target runtime.Object) secretsmanageriface.SecretsManagerAPI {
sClient := secretsmanager.New(session.Session())
sClient.Handlers.Build.PushFrontNamed(getUserAgentHandler())
sClient.Handlers.Complete.PushBack(recordAWSPermissionsIssue(target))
func NewSecretsManagerClient(scopeUser cloud.ScopeUsage, session cloud.Session, target runtime.Object) secretsmanageriface.SecretsManagerAPI {
secretsClient := secretsmanager.New(session.Session())
secretsClient.Handlers.Build.PushFrontNamed(getUserAgentHandler())
secretsClient.Handlers.CompleteAttempt.PushFront(awsmetrics.CaptureRequestMetrics(scopeUser.ControllerName()))
secretsClient.Handlers.Complete.PushBack(recordAWSPermissionsIssue(target))

return sClient
return secretsClient
}

func recordAWSPermissionsIssue(target runtime.Object) func(r *request.Request) {
Expand Down
30 changes: 19 additions & 11 deletions pkg/cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func NewClusterScope(params ClusterScopeParams) (*ClusterScope, error) {
return nil, errors.Wrap(err, "failed to init patch helper")
}
return &ClusterScope{
Logger: params.Logger,
client: params.Client,
Cluster: params.Cluster,
AWSCluster: params.AWSCluster,
patchHelper: helper,
session: session,
Logger: params.Logger,
client: params.Client,
Cluster: params.Cluster,
AWSCluster: params.AWSCluster,
patchHelper: helper,
session: session,
controllerName: params.ControllerName,
}, nil
}

Expand All @@ -83,7 +84,8 @@ type ClusterScope struct {
Cluster *clusterv1.Cluster
AWSCluster *infrav1.AWSCluster

session awsclient.ConfigProvider
session awsclient.ConfigProvider
controllerName string
}

// Network returns the cluster network object.
Expand All @@ -101,7 +103,7 @@ func (s *ClusterScope) Subnets() infrav1.Subnets {
return s.AWSCluster.Spec.NetworkSpec.Subnets
}

// SetSubnets updates the clusters subnets
// SetSubnets updates the clusters subnets.
func (s *ClusterScope) SetSubnets(subnets infrav1.Subnets) {
s.AWSCluster.Spec.NetworkSpec.Subnets = subnets
}
Expand Down Expand Up @@ -219,17 +221,23 @@ func (s *ClusterScope) Session() awsclient.ConfigProvider {
return s.session
}

// Bastion returns the bastion details
// Bastion returns the bastion details.
func (s *ClusterScope) Bastion() *infrav1.Bastion {
return &s.AWSCluster.Spec.Bastion
}

// SetBastionInstance sets the bastion instance in the status of the cluster
// SetBastionInstance sets the bastion instance in the status of the cluster.
func (s *ClusterScope) SetBastionInstance(instance *infrav1.Instance) {
s.AWSCluster.Status.Bastion = instance
}

// SSHKeyName returns the SSH key name to use for instances
// SSHKeyName returns the SSH key name to use for instances.
func (s *ClusterScope) SSHKeyName() *string {
return s.AWSCluster.Spec.SSHKeyName
}

// ControllerName returns the name of the controller that
// created the ClusterScope.
func (s *ClusterScope) ControllerName() string {
return s.controllerName
}
2 changes: 1 addition & 1 deletion pkg/cloud/services/ec2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ type Service struct {
func NewService(clusterScope cloud.ClusterScoper) *Service {
return &Service{
scope: clusterScope,
EC2Client: scope.NewEC2Client(clusterScope, clusterScope.InfraCluster()),
EC2Client: scope.NewEC2Client(clusterScope, clusterScope, clusterScope.InfraCluster()),
}
}
4 changes: 2 additions & 2 deletions pkg/cloud/services/elb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Service struct {
func NewService(elbScope Scope) *Service {
return &Service{
scope: elbScope,
ELBClient: scope.NewELBClient(elbScope, elbScope.InfraCluster()),
ResourceTaggingClient: scope.NewResourgeTaggingClient(elbScope, elbScope.InfraCluster()),
ELBClient: scope.NewELBClient(elbScope, elbScope, elbScope.InfraCluster()),
ResourceTaggingClient: scope.NewResourgeTaggingClient(elbScope, elbScope, elbScope.InfraCluster()),
}
}
2 changes: 1 addition & 1 deletion pkg/cloud/services/secretsmanager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ type Service struct {
func NewService(secretsScope cloud.ClusterScoper) *Service {
return &Service{
scope: secretsScope,
SecretsManagerClient: scope.NewSecretsManagerClient(secretsScope, secretsScope.InfraCluster()),
SecretsManagerClient: scope.NewSecretsManagerClient(secretsScope, secretsScope, secretsScope.InfraCluster()),
}
}