openstack-operator/builders/pod_template_spec.go
Mohammed Naser cc5d82883c Add builder pattern
This builder pattern refactors things so that we can use the same
code to generate manifests/etc.  This means that if we make sure
that we exclusively use those, we can do testing there and keep
something common.

Change-Id: Ibc39f9b9e3e21b18fb255ba2a67d2d8ba3b5c585
2020-03-25 01:32:48 +02:00

47 lines
1.2 KiB
Go
Executable File

package builders
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// PodTemplateSpecBuilder is an interface for building a PodTemplateSpecBuilder
type PodTemplateSpecBuilder struct {
obj *corev1.PodTemplateSpec
podSpec *PodSpecBuilder
}
// PodTemplateSpec returns a builder object for a PodTemplateSpec
func PodTemplateSpec() *PodTemplateSpecBuilder {
podTemplateSpec := &corev1.PodTemplateSpec{}
return &PodTemplateSpecBuilder{
obj: podTemplateSpec,
}
}
// Labels sets up the labels for a PodTemplateSpec
func (pts *PodTemplateSpecBuilder) Labels(labels map[string]string) *PodTemplateSpecBuilder {
pts.obj.ObjectMeta = metav1.ObjectMeta{
Labels: labels,
}
return pts
}
// PodSpec points this builder to PodSpec builder
func (pts *PodTemplateSpecBuilder) PodSpec(podSpec *PodSpecBuilder) *PodTemplateSpecBuilder {
pts.podSpec = podSpec
return pts
}
// Build generates an object ensuring that all sub-objects work
func (pts *PodTemplateSpecBuilder) Build() (corev1.PodTemplateSpec, error) {
podSpec, err := pts.podSpec.Build()
if err != nil {
return corev1.PodTemplateSpec{}, err
}
pts.obj.Spec = podSpec
return *pts.obj, nil
}