Skip to content

Commit

Permalink
Fix eclipse-jkube#267: openshift-maven-plugin does not update Routes
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Trejo <marcos.trejo@gmail.com>
  • Loading branch information
mtrejo authored and TueDissingWork committed Jul 28, 2020
1 parent 604ab55 commit c36d467
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getOrCreateLabels;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getOrCreateMetadata;


/**
* Applies DTOs to the current Kubernetes master
*/
Expand Down Expand Up @@ -244,9 +243,8 @@ public void applyOAuthClient(OAuthClient entity, String sourceName) {
protected void doCreateOAuthClient(OAuthClient entity, String sourceName) {
OpenShiftClient openShiftClient = getOpenShiftClient();
if (openShiftClient != null) {
Object result = null;
try {
result = openShiftClient.oAuthClients().create(entity);
openShiftClient.oAuthClients().create(entity);
} catch (Exception e) {
onApplyError("Failed to create OAuthClient from " + sourceName + ". " + e + ". " + entity, e);
}
Expand Down Expand Up @@ -402,13 +400,7 @@ public void applyPersistentVolumeClaim(PersistentVolumeClaim entity, String sour
doCreatePersistentVolumeClaim(entity, namespace, sourceName);
}
} else {
log.info("Updating a PersistentVolumeClaim from " + sourceName);
try {
HasMetadata answer = patchService.compareAndPatchEntity(namespace, entity, old);
logGeneratedEntity("Updated PersistentVolumeClaim: ", namespace, entity, answer);
} catch (Exception e) {
onApplyError("Failed to update PersistentVolumeClaim from " + sourceName + ". " + e + ". " + entity, e);
}
doPatchEntity(old, entity, namespace, sourceName);
}
}
} else {
Expand Down Expand Up @@ -438,13 +430,7 @@ public void applyCustomResourceDefinition(CustomResourceDefinition entity, Strin
kubernetesClient.customResourceDefinitions().withName(id).delete();
doCreateCustomResourceDefinition(entity, sourceName);
} else {
log.info("Updating a Custom Resource Definition from " + sourceName + " name " + getName(entity));
try {
HasMetadata answer = patchService.compareAndPatchEntity(namespace, entity, old);
log.info("Updated Custom Resource Definition result: " + getName(answer));
} catch (Exception e) {
onApplyError("Failed to update Custom Resource Definition from " + sourceName + ". " + e + ". " + entity, e);
}
doPatchEntity(old, entity, namespace, sourceName);
}
}
} else {
Expand All @@ -459,8 +445,8 @@ public void applyCustomResourceDefinition(CustomResourceDefinition entity, Strin
private void doCreateCustomResourceDefinition(CustomResourceDefinition entity, String sourceName) {
log.info("Creating a Custom Resource Definition from " + sourceName + " name " + getName(entity));
try {
Object answer = kubernetesClient.customResourceDefinitions().create(entity);
log.info("Created Custom Resource Definition result: " + ((CustomResourceDefinition) answer).getMetadata().getName());
CustomResourceDefinition answer = kubernetesClient.customResourceDefinitions().create(entity);
log.info("Created Custom Resource Definition result: " + answer.getMetadata().getName());
} catch (Exception e) {
onApplyError("Failed to create Custom Resource Definition from " + sourceName + ". " + e + ". " + entity, e);
}
Expand Down Expand Up @@ -535,19 +521,12 @@ public void applySecret(Secret secret, String sourceName) throws Exception {
// if the secret already exists and is the same, then do nothing
if (UserConfigurationCompare.configEqual(secret, old)) {
log.info("Secret has not changed so not doing anything");
return;
} else {
if (isRecreateMode()) {
kubernetesClient.secrets().inNamespace(namespace).withName(id).delete();
doCreateSecret(secret, namespace, sourceName);
} else {
log.info("Updating a Secret from " + sourceName);
try {
Object answer = patchService.compareAndPatchEntity(namespace, secret, old);
logGeneratedEntity("Updated Secret:", namespace, secret, answer);
} catch (Exception e) {
onApplyError("Failed to update secret from " + sourceName + ". " + e + ". " + secret, e);
}
doPatchEntity(old, secret, namespace, sourceName);
}
}
} else {
Expand All @@ -559,7 +538,6 @@ public void applySecret(Secret secret, String sourceName) throws Exception {
}
}


protected void doCreateSecret(Secret secret, String namespace, String sourceName) {
log.info("Creating a Secret from " + sourceName + " namespace " + namespace + " name " + getName(secret));
try {
Expand Down Expand Up @@ -642,7 +620,6 @@ public Object processTemplate(Template entity, String sourceName) {
}
}


public void applyRoute(Route entity, String sourceName) {
OpenShiftClient openShiftClient = getOpenShiftClient();
if (openShiftClient != null) {
Expand All @@ -652,21 +629,47 @@ public void applyRoute(Route entity, String sourceName) {
if (StringUtils.isBlank(namespace)) {
namespace = getNamespace();
}
if (isServicesOnlyMode()) {
log.debug("Ignoring Route: " + namespace + ":" + id);
return;
}
Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get();
if (route == null) {
try {
log.info("Creating Route " + namespace + ":" + id + " " +
(entity.getSpec() != null ?
"host: " + entity.getSpec().getHost() :
"No Spec !"));
openShiftClient.routes().inNamespace(namespace).create(entity);
} catch (Exception e) {
onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e);
if (isRunning(route)) {
if (UserConfigurationCompare.configEqual(entity, route)) {
log.info("Route has not changed so not doing anything");
} else {
if (isRecreateMode()) {
log.info("Deleting Route: " + id);
openShiftClient.routes().inNamespace(namespace).withName(id).delete();
doCreateRoute(entity, namespace, sourceName);
} else {
doPatchEntity(route, entity, namespace, sourceName);
}
}
} else {
if (!isAllowCreate()) {
log.warn("Creation disabled so not creating a Route from " + sourceName + " namespace " + namespace + " name " + id);
} else {
doCreateRoute(entity, namespace, sourceName);
}
}
}
}

private void doCreateRoute(Route entity, String namespace, String sourceName) {
OpenShiftClient openShiftClient = getOpenShiftClient();
String id = getName(entity);
try {
log.info("Creating Route " + namespace + ":" + id + " " +
(entity.getSpec() != null ?
"host: " + entity.getSpec().getHost() :
"No Spec !"));
openShiftClient.routes().inNamespace(namespace).create(entity);
} catch (Exception e) {
onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e);
}
}

public void applyBuildConfig(BuildConfig entity, String sourceName) {
OpenShiftClient openShiftClient = getOpenShiftClient();
if (openShiftClient != null) {
Expand All @@ -688,17 +691,7 @@ public void applyBuildConfig(BuildConfig entity, String sourceName) {
openShiftClient.buildConfigs().inNamespace(namespace).withName(id).delete();
doCreateBuildConfig(entity, namespace, sourceName);
} else {
log.info("Updating BuildConfig from " + sourceName);
try {
String resourceVersion = KubernetesHelper.getResourceVersion(old);
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setNamespace(namespace);
metadata.setResourceVersion(resourceVersion);
Object answer = patchService.compareAndPatchEntity(namespace, entity, old);
logGeneratedEntity("Updated BuildConfig: ", namespace, entity, answer);
} catch (Exception e) {
onApplyError("Failed to update BuildConfig from " + sourceName + ". " + e + ". " + entity, e);
}
doPatchEntity(old, entity, namespace, sourceName);
}
}
} else {
Expand Down Expand Up @@ -868,13 +861,7 @@ public void applyService(Service service, String sourceName) throws Exception {
kubernetesClient.services().inNamespace(namespace).withName(id).delete();
doCreateService(service, namespace, sourceName);
} else {
log.info("Updating a Service from " + sourceName);
try {
Object answer = patchService.compareAndPatchEntity(namespace, service, old);
logGeneratedEntity("Updated Service: ", namespace, service, answer);
} catch (Exception e) {
onApplyError("Failed to update Service from " + sourceName + ". " + e + ". " + service, e);
}
doPatchEntity(old, service, namespace, sourceName);
}
}
} else {
Expand Down Expand Up @@ -939,6 +926,17 @@ protected <T extends HasMetadata,L,D> void doCreateResource(T resource, String n
}
}

private <T extends HasMetadata> void doPatchEntity(T oldEntity, T newEntity, String namespace, String sourceName) {
String kind = newEntity.getKind();
log.info("Updating {} from {}", kind, sourceName);
try {
Object answer = patchService.compareAndPatchEntity(namespace, newEntity, oldEntity);
logGeneratedEntity("Updated " + kind + ": ", namespace, newEntity, answer);
} catch (Exception e) {
onApplyError("Failed to update " + kind + " from " + sourceName + ". " + e + ". " + newEntity, e);
}
}

protected void doCreateService(Service service, String namespace, String sourceName) {
log.info("Creating a Service from " + sourceName + " namespace " + namespace + " name " + getName(service));
try {
Expand Down Expand Up @@ -1185,13 +1183,7 @@ public void applyPod(Pod pod, String sourceName) throws Exception {
kubernetesClient.pods().inNamespace(namespace).withName(id).delete();
doCreatePod(pod, namespace, sourceName);
} else {
log.info("Updating a Pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
try {
Object answer = patchService.compareAndPatchEntity(namespace, pod, old);
log.info("Updated Pod result: " + answer);
} catch (Exception e) {
onApplyError("Failed to update Pod from " + sourceName + ". " + e + ". " + pod, e);
}
doPatchEntity(old, pod, namespace, sourceName);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.DoneableBuildConfig;
import io.fabric8.openshift.api.model.DoneableImageStream;
import io.fabric8.openshift.api.model.DoneableRoute;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.OpenShiftClient;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
Expand All @@ -47,7 +49,6 @@ public class PatchService {

private static Map<String, EntityPatcher<? extends HasMetadata>> patchers;


// Interface for patching entities
interface EntityPatcher<T extends HasMetadata> {

Expand All @@ -63,7 +64,6 @@ interface EntityPatcher<T extends HasMetadata> {
T patch(KubernetesClient client, String namespace, T newEntity, T oldEntity);
}


static {
patchers = new HashMap<>();
patchers.put("Pod", podPatcher());
Expand All @@ -75,15 +75,14 @@ interface EntityPatcher<T extends HasMetadata> {
patchers.put("PersistentVolumeClaim", pvcPatcher());
patchers.put("CustomResourceDefinition", crdPatcher());
patchers.put("Job", jobPatcher());
patchers.put("Route", routePatcher());
}


public PatchService(KubernetesClient client, KitLogger log) {
this.kubernetesClient = client;
this.log = log;
}


public <T extends HasMetadata> T compareAndPatchEntity(String namespace, T newDto, T oldDto) {
EntityPatcher<T> dispatcher = (EntityPatcher<T>) patchers.get(newDto.getKind());
if (dispatcher == null) {
Expand Down Expand Up @@ -113,7 +112,7 @@ private static EntityPatcher<Pod> podPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
entity.withSpec(newObj.getSpec());
}
return entity.done();
};
Expand All @@ -136,7 +135,7 @@ private static EntityPatcher<ReplicationController> rcPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
entity.withSpec(newObj.getSpec());
}
return entity.done();
};
Expand Down Expand Up @@ -181,7 +180,7 @@ private static EntityPatcher<Secret> secretPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getData(), oldObj.getData())) {
entity.withData(newObj.getData());
entity.withData(newObj.getData());
}
if(!UserConfigurationCompare.configEqual(newObj.getStringData(), oldObj.getStringData())) {
entity.withStringData(newObj.getStringData());
Expand All @@ -206,7 +205,7 @@ private static EntityPatcher<PersistentVolumeClaim> pvcPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
entity.withSpec(newObj.getSpec());
}
return entity.done();
};
Expand Down Expand Up @@ -282,7 +281,7 @@ private static EntityPatcher<BuildConfig> bcPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
entity.withSpec(newObj.getSpec());
}
return entity.done();
};
Expand All @@ -308,10 +307,37 @@ private static EntityPatcher<ImageStream> isPatcher() {
}

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
entity.withSpec(newObj.getSpec());
}
return entity.done();
};
}

private static EntityPatcher<Route> routePatcher() {
return (client, namespace, newObj, oldObj) -> {
if (UserConfigurationCompare.configEqual(newObj, oldObj)) {
return oldObj;
}
OpenShiftClient openShiftClient = OpenshiftHelper.asOpenShiftClient(client);
if (openShiftClient == null) {
throw new IllegalArgumentException("Route can only be patched when connected to an OpenShift cluster");
}

DoneableRoute entity = openShiftClient.routes()
.inNamespace(namespace)
.withName(oldObj.getMetadata().getName())
.edit();

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

if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
entity.withSpec(newObj.getSpec());
}

return entity.done();
};
}

}

0 comments on commit c36d467

Please sign in to comment.