Skip to content

Commit

Permalink
fix: appmanager kind operator support nodeSelector in deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Twwy committed Apr 18, 2023
1 parent 753bb18 commit 029dff7
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 9,205 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions paas/appmanager-kind-operator/api/v1/microservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type MicroserviceSpec struct {
// 污点设置 (已废弃)
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`

// NodeSelector
NodeSelector map[string]string `json:"nodeSelector,omitempty"`

// Service 配置 (已废弃)
Service corev1.ServiceSpec `json:"service,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (r *MicroserviceReconciler) constructForDeployment(raw *appmanagerabmiov1.M
Volumes: raw.Spec.Volumes,
Affinity: raw.Spec.Affinity,
Tolerations: raw.Spec.Tolerations,
NodeSelector: raw.Spec.NodeSelector,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (r *MicroserviceReconciler) constructForStatefulSet(raw *appmanagerabmiov1.
Volumes: raw.Spec.Volumes,
Affinity: raw.Spec.Affinity,
Tolerations: raw.Spec.Tolerations,
NodeSelector: raw.Spec.NodeSelector,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"parameters":[{
"type": 1,
"name": "chartUrl",
"label": "Chart仓库"
"label": "Chart仓库",
"toolTip": "类似 https://charts.bitnami.com/bitnami 这样的仓库地址"
},{
"type": 1,
"name": "chartName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: ingress.trait.sreworks.io/v1beta1
annotations:
definition.oam.dev/description: "The Kubernetes Ingress"
definition.oam.dev/version: "1.1.2"
definition.oam.dev/version: "1.1.4"
definition.oam.dev/tags: "key=value"
spec:
appliesToWorkloads:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TraitIngress implements TraitHandler {
/**
* 当前内置 Handler 版本
*/
public static final Integer REVISION = 11
public static final Integer REVISION = 12

@Autowired
private KubernetesClientFactory clientFactory
Expand Down Expand Up @@ -129,18 +129,10 @@ class TraitIngress implements TraitHandler {

// 创建 Metadata 对象
ObjectMeta metadata
if(ownerReference == null) {
metadata = new ObjectMetaBuilder()
.withName(ingressName)
.withNamespace(namespace)
.build();
}else{
metadata = new ObjectMetaBuilder()
.withName(ingressName)
.withNamespace(namespace)
.withOwnerReferences(mapper.readValue(ownerReference, OwnerReference.class))
.build();
}
metadata = new ObjectMetaBuilder()
.withName(ingressName)
.withNamespace(namespace)
.build();

IngressSpec ingressSpec = new IngressSpecBuilder()
.addNewRule()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```
name: ingress.trait.sreworks.io/v1beta1
runtime: post
spec:
host: xxxx.cn-beijing.alicontainer.com
path: /
serviceName: xxxx-frontend
servicePort: 80
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: core.oam.dev/v1beta1
kind: TraitDefinition
metadata:
name: node-selector.trait.sreworks.io/v1beta1
annotations:
definition.oam.dev/description: "The Node Selector"
definition.oam.dev/version: "1.0.1"
definition.oam.dev/tags: "key=value"
spec:
appliesToWorkloads:
- microservice.component.sreworks.io/v1beta1
schematic:
groovy:
files:
- kind: TRAIT
path: dynamicscripts/TraitNodeSelectorCommon.groovy
name: "node-selector.trait.sreworks.io/v1beta1"
properties:
runtime: post
definitionSchema:
name: "node-selector.trait.schema.sreworks.io"
jsonSchema: {}
frontend:
files:
- kind: DEPLOY
path: frontends/deploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package dynamicscripts

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.tesla.appmanager.common.enums.DynamicScriptKindEnum
import com.alibaba.tesla.appmanager.common.exception.AppErrorCode
import com.alibaba.tesla.appmanager.common.exception.AppException
import com.alibaba.tesla.appmanager.domain.req.trait.TraitExecuteReq
import com.alibaba.tesla.appmanager.domain.res.trait.TraitExecuteRes
import com.alibaba.tesla.appmanager.kubernetes.KubernetesClientFactory
import com.alibaba.tesla.appmanager.trait.service.handler.TraitHandler
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
/**
* Node Selector
*
* @author [email protected]
*/
class TraitNodeSelectorCommon implements TraitHandler {

private static final Logger log = LoggerFactory.getLogger(TraitNodeSelectorCommon.class)

/**
* 当前内置 Handler 类型
*/
public static final String KIND = DynamicScriptKindEnum.TRAIT.toString()

/**
* 当前内置 Handler 名称
*/
public static final String NAME = "node-selector.trait.sreworks.io/v1beta1"

/**
* 当前内置 Handler 版本
*/
public static final Integer REVISION = 1

@Autowired
private KubernetesClientFactory clientFactory

/**
* Trait 业务侧逻辑执行
*
* @param request Trait 输入参数
* @return Trait 修改后的 Spec 定义
*/
@Override
TraitExecuteRes execute(TraitExecuteReq request) {

/**
- name: node-selector.trait.sreworks.io/v1beta1
runtime: post
spec:
nodeSelector:
a: b
c: d
- name: node-selector.trait.sreworks.io/v1beta1
runtime: post
spec:
nodeSelector:
- key: a
value: b
- key: c
value: d
*/

/**
* 1. get metadata from workload
*/
log.info("start execute node-selector trait {}", request.getSpec().toJSONString())
def spec = request.getSpec()

// nodeSelector 支持为字典或数组
// 数组
// - key: xxx
// value: yyy
// 字段
// key-aa: value-bbb
Map<String, String> selector;
Object raw = request.getSpec().get("nodeSelector")
if (raw instanceof JSONObject){
selector = request.getSpec().getJSONObject("nodeSelector").getInnerMap()
}else if (raw instanceof JSONArray){
selector = new HashMap<>()
JSONArray selectorArray = request.getSpec().getJSONArray("nodeSelector")
for (int i = 0; i < selectorArray.size(); i++) {
String key = selectorArray.getJSONObject(i).getString("key")
String value = selectorArray.getJSONObject(i).getString("value")
selector.put(key, value)
}
}else{
throw new AppException(AppErrorCode.INVALID_USER_ARGS,
"the nodeSelector is not a JSONObject or a JSONArray")
}

JSONObject nodeSelector = new JSONObject(selector)
JSONObject workloadSpec = (JSONObject) request.getRef().getSpec()

// 适配 cloneset 及 advancedstatefulset 类型,最后是兼容历史的类新,直接置到 workload spec 顶部
if (workloadSpec.get("cloneSet") != null) {
JSONObject cloneSetSpec = workloadSpec
.getJSONObject("cloneSet")
.getJSONObject("template")
.getJSONObject("spec")
if (cloneSetSpec.get("nodeSelector") == null) {
cloneSetSpec.put("nodeSelector", nodeSelector)
log.info("nodeSelector {} has applied to workload {}|kind=cloneSet|append=false",
nodeSelector.toJSONString(), JSONObject.toJSONString(request.getRef().getMetadata()))
} else {
cloneSetSpec.getJSONObject("nodeSelector").putAll(nodeSelector)
log.info("nodeSelector {} has applied to workload {}|kind=cloneSet|append=true",
nodeSelector.toJSONString(), JSONObject.toJSONString(request.getRef().getMetadata()))
}
} else if (workloadSpec.get("advancedStatefulSet") != null) {
JSONObject advancedStatefulSetSpec = workloadSpec
.getJSONObject("advancedStatefulSet")
.getJSONObject("template")
.getJSONObject("spec")
if (advancedStatefulSetSpec.get("nodeSelector") == null) {
advancedStatefulSetSpec.put("nodeSelector", nodeSelector)
log.info("nodeSelector {} has applied to workload {}|kind=advancedStatefulSet|append=false",
nodeSelector.toJSONString(), JSONObject.toJSONString(request.getRef().getMetadata()))
} else {
advancedStatefulSetSpec.getJSONObject("nodeSelector").putAll(nodeSelector)
log.info("nodeSelector {} has applied to workload {}|kind=advancedStatefulSet|append=true",
nodeSelector.toJSONString(), JSONObject.toJSONString(request.getRef().getMetadata()))
}
} else {
workloadSpec.put("nodeSelector", nodeSelector)
log.info("nodeSelector {} has applied to workload {}|kind=compatible|append=false",
nodeSelector.toJSONString(), JSONObject.toJSONString(request.getRef().getMetadata()))
}
return TraitExecuteRes.builder()
.spec(spec)
.build()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"parameters": [{
"type": 85,
"name": "nodeSelector",
"label": "节点选择器(nodeSelector)",
"defModel": {
"enableRemove": true,
"pagination": false,
"enableAdd": true,
"enableEdit": true,
"columns": [
{
"editProps": {
"name": "key",
"label": "",
"type": 1,
"required": false,
"inputTip": "key"
},
"dataIndex": "key",
"title": "",
"key": "key"
},
{
"editProps": {
"name": "value",
"label": "",
"type": 1,
"required": false,
"inputTip": "value"
},
"dataIndex": "value",
"title": "",
"key": "value"
}
]
},
"toFieldPath": "spec.selector"
}]
}

0 comments on commit 029dff7

Please sign in to comment.