use of io.fabric8.kubernetes.api.model.PodSpec in project camel by apache.
the class KubernetesPodsProducer method doCreatePod.
protected void doCreatePod(Exchange exchange, String operation) throws Exception {
Pod pod = null;
String podName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_POD_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
PodSpec podSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_POD_SPEC, PodSpec.class);
if (ObjectHelper.isEmpty(podName)) {
LOG.error("Create a specific pod require specify a pod name");
throw new IllegalArgumentException("Create a specific pod require specify a pod name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Create a specific pod require specify a namespace name");
throw new IllegalArgumentException("Create a specific pod require specify a namespace name");
}
if (ObjectHelper.isEmpty(podSpec)) {
LOG.error("Create a specific pod require specify a pod spec bean");
throw new IllegalArgumentException("Create a specific pod require specify a pod spec bean");
}
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class);
Pod podCreating = new PodBuilder().withNewMetadata().withName(podName).withLabels(labels).endMetadata().withSpec(podSpec).build();
pod = getEndpoint().getKubernetesClient().pods().inNamespace(namespaceName).create(podCreating);
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(pod);
}
use of io.fabric8.kubernetes.api.model.PodSpec in project jointware by isdream.
the class PerfComparator method createByObject.
public static void createByObject() {
Deployment dm = new Deployment();
{
ObjectMeta md = new ObjectMeta();
;
md.setName("busybox-dm");
md.setNamespace("wuheng");
{
Map<String, String> labels = new HashMap<String, String>();
labels.put("app", "busybox-dm");
md.setLabels(labels);
}
dm.setMetadata(md);
}
{
DeploymentSpec spec = new DeploymentSpec();
spec.setReplicas(2);
{
PodTemplateSpec template = new PodTemplateSpec();
{
PodSpec pc = new PodSpec();
{
List<Container> containers = new ArrayList<Container>();
{
Container c = new Container();
{
c.setImage("dcr.io:5000/busybox:latest");
c.setImagePullPolicy("IfNotPresent");
c.setName("busybox-dm");
List<String> commands = new ArrayList<String>();
{
commands.add("sleep");
commands.add("3600");
}
c.setCommand(commands);
}
containers.add(c);
}
pc.setContainers(containers);
}
template.setSpec(pc);
}
spec.setTemplate(template);
}
dm.setSpec(spec);
}
}
use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8-maven-plugin by fabric8io.
the class DebugMojo method enableDebugging.
private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
if (template != null) {
PodSpec podSpec = template.getSpec();
if (podSpec != null) {
List<Container> containers = podSpec.getContainers();
boolean enabled = false;
for (int i = 0; i < containers.size(); i++) {
Container container = containers.get(i);
List<EnvVar> env = container.getEnv();
if (env == null) {
env = new ArrayList<>();
}
remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG, "true")) {
container.setEnv(env);
enabled = true;
}
if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(debugSuspend))) {
container.setEnv(env);
enabled = true;
}
List<ContainerPort> ports = container.getPorts();
if (ports == null) {
ports = new ArrayList<>();
}
if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
container.setPorts(ports);
enabled = true;
}
if (debugSuspend) {
// Setting a random session value to force pod restart
this.debugSuspendValue = String.valueOf(new Random().nextLong());
KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
container.setEnv(env);
if (container.getReadinessProbe() != null) {
log.info("Readiness probe will be disabled on " + getKind(entity) + " " + getName(entity) + " to allow attaching a remote debugger during suspension");
container.setReadinessProbe(null);
}
enabled = true;
} else {
if (KubernetesResourceUtil.removeEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION)) {
container.setEnv(env);
enabled = true;
}
}
}
if (enabled) {
log.info("Enabling debug on " + getKind(entity) + " " + getName(entity));
return true;
}
}
}
return false;
}
use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8-maven-plugin by fabric8io.
the class DebugMojo method podHasEnvVarValue.
private boolean podHasEnvVarValue(Pod pod, String envVarName, String envVarValue) {
PodSpec spec = pod.getSpec();
if (spec != null) {
List<Container> containers = spec.getContainers();
if (containers != null && !containers.isEmpty()) {
Container container = containers.get(0);
List<EnvVar> env = container.getEnv();
if (env != null) {
for (EnvVar envVar : env) {
if (Objects.equal(envVar.getName(), envVarName) && Objects.equal(envVar.getValue(), envVarValue)) {
return true;
}
}
}
}
}
return false;
}
use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergePodSpec.
public static void mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec, String defaultName) {
List<Container> containers = builder.buildContainers();
List<Container> defaultContainers = defaultPodSpec.getContainers();
int size = defaultContainers.size();
if (size > 0) {
if (containers == null || containers.isEmpty()) {
builder.addToContainers(defaultContainers.toArray(new Container[size]));
} else {
int idx = 0;
for (Container defaultContainer : defaultContainers) {
Container container;
if (idx < containers.size()) {
container = containers.get(idx);
} else {
container = new Container();
containers.add(container);
}
mergeSimpleFields(container, defaultContainer);
List<EnvVar> defaultEnv = defaultContainer.getEnv();
if (defaultEnv != null) {
for (EnvVar envVar : defaultEnv) {
ensureHasEnv(container, envVar);
}
}
List<ContainerPort> defaultPorts = defaultContainer.getPorts();
if (defaultPorts != null) {
for (ContainerPort port : defaultPorts) {
ensureHasPort(container, port);
}
}
if (container.getReadinessProbe() == null) {
container.setReadinessProbe(defaultContainer.getReadinessProbe());
}
if (container.getLivenessProbe() == null) {
container.setLivenessProbe(defaultContainer.getLivenessProbe());
}
if (container.getSecurityContext() == null) {
container.setSecurityContext(defaultContainer.getSecurityContext());
}
idx++;
}
builder.withContainers(containers);
}
} else if (!containers.isEmpty()) {
// lets default the container name if there's none specified in the custom yaml file
Container container = containers.get(0);
if (isNullOrBlank(container.getName())) {
container.setName(defaultName);
}
builder.withContainers(containers);
}
}
Aggregations