-
Notifications
You must be signed in to change notification settings - Fork 458
/
deleg_builder.go
143 lines (122 loc) · 5.34 KB
/
deleg_builder.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
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This 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
// limitations under the License.
package builder
import (
"fmt"
"github.com/gardener/gardener/pkg/client/kubernetes/clientmap"
"github.com/gardener/gardener/pkg/client/kubernetes/clientmap/internal"
"github.com/go-logr/logr"
)
// DelegatingClientMapBuilder can build a DelegatingClientMap which will delegate calls to different ClientMaps
// based on the type of the key (e.g. a call with keys.ForShoot() will be delegated to the ShootClientMap).
type DelegatingClientMapBuilder struct {
log logr.Logger
gardenClientMapFunc func() (clientmap.ClientMap, error)
seedClientMapFunc func() (clientmap.ClientMap, error)
shootClientMapFunc func(gardenClients, seedClients clientmap.ClientMap) (clientmap.ClientMap, error)
plantClientMapFunc func(gardenClients clientmap.ClientMap) (clientmap.ClientMap, error)
}
// NewDelegatingClientMapBuilder creates a new DelegatingClientMapBuilder.
func NewDelegatingClientMapBuilder(log logr.Logger) *DelegatingClientMapBuilder {
return &DelegatingClientMapBuilder{
log: log,
gardenClientMapFunc: func() (clientmap.ClientMap, error) {
return nil, fmt.Errorf("garden ClientMap is required but not set")
},
}
}
// WithGardenClientMap sets the ClientMap that should be used for Garden clients.
func (b *DelegatingClientMapBuilder) WithGardenClientMap(clientMap clientmap.ClientMap) *DelegatingClientMapBuilder {
b.gardenClientMapFunc = func() (clientmap.ClientMap, error) {
return clientMap, nil
}
return b
}
// WithGardenClientMapBuilder sets a ClientMap builder that should be used to build a ClientMap for Garden clients.
func (b *DelegatingClientMapBuilder) WithGardenClientMapBuilder(builder *GardenClientMapBuilder) *DelegatingClientMapBuilder {
b.gardenClientMapFunc = func() (clientmap.ClientMap, error) {
return builder.
Build(b.log)
}
return b
}
// WithSeedClientMap sets the ClientMap that should be used for Seed clients.
func (b *DelegatingClientMapBuilder) WithSeedClientMap(clientMap clientmap.ClientMap) *DelegatingClientMapBuilder {
b.seedClientMapFunc = func() (clientmap.ClientMap, error) {
return clientMap, nil
}
return b
}
// WithSeedClientMapBuilder sets a ClientMap builder that should be used to build a ClientMap for Seed clients.
func (b *DelegatingClientMapBuilder) WithSeedClientMapBuilder(builder *SeedClientMapBuilder) *DelegatingClientMapBuilder {
b.seedClientMapFunc = func() (clientmap.ClientMap, error) {
return builder.
Build(b.log)
}
return b
}
// WithShootClientMap sets the ClientMap that should be used for Shoot clients.
func (b *DelegatingClientMapBuilder) WithShootClientMap(clientMap clientmap.ClientMap) *DelegatingClientMapBuilder {
b.shootClientMapFunc = func(_, _ clientmap.ClientMap) (clientmap.ClientMap, error) {
return clientMap, nil
}
return b
}
// WithShootClientMapBuilder sets a ClientMap builder that should be used to build a ClientMap for Shoot clients.
func (b *DelegatingClientMapBuilder) WithShootClientMapBuilder(builder *ShootClientMapBuilder) *DelegatingClientMapBuilder {
b.shootClientMapFunc = func(gardenClients, seedClients clientmap.ClientMap) (clientmap.ClientMap, error) {
return builder.
WithGardenClientMap(gardenClients).
WithSeedClientMap(seedClients).
Build(b.log)
}
return b
}
// WithPlantClientMap sets the ClientMap that should be used for Plant clients.
func (b *DelegatingClientMapBuilder) WithPlantClientMap(clientMap clientmap.ClientMap) *DelegatingClientMapBuilder {
b.plantClientMapFunc = func(_ clientmap.ClientMap) (clientmap.ClientMap, error) {
return clientMap, nil
}
return b
}
// Build builds the DelegatingClientMap using the provided attributes.
func (b *DelegatingClientMapBuilder) Build() (clientmap.ClientMap, error) {
gardenClients, err := b.gardenClientMapFunc()
if err != nil {
return nil, fmt.Errorf("failed to construct garden ClientMap: %w", err)
}
var seedClients, shootClients, plantClients clientmap.ClientMap
if b.seedClientMapFunc != nil {
seedClients, err = b.seedClientMapFunc()
if err != nil {
return nil, fmt.Errorf("failed to construct seed ClientMap: %w", err)
}
}
if b.shootClientMapFunc != nil {
if seedClients == nil {
return nil, fmt.Errorf("failed to construct shoot ClientMap, seed ClientMap is required but not set")
}
shootClients, err = b.shootClientMapFunc(gardenClients, seedClients)
if err != nil {
return nil, fmt.Errorf("failed to construct shoot ClientMap: %w", err)
}
}
if b.plantClientMapFunc != nil {
plantClients, err = b.plantClientMapFunc(gardenClients)
if err != nil {
return nil, fmt.Errorf("failed to construct plant ClientMap: %w", err)
}
}
return internal.NewDelegatingClientMap(gardenClients, seedClients, shootClients, plantClients), nil
}