Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Custom Resource fragments can be replaced and recreated with FKC 5.3.x #689

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Usage:
* Fix #630: DeploymentConfigEnricher and DefaultControllerEnricher refactored and aligned
* Fix #639: Quotas for OpenShift BuildConfig not working
* Fix #688: Multiple Custom Resources with same (different apiGroup) name can be added
* Fix #689: Recreate and update of CustomResource fragments works

### 1.2.0 (2021-03-31)
* Fix #529: `.maven-dockerignore`, `.maven-dockerexclude`, `.maven-dockerinclude` are no longer supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@
*/
package org.eclipse.jkube.kit.config.service;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jkube.kit.common.GenericCustomResource;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.common.util.UserConfigurationCompare;
import org.eclipse.jkube.kit.config.resource.JKubeAnnotations;
import org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -55,33 +78,12 @@
import io.fabric8.openshift.api.model.TagReference;
import io.fabric8.openshift.api.model.Template;
import io.fabric8.openshift.client.OpenShiftClient;
import org.eclipse.jkube.kit.common.GenericCustomResource;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.common.util.UserConfigurationCompare;
import org.eclipse.jkube.kit.config.resource.JKubeAnnotations;
import org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getCrdContext;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getFullyQualifiedApiGroupWithKind;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getKind;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getName;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.getOrCreateLabels;
Expand Down Expand Up @@ -395,7 +397,7 @@ protected void doCreateServiceAccount(ServiceAccount serviceAccount, String name
(serviceAccount));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.serviceAccounts().inNamespace(namespace).create(serviceAccount);
} else {
answer = kubernetesClient.serviceAccounts().inNamespace(getNamespace()).create(serviceAccount);
Expand Down Expand Up @@ -484,28 +486,27 @@ private void doCreateCustomResourceDefinition(CustomResourceDefinition entity, S
}
}

public void applyCustomResource(CustomResourceDefinitionContext context, GenericCustomResource genericCustomResource, String sourceName)
private void applyCustomResource(CustomResourceDefinitionContext context, GenericCustomResource genericCustomResource, String sourceName)
throws IOException {

String customResourceStr = Serialization.jsonMapper().writeValueAsString(genericCustomResource);
String name = genericCustomResource.getMetadata().getName();
String apiGroupWithKind = KubernetesHelper.getFullyQualifiedApiGroupWithKind(context);
Objects.requireNonNull(name, "No name for " + genericCustomResource + " " + sourceName);

if (isRecreateMode()) {
KubernetesClientUtil.doDeleteCustomResource(kubernetesClient, context, namespace, name);
KubernetesClientUtil.doCreateCustomResource(kubernetesClient, context, namespace, customResourceStr);
log.info("Created Custom Resource: %s %s/%s", apiGroupWithKind, namespace, name);
} else {
Map<String, Object> crFromServer = KubernetesClientUtil.doGetCustomResource(kubernetesClient, context, namespace, name);
if (crFromServer == null) {
KubernetesClientUtil.doCreateCustomResource(kubernetesClient, context, namespace, customResourceStr);
log.info("Created Custom Resource: %s %s/%s", apiGroupWithKind, namespace, name);
} else {
KubernetesClientUtil.doEditCustomResource(kubernetesClient, context, namespace, name, customResourceStr);
log.info("Updated Custom Resource: %s %s/%s", KubernetesHelper.getFullyQualifiedApiGroupWithKind(context), namespace, name);
}
log.info("Attempting to delete Custom Resource: %s %s/%s", apiGroupWithKind, namespace, name);
KubernetesClientUtil.doDeleteAndWait(kubernetesClient, context, namespace, name, 10L);

}
final GenericCustomResource existentCR = KubernetesClientUtil.doGetCustomResource(kubernetesClient, context, namespace, name);
if (existentCR != null && isBlank(existentCR.getMetadata().getDeletionTimestamp())) {
log.info("Replacing Custom Resource: %s %s/%s",
KubernetesHelper.getFullyQualifiedApiGroupWithKind(context), namespace, name);
genericCustomResource.getMetadata().setResourceVersion(existentCR.getMetadata().getResourceVersion());
}
kubernetesClient.customResource(context).inNamespace(namespace).withName(name)
.createOrReplace(Serialization.jsonMapper().writeValueAsString(genericCustomResource));
log.info("Created Custom Resource: %s %s/%s", apiGroupWithKind, namespace, name);
}

protected boolean isBound(PersistentVolumeClaim claim) {
Expand All @@ -518,7 +519,7 @@ protected void doCreatePersistentVolumeClaim(PersistentVolumeClaim entity, Strin
log.info("Creating a PersistentVolumeClaim from " + sourceName + " namespace " + namespace + " name " + getName(entity));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(entity);
} else {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(getNamespace()).create(entity);
Expand Down Expand Up @@ -565,7 +566,7 @@ protected void doCreateSecret(Secret secret, String namespace, String sourceName
log.info("Creating a Secret from " + sourceName + " namespace " + namespace + " name " + getName(secret));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.secrets().inNamespace(namespace).create(secret);
} else {
answer = kubernetesClient.secrets().inNamespace(getNamespace()).create(secret);
Expand All @@ -582,7 +583,7 @@ protected void logGeneratedEntity(String message, String namespace, HasMetadata
namespaceDir.mkdirs();
String kind = getKind(entity);
String name = getName(entity);
if (StringUtils.isNotBlank(kind)) {
if (isNotBlank(kind)) {
name = kind.toLowerCase() + "-" + name;
}
if (StringUtils.isBlank(name)) {
Expand Down Expand Up @@ -938,7 +939,7 @@ protected <T extends HasMetadata, L> void doCreateResource(T resource, String na
log.info("Creating a " + kind + " from " + sourceName + " namespace " + namespace + " name " + getName(resource));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = resources.inNamespace(namespace).create(resource);
} else {
answer = resources.inNamespace(getNamespace()).create(resource);
Expand All @@ -964,7 +965,7 @@ protected void doCreateService(Service service, String namespace, String sourceN
log.info("Creating a Service from " + sourceName + " namespace " + namespace + " name " + getName(service));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.services().inNamespace(namespace).create(service);
} else {
answer = kubernetesClient.services().inNamespace(getNamespace()).create(service);
Expand Down Expand Up @@ -1023,7 +1024,7 @@ public void applyNamespace(String namespaceName, Map<String,String> labels) {
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String kubernetesClientNamespace = kubernetesClient.getNamespace();
if (StringUtils.isNotBlank(kubernetesClientNamespace)) {
if (isNotBlank(kubernetesClientNamespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
Expand All @@ -1039,7 +1040,7 @@ public void applyNamespace(String namespaceName, Map<String,String> labels) {
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String kubernetesClientNamespace = kubernetesClient.getNamespace();
if (StringUtils.isNotBlank(kubernetesClientNamespace)) {
if (isNotBlank(kubernetesClientNamespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
Expand Down Expand Up @@ -1177,7 +1178,7 @@ protected void doCreateReplicationController(ReplicationController replicationCo
log.info("Creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.replicationControllers().inNamespace(namespace).create(replicationController);
} else {
answer = kubernetesClient.replicationControllers().inNamespace(getNamespace()).create(replicationController);
Expand Down Expand Up @@ -1222,7 +1223,7 @@ protected void doCreatePod(Pod pod, String namespace, String sourceName) {
log.info("Creating a Pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
try {
Object answer;
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
answer = kubernetesClient.pods().inNamespace(namespace).create(pod);
} else {
answer = kubernetesClient.pods().inNamespace(getNamespace()).create(pod);
Expand Down Expand Up @@ -1256,7 +1257,7 @@ protected void applyJob(Job job, String sourceName) {
}

public void doCreateJob(Job job, String namespace, String sourceName) {
if (StringUtils.isNotBlank(namespace)) {
if (isNotBlank(namespace)) {
kubernetesClient.batch().jobs().inNamespace(namespace).create(job);
} else {
kubernetesClient.batch().jobs().inNamespace(getNamespace()).create(job);
Expand Down
Loading