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

deps: bump Kubernetes Client to 6.7.1 #2192

Merged
merged 1 commit into from Jun 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@ Usage:
* Fix #2170: `internal-microservice` profile prevents Service exposure
* Fix #2174: Profile merge constructor accounts for parentProfile field
* Fix #2187: `serviceAccount` configuration option has stopped working
* Fix #2192: Bump Kubernetes Client to 6.7.0 (use JKube Serialization util to wrap around the Kubernetes Client KubernetesSerialization)

### 1.12.0 (2023-04-03)
* Fix #1179: Move storageClass related functionality out of VolumePermissionEnricher to PersistentVolumeClaimStorageClassEnricher
Expand Down
Expand Up @@ -15,11 +15,10 @@


import com.fasterxml.jackson.core.type.TypeReference;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.eclipse.jkube.kit.build.api.auth.AuthConfig;
import org.eclipse.jkube.kit.common.util.Serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -100,8 +99,8 @@ private static Map<String, Object> readKubeConfig() {
final File applicableFile = kubeConfig == null ?
getHomeDir().toPath().resolve(KUBECONFIG_FILE).toFile() : new File(kubeConfig);
if (applicableFile.exists()) {
try (FileInputStream fis = new FileInputStream(applicableFile)) {
return Serialization.unmarshal(fis, new TypeReference<Map<String, Object>>() {});
try {
return Serialization.unmarshal(applicableFile, new TypeReference<Map<String, Object>>() {});
} catch (IOException ex) {
// Ignore
}
Expand Down
Expand Up @@ -14,12 +14,13 @@
package org.eclipse.jkube.kit.common;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.Serialization;

/**
* Type of resources supported
Expand All @@ -29,30 +30,20 @@
*/
public enum ResourceFileType {

json("json","json") {
@Override
public ObjectMapper getObjectMapper() {
return Serialization.jsonMapper();
}
},
json("json","json", Serialization::saveJson),

yaml("yml","yml") {
@Override
public ObjectMapper getObjectMapper() {
return Serialization.yamlMapper();
}
};
yaml("yml","yml", Serialization::saveYaml);

private final String extension;
private final String artifactType;
private final Serializer serializer;

ResourceFileType(String extension, String artifactType) {
ResourceFileType(String extension, String artifactType, Serializer serializer) {
this.extension = extension;
this.artifactType = artifactType;
this.serializer = serializer;
}

public abstract ObjectMapper getObjectMapper();

public File addExtensionIfMissing(File file) {
String path = file.getAbsolutePath();
if (!path.endsWith("." + extension)) {
Expand All @@ -66,6 +57,11 @@ public String getArtifactType() {
return artifactType;
}

public void serialize(File file, Object object) throws IOException {
FileUtil.createDirectory(file.getParentFile());
serializer.serialize(file, object);
}

public static ResourceFileType fromExtension(String ext) {
try {
return ResourceFileType.valueOf(ext);
Expand All @@ -88,5 +84,10 @@ public static ResourceFileType fromFile(File file) {
throw new IllegalArgumentException(String.format("Unsupported extension '%s' for file %s. Must be one of %s", ext, file, Arrays.asList(values())));
}
}

@FunctionalInterface
public interface Serializer {
void serialize(File file, Object object) throws IOException;
}
}

Expand Up @@ -55,11 +55,9 @@ private IoUtil() { }
*/
public static void download(KitLogger log, URL downloadUrl, File target) throws IOException {
log.progressStart();
try (HttpClient client = HttpClientUtils.createHttpClient(Config.empty())
.newBuilder().readTimeout(30, TimeUnit.MINUTES).build()
) {
try (HttpClient client = HttpClientUtils.createHttpClient(Config.empty()).newBuilder().build()) {
final HttpResponse<InputStream> response = client.sendAsync(
client.newHttpRequestBuilder().url(downloadUrl).build(), InputStream.class)
client.newHttpRequestBuilder().timeout(30, TimeUnit.MINUTES).url(downloadUrl).build(), InputStream.class)
.get();
final int length = Integer.parseInt(response.headers(StandardHttpHeaders.CONTENT_LENGTH)
.stream().findAny().orElse("-1"));
Expand Down
Expand Up @@ -13,7 +13,6 @@
*/
package org.eclipse.jkube.kit.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -23,7 +22,6 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.Parameter;
import io.fabric8.openshift.api.model.Template;
import io.fabric8.openshift.client.OpenShiftClient;
Expand Down Expand Up @@ -54,7 +52,7 @@ public static boolean isOpenShift(KubernetesClient client) {
}


public static KubernetesList processTemplatesLocally(Template entity, boolean failOnMissingParameterValue) throws IOException {
public static KubernetesList processTemplatesLocally(Template entity, boolean failOnMissingParameterValue) {
List<HasMetadata> objects = null;
if (entity != null) {
objects = entity.getObjects();
Expand All @@ -65,7 +63,7 @@ public static KubernetesList processTemplatesLocally(Template entity, boolean fa
List<Parameter> parameters = entity != null ? entity.getParameters() : null;
if (parameters != null && !parameters.isEmpty()) {
String json = "{\"kind\": \"List\", \"apiVersion\": \"" + DEFAULT_API_VERSION + "\",\n" +
" \"items\": " + ResourceUtil.toJson(objects) + " }";
" \"items\": " + Serialization.asJson(objects) + " }";

// let's make a few passes in case there's expressions in values
for (int i = 0; i < 5; i++) {
Expand All @@ -85,7 +83,7 @@ public static KubernetesList processTemplatesLocally(Template entity, boolean fa
json = json.replace(from, value);
}
}
return Serialization.jsonMapper().readerFor(KubernetesList.class).readValue(json);
return Serialization.unmarshal(json, KubernetesList.class);
} else {
KubernetesList answer = new KubernetesList();
answer.setItems(objects);
Expand Down
Expand Up @@ -15,8 +15,6 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -25,16 +23,9 @@

import org.eclipse.jkube.kit.common.ResourceFileType;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.HasMetadataComparator;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.Template;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -46,17 +37,6 @@
*/
public class ResourceUtil {

static {
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setRestrictToTemplates(false);
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setLogWarnings(false);
for (ObjectMapper mapper : new ObjectMapper[]{Serialization.jsonMapper(), Serialization.yamlMapper()}) {
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
((YAMLFactory)Serialization.yamlMapper().getFactory())
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true);
}

private ResourceUtil() {}

/**
Expand All @@ -77,10 +57,7 @@ public static List<HasMetadata> deserializeKubernetesListOrTemplate(File manifes
if (!manifest.isFile() || !manifest.exists()) {
return Collections.emptyList();
}
final List<HasMetadata> kubernetesResources = new ArrayList<>();
try (InputStream fis = Files.newInputStream(manifest.toPath())) {
kubernetesResources.addAll(split(Serialization.unmarshal(fis, Collections.emptyMap())));
}
final List<HasMetadata> kubernetesResources = new ArrayList<>(split(Serialization.unmarshal(manifest)));
kubernetesResources.sort(new HasMetadataComparator());
return kubernetesResources;
}
Expand All @@ -105,37 +82,17 @@ private static List<HasMetadata> split(Object resource) throws IOException {
return Collections.emptyList();
}

public static <T extends KubernetesResource> T load(File file, Class<T> clazz) throws IOException {
return Serialization.unmarshal(Files.newInputStream(file.toPath()), clazz);
}

public static File save(File file, Object data) throws IOException {
return save(file, data, ResourceFileType.fromFile(file));
}

public static File save(File file, Object data, ResourceFileType type) throws IOException {
boolean hasExtension = FilenameUtils.indexOfExtension(file.getAbsolutePath()) != -1;
File output = hasExtension ? file : type.addExtensionIfMissing(file);
FileUtil.createDirectory(file.getParentFile());
getObjectMapper(type).writeValue(output, data);
type.serialize(output, data);
return output;
}

public static String toJson(Object resource) throws JsonProcessingException {
return serializeAsString(resource, ResourceFileType.json);
}

private static String serializeAsString(Object resource, ResourceFileType resourceFileType) throws JsonProcessingException {
return getObjectMapper(resourceFileType).writeValueAsString(resource);
}

private static ObjectMapper getObjectMapper(ResourceFileType resourceFileType) {
return resourceFileType.getObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}

public static List<File> getFinalResourceDirs(File resourceDir, String environmentAsCommaSeparateStr) {
List<File> resourceDirs = new ArrayList<>();

Expand Down
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;

public class Serialization {

private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory()
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true));
private static final KubernetesSerialization KUBERNETES_SERIALIZATION = new KubernetesSerialization(JSON_MAPPER, true);
static {
for (ObjectMapper mapper : new ObjectMapper[]{JSON_MAPPER, YAML_MAPPER}) {
mapper.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
YAML_MAPPER.registerModules(new JavaTimeModule(), KUBERNETES_SERIALIZATION.getUnmatchedFieldTypeModule());
KUBERNETES_SERIALIZATION.getUnmatchedFieldTypeModule().setRestrictToTemplates(false);
KUBERNETES_SERIALIZATION.getUnmatchedFieldTypeModule().setLogWarnings(false);
}

private Serialization() {}

public static <T> T unmarshal(String objectAsString) {
return unmarshal(objectAsString, (Class<T>) KubernetesResource.class);
}

public static <T> T unmarshal(URL url) throws IOException {
return unmarshal(url, (Class<T>) KubernetesResource.class);
}

public static <T> T unmarshal(File file) throws IOException {
return unmarshal(file, (Class<T>) KubernetesResource.class);
}

public static <T> T unmarshal(File file, Class<T> clazz) throws IOException {
try (InputStream fis = Files.newInputStream(file.toPath())) {
return unmarshal(fis, clazz);
}
}

public static <T> T unmarshal(File file, TypeReference<T> type) throws IOException {
try (InputStream fis = Files.newInputStream(file.toPath())) {
return unmarshal(fis, type);
}
}
public static <T> T unmarshal(URL url, Class<T> type) throws IOException {
try (InputStream is = url.openStream()){
return unmarshal(is, type);
}
}

public static <T> T unmarshal(URL url, TypeReference<T> type) throws IOException {
try (InputStream is = url.openStream()){
return unmarshal(is, type);
}
}

public static <T> T unmarshal(InputStream is, Class<T> clazz) {
return KUBERNETES_SERIALIZATION.unmarshal(is, clazz);
}

public static <T> T unmarshal(InputStream is, TypeReference<T> type) {
return KUBERNETES_SERIALIZATION.unmarshal(is, type);
}

public static <T> T unmarshal(String string, Class<T> type) {
return KUBERNETES_SERIALIZATION.unmarshal(string, type);
}

public static <T> T convertValue(Object object, Class<T> type) {
return JSON_MAPPER.convertValue(object, type);
}

public static String asJson(Object object) {
return KUBERNETES_SERIALIZATION.asJson(object);
}

public static void saveJson(File resultFile, Object value) throws IOException {
JSON_MAPPER.writeValue(resultFile, value);
}

public static void saveYaml(File resultFile, Object value) throws IOException {
YAML_MAPPER.writeValue(resultFile, value);
}
}
Expand Up @@ -20,12 +20,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class IoUtilTest {


@Test
void findOpenPort() throws IOException {
int port = IoUtil.getFreeRandomPort();
Expand Down
Expand Up @@ -29,7 +29,6 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.eclipse.jkube.kit.common.KitLogger;

import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
Expand Down