Skip to content

Commit

Permalink
moving apiVersion modification to BaseOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 14, 2022
1 parent cc6529f commit a3ca69b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* 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.
*/
* 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.servicecatalog.examples;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class V1CertificatesAPIGroupClient extends ClientAdapter<V1CertificatesAP
@Override
public NonNamespaceOperation<CertificateSigningRequest, CertificateSigningRequestList, CertificateSigningRequestResource<CertificateSigningRequest>> certificateSigningRequests() {
// we need the cast to satisfy java 8
return (NonNamespaceOperation<CertificateSigningRequest, CertificateSigningRequestList, CertificateSigningRequestResource<CertificateSigningRequest>>)resources(CertificateSigningRequest.class, CertificateSigningRequestList.class,
return (NonNamespaceOperation<CertificateSigningRequest, CertificateSigningRequestList, CertificateSigningRequestResource<CertificateSigningRequest>>) resources(
CertificateSigningRequest.class, CertificateSigningRequestList.class,
CertificateSigningRequestResource.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.fabric8.kubernetes.client.informers.impl.DefaultSharedIndexInformer;
import io.fabric8.kubernetes.client.readiness.Readiness;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.URLUtils;
Expand Down Expand Up @@ -74,12 +75,12 @@
import java.util.function.UnaryOperator;

public class BaseOperation<T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>>
extends CreateOnlyResourceOperation<T, T>
implements
OperationInfo,
MixedOperation<T, L, R>,
ExtensibleResource<T>,
ListerWatcher<T, L> {
extends CreateOnlyResourceOperation<T, T>
implements
OperationInfo,
MixedOperation<T, L, R>,
ExtensibleResource<T>,
ListerWatcher<T, L> {

private static final String WATCH = "watch";
private static final String READ_ONLY_UPDATE_EXCEPTION_MESSAGE = "Cannot update read-only resources";
Expand Down Expand Up @@ -108,12 +109,13 @@ protected BaseOperation(OperationContext ctx) {
this.resourceVersion = ctx.getResourceVersion();
this.gracePeriodSeconds = ctx.getGracePeriodSeconds();
this.propagationPolicy = ctx.getPropagationPolicy();
this.apiVersion = ApiVersionUtil.joinApiGroupAndVersion(getAPIGroupName(), getAPIGroupVersion());
}

public BaseOperation<T, L, R> newInstance(OperationContext context) {
return new BaseOperation<>(context);
}

protected R newResource(OperationContext context) {
return (R) newInstance(context);
}
Expand Down Expand Up @@ -217,7 +219,6 @@ public void visit(V item) {
});
}


@Override
public T accept(Consumer<T> consumer) {
throw new KubernetesClientException(READ_ONLY_EDIT_EXCEPTION_MESSAGE);
Expand Down Expand Up @@ -309,11 +310,10 @@ public final T createOrReplace(T... items) {
// use the Resource in case create or replace is overriden
Resource<T> resource = newResource(context);
CreateOrReplaceHelper<T> createOrReplaceHelper = new CreateOrReplaceHelper<>(
resource::create,
resource::replace,
m -> waitUntilCondition(Objects::nonNull, 1, TimeUnit.SECONDS),
m -> fromServer().get()
);
resource::create,
resource::replace,
m -> waitUntilCondition(Objects::nonNull, 1, TimeUnit.SECONDS),
m -> fromServer().get());

return createOrReplaceHelper.createOrReplace(finalItemToCreateOrReplace);
}
Expand Down Expand Up @@ -413,7 +413,7 @@ public L list(Integer limitVal, String continueVal) {
public L list(ListOptions listOptions) {
try {
return listRequestHelper(
fetchListUrl(getNamespacedUrl(), defaultListOptions(listOptions, null)));
fetchListUrl(getNamespacedUrl(), defaultListOptions(listOptions, null)));
} catch (MalformedURLException e) {
throw KubernetesClientException.launderThrowable(forOperationType("list"), e);
}
Expand Down Expand Up @@ -519,7 +519,7 @@ public R withItem(T item) {
String itemNs = KubernetesResourceUtil.getNamespace(item);
OperationContext ctx = context.withName(KubernetesResourceUtil.getName(item)).withItem(item);
if (Utils.isNotNullOrEmpty(itemNs)) {
ctx = ctx.withNamespace(itemNs);
ctx = ctx.withNamespace(itemNs);
}
return newResource(ctx);
}
Expand All @@ -530,7 +530,8 @@ void deleteThis() {
updateApiVersion(item);
handleDelete(item, gracePeriodSeconds, propagationPolicy, resourceVersion, cascading);
} else {
handleDelete(getResourceURLForWriteOperation(getResourceUrl()), gracePeriodSeconds, propagationPolicy, resourceVersion, cascading);
handleDelete(getResourceURLForWriteOperation(getResourceUrl()), gracePeriodSeconds, propagationPolicy, resourceVersion,
cascading);
}
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(forOperationType("delete"), e);
Expand All @@ -554,8 +555,8 @@ public Watch watch(final Watcher<T> watcher) {
@Override
public Watch watch(String resourceVersion, Watcher<T> watcher) {
return watch(new ListOptionsBuilder()
.withResourceVersion(resourceVersion)
.build(), watcher);
.withResourceVersion(resourceVersion)
.build(), watcher);
}

@Override
Expand All @@ -565,14 +566,13 @@ public Watch watch(ListOptions options, final Watcher<T> watcher) {
WatchConnectionManager<T, L> watch = null;
try {
watch = new WatchConnectionManager<>(
httpClient,
this,
options,
watcherToggle,
config.getWatchReconnectInterval(),
config.getWatchReconnectLimit(),
config.getWebsocketTimeout()
);
httpClient,
this,
options,
watcherToggle,
config.getWatchReconnectInterval(),
config.getWatchReconnectLimit(),
config.getWebsocketTimeout());
watch.waitUntilReady();
return watch;
} catch (MalformedURLException e) {
Expand All @@ -599,13 +599,12 @@ public Watch watch(ListOptions options, final Watcher<T> watcher) {
// websockets. Issue: https://github.com/kubernetes/kubernetes/issues/25126
try {
return new WatchHTTPManager<>(
httpClient,
this,
options,
watcher,
config.getWatchReconnectInterval(),
config.getWatchReconnectLimit()
);
httpClient,
this,
options,
watcher,
config.getWatchReconnectInterval(),
config.getWatchReconnectLimit());
} catch (MalformedURLException e) {
throw KubernetesClientException.launderThrowable(forOperationType(WATCH), e);
}
Expand Down Expand Up @@ -973,7 +972,8 @@ private DefaultSharedIndexInformer<T, L> createInformer(long resync) {
}

// use the local context / namespace but without a resourceVersion
DefaultSharedIndexInformer<T, L> informer = new DefaultSharedIndexInformer<>(getType(), this.withResourceVersion(null).withLimit(this.limit), resync, Runnable::run); // just run the event notification in the websocket thread
DefaultSharedIndexInformer<T, L> informer = new DefaultSharedIndexInformer<>(getType(),
this.withResourceVersion(null).withLimit(this.limit), resync, Runnable::run); // just run the event notification in the websocket thread
if (indexers != null) {
informer.addIndexers(indexers);
}
Expand Down Expand Up @@ -1019,4 +1019,3 @@ public static URL appendListOptionParams(URL base, ListOptions listOptions) {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public T replaceStatus(T item) {

/**
* Modify the item prior to a replace or a JSON patch diff
*
*
* @param current item from the server
* @param item to be modified
* @return the modified item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.internal.KubernetesDeserializer;

import static io.fabric8.kubernetes.client.utils.KubernetesResourceUtil.inferListType;
Expand All @@ -48,14 +47,8 @@ public HasMetadataOperationsImpl(OperationContext context, ResourceDefinitionCon

this.rdc = rdc;

// use the group / version from the context, not from the item
// the item is allowed to differ as long as it can be parsed as the current type
this.apiGroupName = rdc.getGroup();
this.apiGroupVersion = rdc.getVersion();
this.apiVersion = ApiVersionUtil.joinApiGroupAndVersion(getAPIGroupName(), getAPIGroupVersion());

if (!GenericKubernetesResource.class.isAssignableFrom(type)) {
// TODO: the static nature of these registrations is problematic,
// TODO: the static nature of these registrations is problematic,
// we should ensure that we aren't redefining an existing type
KubernetesDeserializer.registerCustomKind(apiVersion, kind(rdc), type);
if (KubernetesResource.class.isAssignableFrom(this.listType)) {
Expand Down

0 comments on commit a3ca69b

Please sign in to comment.