-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: appmanager kind operator support nodeSelector in deployment
- Loading branch information
Showing
12 changed files
with
233 additions
and
9,205 deletions.
There are no files selected for viewing
846 changes: 3 additions & 843 deletions
846
chart/sreworks-chart/charts/appmanager/templates/operator-crd-jobs.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
8,350 changes: 3 additions & 8,347 deletions
8,350
chart/sreworks-chart/charts/appmanager/templates/operator-crd-microservices.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...one/src/main/resources/traits/node-selector.trait.sreworks.io/v1beta1/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
26 changes: 26 additions & 0 deletions
26
...ndalone/src/main/resources/traits/node-selector.trait.sreworks.io/v1beta1/definition.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
139 changes: 139 additions & 0 deletions
139
...its/node-selector.trait.sreworks.io/v1beta1/dynamicscripts/TraitNodeSelectorCommon.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...e/src/main/resources/traits/node-selector.trait.sreworks.io/v1beta1/frontends/deploy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}] | ||
} |