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

Scope nodeRef to workload cluster #744

Merged
merged 1 commit into from
Apr 25, 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
1 change: 1 addition & 0 deletions cmd/manager/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_library(
"//pkg/cloud/aws/actuators/cluster:go_default_library",
"//pkg/cloud/aws/actuators/machine:go_default_library",
"//pkg/record:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/apis:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/common:go_default_library",
Expand Down
9 changes: 8 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"flag"
"time"

corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog"
"sigs.k8s.io/cluster-api-provider-aws/pkg/apis"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/cluster"
Expand Down Expand Up @@ -65,6 +66,11 @@ func main() {
klog.Fatalf("Failed to create client from configuration: %v", err)
}

coreClient, err := corev1.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Failed to create corev1 client from configuration: %v", err)
}

// Initialize event recorder.
record.InitFromRecorder(mgr.GetRecorder("aws-controller"))

Expand All @@ -76,7 +82,8 @@ func main() {

// Initialize machine actuator.
machineActuator := machine.NewActuator(machine.ActuatorParams{
Client: cs.ClusterV1alpha1(),
CoreClient: coreClient,
ClusterClient: cs.ClusterV1alpha1(),
LoggingContext: "[machine-actuator]",
})

Expand Down
44 changes: 20 additions & 24 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,25 @@ const (
type Actuator struct {
*deployer.Deployer

client client.ClusterV1alpha1Interface
log logr.Logger
coreClient corev1.CoreV1Interface
clusterClient client.ClusterV1alpha1Interface
log logr.Logger
}

// ActuatorParams holds parameter information for Actuator.
type ActuatorParams struct {
Client client.ClusterV1alpha1Interface
CoreClient corev1.CoreV1Interface
ClusterClient client.ClusterV1alpha1Interface
LoggingContext string
}

// NewActuator returns an actuator.
func NewActuator(params ActuatorParams) *Actuator {
return &Actuator{
Deployer: deployer.New(deployer.Params{ScopeGetter: actuators.DefaultScopeGetter}),
client: params.Client,
log: klogr.New().WithName(params.LoggingContext),
Deployer: deployer.New(deployer.Params{ScopeGetter: actuators.DefaultScopeGetter}),
coreClient: params.CoreClient,
clusterClient: params.ClusterClient,
log: klogr.New().WithName(params.LoggingContext),
}
}

Expand Down Expand Up @@ -102,7 +105,7 @@ func (a *Actuator) isNodeJoin(scope *actuators.MachineScope, controlPlaneMachine
m, err := actuators.NewMachineScope(actuators.MachineScopeParams{
Machine: cm,
Cluster: scope.Cluster,
Client: a.client,
Client: a.clusterClient,
Logger: a.log,
})

Expand Down Expand Up @@ -134,7 +137,7 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
}
a.log.Info("Creating machine in cluster", "machine-name", machine.Name, "machine-namespace", machine.Namespace, "cluster-name", cluster.Name)

scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client, Logger: a.log})
scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.clusterClient, Logger: a.log})
if err != nil {
return errors.Errorf("failed to create scope: %+v", err)
}
Expand Down Expand Up @@ -243,7 +246,7 @@ func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machi
}
a.log.Info("Deleting machine in cluster", "machine-name", machine.Name, "machine-namespace", machine.Namespace, "cluster-name", cluster.Name)

scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client, Logger: a.log})
scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.clusterClient, Logger: a.log})
if err != nil {
return errors.Errorf("failed to create scope: %+v", err)
}
Expand Down Expand Up @@ -349,7 +352,7 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, machi

a.log.Info("Updating machine in cluster", "machine-name", machine.Name, "machine-namespace", machine.Namespace, "cluster-name", cluster.Name)

scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client, Logger: a.log})
scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.clusterClient, Logger: a.log})
if err != nil {
return errors.Errorf("failed to create scope: %+v", err)
}
Expand Down Expand Up @@ -400,7 +403,7 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi

a.log.Info("Checking if machine exists in cluster", "machine-name", machine.Name, "machine-namespace", machine.Namespace, "cluster-name", cluster.Name)

scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client, Logger: a.log})
scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.clusterClient, Logger: a.log})
if err != nil {
return false, errors.Errorf("failed to create scope: %+v", err)
}
Expand Down Expand Up @@ -451,14 +454,12 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
// Set the Machine NodeRef.
if machine.Status.NodeRef == nil {
nodeRef, err := a.getNodeReference(scope)
if err != nil {
a.log.Info("Failed to set nodeRef", "error", err)
// non critical error
return true, nil
if err == nil {
scope.Machine.Status.NodeRef = nodeRef
a.log.V(2).Info("Setting machine's nodeRef", "machine-name", scope.Name(), "machine-namespace", scope.Namespace(), "nodeRef", nodeRef.Name)
} else {
a.log.Info("Cannot set nodeRef", "error", err)
}

scope.Machine.Status.NodeRef = nodeRef
a.log.V(2).Info("Setting machine's nodeRef", "machine-name", scope.Name(), "machine-namespace", scope.Namespace(), "nodeRef", nodeRef.Name)
}

return true, nil
Expand All @@ -467,15 +468,10 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
func (a *Actuator) getNodeReference(scope *actuators.MachineScope) (*apicorev1.ObjectReference, error) {
instanceID := *scope.MachineStatus.InstanceID

coreClient, err := a.coreV1Client(scope.Cluster)
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve corev1 client for cluster %q", scope.Cluster.Name)
}

listOpt := metav1.ListOptions{}

for {
nodeList, err := coreClient.Nodes().List(listOpt)
nodeList, err := a.coreClient.Nodes().List(listOpt)
if err != nil {
return nil, errors.Wrap(err, "failed to query cluster nodes")
}
Expand Down