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

[WIP] Add ability to sync aws machine and node labels #1876

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
"sigs.k8s.io/cluster-api/controllers/remote"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
Expand All @@ -53,6 +54,7 @@ import (
type AWSMachineReconciler struct {
client.Client
Log logr.Logger
Tracker *remote.ClusterCacheTracker
Recorder record.EventRecorder
ec2ServiceFactory func(*scope.ClusterScope) services.EC2MachineInterface
secretsManagerServiceFactory func(*scope.ClusterScope) services.SecretsManagerInterface
Expand Down Expand Up @@ -483,6 +485,17 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
// TODO(vincepri): Remove this annotation when clusterctl is no longer relevant.
machineScope.SetAnnotation("cluster-api-provider-aws", "true")

// Get the remote cluster client.
remoteClient, err := r.Tracker.GetClient(context.Background(), util.ObjectKey(machineScope.Cluster))
if err != nil {
return ctrl.Result{}, err
}

err = machineScope.SyncMachineNodeLabel(context.Background(), remoteClient)
if err != nil {
return ctrl.Result{}, err
}

switch instance.State {
case infrav1.InstanceStatePending:
machineScope.SetNotReady()
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/pkg/record"
"sigs.k8s.io/cluster-api-provider-aws/version"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/controllers/remote"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
Expand Down Expand Up @@ -67,6 +68,7 @@ func main() {
profilerAddress string
awsClusterConcurrency int
awsMachineConcurrency int
clusterConcurrency int
syncPeriod time.Duration
webhookPort int
healthAddr string
Expand Down Expand Up @@ -119,6 +121,9 @@ func main() {
"Number of AWSMachines to process simultaneously",
)

flag.IntVar(&clusterConcurrency, "cluster-concurrency", 10,
"Number of clusters to process simultaneously")

flag.DurationVar(&syncPeriod,
"sync-period",
10*time.Minute,
Expand Down Expand Up @@ -179,10 +184,30 @@ func main() {
record.InitFromRecorder(mgr.GetEventRecorderFor("aws-controller"))

if webhookPort == 0 {
// Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers
// requiring a connection to a remote cluster
tracker, err := remote.NewClusterCacheTracker(
ctrl.Log.WithName("remote").WithName("ClusterCacheTracker"),
mgr,
)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
os.Exit(1)
}
if err := (&remote.ClusterCacheReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
Tracker: tracker,
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: clusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler")
os.Exit(1)
}

if err = (&controllers.AWSMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AWSMachine"),
Recorder: mgr.GetEventRecorderFor("awsmachine-controller"),
Tracker: tracker,
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: awsMachineConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AWSMachine")
os.Exit(1)
Expand Down
37 changes: 37 additions & 0 deletions pkg/cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"fmt"
"strings"

"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand Down Expand Up @@ -181,6 +182,42 @@ func (m *MachineScope) SetAnnotation(key, value string) {
m.AWSMachine.Annotations[key] = value
}

// SyncMachineNodeLabel sets a key value label on the AWSMachine node.
func (m *MachineScope) SyncMachineNodeLabel(ctx context.Context, remoteClient client.Client) error {
Copy link
Member

Choose a reason for hiding this comment

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

Is SyncMachineNodeLabel really accurate as a function name?

Looks like we only ever copy labels if they have the correct prefix. There's no ability to remove these labels.

if m.Machine.Status.NodeRef == nil {
return nil
}

node := &corev1.Node{}
err := remoteClient.Get(ctx, types.NamespacedName{Name: m.Machine.Status.NodeRef.Name}, node)
if err != nil {
return err
}

if node.Labels == nil {
node.Labels = map[string]string{}
}

machineLabels := m.AWSMachine.Labels
if machineLabels == nil {
return nil
}

for key, value := range machineLabels {
// sync only labels with "cluster.x-k8s.io", so users can understand where labels come from
if strings.HasPrefix(key, "cluster.x-k8s.io") {
node.Labels[key] = value
}
}

err = remoteClient.Update(ctx, node)
if err != nil {
return err
}

return nil
}

// UseSecretsManager returns the computed value of whether or not
// userdata should be stored using AWS Secrets Manager.
func (m *MachineScope) UseSecretsManager() bool {
Expand Down