forked from openshift/hive
/
syncset.go
129 lines (105 loc) · 3.2 KB
/
syncset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package syncset
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
hivev1 "github.com/openshift/hive/apis/hive/v1"
"github.com/openshift/hive/pkg/test/generic"
)
// Option defines a function signature for any function that wants to be passed into Build
type Option func(*hivev1.SyncSet)
// Build runs each of the functions passed in to generate the object.
func Build(opts ...Option) *hivev1.SyncSet {
retval := &hivev1.SyncSet{}
for _, o := range opts {
o(retval)
}
return retval
}
type Builder interface {
Build(opts ...Option) *hivev1.SyncSet
Options(opts ...Option) Builder
GenericOptions(opts ...generic.Option) Builder
}
func BasicBuilder() Builder {
return &builder{}
}
func FullBuilder(namespace, name string, typer runtime.ObjectTyper) Builder {
b := &builder{}
return b.GenericOptions(
generic.WithTypeMeta(typer),
generic.WithResourceVersion("1"),
generic.WithNamespace(namespace),
generic.WithName(name),
)
}
type builder struct {
options []Option
}
func (b *builder) Build(opts ...Option) *hivev1.SyncSet {
return Build(append(b.options, opts...)...)
}
func (b *builder) Options(opts ...Option) Builder {
return &builder{
options: append(b.options, opts...),
}
}
func (b *builder) GenericOptions(opts ...generic.Option) Builder {
options := make([]Option, len(opts))
for i, o := range opts {
options[i] = Generic(o)
}
return b.Options(options...)
}
// Generic allows common functions applicable to all objects to be used as Options to Build
func Generic(opt generic.Option) Option {
return func(syncSet *hivev1.SyncSet) {
opt(syncSet)
}
}
// WithName sets the object.Name field when building an object with Build.
func WithName(name string) Option {
return Generic(generic.WithName(name))
}
// WithNamespace sets the object.Namespace field when building an object with Build.
func WithNamespace(namespace string) Option {
return Generic(generic.WithNamespace(namespace))
}
func WithGeneration(generation int64) Option {
return Generic(generic.WithGeneration(generation))
}
func ForClusterDeployments(clusterDeploymentNames ...string) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.ClusterDeploymentRefs = make([]corev1.LocalObjectReference, len(clusterDeploymentNames))
for i, name := range clusterDeploymentNames {
syncSet.Spec.ClusterDeploymentRefs[i] = corev1.LocalObjectReference{Name: name}
}
}
}
func WithApplyMode(applyMode hivev1.SyncSetResourceApplyMode) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.ResourceApplyMode = applyMode
}
}
func WithApplyBehavior(applyBehavior hivev1.SyncSetApplyBehavior) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.ApplyBehavior = applyBehavior
}
}
func WithResources(objs ...hivev1.MetaRuntimeObject) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.Resources = make([]runtime.RawExtension, len(objs))
for i, obj := range objs {
syncSet.Spec.Resources[i].Object = obj
}
}
}
func WithSecrets(secrets ...hivev1.SecretMapping) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.Secrets = secrets
}
}
func WithPatches(patches ...hivev1.SyncObjectPatch) Option {
return func(syncSet *hivev1.SyncSet) {
syncSet.Spec.Patches = patches
}
}