|
| 1 | +// Package kubernetes is a plugin for instantiating multiple container instances in a Kubernetes cluster. |
| 2 | +// |
| 3 | +// # Wiring Spec Usage |
| 4 | +// |
| 5 | +// To use the kubernetes plugin in your wiring spec, you can declare a Kuberenetes application, giving it a name and specifying which containers to include. Each container will be deployed in a separate pod in the Kubernetes cluster. |
| 6 | +// |
| 7 | +// kubernetes.NewApplication(spec, "my_app", "my_container_1", "my_container_2") |
| 8 | +// |
| 9 | +// You can add more containers to an existing application: |
| 10 | +// |
| 11 | +// kubernetes.AddContainerToDeployment(spec, "my_app", "my_container_3") |
| 12 | +// |
| 13 | +// You can also deploy multiple containers in a single pod: |
| 14 | +// |
| 15 | +// kubernetes.AddPodToApplication(spec, "my_app", "my_container_4", "my_container_5") |
| 16 | +// |
| 17 | +// # Artifacts Generated |
| 18 | +// |
| 19 | +// During compilation, the plugin generates deployment.yaml and service.yaml files for each Pod. |
| 20 | +// |
| 21 | +// # Running Artifacts |
| 22 | +// |
| 23 | +// You need to have a working kubernetes cluster and `kubectl` installed. |
| 24 | +// To deploy the pods to the cluster, use the following commands: |
| 25 | +// |
| 26 | +// kubectl apply -f podName-deployment.yaml |
| 27 | +// kubectl apply -f podName-service.yaml |
| 28 | +// |
| 29 | +package kubernetes |
| 30 | + |
| 31 | +import ( |
| 32 | + "github.com/blueprint-uservices/blueprint/blueprint/pkg/coreplugins/namespaceutil" |
| 33 | + "github.com/blueprint-uservices/blueprint/blueprint/pkg/ir" |
| 34 | + "github.com/blueprint-uservices/blueprint/blueprint/pkg/wiring" |
| 35 | + "github.com/blueprint-uservices/blueprint/plugins/kubernetes/kubepod" |
| 36 | +) |
| 37 | + |
| 38 | +// [AddContainerToApplication] can be used by wiring specs to add more containers to a Kubernetes application |
| 39 | +func AddContainerToApplication(spec wiring.WiringSpec, appName string, containerName string) { |
| 40 | + AddPodToApplication(spec, appName, containerName) |
| 41 | +} |
| 42 | + |
| 43 | +// [AddPodToApplication] can be used by wiring specs to bundle multiple containers in a single Kubernetes Pod and add that pod to an application |
| 44 | +func AddPodToApplication(spec wiring.WiringSpec, appName string, containers ...string) { |
| 45 | + podName := kubepod.NewKubePod(spec, containers[0], containers...) |
| 46 | + namespaceutil.AddNodeTo[Application](spec, appName, podName) |
| 47 | +} |
| 48 | + |
| 49 | +// [NewApplication] can be used by wiring specs to create a Kubernetes Application that instantiates a number of kubernetes pod deployments as services. For each provided container, a new pod deployment is created with that container added to the pod. |
| 50 | +// |
| 51 | +// Further pod deployments for containers can be generated by calling [AddContainerToApplication]. |
| 52 | +// |
| 53 | +// If one wishes to bundle multiple containers into a single pod, then that can be done by calling [AddPodToApplication]. Note that the containers provided to that must not have already been added to the application before. |
| 54 | +// |
| 55 | +// During compilation, generates the various configuration files for generating pod deployments and services. |
| 56 | +// |
| 57 | +// Returns appName |
| 58 | +func NewApplication(spec wiring.WiringSpec, appName string, containers ...string) string { |
| 59 | + |
| 60 | + // If any children were provided in this call, add them to the app via a property |
| 61 | + for _, containerName := range containers { |
| 62 | + AddContainerToApplication(spec, appName, containerName) |
| 63 | + } |
| 64 | + |
| 65 | + spec.Define(appName, &Application{}, func(ns wiring.Namespace) (ir.IRNode, error) { |
| 66 | + application := &Application{AppName: appName} |
| 67 | + _, err := namespaceutil.InstantiateNamespace(ns, &applicationNamespace{application}) |
| 68 | + return application, err |
| 69 | + }) |
| 70 | + |
| 71 | + return appName |
| 72 | +} |
| 73 | + |
| 74 | +// A [wiring.NamespaceHandler] used to build kubernetes deployments |
| 75 | +type applicationNamespace struct { |
| 76 | + *Application |
| 77 | +} |
| 78 | + |
| 79 | +// Implements [wiring.NamespaceHandler] |
| 80 | +func (application *Application) Accepts(nodeType any) bool { |
| 81 | + _, isPodDeploymentNode := nodeType.(kubepod.PodDeployment) |
| 82 | + return isPodDeploymentNode |
| 83 | +} |
| 84 | + |
| 85 | +// Implements [wiring.NamespaceHandler] |
| 86 | +func (application *Application) AddEdge(name string, edge ir.IRNode) error { |
| 87 | + application.Edges = append(application.Edges, edge) |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// Implements [wiring.NamespaceHandler] |
| 92 | +func (application *Application) AddNode(name string, node ir.IRNode) error { |
| 93 | + application.Nodes = append(application.Nodes, node) |
| 94 | + return nil |
| 95 | +} |
0 commit comments