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
  • Loading branch information
shawkins committed Sep 14, 2021
1 parent 2dbfe29 commit ff616ee
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 136 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#### Improvements
#### Dependency Upgrade
#### New Features
* Fix #3294: Support fetching APIGroupList
* Fix #3303: Support fetching APIResourceList

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

### 5.7.3 (2021-09-09)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.api.model.APIGroup;
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 +35,8 @@


public abstract class BaseClient implements Client, HttpClientAware {

public static final String APIS = "/apis";

protected OkHttpClient httpClient;
private URL masterUrl;
Expand All @@ -53,7 +58,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 +139,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 +161,19 @@ public boolean supportsApiPath(String apiPath) {
}
return false;
}

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

@Override
public APIGroup getApiGroup(String name) {
return newBaseOperation(httpClient, configuration).restCall(APIGroup.class, APIS, name);
}

@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,9 @@

package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.api.model.APIGroup;
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 +51,26 @@ 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 a single api group
* @param name of the group
* @return the {@link APIGroup} metadata
*/
APIGroup getApiGroup(String name);

/**
* 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,68 @@
/**
* 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.APIGroup;
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.assertNotNull;
import static org.junit.Assert.assertNull;
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 testApiGroup() throws InterruptedException {
APIGroup group = client.getApiGroup("apps");

assertNotNull(group);

group = client.getApiGroup("apps-that-wont-exist");

assertNull(group);
}

@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 ff616ee

Please sign in to comment.