2

I'm developing an application that manages many different components, including various objects in a Kubernetes cluster.

There's a nice Java library to manage K8S objects, I use it to examine objects and manipulate them, but for most of the objects, I need some kind of a "wrapper-object" to store certain internal data of my application. For example, there's the V1Pod class, it's being used for K8S pods. An object of this class contains the list of related container objects (class V1Container) and so on and so forth. I need to keep some records about this pod, these records can't be kept in the K8S object, so I have to create my own object (we can call it MyV1Pod) with extra fields and a field containing a reference to the V1Pod object. Moreover, I also have to create my own objects of the MyV1Container class containing extra fields and references to the corresponding object of the V1Container class. And I have to keep the list of these MyV1Container-objects in MyV1Pod-object.

I find it a bit cumbersome to create a separate "wrapper" for each of all these classes. Is there any way to automate that? For example, when an application starts, it would scan all the classes in that library and create a "decorating" class for each of the classes that I need? For example, it would find the V1Pod class, create something like MyV1Pod and add a field containing a reference to the corresponding the V1Pod object and that additional fields that I need.

Something similar to the Decorator pattern, but with certain automation.

Or maybe I should go another way?

Any piece of advice would be very welcome!

4
  • Do your wrapper classes expose the same public methods as the Java library? Commented Oct 24, 2019 at 15:51
  • No, absolutely. They have their own methods and fields, their names are different. Commented Oct 24, 2019 at 16:58
  • What do these "wrapper" classes do? Sure they have additional fields and methods, but what behavior do they encapsulate --- what actions do they perform? Commented Oct 24, 2019 at 17:11
  • For example, it should keep the timestamp of the moment when this object's status had been updated. It also should keep a reference to the system (under the "system" word I mean the application, not K8S) account it belongs to. Commented Oct 26, 2019 at 16:03

1 Answer 1

2

If the extra fields are the same between containers and pods, you could create a single wrapper class using generics. Something like

class MyK8sWrapper<T> {
  private T k8sObject;
  private int extraField1;
  private int extraField2;

  public T getK8sObject() {
     return k8sObject;
  }
}

Otherwise, you could probably script something to generate your container classes once then just use them.

1
  • Yes, this idea sounds good for my case, I thought about it too. Maybe there'll be other opinions, but if there aren't - I'll mark this answer as the best one. Commented Oct 26, 2019 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.