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 actuators around a scoped struct #381

Merged
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
4 changes: 2 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func main() {

// Initialize cluster actuator.
clusterActuator := cluster.NewActuator(cluster.ActuatorParams{
ClustersGetter: cs.ClusterV1alpha1(),
Client: cs.ClusterV1alpha1(),
})

// Initialize machine actuator.
machineActuator := machine.NewActuator(machine.ActuatorParams{
MachinesGetter: cs.ClusterV1alpha1(),
Client: cs.ClusterV1alpha1(),
})

// Register our cluster deployer (the interface is in clusterctl and we define the Deployer interface on the actuator)
Expand Down
22 changes: 22 additions & 0 deletions pkg/cloud/aws/actuators/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["scope.go"],
importpath = "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/awsprovider/v1alpha1:go_default_library",
"//pkg/cloud/aws/services:go_default_library",
"//pkg/cloud/aws/services/ec2:go_default_library",
"//pkg/cloud/aws/services/elb:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws/session:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/elb:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1:go_default_library",
],
)
2 changes: 1 addition & 1 deletion pkg/cloud/aws/actuators/cluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
importpath = "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/cluster",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/awsprovider/v1alpha1:go_default_library",
"//pkg/cloud/aws/actuators:go_default_library",
"//pkg/cloud/aws/services:go_default_library",
"//pkg/cloud/aws/services/certificates:go_default_library",
"//pkg/deployer:go_default_library",
Expand Down
128 changes: 22 additions & 106 deletions pkg/cloud/aws/actuators/cluster/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package cluster
import (
"github.com/pkg/errors"
"k8s.io/klog"
providerv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/certificates"
"sigs.k8s.io/cluster-api-provider-aws/pkg/deployer"
Expand All @@ -29,88 +29,55 @@ import (
type Actuator struct {
*deployer.Deployer

clustersGetter client.ClustersGetter
servicesGetter services.Getter
client client.ClusterV1alpha1Interface
}

// ActuatorParams holds parameter information for Actuator
type ActuatorParams struct {
ClustersGetter client.ClustersGetter
ServicesGetter services.Getter
Client client.ClusterV1alpha1Interface
}

// NewActuator creates a new Actuator
func NewActuator(params ActuatorParams) *Actuator {
res := &Actuator{
clustersGetter: params.ClustersGetter,
servicesGetter: params.ServicesGetter,
client: params.Client,
}

if res.servicesGetter == nil {
res.servicesGetter = services.NewSDKGetter()
}

res.Deployer = deployer.New(res.servicesGetter)
res.Deployer = deployer.New(services.NewSDKGetter())
return res
}

// Reconcile reconciles a cluster and is invoked by the Cluster Controller
func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) (reterr error) {
klog.Infof("Reconciling cluster %v.", cluster.Name)

// Load provider config.
config, err := providerv1.ClusterConfigFromProviderConfig(cluster.Spec.ProviderConfig)
if err != nil {
return errors.Errorf("failed to load cluster provider config: %v", err)
}
func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
klog.Infof("Reconciling cluster %v", cluster.Name)

// Load provider status.
status, err := providerv1.ClusterStatusFromProviderStatus(cluster.Status.ProviderStatus)
scope, err := actuators.NewScope(actuators.ScopeParams{Cluster: cluster, Client: a.client})
if err != nil {
return errors.Errorf("failed to load cluster provider status: %v", err)
return err
}

defer func() {
if err := a.storeClusterConfig(cluster, config); err != nil {
klog.Errorf("failed to store provider config for cluster %q in namespace %q: %v", cluster.Name, cluster.Namespace, err)
}

if err := a.storeClusterStatus(cluster, status); err != nil {
klog.Errorf("failed to store provider status for cluster %q in namespace %q: %v", cluster.Name, cluster.Namespace, err)
}
}()
defer scope.Close()

// Store some config parameters in the status.
status.Region = config.Region

if len(config.CACertificate) == 0 {
if len(scope.ClusterConfig.CACertificate) == 0 {
caCert, caKey, err := certificates.NewCertificateAuthority()
if err != nil {
return errors.Wrap(err, "Failed to generate a CA for the control plane")
}

config.CACertificate = certificates.EncodeCertPEM(caCert)
config.CAPrivateKey = certificates.EncodePrivateKeyPEM(caKey)
scope.ClusterConfig.CACertificate = certificates.EncodeCertPEM(caCert)
scope.ClusterConfig.CAPrivateKey = certificates.EncodePrivateKeyPEM(caKey)
}

// Create new aws session.
sess := a.servicesGetter.Session(config)

// Load ec2 client.
ec2 := a.servicesGetter.EC2(sess)

if err := ec2.ReconcileNetwork(cluster.Name, &status.Network); err != nil {
if err := scope.EC2.ReconcileNetwork(cluster.Name, &scope.ClusterStatus.Network); err != nil {
return errors.Errorf("unable to reconcile network: %v", err)
}

if err := ec2.ReconcileBastion(cluster.Name, config.SSHKeyName, status); err != nil {
if err := scope.EC2.ReconcileBastion(cluster.Name, scope.ClusterConfig.SSHKeyName, scope.ClusterStatus); err != nil {
return errors.Errorf("unable to reconcile network: %v", err)
}

// Load elb client.
elb := a.servicesGetter.ELB(sess)

if err := elb.ReconcileLoadbalancers(cluster.Name, &status.Network); err != nil {
if err := scope.ELB.ReconcileLoadbalancers(cluster.Name, &scope.ClusterStatus.Network); err != nil {
return errors.Errorf("unable to reconcile load balancers: %v", err)
}

Expand All @@ -121,39 +88,22 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) (reterr error) {
func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {
klog.Infof("Deleting cluster %v.", cluster.Name)

// Load provider config.
config, err := providerv1.ClusterConfigFromProviderConfig(cluster.Spec.ProviderConfig)
if err != nil {
return errors.Errorf("failed to load cluster provider config: %v", err)
}

// Load provider status.
status, err := providerv1.ClusterStatusFromProviderStatus(cluster.Status.ProviderStatus)
scope, err := actuators.NewScope(actuators.ScopeParams{Cluster: cluster, Client: a.client})
if err != nil {
return errors.Errorf("failed to load cluster provider status: %v", err)
return err
}

// Store some config parameters in the status.
status.Region = config.Region
defer scope.Close()

// Create new aws session.
sess := a.servicesGetter.Session(config)

// Load ec2 client.
ec2 := a.servicesGetter.EC2(sess)

// Load elb client.
elb := a.servicesGetter.ELB(sess)

if err := elb.DeleteLoadbalancers(cluster.Name, &status.Network); err != nil {
if err := scope.ELB.DeleteLoadbalancers(cluster.Name, &scope.ClusterStatus.Network); err != nil {
return errors.Errorf("unable to delete load balancers: %v", err)
}

if err := ec2.DeleteBastion(cluster.Name, status); err != nil {
if err := scope.EC2.DeleteBastion(cluster.Name, scope.ClusterStatus); err != nil {
return errors.Errorf("unable to delete bastion: %v", err)
}

if err := ec2.DeleteNetwork(cluster.Name, &status.Network); err != nil {
if err := scope.EC2.DeleteNetwork(cluster.Name, &scope.ClusterStatus.Network); err != nil {
klog.Errorf("Error deleting cluster %v: %v.", cluster.Name, err)
return &controllerError.RequeueAfterError{
RequeueAfter: 5 * 1000 * 1000 * 1000,
Expand All @@ -162,37 +112,3 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {

return nil
}

func (a *Actuator) storeClusterConfig(cluster *clusterv1.Cluster, config *providerv1.AWSClusterProviderConfig) error {
clusterClient := a.clustersGetter.Clusters(cluster.Namespace)

ext, err := providerv1.EncodeClusterConfig(config)
if err != nil {
return err
}

cluster.Spec.ProviderConfig.Value = ext

if _, err := clusterClient.Update(cluster); err != nil {
return err
}

return nil
}

func (a *Actuator) storeClusterStatus(cluster *clusterv1.Cluster, status *providerv1.AWSClusterProviderStatus) error {
clusterClient := a.clustersGetter.Clusters(cluster.Namespace)

ext, err := providerv1.EncodeClusterStatus(status)
if err != nil {
return err
}

cluster.Status.ProviderStatus = ext

if _, err := clusterClient.UpdateStatus(cluster); err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions pkg/cloud/aws/actuators/machine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/awsprovider/v1alpha1:go_default_library",
"//pkg/cloud/aws/actuators:go_default_library",
"//pkg/cloud/aws/services:go_default_library",
"//pkg/cloud/aws/services/awserrors:go_default_library",
"//pkg/deployer:go_default_library",
Expand Down
Loading