Skip to content

Commit

Permalink
update NodeLabelController to allow for external importing
Browse files Browse the repository at this point in the history
  • Loading branch information
joemiller committed Nov 8, 2024
1 parent 0695cc2 commit 142bc31
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
44 changes: 24 additions & 20 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ import (

type NodeLabelController struct {
client.Client
awsEC2Client ec2Client
gcpGCEClient gceClient
labels []string
cloud string
EC2Client ec2Client
GCEClient gceClient

// Labels is a list of label keys to sync from the node to the cloud provider
Labels []string

// Cloud is the cloud provider (aws or gcp)
Cloud string
}

func (r *NodeLabelController) SetupCloudProvider(ctx context.Context) error {
switch r.cloud {
switch r.Cloud {
case "aws":
cfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("unable to load AWS config: %v", err)
}
r.awsEC2Client = ec2.NewFromConfig(cfg)
r.EC2Client = ec2.NewFromConfig(cfg)
case "gcp":
c, err := gce.NewService(ctx)
if err != nil {
return fmt.Errorf("unable to create GCP client: %v", err)
}
r.gcpGCEClient = newGCEComputeClient(c)
r.GCEClient = newGCEComputeClient(c)
default:
return fmt.Errorf("unsupported cloud provider: %q", r.cloud)
return fmt.Errorf("unsupported cloud provider: %q", r.Cloud)
}
return nil
}
Expand All @@ -61,15 +65,15 @@ func (r *NodeLabelController) SetupWithManager(mgr ctrl.Manager) error {
if !ok {
return false
}
return shouldProcessNodeUpdate(oldNode, newNode, r.labels)
return shouldProcessNodeUpdate(oldNode, newNode, r.Labels)
},

CreateFunc: func(e event.CreateEvent) bool {
node, ok := e.Object.(*corev1.Node)
if !ok {
return false
}
return shouldProcessNodeCreate(node, r.labels)
return shouldProcessNodeCreate(node, r.Labels)
},

DeleteFunc: func(e event.DeleteEvent) bool {
Expand Down Expand Up @@ -136,14 +140,14 @@ func (r *NodeLabelController) Reconcile(ctx context.Context, req ctrl.Request) (
}

labels := make(map[string]string)
for _, k := range r.labels {
for _, k := range r.Labels {
if value, exists := node.Labels[k]; exists {
labels[k] = value
}
}

var err error
switch r.cloud {
switch r.Cloud {
case "aws":
err = r.syncAWSTags(ctx, providerID, labels)
case "gcp":
Expand All @@ -165,7 +169,7 @@ func (r *NodeLabelController) syncAWSTags(ctx context.Context, providerID string
return fmt.Errorf("invalid AWS provider ID format: %q", providerID)
}

result, err := r.awsEC2Client.DescribeTags(ctx, &ec2.DescribeTagsInput{
result, err := r.EC2Client.DescribeTags(ctx, &ec2.DescribeTagsInput{
Filters: []types.Filter{
{
Name: aws.String("resource-id"),
Expand All @@ -179,7 +183,7 @@ func (r *NodeLabelController) syncAWSTags(ctx context.Context, providerID string

currentTags := make(map[string]string)
for _, tag := range result.Tags {
if key := aws.ToString(tag.Key); key != "" && slices.Contains(r.labels, key) {
if key := aws.ToString(tag.Key); key != "" && slices.Contains(r.Labels, key) {
currentTags[key] = aws.ToString(tag.Value)
}
}
Expand All @@ -199,7 +203,7 @@ func (r *NodeLabelController) syncAWSTags(ctx context.Context, providerID string

// find monitored tags to remove
for k := range currentTags {
if slices.Contains(r.labels, k) {
if slices.Contains(r.Labels, k) {
if _, exists := desiredLabels[k]; !exists {
toDelete = append(toDelete, types.Tag{
Key: aws.String(k),
Expand All @@ -209,7 +213,7 @@ func (r *NodeLabelController) syncAWSTags(ctx context.Context, providerID string
}

if len(toAdd) > 0 {
_, err := r.awsEC2Client.CreateTags(ctx, &ec2.CreateTagsInput{
_, err := r.EC2Client.CreateTags(ctx, &ec2.CreateTagsInput{
Resources: []string{instanceID},
Tags: toAdd,
})
Expand All @@ -219,7 +223,7 @@ func (r *NodeLabelController) syncAWSTags(ctx context.Context, providerID string
}

if len(toDelete) > 0 {
_, err := r.awsEC2Client.DeleteTags(ctx, &ec2.DeleteTagsInput{
_, err := r.EC2Client.DeleteTags(ctx, &ec2.DeleteTagsInput{
Resources: []string{instanceID},
Tags: toDelete,
})
Expand All @@ -237,7 +241,7 @@ func (r *NodeLabelController) syncGCPLabels(ctx context.Context, providerID stri
return fmt.Errorf("failed to parse GCP provider ID: %v", err)
}

instance, err := r.gcpGCEClient.GetInstance(ctx, project, zone, name)
instance, err := r.GCEClient.GetInstance(ctx, project, zone, name)
if err != nil {
return fmt.Errorf("failed to get GCP instance: %v", err)
}
Expand All @@ -249,7 +253,7 @@ func (r *NodeLabelController) syncGCPLabels(ctx context.Context, providerID stri

// create a set of sanitized monitored keys for easy lookup
monitoredKeys := make(map[string]string) // sanitized -> original
for _, k := range r.labels {
for _, k := range r.Labels {
monitoredKeys[sanitizeKeyForGCP(k)] = k
}

Expand All @@ -272,7 +276,7 @@ func (r *NodeLabelController) syncGCPLabels(ctx context.Context, providerID stri
return nil
}

err = r.gcpGCEClient.SetLabels(ctx, project, zone, name, &gce.InstancesSetLabelsRequest{
err = r.GCEClient.SetLabels(ctx, project, zone, name, &gce.InstancesSetLabelsRequest{
Labels: newLabels,
LabelFingerprint: instance.LabelFingerprint,
})
Expand Down
16 changes: 8 additions & 8 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func TestReconcileAWS(t *testing.T) {
mock := &mockEC2Client{currentTags: tt.currentTags}

r := &NodeLabelController{
Client: k8s,
labels: tt.labelsToCopy,
cloud: "aws",
awsEC2Client: mock,
Client: k8s,
Labels: tt.labelsToCopy,
Cloud: "aws",
EC2Client: mock,
}

_, err := r.Reconcile(context.Background(), ctrl.Request{
Expand Down Expand Up @@ -200,10 +200,10 @@ func TestReconcileGCP(t *testing.T) {
mock := &mockGCEClient{instance: &gce.Instance{Labels: tt.currentLabels}}

r := &NodeLabelController{
Client: k8s,
labels: tt.labelsToCopy,
cloud: "gcp",
gcpGCEClient: mock,
Client: k8s,
Labels: tt.labelsToCopy,
Cloud: "gcp",
GCEClient: mock,
}

_, err := r.Reconcile(context.Background(), ctrl.Request{
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func main() {

controller := &NodeLabelController{
Client: mgr.GetClient(),
labels: labelKeys,
cloud: cloudProvider,
Labels: labelKeys,
Cloud: cloudProvider,
}

ctx := ctrl.SetupSignalHandler()
Expand Down

0 comments on commit 142bc31

Please sign in to comment.