openstack-operator/builders/mcrouter.go
okozachenko 491c48ab7d mcrouter builder to merge CR labels
Change-Id: I03898d3a759bfd73a7adeefc7ea89c8252c73633
2020-04-08 09:07:42 -07:00

74 lines
2.1 KiB
Go

package builders
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// McrouterBuilder defines the interface to build a Mcrouter
type McrouterBuilder struct {
obj *infrastructurev1alpha1.Mcrouter
poolSpecs map[string]*McrouterPoolSpecBuilder
owner metav1.Object
scheme *runtime.Scheme
}
// Mcrouter returns a new mcrouter builder
func Mcrouter(existing *infrastructurev1alpha1.Mcrouter, owner metav1.Object, scheme *runtime.Scheme) *McrouterBuilder {
if existing.Spec.Pools == nil {
existing.Spec.Pools = map[string]infrastructurev1alpha1.McrouterPoolSpec{}
}
return &McrouterBuilder{
obj: existing,
poolSpecs: map[string]*McrouterPoolSpecBuilder{},
owner: owner,
scheme: scheme,
}
}
// Labels specifies labels for the Mcrouter
func (z *McrouterBuilder) Labels(labels map[string]string) *McrouterBuilder {
z.obj.ObjectMeta.Labels = labels
return z
}
// NodeSelector defines a NodeSelector for Mcrouter
func (z *McrouterBuilder) NodeSelector(selector map[string]string) *McrouterBuilder {
z.obj.Spec.NodeSelector = selector
return z
}
// Tolerations defines tolerations for Mcrouter
func (z *McrouterBuilder) Tolerations(tolerations []v1.Toleration) *McrouterBuilder {
z.obj.Spec.Tolerations = tolerations
return z
}
// Route defines route for Mcrouter
func (z *McrouterBuilder) Route(route string) *McrouterBuilder {
z.obj.Spec.Route = route
return z
}
// Pool defines one set pool for Mcrouter
func (z *McrouterBuilder) Pool(poolName string, poolSpec *McrouterPoolSpecBuilder) *McrouterBuilder {
z.poolSpecs[poolName] = poolSpec
return z
}
// Build returns a complete Mcrouter object
func (z *McrouterBuilder) Build() error {
for key, value := range z.poolSpecs {
pool, err := value.Build()
if err != nil {
return err
}
z.obj.Spec.Pools[key] = pool
}
return controllerutil.SetControllerReference(z.owner, z.obj, z.scheme)
}