Junior — Middle
How have you used inheritance or similar concepts when working with resources and objects in Kubernetes?
sobes.tech AI
Answer from AI
Kubernetes does not have classical inheritance like in object-oriented programming, but you can use concepts similar to inheritance through resource templates and composition.
For example, you can create base manifests (such as a Deployment with common settings) and use tools like Helm or Kustomize to override and extend these base configurations. This allows reusing and modifying configurations without duplication.
Also, in the Java client for Kubernetes, you can create base classes or interfaces that encapsulate common logic for working with resources, and then inherit from them for specific resource types, which simplifies maintenance and extension of the code.
Example: a base class for working with resources
public abstract class BaseK8sResourceHandler<T> {
protected KubernetesClient client;
public BaseK8sResourceHandler(KubernetesClient client) {
this.client = client;
}
public abstract void createResource(T resource);
public abstract T getResource(String name);
}
public class DeploymentHandler extends BaseK8sResourceHandler<Deployment> {
public DeploymentHandler(KubernetesClient client) {
super(client);
}
@Override
public void createResource(Deployment deployment) {
client.apps().deployments().create(deployment);
}
@Override
public Deployment getResource(String name) {
return client.apps().deployments().withName(name).get();
}
}