openstack-operator/builders/config_map.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

38 lines
970 B
Go
Executable File

package builders
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// ConfigMapBuilder defines the interface to build a ConfigMap
type ConfigMapBuilder struct {
obj *corev1.ConfigMap
owner metav1.Object
scheme *runtime.Scheme
}
// ConfigMap returns a new service builder
func ConfigMap(existing *corev1.ConfigMap, owner metav1.Object, scheme *runtime.Scheme) *ConfigMapBuilder {
existing.Data = map[string]string{}
return &ConfigMapBuilder{
obj: existing,
owner: owner,
scheme: scheme,
}
}
// Data sets a key inside this ConfigMap
func (cm *ConfigMapBuilder) Data(key, value string) *ConfigMapBuilder {
cm.obj.Data[key] = value
return cm
}
// Build returns a complete ConfigMap object
func (cm *ConfigMapBuilder) Build() error {
return controllerutil.SetControllerReference(cm.owner, cm.obj, cm.scheme)
}