Skip to content

Commit 817527f

Browse files
author
玖宇
committed
Feature: support rbg scaling adapter
1 parent ce6596d commit 817527f

11 files changed

+907
-0
lines changed

api/workloads/v1alpha1/constant.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ const (
5959
StatefulSetWorkloadType string = "apps/v1/StatefulSet"
6060
LeaderWorkerSetWorkloadType string = "leaderworkerset.x-k8s.io/v1/LeaderWorkerSet"
6161
)
62+
63+
type AdapterPhase string
64+
65+
const (
66+
AdapterPhaseNone AdapterPhase = ""
67+
AdapterPhaseNotBound AdapterPhase = "NotBound"
68+
AdapterPhaseBound AdapterPhase = "Bound"
69+
)

api/workloads/v1alpha1/helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ func (rbg *RoleBasedGroup) GetRoleStatus(roleName string) (status RoleStatus, fo
5353
}
5454
return
5555
}
56+
57+
func (rbgsa *RoleBasedGroupScalingAdapter) ContainsRbgOwner(rbg *RoleBasedGroup) bool {
58+
for _, owner := range rbgsa.OwnerReferences {
59+
if owner.UID == rbg.UID {
60+
return true
61+
}
62+
}
63+
return false
64+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2025.
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 v1alpha1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
// RoleBasedGroupScalingAdapterSpec defines the desired state of RoleBasedGroupScalingAdapter.
22+
type RoleBasedGroupScalingAdapterSpec struct {
23+
// Replicas is the number of RoleBasedGroupRole that will be scaled.
24+
Replicas *int32 `json:"replicas,omitempty"`
25+
26+
// ScaleTargetRef is a reference to the target resource that should be scaled.
27+
ScaleTargetRef *AdapterScaleTargetRef `json:"scaleTargetRef"`
28+
}
29+
30+
// RoleBasedGroupScalingAdapterStatus shows the current state of a RoleBasedGroupScalingAdapter.
31+
type RoleBasedGroupScalingAdapterStatus struct {
32+
// Phase indicates the current phase of the RoleBasedGroupScalingAdapter.
33+
Phase AdapterPhase `json:"phase,omitempty"`
34+
35+
// Replicas is the current effective number of target RoleBasedGroupRole.
36+
Replicas *int32 `json:"replicas,omitempty"`
37+
38+
// Selector is a label query used to filter and identify a set of resources targeted for metrics collection.
39+
Selector string `json:"selector,omitempty"`
40+
41+
// LastScaleTime is the last time the RoleBaseGroupScalingAdapter scaled the number of pods,
42+
LastScaleTime *metav1.Time `json:"lastScaleTime,omitempty"`
43+
}
44+
45+
type AdapterScaleTargetRef struct {
46+
Name string `json:"name"`
47+
Role string `json:"role"`
48+
}
49+
50+
// +kubebuilder:object:root=true
51+
// +kubebuilder:subresource:status
52+
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
53+
// +kubebuilder:printcolumn:name="PHASE",type="string",JSONPath=".status.phase"
54+
// +kubebuilder:printcolumn:name="REPLICAS",type="string",JSONPath=".status.replicas"
55+
// +kubebuilder:resource:shortName={rbgsa}
56+
57+
// RoleBasedGroupScalingAdapter is the Schema for the rolebasedgroupscalingadapters API.
58+
type RoleBasedGroupScalingAdapter struct {
59+
metav1.TypeMeta `json:",inline"`
60+
metav1.ObjectMeta `json:"metadata,omitempty"`
61+
62+
Spec RoleBasedGroupScalingAdapterSpec `json:"spec,omitempty"`
63+
Status RoleBasedGroupScalingAdapterStatus `json:"status,omitempty"`
64+
}
65+
66+
// +kubebuilder:object:root=true
67+
68+
// RoleBasedGroupScalingAdapterList contains a list of RoleBasedGroupScalingAdapter.
69+
type RoleBasedGroupScalingAdapterList struct {
70+
metav1.TypeMeta `json:",inline"`
71+
metav1.ListMeta `json:"metadata,omitempty"`
72+
Items []RoleBasedGroupScalingAdapter `json:"items"`
73+
}
74+
75+
func init() {
76+
SchemeBuilder.Register(&RoleBasedGroupScalingAdapter{}, &RoleBasedGroupScalingAdapterList{})
77+
}

api/workloads/v1alpha1/zz_generated.deepcopy.go

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/rbgs/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ func main() {
260260
os.Exit(1)
261261
}
262262

263+
rbgScalingAdapterReconciler := workloadscontroller.NewRoleBasedGroupScalingAdapterReconciler(mgr)
264+
if err = rbgScalingAdapterReconciler.CheckCrdExists(); err != nil {
265+
setupLog.Error(err, "unable to create controller", "controller", "RoleBasedGroupScalingAdapter")
266+
os.Exit(1)
267+
}
268+
if err = rbgScalingAdapterReconciler.SetupWithManager(mgr, options); err != nil {
269+
setupLog.Error(err, "unable to create controller", "controller", "RoleBasedGroupScalingAdapter")
270+
os.Exit(1)
271+
}
272+
263273
rbgsReconciler := workloadscontroller.NewRoleBasedGroupSetReconciler(mgr)
264274
if err = rbgsReconciler.CheckCrdExists(); err != nil {
265275
setupLog.Error(err, "unable to create rbgs controller", "controller", "RoleBasedGroupSet")
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.17.2
7+
name: rolebasedgroupscalingadapters.workloads.x-k8s.io
8+
spec:
9+
group: workloads.x-k8s.io
10+
names:
11+
kind: RoleBasedGroupScalingAdapter
12+
listKind: RoleBasedGroupScalingAdapterList
13+
plural: rolebasedgroupscalingadapters
14+
shortNames:
15+
- rbgsa
16+
singular: rolebasedgroupscalingadapter
17+
scope: Namespaced
18+
versions:
19+
- additionalPrinterColumns:
20+
- jsonPath: .status.phase
21+
name: PHASE
22+
type: string
23+
- jsonPath: .status.replicas
24+
name: REPLICAS
25+
type: string
26+
name: v1alpha1
27+
schema:
28+
openAPIV3Schema:
29+
description: RoleBasedGroupScalingAdapter is the Schema for the rolebasedgroupscalingadapters
30+
API.
31+
properties:
32+
apiVersion:
33+
description: |-
34+
APIVersion defines the versioned schema of this representation of an object.
35+
Servers should convert recognized schemas to the latest internal value, and
36+
may reject unrecognized values.
37+
type: string
38+
kind:
39+
description: |-
40+
Kind is a string value representing the REST resource this object represents.
41+
Servers may infer this from the endpoint the client submits requests to.
42+
Cannot be updated.
43+
In CamelCase.
44+
type: string
45+
metadata:
46+
type: object
47+
spec:
48+
description: RoleBasedGroupScalingAdapterSpec defines the desired state
49+
of RoleBasedGroupScalingAdapter.
50+
properties:
51+
replicas:
52+
description: Replicas is the number of RoleBasedGroupRole that will
53+
be scaled.
54+
format: int32
55+
type: integer
56+
scaleTargetRef:
57+
description: ScaleTargetRef is a reference to the target resource
58+
that should be scaled.
59+
properties:
60+
name:
61+
type: string
62+
role:
63+
type: string
64+
required:
65+
- name
66+
- role
67+
type: object
68+
required:
69+
- scaleTargetRef
70+
type: object
71+
status:
72+
description: RoleBasedGroupScalingAdapterStatus shows the current state
73+
of a RoleBasedGroupScalingAdapter.
74+
properties:
75+
lastScaleTime:
76+
description: LastScaleTime is the last time the RoleBaseGroupScalingAdapter
77+
scaled the number of pods,
78+
format: date-time
79+
type: string
80+
phase:
81+
description: Phase indicates the current phase of the RoleBasedGroupScalingAdapter.
82+
type: string
83+
replicas:
84+
description: Replicas is the current effective number of target RoleBasedGroupRole.
85+
format: int32
86+
type: integer
87+
selector:
88+
description: Selector is a label query used to filter and identify
89+
a set of resources targeted for metrics collection.
90+
type: string
91+
type: object
92+
type: object
93+
served: true
94+
storage: true
95+
subresources:
96+
scale:
97+
labelSelectorPath: .status.selector
98+
specReplicasPath: .spec.replicas
99+
statusReplicasPath: .status.replicas
100+
status: {}

0 commit comments

Comments
 (0)