Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Fix #1699: Ability to specify object namespace in fragments (#1703)
Browse files Browse the repository at this point in the history
namespace inside resource fragments should be given more priority over
default namespace
  • Loading branch information
rohanKanojia authored Oct 3, 2019
1 parent 724bfae commit b5cf2c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Fix #1697: NullpointerException when trying to apply custom resources
* Fix #1696: fmp not setting imagestreams resourceVersion properly.
* Fix #1676: Support for latest kubernetes client
* Fix #1699: Ability to specify object namespace in fragments
* Feature #1536: Java Image Builder Support
* Fix #1704: fabric8-build failing on openshift
* Feature #1706: Prometheus Enricher; Configuration support for Prometheus path
Expand Down
75 changes: 45 additions & 30 deletions core/src/main/java/io/fabric8/maven/core/service/ApplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void applyEntity(Object dto, String sourceName) throws Exception {
HasMetadata entity = (HasMetadata) dto;
try {
log.info("Applying " + getKind(entity) + " " + getName(entity) + " from " + sourceName);
kubernetesClient.resource(entity).inNamespace(getNamespace()).createOrReplace();
kubernetesClient.resource(entity).inNamespace(getNamespace(entity)).createOrReplace();
} catch (Exception e) {
onApplyError("Failed to create " + getKind(entity) + " from " + sourceName + ". " + e, e);
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public void installTemplate(Template entity, String sourceName) {
return;
}
if (!isProcessTemplatesLocally()) {
String namespace = getNamespace();
String namespace = getNamespace(entity);
String id = getName(entity);
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
Template old = openShiftClient.templates().inNamespace(namespace).withName(id).get();
Expand Down Expand Up @@ -329,7 +329,7 @@ protected void doCreateTemplate(Template entity, String namespace, String source
* Creates/updates a service account and processes it returning the processed DTOs
*/
public void applyServiceAccount(ServiceAccount serviceAccount, String sourceName) throws Exception {
String namespace = getNamespace();
String namespace = getNamespace(serviceAccount);
String id = getName(serviceAccount);
Objects.requireNonNull(id, "No name for " + serviceAccount + " " + sourceName);
if (isServicesOnlyMode()) {
Expand Down Expand Up @@ -371,7 +371,7 @@ protected void doCreateServiceAccount(ServiceAccount serviceAccount, String name
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.serviceAccounts().inNamespace(namespace).create(serviceAccount);
} else {
answer = kubernetesClient.serviceAccounts().inNamespace(getNamespace()).create(serviceAccount);
answer = kubernetesClient.serviceAccounts().inNamespace(this.namespace).create(serviceAccount);
}
logGeneratedEntity("Created ServiceAccount: ", namespace, serviceAccount, answer);
} catch (Exception e) {
Expand All @@ -382,7 +382,7 @@ protected void doCreateServiceAccount(ServiceAccount serviceAccount, String name
public void applyPersistentVolumeClaim(PersistentVolumeClaim entity, String sourceName) throws Exception {
// we cannot update PVCs
boolean alwaysRecreate = true;
String namespace = getNamespace();
String namespace = getNamespace(entity);
String id = getName(entity);
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
if (isServicesOnlyMode()) {
Expand Down Expand Up @@ -424,7 +424,7 @@ public void applyPersistentVolumeClaim(PersistentVolumeClaim entity, String sour
}

public void applyCustomResourceDefinition(CustomResourceDefinition entity, String sourceName) {
String namespace = getNamespace();
String namespace = getNamespace(entity);
String id = getName(entity);
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
if (isServicesOnlyMode()) {
Expand Down Expand Up @@ -474,6 +474,10 @@ public void applyCustomResource(File customResourceFile, String namespace, Custo
Map<String, Object> objectMeta = (Map<String, Object>)cr.get("metadata");
String name = objectMeta.get("name").toString();

if (objectMeta.get("namespace") != null) {
namespace = objectMeta.get("namespace").toString();
}

if (isRecreateMode()) {
KubernetesClientUtil.doDeleteCustomResource(kubernetesClient, context, namespace, name);
KubernetesClientUtil.doCreateCustomResource(kubernetesClient, context, namespace, customResourceFile);
Expand All @@ -484,8 +488,23 @@ public void applyCustomResource(File customResourceFile, String namespace, Custo
KubernetesClientUtil.doCreateCustomResource(kubernetesClient, context, namespace, customResourceFile);
log.info("Created Custom Resource: " + name);
} else {
KubernetesClientUtil.doEditCustomResource(kubernetesClient, context, namespace, name, customResourceFile);
log.info("Updated Custom Resource: " + name);
Map<String, Object> newCustomResource = KubernetesClientUtil.doLoadCustomResource(kubernetesClient, context, customResourceFile);
if (UserConfigurationCompare.configEqual(newCustomResource, cr)) {
log.info("Custom resource " + name + " not changes so not doing anything");
} else {
/*
* This is in order to resolve https://github.com/fabric8io/kubernetes-client/issues/1724
* You need to add metadata.resourceVersion in object in order to edit.
*/
Map<String, Object> metadata = (Map<String, Object>)newCustomResource.get("metadata");
if (metadata != null) {
metadata.put("resourceVersion", ((Map<String, Object>) cr.get("metadata")).get("resourceVersion"));
newCustomResource.put("metadata", metadata);
}

KubernetesClientUtil.doEditCustomResource(kubernetesClient, context, namespace, name, newCustomResource);
log.info("Updated Custom Resource: " + name);
}
}
}
}
Expand All @@ -511,7 +530,7 @@ protected void doCreatePersistentVolumeClaim(PersistentVolumeClaim entity, Strin
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(entity);
} else {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(getNamespace()).create(entity);
answer = kubernetesClient.persistentVolumeClaims().inNamespace(getNamespace(entity)).create(entity);
}
logGeneratedEntity("Created PersistentVolumeClaim: ", namespace, entity, answer);
} catch (Exception e) {
Expand Down Expand Up @@ -566,7 +585,7 @@ protected void doCreateSecret(Secret secret, String namespace, String sourceName
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.secrets().inNamespace(namespace).create(secret);
} else {
answer = kubernetesClient.secrets().inNamespace(getNamespace()).create(secret);
answer = kubernetesClient.secrets().inNamespace(getNamespace(secret)).create(secret);
}
logGeneratedEntity("Created Secret: ", namespace, secret, answer);
} catch (Exception e) {
Expand Down Expand Up @@ -649,7 +668,7 @@ public void applyRoute(Route entity, String sourceName) {
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
String namespace = KubernetesHelper.getNamespace(entity);
if (StringUtils.isBlank(namespace)) {
namespace = getNamespace();
namespace = getNamespace(entity);
}
Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get();
if (route == null) {
Expand All @@ -674,7 +693,7 @@ public void applyBuildConfig(BuildConfig entity, String sourceName) {
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
String namespace = KubernetesHelper.getNamespace(entity);
if (StringUtils.isBlank(namespace)) {
namespace = getNamespace();
namespace = getNamespace(entity);
}
applyNamespace(namespace);
BuildConfig old = openShiftClient.buildConfigs().inNamespace(namespace).withName(id).get();
Expand Down Expand Up @@ -729,7 +748,7 @@ public void applyRoleBinding(RoleBinding entity, String sourceName) {
Objects.requireNonNull(id, "No name for " + entity + " " + sourceName);
String namespace = KubernetesHelper.getNamespace(entity);
if (StringUtils.isBlank(namespace)) {
namespace = getNamespace();
namespace = getNamespace(entity);
}
applyNamespace(namespace);
RoleBinding old = openShiftClient.rbac().roleBindings().inNamespace(namespace).withName(id).get();
Expand Down Expand Up @@ -781,7 +800,7 @@ public void applyImageStream(ImageStream entity, String sourceName) {
if (openShiftClient != null) {
String kind = getKind(entity);
String name = getName(entity);
String namespace = getNamespace();
String namespace = getNamespace(entity);
try {
Resource<ImageStream, DoneableImageStream> resource = openShiftClient.imageStreams().inNamespace(namespace).withName(name);
ImageStream old = resource.get();
Expand Down Expand Up @@ -855,7 +874,7 @@ public void applyList(KubernetesList list, String sourceName) throws Exception {
}

public void applyService(Service service, String sourceName) throws Exception {
String namespace = getNamespace();
String namespace = getNamespace(service);
String id = getName(service);
Objects.requireNonNull(id, "No name for " + service + " " + sourceName);
if (isIgnoreServiceMode()) {
Expand Down Expand Up @@ -890,8 +909,8 @@ public void applyService(Service service, String sourceName) throws Exception {
}
}

public <T extends HasMetadata,L,D> void applyResource(T resource, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
String namespace = getNamespace();
public <T extends HasMetadata, L, D> void applyResource(T resource, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
String namespace = getNamespace(resource);
String id = getName(resource);
String kind = getKind(resource);
Objects.requireNonNull(id, "No name for " + resource + " " + sourceName);
Expand Down Expand Up @@ -935,7 +954,7 @@ protected <T extends HasMetadata,L,D> void doCreateResource(T resource, String n
if (StringUtils.isNotBlank(namespace)) {
answer = resources.inNamespace(namespace).create(resource);
} else {
answer = resources.inNamespace(getNamespace()).create(resource);
answer = resources.inNamespace(this.namespace).create(resource);
}
logGeneratedEntity("Created " + kind + ": ", namespace, resource, answer);
} catch (Exception e) {
Expand All @@ -950,7 +969,7 @@ protected void doCreateService(Service service, String namespace, String sourceN
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.services().inNamespace(namespace).create(service);
} else {
answer = kubernetesClient.services().inNamespace(getNamespace()).create(service);
answer = kubernetesClient.services().inNamespace(this.namespace).create(service);
}
logGeneratedEntity("Created Service: ", namespace, service, answer);
} catch (Exception e) {
Expand Down Expand Up @@ -1095,7 +1114,7 @@ public boolean applyProjectRequest(ProjectRequest entity) {
}

public void applyReplicationController(ReplicationController replicationController, String sourceName) throws Exception {
String namespace = getNamespace();
String namespace = getNamespace(replicationController);
String id = getName(replicationController);
Objects.requireNonNull(id, "No name for " + replicationController + " " + sourceName);
if (isServicesOnlyMode()) {
Expand Down Expand Up @@ -1157,7 +1176,7 @@ protected void doCreateReplicationController(ReplicationController replicationCo
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.replicationControllers().inNamespace(namespace).create(replicationController);
} else {
answer = kubernetesClient.replicationControllers().inNamespace(getNamespace()).create(replicationController);
answer = kubernetesClient.replicationControllers().inNamespace(this.namespace).create(replicationController);
}
logGeneratedEntity("Created ReplicationController: ", namespace, replicationController, answer);
} catch (Exception e) {
Expand All @@ -1166,7 +1185,7 @@ protected void doCreateReplicationController(ReplicationController replicationCo
}

public void applyPod(Pod pod, String sourceName) throws Exception {
String namespace = getNamespace();
String namespace = getNamespace(pod);
String id = getName(pod);
Objects.requireNonNull(id, "No name for " + pod + " " + sourceName);
if (isServicesOnlyMode()) {
Expand Down Expand Up @@ -1208,7 +1227,7 @@ protected void doCreatePod(Pod pod, String namespace, String sourceName) {
if (StringUtils.isNotBlank(namespace)) {
answer = kubernetesClient.pods().inNamespace(namespace).create(pod);
} else {
answer = kubernetesClient.pods().inNamespace(getNamespace()).create(pod);
answer = kubernetesClient.pods().inNamespace(this.namespace).create(pod);
}
log.info("Created Pod result: " + answer);
} catch (Exception e) {
Expand All @@ -1217,7 +1236,7 @@ protected void doCreatePod(Pod pod, String namespace, String sourceName) {
}

protected void applyJob(Job job, String sourceName) {
String namespace = getNamespace();
String namespace = getNamespace(job);
String id = getName(job);
Objects.requireNonNull(id, "No name for " + job + " " + sourceName);
if (isServicesOnlyMode()) {
Expand All @@ -1242,23 +1261,19 @@ public void doCreateJob(Job job, String namespace, String sourceName) throws Kub
if (StringUtils.isNotBlank(namespace)) {
kubernetesClient.batch().jobs().inNamespace(namespace).create(job);
} else {
kubernetesClient.batch().jobs().inNamespace(getNamespace()).create(job);
kubernetesClient.batch().jobs().inNamespace(this.namespace).create(job);
}
log.info("Creating a Job from " + sourceName + " namespace " + namespace + " name " + getName(job));
}

public String getNamespace() {
return namespace;
}


/**
* Returns the namespace defined in the entity or the configured namespace
*/
protected String getNamespace(HasMetadata entity) {
String answer = KubernetesHelper.getNamespace(entity);
if (StringUtils.isBlank(answer)) {
answer = getNamespace();
answer = this.namespace;
}
// lest make sure the namespace exists
applyNamespace(answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -239,11 +241,15 @@ public static Map<String, Object> doCreateCustomResource(KubernetesClient kubern
}
}

public static Map<String, Object> doEditCustomResource(KubernetesClient kubernetesClient, CustomResourceDefinitionContext crdContext, String namespace, String name, File customResourceFile) throws IOException {
public static Map<String, Object> doLoadCustomResource(KubernetesClient kubernetesClient, CustomResourceDefinitionContext crdContext, File customResourceFile) throws IOException {
return kubernetesClient.customResource(crdContext).load(new FileInputStream(customResourceFile));
}

public static Map<String, Object> doEditCustomResource(KubernetesClient kubernetesClient, CustomResourceDefinitionContext crdContext, String namespace, String name, Map<String, Object> customResource) throws IOException {
if ("Namespaced".equals(crdContext.getScope())) {
return kubernetesClient.customResource(crdContext).edit(namespace, name, new FileInputStream(customResourceFile.getAbsolutePath()));
return kubernetesClient.customResource(crdContext).edit(namespace, name, customResource);
} else {
return kubernetesClient.customResource(crdContext).edit(name, doGetCustomResourceAsString(customResourceFile));
return kubernetesClient.customResource(crdContext).edit(name, customResource);
}
}

Expand Down

0 comments on commit b5cf2c7

Please sign in to comment.