-
Notifications
You must be signed in to change notification settings - Fork 461
/
get.go
79 lines (67 loc) · 2 KB
/
get.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package manager
import (
corev1 "k8s.io/api/core/v1"
)
func (m *manager) Get(name string, opts ...GetOption) (*corev1.Secret, bool) {
options := &GetOptions{}
options.ApplyOptions(opts)
secrets, found := m.getFromStore(name)
if !found {
return nil, false
}
class := bundle
if options.Class != nil {
class = *options.Class
} else if secrets.bundle == nil {
class = current // fall back to current secret if there is no bundle secret and if class was not explicitly set
}
switch class {
case current:
return secrets.current.obj, true
case old:
if secrets.old == nil {
return nil, false
}
return secrets.old.obj, true
default:
if secrets.bundle == nil {
return nil, false
}
return secrets.bundle.obj, true
}
}
// GetOption is some configuration that modifies options for a Get request.
type GetOption interface {
// ApplyToOptions applies this configuration to the given options.
ApplyToOptions(*GetOptions)
}
// GetOptions are options for Get calls.
type GetOptions struct {
// Class specifies whether which secret should be returned. By default, the bundle secret is returned. If there is
// no bundle secret then it falls back to the current secret.
Class *secretClass
}
// ApplyOptions applies the given update options on these options, and then returns itself (for convenient chaining).
func (o *GetOptions) ApplyOptions(opts []GetOption) *GetOptions {
for _, opt := range opts {
opt.ApplyToOptions(o)
}
return o
}
var (
// Current sets the Class field to 'current' in the GetOptions.
Current = classOption{class: current}
// Old sets the Class field to 'old' in the GetOptions.
Old = classOption{class: old}
// Bundle sets the Class field to 'bundle' in the GetOptions.
Bundle = classOption{class: bundle}
)
type classOption struct {
class secretClass
}
func (c classOption) ApplyToOptions(options *GetOptions) {
options.Class = &c.class
}