Skip to content

Commit

Permalink
fix fabric8io#3845 fabric8io#3407: refining withItem support
Browse files Browse the repository at this point in the history
Kubernetes.resource now returns NamespaceableResource

also adds sanity checks useful for fabric8io#3859 as well
  • Loading branch information
shawkins committed Feb 16, 2022
1 parent f6927d7 commit ed141c1
Show file tree
Hide file tree
Showing 21 changed files with 459 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ <T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<
*/
default <T extends HasMetadata, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> resources(
Class<T> resourceType, Class<L> listClass) {
return resources(resourceType, listClass, Resource.class);
return resources(resourceType, listClass, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
import io.fabric8.kubernetes.client.dsl.MetricAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.Namespaceable;
import io.fabric8.kubernetes.client.dsl.NamespaceableResource;
import io.fabric8.kubernetes.client.dsl.NetworkAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
Expand Down Expand Up @@ -176,6 +176,7 @@ default <T extends HasMetadata> MixedOperation<T, KubernetesResourceList<T>, Res
* @param <L> L type represents resource list type
* @return returns a MixedOperation object with which you can do basic resource operations. If the class is a known type the dsl operation logic will be used.
*/
@Override
<T extends HasMetadata, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> resources(Class<T> resourceType, Class<L> listClass);

/**
Expand Down Expand Up @@ -417,7 +418,7 @@ default MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList,
* @param <T> type of Kubernetes resource
* @return operations object for Kubernetes resource
*/
<T extends HasMetadata> NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<T> resource(T is);
<T extends HasMetadata> NamespaceableResource<T> resource(T is);

/**
* KubernetesResource operations. You can pass any Kubernetes resource as string object and do
Expand All @@ -426,7 +427,7 @@ default MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList,
* @param s Kubernetes resource object as string
* @return operations object for Kubernetes resource
*/
NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(String s);
NamespaceableResource<HasMetadata> resource(String s);

/**
* Operations for Binding resource in APIgroup core/v1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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.dsl;

public interface NamespaceableResource<T> extends Resource<T> {

Resource<T> inNamespace(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.Utils;

public class ResourceDefinitionContext {
Expand All @@ -41,11 +42,11 @@ public String getVersion() {
public String getKind() {
return kind;
}

public boolean isNamespaceScoped() {
return namespaced;
}

protected void validate() {
if (plural == null) {
if (kind == null) {
Expand All @@ -56,13 +57,18 @@ protected void validate() {
}

public static ResourceDefinitionContext fromResourceType(Class<? extends KubernetesResource> resource) {
return new Builder()
.withGroup(HasMetadata.getGroup(resource))
.withVersion(HasMetadata.getVersion(resource))
.withNamespaced(Utils.isResourceNamespaced(resource))
.withPlural(HasMetadata.getPlural(resource))
.withKind(HasMetadata.getKind(resource))
.build();
try {
return new Builder()
.withGroup(HasMetadata.getGroup(resource))
.withVersion(HasMetadata.getVersion(resource))
.withNamespaced(Utils.isResourceNamespaced(resource))
.withPlural(HasMetadata.getPlural(resource))
.withKind(HasMetadata.getKind(resource))
.build();
} catch (IllegalArgumentException e) {
throw new KubernetesClientException(
String.format("%s is not annotated appropriately: %s", resource.getName(), e.getMessage()), e);
}
}

public static class Builder {
Expand Down Expand Up @@ -96,7 +102,7 @@ public Builder withKind(String kind) {
this.resourceDefinitionContext.kind = kind;
return this;
}

public ResourceDefinitionContext build() {
this.resourceDefinitionContext.validate();
return this.resourceDefinitionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasM
}

@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(HasMetadata is) {
public NamespaceableResource<HasMetadata> resource(HasMetadata is) {
return delegate.resource(is);
}

@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(String s) {
public NamespaceableResource<HasMetadata> resource(String s) {
return delegate.resource(s);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected SimpleClientContext newState(Config updated) {
@Override
public <T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>> MixedOperation<T, L, R> resources(
Class<T> resourceType, Class<L> listClass, Class<R> resourceClass) {
// TODO: make this more robust
// TODO: make this more robust - list and resource class could be null
return Handlers.getOperation(resourceType, listClass, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
import io.fabric8.kubernetes.client.dsl.MetricAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.NamespaceableResource;
import io.fabric8.kubernetes.client.dsl.NetworkAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
Expand All @@ -95,20 +95,19 @@
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.KubernetesListOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl;
import io.fabric8.kubernetes.client.dsl.internal.apps.v1.DeploymentOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.apps.v1.ReplicaSetOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.apps.v1.StatefulSetOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.batch.v1.JobOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.batch.v1beta1.CronJobOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.KubernetesListOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl;
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl;
import io.fabric8.kubernetes.client.dsl.internal.certificates.v1.CertificateSigningRequestOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.BindingOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.ComponentStatusOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.ReplicationControllerOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.ServiceOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.certificates.v1.CertificateSigningRequestOperationsImpl;
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
import io.fabric8.kubernetes.client.extended.run.RunOperations;
import io.fabric8.kubernetes.client.extended.run.RunOperationsImpl;
Expand Down Expand Up @@ -251,20 +250,20 @@ public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s) {
return resourceListFor(s);
}

/**
* {@inheritDoc}
*/

@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(HasMetadata item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(this, item);
public <T extends HasMetadata> NamespaceableResource<T> resource(T item) {
// lookup the operation given the item
ResourceHandler<T, ?> resourceHandler = Handlers.get(item, this);
HasMetadataOperation<T, ?, ?> op = resourceHandler.operation(this, null);
return new NamespaceableResourceAdapter<T>(item, op);
}

/**
* {@inheritDoc}
*/
@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(String s) {
public NamespaceableResource<HasMetadata> resource(String s) {
return resource((HasMetadata)Serialization.unmarshal(s));
}

Expand Down Expand Up @@ -469,6 +468,9 @@ public MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList,
public <T extends HasMetadata, L extends KubernetesResourceList<T>> HasMetadataOperation<T, L, Resource<T>> resources(
Class<T> resourceType, Class<L> listClass) {
try {
if (GenericKubernetesResource.class.equals(resourceType)) {
throw new KubernetesClientException("resources cannot be called with a generic type");
}
return Handlers.getOperation(resourceType, listClass, this);
} catch (Exception e) {
//may be the wrong list type, try more general
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static <T extends HasMetadata> void unregister(Class<T> type) {
/**
* Returns a {@link ResourceHandler} for the given item. The client is optional, if it is supplied, then the server can be
* consulted for resource metadata if a generic item is passed in.
*
* @return the handler
* @throws KubernetesClientException if a handler cannot be found
*/
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(T meta, BaseClient client) {
Class<T> type = (Class<T>)meta.getClass();
Expand All @@ -70,7 +73,11 @@ public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> Resource
}
}

return get(type);
ResourceHandler<T, V> result = get(type);
if (result == null) {
throw new KubernetesClientException("Could not find a registered handler for item: [" + meta + "].");
}
return result;
}

public static ResourceDefinitionContext getResourceDefinitionContext(String apiVersion, String kind, BaseClient client) {
Expand Down
Loading

0 comments on commit ed141c1

Please sign in to comment.