|
| 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 volumesize |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/awslabs/operatorpkg/reasonable" |
| 24 | + "k8s.io/apimachinery/pkg/api/equality" |
| 25 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 30 | + "sigs.k8s.io/karpenter/pkg/operator/injection" |
| 31 | + "sigs.k8s.io/karpenter/pkg/utils/resources" |
| 32 | + |
| 33 | + "github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/apis/v1alpha1" |
| 34 | +) |
| 35 | + |
| 36 | +type Controller struct { |
| 37 | + kubeClient client.Client |
| 38 | +} |
| 39 | + |
| 40 | +func NewController(kubeClient client.Client) *Controller { |
| 41 | + return &Controller{ |
| 42 | + kubeClient: kubeClient, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (c *Controller) Reconcile(ctx context.Context, nodeClass *v1alpha1.ECSNodeClass) (reconcile.Result, error) { |
| 47 | + ctx = injection.WithControllerName(ctx, "nodeclass.volumesize") |
| 48 | + |
| 49 | + stored := nodeClass.DeepCopy() |
| 50 | + |
| 51 | + if nodeClass.Spec.SystemDisk.Size != nil { |
| 52 | + nodeClass.Spec.SystemDisk.VolumeSize = resources.Quantity(fmt.Sprintf("%dG", *nodeClass.Spec.SystemDisk.Size)) |
| 53 | + } |
| 54 | + if !equality.Semantic.DeepEqual(stored, nodeClass) { |
| 55 | + if err := c.kubeClient.Patch(ctx, nodeClass, client.MergeFrom(stored)); err != nil { |
| 56 | + return reconcile.Result{}, err |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return reconcile.Result{}, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Controller) Register(_ context.Context, m manager.Manager) error { |
| 64 | + return controllerruntime.NewControllerManagedBy(m). |
| 65 | + Named("nodeclass.volumesize"). |
| 66 | + For(&v1alpha1.ECSNodeClass{}). |
| 67 | + WithOptions(controller.Options{ |
| 68 | + RateLimiter: reasonable.RateLimiter(), |
| 69 | + MaxConcurrentReconciles: 10, |
| 70 | + }). |
| 71 | + Complete(reconcile.AsReconciler(m.GetClient(), c)) |
| 72 | +} |
0 commit comments