|
| 1 | +/* |
| 2 | +Copyright 2022 The Katalyst 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 control |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + autoscaling "k8s.io/api/autoscaling/v2" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "k8s.io/apimachinery/pkg/util/strategicpatch" |
| 28 | + "k8s.io/client-go/kubernetes" |
| 29 | + |
| 30 | + apis "github.com/kubewharf/katalyst-api/pkg/apis/autoscaling/v1alpha2" |
| 31 | + clientset "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned" |
| 32 | + "github.com/kubewharf/katalyst-core/pkg/util/general" |
| 33 | +) |
| 34 | + |
| 35 | +type HPAManager interface { |
| 36 | + Create(ctx context.Context, new *autoscaling.HorizontalPodAutoscaler, opts metav1.CreateOptions) (*autoscaling.HorizontalPodAutoscaler, error) |
| 37 | + Patch(ctx context.Context, old, new *autoscaling.HorizontalPodAutoscaler, opts metav1.PatchOptions) (*autoscaling.HorizontalPodAutoscaler, error) |
| 38 | +} |
| 39 | + |
| 40 | +type HPAManageImpl struct { |
| 41 | + client kubernetes.Interface |
| 42 | +} |
| 43 | + |
| 44 | +func NewHPAManager(client kubernetes.Interface) HPAManager { |
| 45 | + return &HPAManageImpl{ |
| 46 | + client: client, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (h *HPAManageImpl) Create(ctx context.Context, |
| 51 | + new *autoscaling.HorizontalPodAutoscaler, opts metav1.CreateOptions, |
| 52 | +) (*autoscaling.HorizontalPodAutoscaler, error) { |
| 53 | + if new == nil { |
| 54 | + return nil, fmt.Errorf("can't create a nil HPA") |
| 55 | + } |
| 56 | + return h.client.AutoscalingV2().HorizontalPodAutoscalers(new.Namespace).Create(ctx, new, opts) |
| 57 | +} |
| 58 | + |
| 59 | +func (h *HPAManageImpl) Patch(ctx context.Context, old, |
| 60 | + new *autoscaling.HorizontalPodAutoscaler, opts metav1.PatchOptions, |
| 61 | +) (*autoscaling.HorizontalPodAutoscaler, error) { |
| 62 | + if old == nil || new == nil { |
| 63 | + return nil, fmt.Errorf("can't patch a nil HPA") |
| 64 | + } |
| 65 | + |
| 66 | + oldData, err := json.Marshal(old) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + newData, err := json.Marshal(new) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &autoscaling.HorizontalPodAutoscaler{}) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to create merge patch for hpa %q/%q: %v", old.Namespace, old.Name, err) |
| 79 | + } else if general.JsonPathEmpty(patchBytes) { |
| 80 | + return nil, nil |
| 81 | + } |
| 82 | + |
| 83 | + return h.client.AutoscalingV2().HorizontalPodAutoscalers(old.Namespace).Patch(ctx, old.Name, types.StrategicMergePatchType, patchBytes, opts) |
| 84 | +} |
| 85 | + |
| 86 | +type IHPAUpdater interface { |
| 87 | + Update(ctx context.Context, ihpa *apis.IntelligentHorizontalPodAutoscaler, |
| 88 | + opts metav1.UpdateOptions) (*apis.IntelligentHorizontalPodAutoscaler, error) |
| 89 | + UpdateStatus(ctx context.Context, ihpa *apis.IntelligentHorizontalPodAutoscaler, |
| 90 | + opts metav1.UpdateOptions) (*apis.IntelligentHorizontalPodAutoscaler, error) |
| 91 | +} |
| 92 | + |
| 93 | +type IHPAUpdateImpl struct { |
| 94 | + client clientset.Interface |
| 95 | +} |
| 96 | + |
| 97 | +func NewIHPAUpdater(client clientset.Interface) IHPAUpdater { |
| 98 | + return &IHPAUpdateImpl{ |
| 99 | + client: client, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (i *IHPAUpdateImpl) Update(ctx context.Context, ihpa *apis.IntelligentHorizontalPodAutoscaler, opts metav1.UpdateOptions) (*apis.IntelligentHorizontalPodAutoscaler, error) { |
| 104 | + if ihpa == nil { |
| 105 | + return nil, fmt.Errorf("can't update a nil ihpa") |
| 106 | + } |
| 107 | + |
| 108 | + return i.client.AutoscalingV1alpha2().IntelligentHorizontalPodAutoscalers(ihpa.Namespace).Update( |
| 109 | + ctx, ihpa, opts) |
| 110 | +} |
| 111 | + |
| 112 | +func (i *IHPAUpdateImpl) UpdateStatus(ctx context.Context, ihpa *apis.IntelligentHorizontalPodAutoscaler, opts metav1.UpdateOptions) (*apis.IntelligentHorizontalPodAutoscaler, error) { |
| 113 | + if ihpa == nil { |
| 114 | + return nil, fmt.Errorf("can't update a nil ihpa's status") |
| 115 | + } |
| 116 | + |
| 117 | + return i.client.AutoscalingV1alpha2().IntelligentHorizontalPodAutoscalers(ihpa.Namespace).UpdateStatus( |
| 118 | + ctx, ihpa, opts) |
| 119 | +} |
| 120 | + |
| 121 | +type VirtualWorkloadManager interface { |
| 122 | + Create(ctx context.Context, vw *apis.VirtualWorkload, |
| 123 | + opts metav1.CreateOptions) (*apis.VirtualWorkload, error) |
| 124 | + Update(ctx context.Context, vw *apis.VirtualWorkload, |
| 125 | + opts metav1.UpdateOptions) (*apis.VirtualWorkload, error) |
| 126 | + UpdateStatus(ctx context.Context, vw *apis.VirtualWorkload, |
| 127 | + opts metav1.UpdateOptions) (*apis.VirtualWorkload, error) |
| 128 | +} |
| 129 | + |
| 130 | +type vmManageImpl struct { |
| 131 | + client clientset.Interface |
| 132 | +} |
| 133 | + |
| 134 | +func NewVirtualWorkloadManager(client clientset.Interface) VirtualWorkloadManager { |
| 135 | + return &vmManageImpl{ |
| 136 | + client: client, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func (i *vmManageImpl) Create(ctx context.Context, vw *apis.VirtualWorkload, opts metav1.CreateOptions) (*apis.VirtualWorkload, error) { |
| 141 | + if vw == nil { |
| 142 | + return nil, fmt.Errorf("can't create a nil vw") |
| 143 | + } |
| 144 | + |
| 145 | + return i.client.AutoscalingV1alpha2().VirtualWorkloads(vw.Namespace).Create( |
| 146 | + ctx, vw, opts) |
| 147 | +} |
| 148 | + |
| 149 | +func (i *vmManageImpl) Update(ctx context.Context, vw *apis.VirtualWorkload, opts metav1.UpdateOptions) (*apis.VirtualWorkload, error) { |
| 150 | + if vw == nil { |
| 151 | + return nil, fmt.Errorf("can't update a nil vw") |
| 152 | + } |
| 153 | + |
| 154 | + return i.client.AutoscalingV1alpha2().VirtualWorkloads(vw.Namespace).Update( |
| 155 | + ctx, vw, opts) |
| 156 | +} |
| 157 | + |
| 158 | +func (i *vmManageImpl) UpdateStatus(ctx context.Context, vw *apis.VirtualWorkload, opts metav1.UpdateOptions) (*apis.VirtualWorkload, error) { |
| 159 | + if vw == nil { |
| 160 | + return nil, fmt.Errorf("can't update a nil vw's status") |
| 161 | + } |
| 162 | + |
| 163 | + return i.client.AutoscalingV1alpha2().VirtualWorkloads(vw.Namespace).UpdateStatus( |
| 164 | + ctx, vw, opts) |
| 165 | +} |
0 commit comments