enhance: add annotations config in clb plugin (#137)

Co-authored-by: 李志朋 <lizhipeng.629@bytedance.com>
This commit is contained in:
lizhipeng629 2024-04-18 11:15:04 +08:00 committed by GitHub
parent 69babe66fe
commit 53b69204e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 60 additions and 10 deletions

View File

@ -130,7 +130,7 @@ $(CONTROLLER_GEN): $(LOCALBIN)
.PHONY: envtest .PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN) $(ENVTEST): $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@c7e1dc9b5302d649d5531e19168dd7ea0013736d
HELM = $(shell pwd)/bin/helm HELM = $(shell pwd)/bin/helm
helm: ## Download helm locally if necessary. helm: ## Download helm locally if necessary.

View File

@ -34,6 +34,10 @@ min_port = 500
- Value{containerName_0},{containerName_1},... egsidecar - Value{containerName_0},{containerName_1},... egsidecar
- ConfigurableIt cannot be changed during the in-place updating process. - ConfigurableIt cannot be changed during the in-place updating process.
#### Annotations
- Meaningthe anno added to the service
- Valuekey1:value1,key2:value2...
- ConfigurableY
### Example ### Example
```yaml ```yaml
@ -62,6 +66,10 @@ spec:
- name: Fixed - name: Fixed
#Fill in here whether a fixed IP is required [optional] ; Default is false #Fill in here whether a fixed IP is required [optional] ; Default is false
value: "false" value: "false"
- name: Annotations
#Fill in the anno related to clb on the service
#The format is as follows: {key1}:{value1},{key2}:{value2}...
value: "key1:value1,key2:value2"
gameServerTemplate: gameServerTemplate:
spec: spec:
containers: containers:

View File

@ -32,7 +32,12 @@ min_port = 500
#### AllowNotReadyContainers #### AllowNotReadyContainers
- 含义:在容器原地升级时允许不断流的对应容器名称,可填写多个 - 含义:在容器原地升级时允许不断流的对应容器名称,可填写多个
- 填写格式:{containerName_0},{containerName_1},... 例如sidecar - 填写格式:{containerName_0},{containerName_1},... 例如sidecar
- 是否支持变更:在原地升级过程中不可变更。 - 是否支持变更:在原地升级过程中不可变更
#### Annotations
- 含义添加在service上的anno可填写多个
- 填写格式key1:value1,key2:value2...
- 是否支持变更:是
### 使用示例 ### 使用示例
@ -62,6 +67,10 @@ spec:
- name: Fixed - name: Fixed
#Fill in here whether a fixed IP is required [optional] ; Default is false #Fill in here whether a fixed IP is required [optional] ; Default is false
value: "false" value: "false"
- name: Annotations
#Fill in the anno related to clb on the service
#The format is as follows: {key1}:{value1},{key2}:{value2}...
value: "key1:value1,key2:value2"
gameServerTemplate: gameServerTemplate:
spec: spec:
containers: containers:

View File

@ -45,6 +45,7 @@ const (
ClbIdsConfigName = "ClbIds" ClbIdsConfigName = "ClbIds"
PortProtocolsConfigName = "PortProtocols" PortProtocolsConfigName = "PortProtocols"
FixedConfigName = "Fixed" FixedConfigName = "Fixed"
ClbAnnotations = "Annotations"
ClbConfigHashKey = "game.kruise.io/network-config-hash" ClbConfigHashKey = "game.kruise.io/network-config-hash"
ClbIdAnnotationKey = "service.beta.kubernetes.io/volcengine-loadbalancer-id" ClbIdAnnotationKey = "service.beta.kubernetes.io/volcengine-loadbalancer-id"
ClbAddressTypeKey = "service.beta.kubernetes.io/volcengine-loadbalancer-address-type" ClbAddressTypeKey = "service.beta.kubernetes.io/volcengine-loadbalancer-address-type"
@ -69,6 +70,7 @@ type clbConfig struct {
targetPorts []int targetPorts []int
protocols []corev1.Protocol protocols []corev1.Protocol
isFixed bool isFixed bool
annotations map[string]string
} }
func (c *ClbPlugin) Name() string { func (c *ClbPlugin) Name() string {
@ -351,6 +353,7 @@ func parseLbConfig(conf []gamekruiseiov1alpha1.NetworkConfParams) *clbConfig {
ports := make([]int, 0) ports := make([]int, 0)
protocols := make([]corev1.Protocol, 0) protocols := make([]corev1.Protocol, 0)
isFixed := false isFixed := false
annotations := map[string]string{}
for _, c := range conf { for _, c := range conf {
switch c.Name { switch c.Name {
case ClbIdsConfigName: case ClbIdsConfigName:
@ -379,6 +382,15 @@ func parseLbConfig(conf []gamekruiseiov1alpha1.NetworkConfParams) *clbConfig {
continue continue
} }
isFixed = v isFixed = v
case ClbAnnotations:
for _, anno := range strings.Split(c.Value, ",") {
annoKV := strings.Split(anno, ":")
if len(annoKV) == 2 {
annotations[annoKV[0]] = annoKV[1]
} else {
log.Warningf("clb annotation %s is invalid", annoKV[0])
}
}
} }
} }
return &clbConfig{ return &clbConfig{
@ -386,6 +398,7 @@ func parseLbConfig(conf []gamekruiseiov1alpha1.NetworkConfParams) *clbConfig {
protocols: protocols, protocols: protocols,
targetPorts: ports, targetPorts: ports,
isFixed: isFixed, isFixed: isFixed,
annotations: annotations,
} }
} }
@ -420,16 +433,21 @@ func (c *ClbPlugin) consSvc(config *clbConfig, pod *corev1.Pod, client client.Cl
}) })
} }
annotations := map[string]string{
ClbSchedulerKey: ClbSchedulerWRR,
ClbAddressTypeKey: ClbAddressTypePublic,
ClbIdAnnotationKey: lbId,
ClbConfigHashKey: util.GetHash(config),
}
for key, value := range config.annotations {
annotations[key] = value
}
svc := &corev1.Service{ svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: pod.GetName(), Name: pod.GetName(),
Namespace: pod.GetNamespace(), Namespace: pod.GetNamespace(),
Annotations: map[string]string{ Annotations: annotations,
ClbSchedulerKey: ClbSchedulerWRR,
ClbAddressTypeKey: ClbAddressTypePublic,
ClbIdAnnotationKey: lbId,
ClbConfigHashKey: util.GetHash(config),
},
OwnerReferences: getSvcOwnerReference(client, ctx, pod, config.isFixed), OwnerReferences: getSvcOwnerReference(client, ctx, pod, config.isFixed),
}, },
Spec: corev1.ServiceSpec{ Spec: corev1.ServiceSpec{

View File

@ -259,6 +259,12 @@ func TestClbPlugin_consSvc(t *testing.T) {
corev1.ProtocolTCP, corev1.ProtocolTCP,
}, },
isFixed: false, isFixed: false,
annotations: map[string]string{
"service.beta.kubernetes.io/volcengine-loadbalancer-health-check-flag": "on",
"service.beta.kubernetes.io/volcengine-loadbalancer-healthy-threshold": "3",
"service.beta.kubernetes.io/volcengine-loadbalancer-scheduler": "wrr",
"service.beta.kubernetes.io/volcengine-loadbalancer-pass-through": "true",
},
}, },
pod: &corev1.Pod{ pod: &corev1.Pod{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
@ -289,7 +295,16 @@ func TestClbPlugin_consSvc(t *testing.T) {
corev1.ProtocolTCP, corev1.ProtocolTCP,
}, },
isFixed: false, isFixed: false,
annotations: map[string]string{
"service.beta.kubernetes.io/volcengine-loadbalancer-health-check-flag": "on",
"service.beta.kubernetes.io/volcengine-loadbalancer-healthy-threshold": "3",
"service.beta.kubernetes.io/volcengine-loadbalancer-scheduler": "wrr",
"service.beta.kubernetes.io/volcengine-loadbalancer-pass-through": "true",
},
}), }),
"service.beta.kubernetes.io/volcengine-loadbalancer-health-check-flag": "on",
"service.beta.kubernetes.io/volcengine-loadbalancer-healthy-threshold": "3",
"service.beta.kubernetes.io/volcengine-loadbalancer-pass-through": "true",
}, },
OwnerReferences: []metav1.OwnerReference{ OwnerReferences: []metav1.OwnerReference{
{ {