-
Notifications
You must be signed in to change notification settings - Fork 67
/
factory.go
200 lines (170 loc) · 5.55 KB
/
factory.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"fmt"
"github.com/gardener/controller-manager-library/pkg/config"
"github.com/gardener/controller-manager-library/pkg/controllermanager/controller"
"github.com/gardener/controller-manager-library/pkg/controllermanager/extension"
"github.com/gardener/controller-manager-library/pkg/utils"
dnsutils "github.com/gardener/external-dns-management/pkg/dns/utils"
)
type DNSHandlerCreatorFunction func(config *DNSHandlerConfig) (DNSHandler, error)
type Factory struct {
typecode string
create DNSHandlerCreatorFunction
optionCreator extension.OptionSourceCreator
genericDefaults *GenericFactoryOptions
supportZoneStateCache bool
}
var _ DNSHandlerFactory = &Factory{}
func NewDNSHandlerFactory(typecode string, create DNSHandlerCreatorFunction, disableZoneStateCache ...bool) *Factory {
disable := false
for _, b := range disableZoneStateCache {
disable = disable || b
}
return &Factory{
typecode: typecode,
create: create,
supportZoneStateCache: !disable,
}
}
func (this *Factory) SetOptionSourceCreator(creator extension.OptionSourceCreator, defaults ...GenericFactoryOptions) *Factory {
this.optionCreator = creator
return this.SetGenericFactoryOptionDefaults(defaults...)
}
func (this *Factory) SetGenericFactoryOptionDefaults(defaults ...GenericFactoryOptions) *Factory {
if len(defaults) == 1 {
this.genericDefaults = &defaults[0]
}
if len(defaults) > 1 {
panic("invalid call to SetGenericFactoryOptionDefaults: only one default possible")
}
return this
}
func (this *Factory) SetOptionSourceByExample(proto config.OptionSource, defaults ...GenericFactoryOptions) *Factory {
this.optionCreator = controller.OptionSourceCreator(proto)
return this.SetGenericFactoryOptionDefaults(defaults...)
}
////////////////////////////////////////////////////////////////////////////////
func (this *Factory) IsResponsibleFor(object *dnsutils.DNSProviderObject) bool {
return object.DNSProvider().Spec.Type == this.typecode
}
func (this *Factory) TypeCodes() utils.StringSet {
return utils.NewStringSet(this.typecode)
}
func (this *Factory) Name() string {
return this.typecode
}
func (this *Factory) Create(typecode string, config *DNSHandlerConfig) (DNSHandler, error) {
if typecode == this.typecode {
err := config.Complete()
if err != nil {
return nil, err
}
return this.create(config)
}
return nil, fmt.Errorf("not responsible for %q", typecode)
}
func (this *Factory) CreateOptionSource() (config.OptionSource, *GenericFactoryOptions) {
if this.optionCreator != nil {
return this.optionCreator(), this.genericDefaults
}
return nil, this.genericDefaults
}
func (this *Factory) SupportZoneStateCache(typecode string) (bool, error) {
if typecode == this.typecode {
return this.supportZoneStateCache, nil
}
return false, fmt.Errorf("not responsible for %q", typecode)
}
///////////////////////////////////////////////////////////////////////////////
type CompoundFactory struct {
name string
typecodes utils.StringSet
finalizers utils.StringSet
factories map[string]DNSHandlerFactory
}
var _ DNSHandlerFactory = &CompoundFactory{}
func NewDNSHandlerCompoundFactory(name string) *CompoundFactory {
return &CompoundFactory{
name,
utils.StringSet{},
utils.StringSet{},
map[string]DNSHandlerFactory{},
}
}
func (this *CompoundFactory) Add(f DNSHandlerFactory, finalizer ...string) error {
typecodes := f.TypeCodes()
for t := range typecodes {
if this.typecodes.Contains(t) {
return fmt.Errorf("typecode %q already registered at compund factory %s", t, this.name)
}
}
for t := range typecodes {
this.factories[t] = f
this.typecodes.Add(t)
this.finalizers.Add(controller.FinalizerName("dns.gardener.cloud", t))
}
this.finalizers.Add(finalizer...)
return nil
}
func (this *CompoundFactory) IsResponsibleFor(object *dnsutils.DNSProviderObject) bool {
_, ok := this.factories[object.DNSProvider().Spec.Type]
return ok
}
func (this *CompoundFactory) TypeCodes() utils.StringSet {
return this.typecodes.Copy()
}
func (this *CompoundFactory) Name() string {
return this.name
}
func (this *CompoundFactory) Finalizers() utils.StringSet {
return this.finalizers.Copy()
}
func (this *CompoundFactory) Create(typecode string, cfg *DNSHandlerConfig) (DNSHandler, error) {
f := this.factories[typecode]
if f != nil {
if cfg.Options != nil {
compound := cfg.Options.Options.(config.OptionSet)
local := *cfg
cfg = &local
src := compound.GetSource(f.Name())
if src != nil {
local.Options = GetFactoryOptions(src)
} else {
local.Options = nil
}
}
return f.Create(typecode, cfg)
}
return nil, fmt.Errorf("not responsible for %q", typecode)
}
func HandlerStringMapper(name string) func(s string) string {
return func(s string) string {
return fmt.Sprintf("%s for provider type %q", s, name)
}
}
func (this *CompoundFactory) CreateOptionSource() (config.OptionSource, *GenericFactoryOptions) {
found := false
compound := config.NewSharedOptionSet("compound", "")
for n, f := range this.factories {
src := CreateFactoryOptionSource(f, n)
if src != nil {
compound.AddSource(n, src)
found = true
}
}
if found {
return compound, nil
}
return nil, nil
}
func (this *CompoundFactory) SupportZoneStateCache(typecode string) (bool, error) {
f := this.factories[typecode]
if f != nil {
return f.SupportZoneStateCache(typecode)
}
return false, fmt.Errorf("not responsible for %q", typecode)
}