Skip to content

Commit

Permalink
fix fabric8io#3972 load should not be overloaded
Browse files Browse the repository at this point in the history
also moving more of the openshift impl to be client based
  • Loading branch information
shawkins committed Dec 8, 2022
1 parent b8f6fb4 commit 27342b9
Show file tree
Hide file tree
Showing 22 changed files with 718 additions and 382 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@
* Fix #4579: the implicit registration of resource and list types that happens when using the resource(class) methods has been removed. This makes the behavior of the client more predictable as that was an undocumented side-effect. If you expect to see instances of a custom type from an untyped api call - typically KubernetesClient.load, KubernetesClient.resourceList, KubernetesClient.resource(InputStream|String), then you must either create a META-INF/services/io.fabric8.kubernetes.api.model.KubernetesResource file (see above #3923), or make calls to KubernetesDeserializer.registerCustomKind - however since KubernetesDeserializer is an internal class that mechanism is not preferred.
* Fix #4597: remove the deprecated support for `javax.validation.constraints.NotNull` in the `crd-generator`, to mark a property as `required` it needs to be annotated with `io.fabric8.generator.annotation.Required`
* Fix #3973: removed support for Parameterizable - that was only needed as a workaround for non-string parameters. You should instead include those parameter values in the map passed to processLocally.
* Fix #3973: openShiftClient.templates().load and openShiftClient.load will no longer automatically process / create templates. Use the method openshiftClient.templates().from to get a template from any stream.

### 6.2.0 (2022-10-20)

Expand Down
Expand Up @@ -15,13 +15,11 @@
*/
package io.fabric8.kubernetes.client.extended.leaderelection.resourcelock;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,13 +34,11 @@ public class ConfigMapLock implements Lock {
private final String configMapNamespace;
private final String configMapName;
private final String identity;
private final ObjectMapper objectMapper;

public ConfigMapLock(String configMapNamespace, String configMapName, String identity) {
this.configMapNamespace = Objects.requireNonNull(configMapNamespace, "configMapNamespace is required");
this.configMapName = Objects.requireNonNull(configMapName, "configMapName is required");
this.identity = Objects.requireNonNull(identity, "identity is required");
objectMapper = Serialization.jsonMapper();
}

/**
Expand All @@ -58,9 +54,8 @@ public LeaderElectionRecord get(KubernetesClient client) {
.map(annotations -> annotations.get(LEADER_ELECTION_RECORD_ANNOTATION_KEY))
.map(annotation -> {
try {
return objectMapper.readValue(annotation, new TypeReference<LeaderElectionRecord>() {
});
} catch (JsonProcessingException ex) {
return Serialization.unmarshal(annotation, LeaderElectionRecord.class);
} catch (KubernetesClientException ex) {
LOGGER.error("Error deserializing LeaderElectionRecord from ConfigMap", ex);
return null;
}
Expand All @@ -80,11 +75,11 @@ public void create(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {

try {
client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder()
client.configMaps().inNamespace(configMapNamespace).resource(new ConfigMapBuilder()
.editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName)
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord))
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord))
.endMetadata()
.build());
.build()).create();
} catch (Exception e) {
throw new LockException("Unable to create ConfigMapLock", e);
}
Expand All @@ -100,11 +95,11 @@ public void update(
try {
final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get();
toReplace.getMetadata().getAnnotations()
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord));
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord));
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.configMaps().inNamespace(configMapNamespace).withName(configMapName)
client.configMaps().inNamespace(configMapNamespace).resource(toReplace)
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
.replace(toReplace);
.replace();
} catch (Exception e) {
throw new LockException("Unable to update ConfigMapLock", e);
}
Expand Down

0 comments on commit 27342b9

Please sign in to comment.