Skip to content

Commit

Permalink
fix fabric8io#3294 fabric8io#3303 exposing apigroups and apiresource
Browse files Browse the repository at this point in the history
also trying to consolidate existing logic about openshift adapting
  • Loading branch information
shawkins committed Aug 31, 2021
1 parent 7d20990 commit 9cf5938
Show file tree
Hide file tree
Showing 18 changed files with 782 additions and 135 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#### Dependency Upgrade

#### New Features
* Fix #3294: Support fetching APIGroupList
* Fix #3303: Support fetching APIResourceList

#### _**Note**_: Breaking changes in the API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIResourceList;
import io.fabric8.kubernetes.api.model.RootPaths;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
Expand All @@ -32,6 +34,8 @@


public abstract class BaseClient implements Client, HttpClientAware {

public static final String APIS = "/apis";

protected OkHttpClient httpClient;
private URL masterUrl;
Expand All @@ -53,7 +57,7 @@ public BaseClient(final Config config) {

public BaseClient(final OkHttpClient httpClient, Config config) {
try {
this.httpClient = httpClient;
this.httpClient = config.adaptClient(httpClient);
this.namespace = config.getNamespace();
this.configuration = config;
this.apiVersion = config.getApiVersion();
Expand Down Expand Up @@ -134,8 +138,11 @@ public <C> C adapt(Class<C> type) {

@Override
public RootPaths rootPaths() {
return new BaseOperation(new OperationContext().withOkhttpClient(httpClient).withConfig(configuration)) {
}.getRootPaths();
return newBaseOperation(httpClient, configuration).getRootPaths();
}

static BaseOperation<?, ?, ?> newBaseOperation(OkHttpClient httpClient, Config configuration) {
return new BaseOperation(new OperationContext().withOkhttpClient(httpClient).withConfig(configuration)) {};
}

@Override
Expand All @@ -153,4 +160,14 @@ public boolean supportsApiPath(String apiPath) {
}
return false;
}

@Override
public APIGroupList getApiGroups() {
return newBaseOperation(httpClient, configuration).restCall(APIGroupList.class, APIS);
}

@Override
public APIResourceList getApiResources(String groupVersion) {
return newBaseOperation(httpClient, configuration).restCall(APIResourceList.class, APIS, groupVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIResourceList;
import io.fabric8.kubernetes.api.model.RootPaths;

import java.io.Closeable;
Expand Down Expand Up @@ -48,5 +50,19 @@ public interface Client extends ConfigAware, Closeable {
*/
boolean supportsApiPath(String path);

@Override
void close();

/**
* Returns the api groups
* @return the {@link APIGroupList} metadata
*/
APIGroupList getApiGroups();

/**
* Return the api resource metadata for the given groupVersion
* @param the groupVersion - group/version
* @return the {@link APIResourceList} for the groupVersion
*/
APIResourceList getApiResources(String groupVersion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
import io.sundr.builder.annotations.Buildable;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down Expand Up @@ -1341,4 +1342,8 @@ public Readiness getReadiness() {
return Readiness.getInstance();
}

public OkHttpClient adaptClient(OkHttpClient httpClient) {
return httpClient;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,18 @@ public T getMandatory() {
}

public RootPaths getRootPaths() {
return restCall(RootPaths.class);
}

public <Res> Res restCall(Class<Res> result, String... path) {
try {
URL requestUrl = new URL(config.getMasterUrl());
Request.Builder req = new Request.Builder().get().url(requestUrl);
return handleResponse(req, RootPaths.class);
String url = requestUrl.toString();
if (path != null && path.length > 0) {
url = URLUtils.join(url, URLUtils.pathJoin(path));
}
Request.Builder req = new Request.Builder().get().url(url);
return handleResponse(req, result);
} catch (KubernetesClientException e) {
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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;

import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIResourceList;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;

@RunWith(ArquillianConditionalRunner.class)
@RequiresKubernetes
public class ApiGroupResourceListsIT {

@ArquillianResource
KubernetesClient client;

@ArquillianResource
Session session;

@Test
public void testApiGroups() throws InterruptedException {
APIGroupList list = client.getApiGroups();

assertTrue(list.getGroups().stream().anyMatch(g -> "apps".equals(g.getName())));
}

@Test
public void testApiResources() throws InterruptedException {
APIResourceList list = client.getApiResources("apps/v1");

assertTrue(list.getResources().stream().anyMatch(r -> "deployments".equals(r.getName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Schema struct {
Info apimachineryversion.Info
APIGroup metav1.APIGroup
APIGroupList metav1.APIGroupList
APIResource metav1.APIResource
APIResourceList metav1.APIResourceList
BaseKubernetesList metav1.List
ObjectMeta metav1.ObjectMeta
TypeMeta metav1.TypeMeta
Expand Down
Loading

0 comments on commit 9cf5938

Please sign in to comment.