Skip to content

Commit e200262

Browse files
authored
enchancement: auto generate shardingconfig (#8)
1 parent 81beb4c commit e200262

File tree

108 files changed

+1361
-324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1361
-324
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Thank you for contributing to Karbour!
1+
<!-- Thank you for contributing to Kridge!
22
33
Note:
44

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ bin/
1616
# Dependency directories (remove the comment below to include it)
1717
# vendor/
1818

19-
.idea/
19+
.idea/**
20+
demo/**
21+
testdemo/**

config/crd/bases/kridge.kusionstack.io_shardingconfigs.yaml

+20-23
Original file line numberDiff line numberDiff line change
@@ -114,37 +114,35 @@ spec:
114114
type: array
115115
root:
116116
properties:
117+
auto:
118+
description: Auto is config to automatically generate child ShardingConfig
119+
properties:
120+
everyShardReplicas:
121+
type: integer
122+
shardingSize:
123+
type: integer
124+
required:
125+
- everyShardReplicas
126+
- shardingSize
127+
type: object
117128
canary:
129+
description: Canary is canary shard config
118130
properties:
119131
inNamespaces:
120132
items:
121133
type: string
122134
type: array
123-
inNumbers:
135+
inShardHash:
124136
items:
125137
type: string
126138
type: array
139+
replicas:
140+
type: integer
141+
required:
142+
- replicas
127143
type: object
128-
canaryReplicas:
129-
type: integer
130144
disable:
131145
type: boolean
132-
manual:
133-
items:
134-
properties:
135-
id:
136-
type: integer
137-
numbers:
138-
items:
139-
type: string
140-
type: array
141-
required:
142-
- id
143-
- numbers
144-
type: object
145-
type: array
146-
normalReplicas:
147-
type: integer
148146
prefix:
149147
type: string
150148
resourceSelector:
@@ -214,12 +212,11 @@ spec:
214212
x-kubernetes-map-type: atomic
215213
type: object
216214
type: array
217-
size:
218-
type: integer
219215
targetStatefulSet:
220216
type: string
221-
type:
222-
type: string
217+
required:
218+
- prefix
219+
- targetStatefulSet
223220
type: object
224221
selector:
225222
description: Selector is a label query over pods of this configuration.

config/default/kustomization.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
namespace: kridge
3+
bases:
4+
- ../crd
5+
- ../rbac
6+
- ../manager
7+
- ../webhook
8+
9+
patchesStrategicMerge:
10+
11+
# Protect the /metrics endpoint by putting it behind auth.
12+
# Only one of manager_auth_proxy_patch.yaml and
13+
# manager_prometheus_metrics_patch.yaml should be enabled.
14+
# - manager_auth_proxy_patch.yaml
15+
# If you want your controller-manager to expose the /metrics
16+
# endpoint w/o any authn/z, uncomment the following line and
17+
# comment manager_auth_proxy_patch.yaml.
18+
# Only one of manager_auth_proxy_patch.yaml and
19+
# manager_prometheus_metrics_patch.yaml should be enabled.
20+
#- manager_prometheus_metrics_patch.yaml
21+
22+
#vars:
23+
#- name: WEBHOOK_SECRET_NAME
24+
# objref:
25+
# kind: Secret
26+
# name: webhook-server-secret
27+
# apiVersion: v1

config/demo/shardingconfig-auto.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: kridge.kusionstack.io/v1alpha1
2+
kind: ShardingConfig
3+
metadata:
4+
name: sharding-root
5+
namespace: operator-demo
6+
spec:
7+
root:
8+
prefix: operator-demo
9+
targetStatefulSet: operator-demo
10+
canary:
11+
replicas: 1
12+
inNamespaces:
13+
- ns-canary
14+
auto:
15+
everyShardReplicas: 2
16+
shardingSize: 2
17+
resourceSelector:
18+
- objectSelector:
19+
relateResources:
20+
- apiGroups:
21+
- '*'
22+
resources:
23+
- pods
24+
- services
25+
controller:
26+
leaderElectionName: operator-leader
27+
selector:
28+
matchExpressions:
29+
- key: statefulset.kubernetes.io/pod-name
30+
operator: In
31+
values:
32+
- operator-demo-0

config/manager/deployment.yaml

+14-16
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ spec:
2525
spec:
2626
containers:
2727
- args:
28-
- --logtostderr=true
29-
- --v=4
30-
- --leader-election-namespace=kridge
31-
- --proxy-image=${proxy-image}
32-
- --proxy-cpu=1
33-
- --proxy-memory=2Gi
34-
- --proxy-ephemeral-storage=6Gi
35-
- --leader-election-lease-duration-seconds=15
36-
- --leader-election-renew-deadline-seconds=10
28+
- --logtostderr=true
29+
- --v=4
30+
- --leader-election-namespace=kridge
31+
- --proxy-image=kusionstack/kridge-proxy:latest
32+
- --proxy-cpu=100m
33+
- --proxy-memory=100Mi
34+
- --proxy-ephemeral-storage=100Mi
35+
- --leader-election-lease-duration-seconds=15
36+
- --leader-election-renew-deadline-seconds=10
3737
command:
3838
- /manager
3939
env:
@@ -55,7 +55,7 @@ spec:
5555
value: secret
5656
- name: WATCH_ON_LIMIT
5757
value: 'true'
58-
image: ${manager-image}
58+
image: kusionstack/kridge-manager:latest
5959
imagePullPolicy: IfNotPresent
6060
livenessProbe:
6161
failureThreshold: 3
@@ -80,13 +80,11 @@ spec:
8080
timeoutSeconds: 2
8181
resources:
8282
limits:
83-
cpu: '4'
84-
memory: 8Gi
85-
ephemeral-storage: 30Gi
83+
cpu: 500m
84+
memory: 128Mi
8685
requests:
87-
cpu: '4'
88-
memory: 8Gi
89-
ephemeral-storage: 30Gi
86+
cpu: 10m
87+
memory: 64Mi
9088
securityContext:
9189
allowPrivilegeEscalation: false
9290
terminationMessagePath: /dev/termination-log

config/manager/kustomization.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#apiVersion: kustomize.config.k8s.io/v1beta1
2-
#kind: Kustomization
31
resources:
42
- service.yaml
53
- sharding_resource.yaml
6-
- deployment.yaml
4+
- deployment.yaml

docs/demo.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Demo
2+

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ require (
9898
golang.org/x/crypto v0.5.0 // indirect
9999
golang.org/x/mod v0.6.0 // indirect
100100
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
101-
golang.org/x/sys v0.5.0 // indirect
101+
golang.org/x/sys v0.6.0 // indirect
102102
golang.org/x/term v0.5.0 // indirect
103103
golang.org/x/text v0.7.0 // indirect
104104
golang.org/x/tools v0.2.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
563563
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
564564
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
565565
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
566-
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
567-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
566+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
567+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
568568
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
569569
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
570570
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=

pkg/apis/kridge/types.go

+37-33
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,51 @@ limitations under the License.
1616

1717
package kridge
1818

19+
// Environments
1920
const (
2021
EnvEnableWebhookServer = "ENABLE_WEBHOOK_SERVER"
2122
EnvEnableCalculateRunnable = "ENABLE_CALCULATE_RUNNABLE"
2223
EnvTestMode = "ENV_TEST_MODE"
24+
EnvGlobalSelector = "GLOBAL_SELECTOR"
25+
EnvWatchOnLimit = "WATCH_ON_LIMIT"
26+
)
2327

24-
EnvGlobalSelector = "GLOBAL_SELECTOR"
25-
EnvWatchOnLimit = "WATCH_ON_LIMIT"
26-
27-
KdControlPrefix = "kridge.kusionstack.io/"
28-
29-
KdShardingHashKey = "kridge.kusionstack.io/sharding-hash"
30-
KdControlKey = "kridge.kusionstack.io/control"
31-
KdNamespaceKey = "kridge.kusionstack.io/namespace"
32-
33-
KdIgnoreWebhookKey = "kridge.kusionstack.io/ignore-webhook"
34-
KdIgnoreValidateKey = "kridge.kusionstack.io/ignore-validate"
35-
KdDefaultReplicasKey = "kridge.kusionstack.io/default-replicas"
28+
// Labels
29+
const (
30+
KdControlPrefix = "kridge.kusionstack.io/"
31+
KdShardHashKey = "kridge.kusionstack.io/shard-hash"
32+
KdControlKey = "kridge.kusionstack.io/control"
33+
KdNamespaceKey = "kridge.kusionstack.io/namespace"
34+
KdIgnoreWebhookLabel = "kridge.kusionstack.io/ignore-webhook"
35+
KdIgnoreValidateLabel = "kridge.kusionstack.io/ignore-validate"
36+
KdDefaultReplicasLabel = "kridge.kusionstack.io/default-replicas"
37+
KdEnableProxyLabel = "kridge.kusionstack.io/enable-proxy"
38+
KdAutoShardingRootLabel = "kridge.kusionstack.io/auto-sharding-root"
39+
KdInRollingLabel = "kridge.kusionstack.io/rolling"
40+
KdDisableFakeKubeconfigArgLabel = "kridge.kusionstack.io/disable-fake-kubeconfig-arg"
41+
KdDisableFakeKubeconfigEnvLabel = "kridge.kusionstack.io/disable-fake-kubeconfig-env"
42+
KdSharedLogVolumeLabel = "kridge.kusionstack.io/log-volume"
43+
KdWatchOnLimitLabel = "kridge.kusionstack.io/watching"
44+
KdProxyKubeConfigVolumeLabel = "kridge.kusionstack.io/kubeconfig-volume"
45+
)
3646

37-
KdEnableProxyKey = "kridge.kusionstack.io/enable-proxy"
47+
// Annotations
48+
const (
49+
KdAutoShardingHashAnno = "kridge.kusionstack.io/auto-sharding-hash"
50+
KdRollingStatusAnno = "kridge.kusionstack.io/roll-status"
51+
KdRollingExpectedAnno = "kridge.kusionstack.io/roll-expected"
52+
KdSharedLogPathAnno = "kridge.kusionstack.io/log-path"
53+
KdWebhookEnvConfigAnno = "kridge.kusionstack.io/env-sync"
54+
KdEnvInjectAnno = "kridge.kusionstack.io/env-inject"
55+
KdProxyContainerResourceAnno = "kridge.kusionstack.io/proxy-resource"
56+
)
3857

58+
// Finalizer
59+
const (
3960
ProtectFinalizer = "finalizer.kridge.kusionstack.io/protected"
61+
)
4062

41-
KdAutoShardingRootLabel = "kridge.kusionstack.io/auto-sharding-root"
42-
KdAutoShardingHashAnno = "kridge.kusionstack.io/auto-sharding-hash"
43-
44-
KdInRollingLabel = "kridge.kusionstack.io/rolling"
45-
KdRollingStatusAnno = "kridge.kusionstack.io/roll-status"
46-
KdRollingExpectedAnno = "kridge.kusionstack.io/roll-expected"
47-
48-
KdDisableFakeKubeconfigArg = "kridge.kusionstack.io/disable-fake-kubeconfig-arg"
49-
KdDisableFakeKubeconfigEnv = "kridge.kusionstack.io/disable-fake-kubeconfig-env"
50-
51-
KdSharedLogPathAnnoKey = "kridge.kusionstack.io/log-path"
52-
KdSharedLogVolumeKey = "kridge.kusionstack.io/log-volume"
53-
54-
KdWatchOnLimitKey = "kridge.kusionstack.io/watching"
55-
63+
// Name
64+
const (
5665
ShardingConfigMapName = "kridge-sharding-config"
57-
58-
KdWebhookEnvConfigAnno = "kridge.kusionstack.io/env-sync"
59-
KdEnvInjectAnno = "kridge.kusionstack.io/env-inject"
60-
KdProxyKubeConfigVolume = "kridge.kusionstack.io/kubeconfig-volume"
61-
KdProxyContainerResourceAnno = "kridge.kusionstack.io/proxy-resource"
6266
)

pkg/apis/kridge/v1alpha1/shardingconfig_types.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,38 @@ type ShardingConfigSpec struct {
4040
}
4141

4242
type ShardingConfigRoot struct {
43-
Disable *bool `json:"disable,omitempty"`
44-
Prefix string `json:"prefix,omitempty"`
45-
Canary *CanaryConfig `json:"canary,omitempty"`
46-
Size int `json:"size,omitempty"`
47-
Type ShardingType `json:"type,omitempty"`
48-
Manual []ManualConfig `json:"manual,omitempty"`
49-
CanaryReplicas int `json:"canaryReplicas,omitempty"`
50-
NormalReplicas int `json:"normalReplicas,omitempty"`
51-
TargetStatefulSet string `json:"targetStatefulSet,omitempty"`
52-
ResourceSelector []ObjectLimiter `json:"resourceSelector,omitempty"`
43+
Disable *bool `json:"disable,omitempty"`
44+
Prefix string `json:"prefix"`
45+
TargetStatefulSet string `json:"targetStatefulSet"`
46+
47+
// Canary is canary shard config
48+
Canary *CanaryConfig `json:"canary,omitempty"`
49+
50+
// Auto is config to automatically generate child ShardingConfig
51+
Auto *AutoConfig `json:"auto,omitempty"`
52+
53+
// TODO: config every shard
54+
//Manual []ManualConfig `json:"manual,omitempty"`
55+
56+
ResourceSelector []ObjectLimiter `json:"resourceSelector,omitempty"`
57+
}
58+
59+
type AutoConfig struct {
60+
ShardingSize int `json:"shardingSize"`
61+
EveryShardReplicas int `json:"everyShardReplicas"`
5362
}
5463

5564
type CanaryConfig struct {
65+
Replicas *int `json:"replicas"`
5666
InNamespaces []string `json:"inNamespaces,omitempty"`
57-
InNumbers []string `json:"inNumbers,omitempty"`
67+
InShardHash []string `json:"inShardHash,omitempty"`
5868
}
5969

6070
type ManualConfig struct {
6171
ID int `json:"id"`
6272
Numbers []string `json:"numbers"`
6373
}
6474

65-
type ShardingType string
66-
67-
const (
68-
ShardingAuto ShardingType = "Auto"
69-
ShardingManual ShardingType = "Manual"
70-
)
71-
7275
// ShardingConfigRestConfigOverrides defines overrides to the application's rest config.
7376
type ShardingConfigRestConfigOverrides struct {
7477
// UserAgentOrPrefix can override the UserAgent of application.

0 commit comments

Comments
 (0)