-
Notifications
You must be signed in to change notification settings - Fork 220
feat: upgrade to k8s client 5.0 to simplify CR handling #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
metacosm
wants to merge
23
commits into
operator-framework:feat-update-kubernetes-client-5.0
from
halkyonio:improved-k8s-client
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
85b94d5
feat: upgrade to k8s client 5.0 to simplify CR handling
metacosm ec6dc7e
fix: use proper format for default finalizer identifier
metacosm fa2c290
fix: use CustomResourceList as list type
metacosm f292b49
fix: use CustomResourceList as list type
metacosm de8120a
fix: use -e flag to get more exception details
metacosm 9fbad96
fix: CRs return their api version string for client to work correctly
metacosm e1464a6
fix: finalizer name to follow proper format
metacosm 94a1251
fix: use proper kind
metacosm d6cd795
fix: use proper kind
metacosm 046fd30
fix: CRs need to be marked as Namespaced to retrieve a NS-scoped client
metacosm 1f113c2
fix: restore missing getApiVersion
metacosm 86261be
fix: improper plural in CRD
metacosm 0ac9ea7
fix: use the controller's class canonical name as default finalizer name
metacosm 76955ed
fix: use proper domain name for finalizer prefix
metacosm 4c58470
fix: unit test
metacosm b95956e
fix: actually create a valid finalizer name and check it is
metacosm 858ac6e
fix: set finalizer name instead of using default to match expectations
metacosm e18a90b
chore: remove commons-lang3 dependency
metacosm 126bd86
chore: replace deprecated cascading by withPropagationPolicy call
metacosm 18735e8
feat: update kube client to 5.0.0-alpha-2
metacosm 8ec63d3
fix: use new finalizer generation logic
metacosm 39dd757
refactor: isolate finalizer validation until HasMetadata provides it
metacosm fa9f9b8
fix: add .io suffix to sample.javaoperatorsdk group
metacosm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 21 additions & 62 deletions
83
operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,48 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.javaoperatorsdk.operator.api.Controller; | ||
import io.javaoperatorsdk.operator.api.ResourceController; | ||
import io.fabric8.kubernetes.api.builder.Function; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.client.CustomResourceDoneable; | ||
import javassist.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class ControllerUtils { | ||
|
||
private final static double JAVA_VERSION = Double.parseDouble(System.getProperty("java.specification.version")); | ||
private static final String FINALIZER_NAME_SUFFIX = "/finalizer"; | ||
|
||
// this is just to support testing, this way we don't try to create class multiple times in memory with same name. | ||
// note that other solution is to add a random string to doneable class name | ||
private static Map<Class<? extends CustomResource>, Class<? extends CustomResourceDoneable<? extends CustomResource>>> | ||
doneableClassCache = new HashMap<>(); | ||
|
||
|
||
static String getFinalizer(ResourceController controller) { | ||
final String annotationFinalizerName = getAnnotation(controller).finalizerName(); | ||
if (!Controller.NULL.equals(annotationFinalizerName)) { | ||
return annotationFinalizerName; | ||
} | ||
final String crdName = getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX; | ||
return crdName; | ||
return getDefaultFinalizerIdentifier(controller); | ||
} | ||
|
||
|
||
static String getDefaultFinalizerIdentifier(ResourceController controller) { | ||
return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX; | ||
} | ||
|
||
/** | ||
* @param finalizer | ||
* @return | ||
* @deprecated this should be removed once k8s client provides that method on HasMetadata | ||
*/ | ||
static boolean isFinalizerValid(String finalizer) { | ||
return HasMetadata.FINALIZER_NAME_MATCHER.reset(finalizer).matches(); | ||
} | ||
|
||
static boolean getGenerationEventProcessing(ResourceController controller) { | ||
return getAnnotation(controller).generationAwareEventProcessing(); | ||
} | ||
|
||
static <R extends CustomResource> Class<R> getCustomResourceClass(ResourceController<R> controller) { | ||
return (Class<R>) getAnnotation(controller).customResourceClass(); | ||
} | ||
|
||
static String getCrdName(ResourceController controller) { | ||
return getAnnotation(controller).crdName(); | ||
} | ||
|
||
|
||
public static <T extends CustomResource> Class<? extends CustomResourceDoneable<T>> | ||
getCustomResourceDoneableClass(ResourceController<T> controller) { | ||
try { | ||
Class<? extends CustomResource> customResourceClass = getAnnotation(controller).customResourceClass(); | ||
String className = customResourceClass.getPackage().getName() + "." + customResourceClass.getSimpleName() + "CustomResourceDoneable"; | ||
|
||
if (doneableClassCache.containsKey(customResourceClass)) { | ||
return (Class<? extends CustomResourceDoneable<T>>) doneableClassCache.get(customResourceClass); | ||
} | ||
|
||
ClassPool pool = ClassPool.getDefault(); | ||
pool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader())); | ||
|
||
CtClass superClass = pool.get(CustomResourceDoneable.class.getName()); | ||
CtClass function = pool.get(Function.class.getName()); | ||
CtClass customResource = pool.get(customResourceClass.getName()); | ||
CtClass[] argTypes = {customResource, function}; | ||
CtClass customDoneable = pool.makeClass(className, superClass); | ||
CtConstructor ctConstructor = CtNewConstructor.make(argTypes, null, "super($1, $2);", customDoneable); | ||
customDoneable.addConstructor(ctConstructor); | ||
|
||
Class<? extends CustomResourceDoneable<T>> doneableClass; | ||
if (JAVA_VERSION >= 9) { | ||
doneableClass = (Class<? extends CustomResourceDoneable<T>>) customDoneable.toClass(customResourceClass); | ||
} else { | ||
doneableClass = (Class<? extends CustomResourceDoneable<T>>) customDoneable.toClass(); | ||
} | ||
doneableClassCache.put(customResourceClass, doneableClass); | ||
return doneableClass; | ||
} catch (CannotCompileException | NotFoundException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
|
||
private static Controller getAnnotation(ResourceController controller) { | ||
return controller.getClass().getAnnotation(Controller.class); | ||
} | ||
|
||
public static boolean hasGivenFinalizer(CustomResource resource, String finalizer) { | ||
return resource.getMetadata().getFinalizers() != null && resource.getMetadata().getFinalizers().contains(finalizer); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
operator-framework/src/main/java/io/javaoperatorsdk/operator/api/Controller.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.