|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package metrics |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/awslabs/operatorpkg/singleton" |
| 22 | + "github.com/samber/lo" |
| 23 | + "k8s.io/apimachinery/pkg/util/sets" |
| 24 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 28 | + karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1" |
| 29 | + "sigs.k8s.io/karpenter/pkg/cloudprovider" |
| 30 | +) |
| 31 | + |
| 32 | +type metricDimensions struct { |
| 33 | + instanceType string |
| 34 | + capacityType string |
| 35 | + zone string |
| 36 | +} |
| 37 | + |
| 38 | +type Controller struct { |
| 39 | + kubeClient client.Client |
| 40 | + cloudProvider cloudprovider.CloudProvider |
| 41 | +} |
| 42 | + |
| 43 | +func NewController(kubeClient client.Client, cloudProvider cloudprovider.CloudProvider) *Controller { |
| 44 | + return &Controller{ |
| 45 | + kubeClient: kubeClient, |
| 46 | + cloudProvider: cloudProvider, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (c *Controller) Reconcile(ctx context.Context) (reconcile.Result, error) { |
| 51 | + nodePools := &karpv1.NodePoolList{} |
| 52 | + if err := c.kubeClient.List(ctx, nodePools); err != nil { |
| 53 | + return reconcile.Result{}, err |
| 54 | + } |
| 55 | + availability := map[metricDimensions]bool{} |
| 56 | + price := map[metricDimensions]float64{} |
| 57 | + for _, nodePool := range nodePools.Items { |
| 58 | + instanceTypes, err := c.cloudProvider.GetInstanceTypes(ctx, &nodePool) |
| 59 | + if err != nil { |
| 60 | + return reconcile.Result{}, err |
| 61 | + } |
| 62 | + for _, instanceType := range instanceTypes { |
| 63 | + zones := sets.New[string]() |
| 64 | + for _, offering := range instanceType.Offerings { |
| 65 | + dimensions := metricDimensions{instanceType: instanceType.Name, capacityType: offering.CapacityType(), zone: offering.Zone()} |
| 66 | + availability[dimensions] = availability[dimensions] || offering.Available |
| 67 | + price[dimensions] = offering.Price |
| 68 | + zones.Insert(offering.Zone()) |
| 69 | + } |
| 70 | + for zone := range zones { |
| 71 | + dimensions := metricDimensions{instanceType: instanceType.Name, capacityType: karpv1.CapacityTypeReserved, zone: zone} |
| 72 | + if _, ok := availability[dimensions]; !ok { |
| 73 | + availability[dimensions] = false |
| 74 | + price[dimensions] = 0 |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + for dimensions, available := range availability { |
| 81 | + InstanceTypeOfferingAvailable.Set(float64(lo.Ternary(available, 1, 0)), map[string]string{ |
| 82 | + instanceTypeLabel: dimensions.instanceType, |
| 83 | + capacityTypeLabel: dimensions.capacityType, |
| 84 | + zoneLabel: dimensions.zone, |
| 85 | + }) |
| 86 | + } |
| 87 | + for dimensions, p := range price { |
| 88 | + InstanceTypeOfferingPriceEstimate.Set(p, map[string]string{ |
| 89 | + instanceTypeLabel: dimensions.instanceType, |
| 90 | + capacityTypeLabel: dimensions.capacityType, |
| 91 | + zoneLabel: dimensions.zone, |
| 92 | + }) |
| 93 | + } |
| 94 | + return reconcile.Result{RequeueAfter: time.Minute}, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (c *Controller) Register(_ context.Context, m manager.Manager) error { |
| 98 | + return controllerruntime.NewControllerManagedBy(m). |
| 99 | + Named("cloudprovider.metrics"). |
| 100 | + WatchesRawSource(singleton.Source()). |
| 101 | + Complete(singleton.AsReconciler(c)) |
| 102 | +} |
0 commit comments