Skip to content

Commit

Permalink
Fixes fabric8io#983: log usage of unstable api
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Feb 15, 2018
1 parent 89a2c83 commit 694f38b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
New Feature
* Added support for StorageClass - https://github.com/fabric8io/kubernetes-client/pull/978
* Added support for PodSecurityPolicy - https://github.com/fabric8io/kubernetes-client/pull/992
* The client now warns when using Kubernetes alpha or beta resources - https://github.com/fabric8io/kubernetes-client/pull/1010

Improvements
* Fixed issue of SecurityContextConstraints not working - https://github.com/fabric8io/kubernetes-client/pull/982
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.fabric8.kubernetes.client.internal.VersionUsageUtils;
import io.fabric8.kubernetes.client.utils.Serialization;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -373,6 +374,7 @@ protected <T> T handleResponse(OkHttpClient client, Request.Builder requestBuild
* @throws IOException
*/
protected <T> T handleResponse(OkHttpClient client, Request.Builder requestBuilder, Class<T> type, Map<String, String> parameters) throws ExecutionException, InterruptedException, KubernetesClientException, IOException {
VersionUsageUtils.log(this.resourceT, this.apiVersion);
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
try (ResponseBody body = response.body()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
* Utility class to monitor alhpa/beta version usage and log.
*/
public final class VersionUsageUtils {

private static final Logger LOG = LoggerFactory.getLogger(VersionUsageUtils.class);

private static ConcurrentHashMap<String, Boolean> UNSTABLE_TYPES = new ConcurrentHashMap<>();

private static final boolean LOG_EACH_USAGE = false;

private VersionUsageUtils() {
}

public static void log(String type, String version) {
if (type == null || version == null) {
return;
}

if (isUnstable(version)) {
if (LOG_EACH_USAGE || UNSTABLE_TYPES.putIfAbsent(type + "-" + version, true) == null) {
alert(type, version);
}
}
}

private static boolean isUnstable(String version) {
String lowerCaseVersion = version.toLowerCase();
return lowerCaseVersion.contains("beta") || lowerCaseVersion.contains("alpha");
}

private static void alert(String type, String version) {
LOG.warn("The client is using resource type '{}' with unstable version '{}'", type, version);
}

}

0 comments on commit 694f38b

Please sign in to comment.