-
Notifications
You must be signed in to change notification settings - Fork 66
/
handler.go
151 lines (128 loc) · 4.38 KB
/
handler.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
/*
* Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved. h file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
*/
package mock
import (
"context"
"encoding/json"
"fmt"
"github.com/gardener/controller-manager-library/pkg/logger"
"k8s.io/client-go/util/flowcontrol"
"github.com/gardener/external-dns-management/pkg/dns/provider"
)
type Handler struct {
provider.DefaultDNSHandler
config provider.DNSHandlerConfig
cache provider.ZoneCache
ctx context.Context
mock *provider.InMemory
mockConfig MockConfig
rateLimiter flowcontrol.RateLimiter
}
type MockConfig struct {
Zones []string `json:"zones"`
FailGetZones bool `json:"failGetZones"`
FailDeleteEntry bool `json:"failDeleteEntry"`
}
var _ provider.DNSHandler = &Handler{}
// TestMock allows tests to access mocked DNSHosted Zones
var TestMock *provider.InMemory
func NewHandler(config *provider.DNSHandlerConfig) (provider.DNSHandler, error) {
mock := provider.NewInMemory()
TestMock = mock
h := &Handler{
DefaultDNSHandler: provider.NewDefaultDNSHandler(TYPE_CODE),
config: *config,
mock: mock,
rateLimiter: config.RateLimiter,
}
err := json.Unmarshal(config.Config.Raw, &h.mockConfig)
if err != nil {
return nil, fmt.Errorf("unmarshal mock providerConfig failed with: %s", err)
}
for _, dnsName := range h.mockConfig.Zones {
if dnsName != "" {
logger.Infof("Providing mock DNSZone %s", dnsName)
hostedZone := provider.NewDNSHostedZone(h.ProviderType(), dnsName, dnsName, "", []string{}, false)
mock.AddZone(hostedZone)
}
}
h.cache, err = provider.NewZoneCache(config.CacheConfig, config.Metrics, nil, h.getZones, h.getZoneState)
if err != nil {
return nil, err
}
return h, nil
}
func (h *Handler) Release() {
h.cache.Release()
}
func (h *Handler) GetZones() (provider.DNSHostedZones, error) {
return h.cache.GetZones()
}
func (h *Handler) getZones(cache provider.ZoneCache) (provider.DNSHostedZones, error) {
if h.mockConfig.FailGetZones {
return nil, fmt.Errorf("forced error by mockConfig.FailGetZones")
}
h.config.RateLimiter.Accept()
zones := h.mock.GetZones()
return zones, nil
}
func (h *Handler) GetZoneState(zone provider.DNSHostedZone) (provider.DNSZoneState, error) {
return h.cache.GetZoneState(zone)
}
func (h *Handler) getZoneState(zone provider.DNSHostedZone, cache provider.ZoneCache) (provider.DNSZoneState, error) {
h.config.RateLimiter.Accept()
return h.mock.CloneZoneState(zone)
}
func (h *Handler) ReportZoneStateConflict(zone provider.DNSHostedZone, err error) bool {
return h.cache.ReportZoneStateConflict(zone, err)
}
func (h *Handler) ExecuteRequests(logger logger.LogContext, zone provider.DNSHostedZone, state provider.DNSZoneState, reqs []*provider.ChangeRequest) error {
err := h.executeRequests(logger, zone, state, reqs)
h.cache.ApplyRequests(logger, err, zone, reqs)
return err
}
func (h *Handler) executeRequests(logger logger.LogContext, zone provider.DNSHostedZone, state provider.DNSZoneState, reqs []*provider.ChangeRequest) error {
var succeeded, failed int
for _, r := range reqs {
h.config.RateLimiter.Accept()
var err error
if h.mockConfig.FailDeleteEntry && r.Action == provider.R_DELETE {
err = fmt.Errorf("forced error by mockConfig.FailDeleteEntry")
} else {
err = h.mock.Apply(zone.Id(), r, h.config.Metrics)
}
if err != nil {
failed++
logger.Infof("Apply failed with %s", err.Error())
if r.Done != nil {
r.Done.Failed(err)
}
} else {
succeeded++
if r.Done != nil {
r.Done.Succeeded()
}
}
}
if succeeded > 0 {
logger.Infof("Succeeded updates for records in zone %s: %d", zone.Id(), succeeded)
}
if failed > 0 {
logger.Infof("Failed updates for records in zone %s: %d", zone.Id(), failed)
return fmt.Errorf("%d changed failed", failed)
}
return nil
}