mcrouter builder to merge CR labels
Change-Id: I03898d3a759bfd73a7adeefc7ea89c8252c73633
This commit is contained in:
parent
1d83657ec1
commit
491c48ab7d
73
builders/mcrouter.go
Normal file
73
builders/mcrouter.go
Normal file
@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
31
builders/mcrouter_pool_spec.go
Normal file
31
builders/mcrouter_pool_spec.go
Normal file
@ -0,0 +1,31 @@
|
||||
package builders
|
||||
|
||||
import (
|
||||
infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1"
|
||||
)
|
||||
|
||||
// McrouterPoolSpecBuilder defines the interface to build a McrouterPoolSpec
|
||||
type McrouterPoolSpecBuilder struct {
|
||||
obj *infrastructurev1alpha1.McrouterPoolSpec
|
||||
}
|
||||
|
||||
// McrouterPoolSpec returns a new mcrouterPoolSpec builder
|
||||
func McrouterPoolSpec() *McrouterPoolSpecBuilder {
|
||||
poolSpec := &infrastructurev1alpha1.McrouterPoolSpec{
|
||||
Servers: []string{},
|
||||
}
|
||||
return &McrouterPoolSpecBuilder{
|
||||
obj: poolSpec,
|
||||
}
|
||||
}
|
||||
|
||||
// Servers specifies servers for the McrouterPoolSpec
|
||||
func (ps *McrouterPoolSpecBuilder) Servers(servers []string) *McrouterPoolSpecBuilder {
|
||||
ps.obj.Servers = servers
|
||||
return ps
|
||||
}
|
||||
|
||||
// Build returns a complete McrouterPoolSpec object
|
||||
func (ps *McrouterPoolSpecBuilder) Build() (infrastructurev1alpha1.McrouterPoolSpec, error) {
|
||||
return *ps.obj, nil
|
||||
}
|
@ -221,5 +221,6 @@ func (r *McrouterReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
Owns(&appsv1.Deployment{}).
|
||||
Owns(&corev1.Service{}).
|
||||
Owns(&monitoringv1.PodMonitor{}).
|
||||
Owns(&monitoringv1.PrometheusRule{}).
|
||||
Complete(r)
|
||||
}
|
||||
|
@ -29,7 +29,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1"
|
||||
|
||||
@ -221,21 +220,16 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: req.Namespace,
|
||||
Name: fmt.Sprintf("memcached-%s", req.Name),
|
||||
Labels: mcrouterLabels,
|
||||
},
|
||||
}
|
||||
op, err = controllerutil.CreateOrUpdate(ctx, r, mcrouter, func() error {
|
||||
mcrouter.Spec.NodeSelector = memcached.Spec.NodeSelector
|
||||
mcrouter.Spec.Tolerations = memcached.Spec.Tolerations
|
||||
|
||||
mcrouter.Spec.Route = "PoolRoute|default"
|
||||
mcrouter.Spec.Pools = map[string]infrastructurev1alpha1.McrouterPoolSpec{
|
||||
"default": infrastructurev1alpha1.McrouterPoolSpec{
|
||||
Servers: servers,
|
||||
},
|
||||
}
|
||||
|
||||
return controllerutil.SetControllerReference(&memcached, mcrouter, r.Scheme)
|
||||
op, err = k8sutils.CreateOrUpdate(ctx, r, mcrouter, func() error {
|
||||
return builders.Mcrouter(mcrouter, &memcached, r.Scheme).
|
||||
Labels(mcrouterLabels).
|
||||
NodeSelector(memcached.Spec.NodeSelector).
|
||||
Tolerations(memcached.Spec.Tolerations).
|
||||
Route("PoolRoute|default").
|
||||
Pool("default", builders.McrouterPoolSpec().Servers(servers)).
|
||||
Build()
|
||||
})
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
@ -252,5 +246,6 @@ func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
Owns(&appsv1.Deployment{}).
|
||||
Owns(&infrastructurev1alpha1.Mcrouter{}).
|
||||
Owns(&monitoringv1.PodMonitor{}).
|
||||
Owns(&monitoringv1.PrometheusRule{}).
|
||||
Complete(r)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user