Kubernetes and machines
This page is for a charm with a Kubernetes variant & a machine variant that share code. For other charms, go to 16. Subclass CharmSpecific.
Shared code
In the code that is shared across Kubernetes & machines, subclass CharmSpecificCommon:
import abc
import dataclasses
import charm_refresh
@dataclasses.dataclass(eq=False)
class PostgreSQLRefresh(charm_refresh.CharmSpecificCommon, abc.ABC):
pass
Kubernetes-specific code
In the Kubernetes-specific code, subclass your class from Shared code and CharmSpecificKubernetes:
import dataclasses
import charm_refresh
import common
@dataclasses.dataclass(eq=False)
class KubernetesPostgreSQLRefresh(
common.PostgreSQLRefresh, (1)
charm_refresh.CharmSpecificKubernetes,
):
pass
1 | Your class must come before CharmSpecificKubernetes |
Machines-specific code
In the machines-specific code, subclass your class from Shared code and CharmSpecificMachines:
import dataclasses
import charm_refresh
import common
@dataclasses.dataclass(eq=False)
class MachinesPostgreSQLRefresh(
common.PostgreSQLRefresh, (1)
charm_refresh.CharmSpecificMachines
):
pass
1 | Your class must come before CharmSpecificMachines |
Which class to implement in
In future steps, you will implement code for the CharmSpecific class.
If the code is the same on Kubernetes & machines, implement it in your class that inherits directly from charm_refresh.CharmSpecificCommon.
If the code is unique to Kubernetes or machines, implement it in your class that inherits directly from charm_refresh.CharmSpecificKubernetes or charm_refresh.CharmSpecificMachines, respectively.
Leverage inheritance!
If the code is mostly the same, implement it in your common class.
Then, in your Kubernetes or machines class, override the method and use Inheritance example
|