|
| 1 | +/* |
| 2 | +Copyright 2024 The CloudPilot AI Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tagging |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/awslabs/operatorpkg/reasonable" |
| 25 | + "github.com/samber/lo" |
| 26 | + "k8s.io/apimachinery/pkg/api/equality" |
| 27 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 34 | + karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1" |
| 35 | + "sigs.k8s.io/karpenter/pkg/cloudprovider" |
| 36 | + "sigs.k8s.io/karpenter/pkg/operator/injection" |
| 37 | + |
| 38 | + "github.com/cloudpilot-ai/karpenter-provider-alicloud/pkg/apis/v1alpha1" |
| 39 | + "github.com/cloudpilot-ai/karpenter-provider-alicloud/pkg/operator/options" |
| 40 | + "github.com/cloudpilot-ai/karpenter-provider-alicloud/pkg/providers/instance" |
| 41 | + "github.com/cloudpilot-ai/karpenter-provider-alicloud/pkg/utils" |
| 42 | +) |
| 43 | + |
| 44 | +type Controller struct { |
| 45 | + kubeClient client.Client |
| 46 | + instanceProvider instance.Provider |
| 47 | +} |
| 48 | + |
| 49 | +func NewController(kubeClient client.Client, instanceProvider instance.Provider) *Controller { |
| 50 | + return &Controller{ |
| 51 | + kubeClient: kubeClient, |
| 52 | + instanceProvider: instanceProvider, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (c *Controller) Reconcile(ctx context.Context, nodeClaim *karpv1.NodeClaim) (reconcile.Result, error) { |
| 57 | + ctx = injection.WithControllerName(ctx, "nodeclaim.tagging") |
| 58 | + |
| 59 | + stored := nodeClaim.DeepCopy() |
| 60 | + if !isTaggable(nodeClaim) { |
| 61 | + return reconcile.Result{}, nil |
| 62 | + } |
| 63 | + ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("provider-id", nodeClaim.Status.ProviderID)) |
| 64 | + id, err := utils.ParseInstanceID(nodeClaim.Status.ProviderID) |
| 65 | + if err != nil { |
| 66 | + // We don't throw an error here since we don't want to retry until the ProviderID has been updated. |
| 67 | + log.FromContext(ctx).Error(err, "failed parsing instance id") |
| 68 | + return reconcile.Result{}, nil |
| 69 | + } |
| 70 | + if err = c.tagInstance(ctx, nodeClaim, id); err != nil { |
| 71 | + return reconcile.Result{}, cloudprovider.IgnoreNodeClaimNotFoundError(err) |
| 72 | + } |
| 73 | + nodeClaim.Annotations = lo.Assign(nodeClaim.Annotations, map[string]string{ |
| 74 | + v1alpha1.AnnotationInstanceTagged: "true", |
| 75 | + v1alpha1.AnnotationClusterNameTaggedCompatability: "true", |
| 76 | + }) |
| 77 | + if !equality.Semantic.DeepEqual(nodeClaim, stored) { |
| 78 | + if err := c.kubeClient.Patch(ctx, nodeClaim, client.MergeFrom(stored)); err != nil { |
| 79 | + return reconcile.Result{}, client.IgnoreNotFound(err) |
| 80 | + } |
| 81 | + } |
| 82 | + return reconcile.Result{}, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (c *Controller) Register(_ context.Context, m manager.Manager) error { |
| 86 | + return controllerruntime.NewControllerManagedBy(m). |
| 87 | + Named("nodeclaim.tagging"). |
| 88 | + For(&karpv1.NodeClaim{}). |
| 89 | + WithEventFilter(predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 90 | + return isTaggable(o.(*karpv1.NodeClaim)) |
| 91 | + })). |
| 92 | + // Ok with using the default MaxConcurrentReconciles of 1 to avoid throttling from CreateTag write API |
| 93 | + WithOptions(controller.Options{ |
| 94 | + RateLimiter: reasonable.RateLimiter(), |
| 95 | + }). |
| 96 | + Complete(reconcile.AsReconciler(m.GetClient(), c)) |
| 97 | +} |
| 98 | + |
| 99 | +func (c *Controller) tagInstance(ctx context.Context, nc *karpv1.NodeClaim, id string) error { |
| 100 | + tags := map[string]string{ |
| 101 | + v1alpha1.TagName: nc.Status.NodeName, |
| 102 | + v1alpha1.TagNodeClaim: nc.Name, |
| 103 | + v1alpha1.EKSClusterNameTagKey: options.FromContext(ctx).ClusterName, |
| 104 | + } |
| 105 | + |
| 106 | + // Remove tags which have been already populated |
| 107 | + instance, err := c.instanceProvider.Get(ctx, id) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("tagging nodeclaim, %w", err) |
| 110 | + } |
| 111 | + tags = lo.OmitByKeys(tags, lo.Keys(instance.Tags)) |
| 112 | + if len(tags) == 0 { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + // Ensures that no more than 1 CreateTags call is made per second. Rate limiting is required since CreateTags |
| 117 | + // shares a pool with other mutating calls (e.g. CreateFleet). |
| 118 | + defer time.Sleep(time.Second) |
| 119 | + if err := c.instanceProvider.CreateTags(ctx, id, tags); err != nil { |
| 120 | + return fmt.Errorf("tagging nodeclaim, %w", err) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func isTaggable(nc *karpv1.NodeClaim) bool { |
| 126 | + // Instance has already been tagged |
| 127 | + instanceTagged := nc.Annotations[v1alpha1.AnnotationInstanceTagged] |
| 128 | + clusterNameTagged := nc.Annotations[v1alpha1.AnnotationClusterNameTaggedCompatability] |
| 129 | + if instanceTagged == "true" && clusterNameTagged == "true" { |
| 130 | + return false |
| 131 | + } |
| 132 | + // Node name is not yet known |
| 133 | + if nc.Status.NodeName == "" { |
| 134 | + return false |
| 135 | + } |
| 136 | + // NodeClaim is currently terminating |
| 137 | + if !nc.DeletionTimestamp.IsZero() { |
| 138 | + return false |
| 139 | + } |
| 140 | + return true |
| 141 | +} |
0 commit comments