Skip to content

Commit

Permalink
Fix fabric8io#1648: Job creation fails during fabric8:apply with erro…
Browse files Browse the repository at this point in the history
…r: forbidden
  • Loading branch information
rohanKanojia committed Jun 14, 2019
1 parent 947f32b commit 6694de3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
36 changes: 36 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/service/ApplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.fabric8.kubernetes.api.model.batch.Job;
import io.fabric8.kubernetes.api.model.extensions.Ingress;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
Expand Down Expand Up @@ -65,6 +67,7 @@

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -184,6 +187,8 @@ private void applyEntity(Object dto, String sourceName) throws Exception {
applyPersistentVolumeClaim((PersistentVolumeClaim) dto, sourceName);
}else if (dto instanceof CustomResourceDefinition) {
applyCustomResourceDefinition((CustomResourceDefinition) dto, sourceName);
} else if (dto instanceof Job) {
applyJob((Job) dto, sourceName);
} else if (dto instanceof HasMetadata) {
HasMetadata entity = (HasMetadata) dto;
try {
Expand Down Expand Up @@ -1211,6 +1216,37 @@ protected void doCreatePod(Pod pod, String namespace, String sourceName) {
}
}

protected void applyJob(Job job, String sourceName) {
String namespace = getNamespace();
String id = getName(job);
Objects.requireNonNull(id, "No name for " + job + " " + sourceName);
if (isServicesOnlyMode()) {
log.debug("Only processing Services right now so ignoring Job: " + namespace + ":" + id);
return;
}
// Not using createOrReplace() here (https://github.com/fabric8io/kubernetes-client/issues/1586)
try {
doCreateJob(job, namespace, sourceName);
} catch (KubernetesClientException exception) {
if(exception.getStatus().getCode().equals(HttpURLConnection.HTTP_CONFLICT)) {
Job old = kubernetesClient.batch().jobs().inNamespace(namespace).withName(id).get();
Job updatedJob = patchService.compareAndPatchEntity(namespace, job, old);
log.info("Updated Job: " + old.getMetadata().getName());
return;
}
onApplyError("Failed to apply Job from " + job.getMetadata().getName(), exception);
}
}

public void doCreateJob(Job job, String namespace, String sourceName) throws KubernetesClientException {
if (StringUtils.isNotBlank(namespace)) {
kubernetesClient.batch().jobs().inNamespace(namespace).create(job);
} else {
kubernetesClient.batch().jobs().inNamespace(getNamespace()).create(job);
}
log.info("Creating a Job from " + sourceName + " namespace " + namespace + " name " + getName(job));
}

public String getNamespace() {
return namespace;
}
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/service/PatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.DoneableCustomResourceDefinition;
import io.fabric8.kubernetes.api.model.batch.DoneableJob;
import io.fabric8.kubernetes.api.model.batch.Job;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.maven.core.util.kubernetes.OpenshiftHelper;
import io.fabric8.maven.core.util.kubernetes.UserConfigurationCompare;
Expand Down Expand Up @@ -74,6 +76,7 @@ interface EntityPatcher<T extends HasMetadata> {
patchers.put("Secret", secretPatcher());
patchers.put("PersistentVolumeClaim", pvcPatcher());
patchers.put("CustomResourceDefinition", crdPatcher());
patchers.put("Job", jobPatcher());
}


Expand Down Expand Up @@ -229,6 +232,30 @@ private static EntityPatcher<CustomResourceDefinition> crdPatcher() {
};
}

private static EntityPatcher<Job> jobPatcher() {
return (KubernetesClient client, String namespace, Job newObj, Job oldObj) -> {
if (UserConfigurationCompare.configEqual(newObj, oldObj)) {
return oldObj;
}

DoneableJob entity = client.batch().jobs().withName(oldObj.getMetadata().getName()).edit();

if (!UserConfigurationCompare.configEqual(newObj.getMetadata(), oldObj.getMetadata())) {
entity.withMetadata(newObj.getMetadata());
}

if (!UserConfigurationCompare.configEqual(newObj.getSpec().getSelector(), oldObj.getSpec().getSelector())) {
entity.editSpec().withSelector(newObj.getSpec().getSelector());
}

if (!UserConfigurationCompare.configEqual(newObj.getSpec().getTemplate(), oldObj.getSpec().getSelector())) {
entity.editSpec().withTemplate(newObj.getSpec().getTemplate());
}

return entity.done();
};
}

// ================================================================================
// OpenShift objects:
// =================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder;
import io.fabric8.kubernetes.api.model.batch.JobBuilder;
import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.core.util.MapUtil;
Expand Down Expand Up @@ -130,19 +129,6 @@ public void visit(StatefulSetBuilder builder) {
builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec();
}
});

builder.accept(new TypedVisitor<JobBuilder>() {
@Override
public void visit(JobBuilder builder) {
Map<String, String> selectors = new HashMap<>();
if(builder.buildSpec() != null && builder.buildSpec().getSelector() != null && builder.buildSpec().getSelector().getMatchLabels() != null) {
selectors.putAll(builder.buildSpec().getSelector().getMatchLabels());
}
MapUtil.mergeIfAbsent(selectors, createLabels());
builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec();
}
});

}

@Override
Expand Down

0 comments on commit 6694de3

Please sign in to comment.