Skip to content

Commit

Permalink
Remove code that should be in bootstrap controller
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jul 15, 2019
1 parent 795a8ee commit b031db8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 336 deletions.
31 changes: 7 additions & 24 deletions pkg/cloud/aws/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *Service) InstanceIfExists(id *string) (*v1alpha2.Instance, error) {
}

// createInstance runs an ec2 instance.
func (s *Service) createInstance(scope *actuators.MachineScope, bootstrapToken string) (*v1alpha2.Instance, error) {
func (s *Service) createInstance(scope *actuators.MachineScope) (*v1alpha2.Instance, error) {
s.scope.V(2).Info("Creating an instance for a machine")

input := &v1alpha2.Instance{
Expand Down Expand Up @@ -171,32 +171,15 @@ func (s *Service) createInstance(scope *actuators.MachineScope, bootstrapToken s
)
}

s.scope.V(3).Info("Generating CA key pair")
// caCertHash, err := certificates.GenerateCertificateHash(s.scope.ClusterConfig.CAKeyPair.Cert)
// if err != nil {
// return input, err
// }

// apiServerEndpoint := fmt.Sprintf("%s:%d", scope.Network().APIServerELB.DNSName, apiServerBindPort)

// apply values based on the role of the machine
switch scope.Role() {
case "controlplane":
// TODO
case "node":
// TODO

default:
return nil, errors.Errorf("Unknown node role %q", scope.Role())
}
// Set userdata.
input.UserData = aws.String(*scope.Machine.Spec.Bootstrap.Data)

// Set security groups.
ids, err := s.GetCoreSecurityGroups(scope)
if err != nil {
return nil, err
}
input.SecurityGroupIDs = append(input.SecurityGroupIDs,
ids...,
)
input.SecurityGroupIDs = append(input.SecurityGroupIDs, ids...)

// Pick SSH key, if any.
if scope.ProviderMachine.Spec.KeyName != "" {
Expand Down Expand Up @@ -301,7 +284,7 @@ func (s *Service) MachineExists(scope *actuators.MachineScope) (bool, error) {
}

// CreateOrGetMachine will either return an existing instance or create and return an instance.
func (s *Service) CreateOrGetMachine(scope *actuators.MachineScope, bootstrapToken string) (*v1alpha2.Instance, error) {
func (s *Service) CreateOrGetMachine(scope *actuators.MachineScope) (*v1alpha2.Instance, error) {
s.scope.V(2).Info("Attempting to create or get machine")

// instance id exists, try to get it
Expand All @@ -324,7 +307,7 @@ func (s *Service) CreateOrGetMachine(scope *actuators.MachineScope, bootstrapTok
return instance, nil
}

return s.createInstance(scope, bootstrapToken)
return s.createInstance(scope)
}

func (s *Service) runInstance(role string, i *v1alpha2.Instance) (*v1alpha2.Instance, error) {
Expand Down
70 changes: 6 additions & 64 deletions pkg/controller/awsmachine/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package awsmachine
import (
"context"
"fmt"
"path"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -29,14 +28,11 @@ import (
"k8s.io/apimachinery/pkg/runtime"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog"
"sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/ec2"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/elb"
"sigs.k8s.io/cluster-api-provider-aws/pkg/tokens"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
"sigs.k8s.io/cluster-api/pkg/controller/remote"
capierrors "sigs.k8s.io/cluster-api/pkg/errors"
"sigs.k8s.io/cluster-api/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -63,16 +59,9 @@ func Add(mgr manager.Manager) error {

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) *ReconcileAWSMachine {
coreClient, err := corev1.NewForConfig(mgr.GetConfig())
if err != nil {
klog.Fatalf("Failed to create corev1 client from configuration: %v", err)
}

return &ReconcileAWSMachine{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
coreClient: coreClient,
controlPlaneInitLocker: newControlPlaneInitLocker(coreClient),
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
}
}

Expand Down Expand Up @@ -110,9 +99,8 @@ var _ reconcile.Reconciler = &ReconcileAWSMachine{}
// ReconcileAWSMachine reconciles a AWSMachine object
type ReconcileAWSMachine struct {
client.Client
scheme *runtime.Scheme
coreClient corev1.CoreV1Interface
controlPlaneInitLocker ControlPlaneInitLocker
scheme *runtime.Scheme
coreClient corev1.CoreV1Interface
}

// Reconcile reads that state of the cluster for a AWSMachine object and makes changes based on the state read
Expand Down Expand Up @@ -217,35 +205,8 @@ func (r *ReconcileAWSMachine) create(scope *actuators.MachineScope) error {
return &capierrors.RequeueAfterError{RequeueAfter: waitForControlPlaneMachineExistenceDuration}
}

join, err := r.isNodeJoin(scope)
if err != nil {
return err
}

var bootstrapToken string
if join {
remoteClient, err := remote.NewClusterClient(r.Client, scope.Cluster.Cluster)
if err != nil {
return errors.Wrapf(err, "unable to proceed until control plane is ready (error creating client) for cluster %q",
path.Join(scope.Cluster.Namespace(), scope.Cluster.Name()))
}

coreClient, err := remoteClient.CoreV1()
if err != nil {
return errors.Wrapf(err, "unable to proceed until control plane is ready (error creating client) for cluster %q",
path.Join(scope.Cluster.Namespace(), scope.Cluster.Name()))
}

scope.Info("Machine will join the cluster")
bootstrapToken, err = tokens.NewBootstrap(coreClient, defaultTokenTTL)
if err != nil {
return errors.Wrapf(err, "failed to create new bootstrap token")
}
} else {
scope.Info("Machine will init the cluster")
}

i, err := ec2svc.CreateOrGetMachine(scope, bootstrapToken)
// Create the Machine.
i, err := ec2svc.CreateOrGetMachine(scope)
if err != nil {
return errors.Errorf("failed to create or get machine: %+v", err)
}
Expand Down Expand Up @@ -455,22 +416,3 @@ func (r *ReconcileAWSMachine) isMachineOutdated(spec *infrav1.AWSMachineSpec, i

return errs
}

func (r *ReconcileAWSMachine) isNodeJoin(scope *actuators.MachineScope) (bool, error) {
if scope.Cluster.Cluster.Annotations[v1alpha1.AnnotationControlPlaneReady] == v1alpha1.ValueReady {
return true, nil
}

if !util.IsControlPlaneMachine(scope.Machine) {
// This isn't a control plane machine - have to wait
scope.Info("No control plane machines exist yet - requeuing")
return true, &capierrors.RequeueAfterError{RequeueAfter: waitForControlPlaneMachineExistenceDuration}
}

if r.controlPlaneInitLocker.Acquire(scope.Cluster) {
return false, nil
}

scope.Info("Unable to acquire control plane configmap lock - requeuing")
return true, &capierrors.RequeueAfterError{RequeueAfter: waitForControlPlaneReadyDuration}
}
99 changes: 0 additions & 99 deletions pkg/controller/awsmachine/controlplane_init_locker.go

This file was deleted.

Loading

0 comments on commit b031db8

Please sign in to comment.