Skip to content

Commit

Permalink
Add an initialization container for delayed exposure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phixsura committed Oct 24, 2023
1 parent 6d9836f commit 05818d1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ services:
- MYSQL_PASSWORD=nacos
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
init:
name: wait-for-mysql
image: busybox
command:
- "/bin/sh"
- "-c"
- "until nc -z nacos-mysql 3306; do sleep 1; done;"
- name: wait-for-mysql
image: busybox
command:
- "/bin/sh"
- "-c"
- "until nc -z nacos-mysql 3306; do sleep 1; done;"
expose:
- 3306
healthcheck:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package org.apache.dubbo.scenario.builder;

import org.apache.dubbo.scenario.builder.exception.ConfigureFileNotFoundException;
import org.apache.dubbo.scenario.builder.kubernetes.KubernetesService;
import org.apache.dubbo.scenario.builder.kubernetes.InitContainer;
import org.apache.dubbo.scenario.builder.kubernetes.KubernetesRunningGenerator;
import org.apache.dubbo.scenario.builder.kubernetes.KubernetesService;
import org.apache.dubbo.scenario.builder.vo.CaseConfiguration;
import org.apache.dubbo.scenario.builder.vo.ServiceComponent;

Expand All @@ -41,7 +42,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -727,18 +727,36 @@ private static KubernetesService getKubernetesService(ServiceComponent dependenc

if(dependency.getInit() != null) {
Yaml yaml = new Yaml();
Map<String,Object> initMap = dependency.getInit();
Map<String, String> newMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : initMap.entrySet()) {
if ("command".equals(entry.getKey())) {
dependency.setInitCommand((List<String>) entry.getValue());
continue;
List<Map<String, Object>> inits = (List<Map<String, Object>>) dependency.getInit();
for (Map<String, Object> initMap : inits) {
InitContainer initContainer = new InitContainer();
Map<String, String> newMap = new LinkedHashMap<>();

for (Map.Entry<String, Object> entry : initMap.entrySet()) {
String key = entry.getKey();
if ("command".equals(key)) {
initContainer.setCommand((List<String>) entry.getValue());
continue;
}
String val = yaml.dump(entry.getValue()).trim();
if ("name".equals(key)) {
initContainer.setName(val);
} else if ("image".equals(key)) {
initContainer.setImage(val);
} else {
newMap.put(entry.getKey(), val);
}
}
String value = yaml.dump(entry.getValue());
newMap.put(entry.getKey(), value.trim());
initContainer.setAttributes(newMap);
List<InitContainer> initContainers = service.getInitContainers();
if (initContainers == null) {
service.setInitContainers(new ArrayList<>());
initContainers = service.getInitContainers();
}
initContainers.add(initContainer);
}
service.setInit(newMap);
service.setInitCommand(dependency.getInitCommand());


}

//convert healthcheck to string map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.dubbo.scenario.builder.kubernetes;

import java.util.List;
import java.util.Map;

/**
* Such as:
Expand All @@ -37,6 +38,8 @@ public class InitContainer {

private List<String> command;

private Map<String,String> attributes;

public String getName() {
return name;
}
Expand All @@ -60,4 +63,12 @@ public List<String> getCommand() {
public void setCommand(List<String> command) {
this.command = command;
}

public Map<String, String> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public class KubernetesService {

private Map<String,String> environment;

private Map<String,String> init;

private List<String> initCommand;
private List<InitContainer> initContainers;

public String getName() {
return name;
Expand Down Expand Up @@ -141,19 +139,11 @@ public void setType(String type) {
this.type = type;
}

public Map<String, String> getInit() {
return init;
}

public void setInit(Map<String, String> init) {
this.init = init;
}

public List<String> getInitCommand() {
return initCommand;
public List<InitContainer> getInitContainers() {
return initContainers;
}

public void setInitCommand(List<String> initCommand) {
this.initCommand = initCommand;
public void setInitContainers(List<InitContainer> initContainers) {
this.initContainers = initContainers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,23 @@ spec:
labels:
app: ${service.name}
spec:
<#if service.init??>
<#if service.initContainers??>
initContainers:
<#if service.init?has_key("name")>
<#assign nameValue = service.init.name>
<#assign _ = service.init.remove("name")>
- name: ${nameValue}
</#if>
<#list service.init as key,value>
${key}: ${value}
</#list>
<#if service.initCommand??>
<#list service.initContainers as initContainer>
- name: ${initContainer.name}
image: ${initContainer.image}
<#if initContainer.command??>
command:
<#list service.initCommand as cmd>
<#list initContainer.command as cmd>
- "${cmd}"
</#list>
</#if>
<#if initContainer.attributes??>
<#list initContainer.attributes as key,value>
${key}: ${value}
</#list>
</#if>
</#list>
</#if>
</#if>
containers:
- name: ${service.name}
Expand Down

0 comments on commit 05818d1

Please sign in to comment.