Skip to content

Commit 9d17e34

Browse files
authored
Refactor the SecretStore client manager (external-secrets#3419)
* Refactor the SecretStore client manager Signed-off-by: shuheiktgw <s-kitagawa@mercari.com> * Fix ineffectual assignment to err Signed-off-by: shuheiktgw <s-kitagawa@mercari.com> * Update docs Signed-off-by: shuheiktgw <s-kitagawa@mercari.com> --------- Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>
1 parent 07dabc4 commit 9d17e34

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

apis/externalsecrets/v1beta1/clusterexternalsecret_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ClusterExternalSecretSpec struct {
4141
// +optional
4242
NamespaceSelectors []*metav1.LabelSelector `json:"namespaceSelectors,omitempty"`
4343

44-
// Choose namespaces by name. This field is ORed with anything that NamespaceSelector ends up choosing.
44+
// Choose namespaces by name. This field is ORed with anything that NamespaceSelectors ends up choosing.
4545
// +optional
4646
Namespaces []string `json:"namespaces,omitempty"`
4747

config/crds/bases/external-secrets.io_clusterexternalsecrets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ spec:
615615
type: array
616616
namespaces:
617617
description: Choose namespaces by name. This field is ORed with anything
618-
that NamespaceSelector ends up choosing.
618+
that NamespaceSelectors ends up choosing.
619619
items:
620620
type: string
621621
type: array

deploy/crds/bundle.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ spec:
583583
x-kubernetes-map-type: atomic
584584
type: array
585585
namespaces:
586-
description: Choose namespaces by name. This field is ORed with anything that NamespaceSelector ends up choosing.
586+
description: Choose namespaces by name. This field is ORed with anything that NamespaceSelectors ends up choosing.
587587
items:
588588
type: string
589589
type: array

docs/api/spec.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ Deprecated: Use NamespaceSelectors instead.</p>
13561356
</td>
13571357
<td>
13581358
<em>(Optional)</em>
1359-
<p>Choose namespaces by name. This field is ORed with anything that NamespaceSelector ends up choosing.</p>
1359+
<p>Choose namespaces by name. This field is ORed with anything that NamespaceSelectors ends up choosing.</p>
13601360
</td>
13611361
</tr>
13621362
<tr>
@@ -1544,7 +1544,7 @@ Deprecated: Use NamespaceSelectors instead.</p>
15441544
</td>
15451545
<td>
15461546
<em>(Optional)</em>
1547-
<p>Choose namespaces by name. This field is ORed with anything that NamespaceSelector ends up choosing.</p>
1547+
<p>Choose namespaces by name. This field is ORed with anything that NamespaceSelectors ends up choosing.</p>
15481548
</td>
15491549
</tr>
15501550
<tr>

pkg/controllers/secretstore/client_manager.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"strings"
2121

2222
"github.com/go-logr/logr"
23-
"golang.org/x/exp/slices"
2423
v1 "k8s.io/api/core/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/labels"
2626
"k8s.io/apimachinery/pkg/types"
2727
ctrl "sigs.k8s.io/controller-runtime"
2828
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -61,7 +61,7 @@ type clientVal struct {
6161
store esv1beta1.GenericStore
6262
}
6363

64-
// New constructs a new manager with defaults.
64+
// NewManager constructs a new manager with defaults.
6565
func NewManager(ctrlClient client.Client, controllerClass string, enableFloodgate bool) *Manager {
6666
log := ctrl.Log.WithName("clientmanager")
6767
return &Manager{
@@ -117,12 +117,13 @@ func (m *Manager) Get(ctx context.Context, storeRef esv1beta1.SecretStoreRef, na
117117
}
118118
// when using ClusterSecretStore, validate the ClusterSecretStore namespace conditions
119119
shouldProcess, err := m.shouldProcessSecret(store, namespace)
120-
if err != nil || !shouldProcess {
121-
if err == nil && !shouldProcess {
122-
err = fmt.Errorf(errClusterStoreMismatch, store.GetName(), namespace)
123-
}
120+
if err != nil {
124121
return nil, err
125122
}
123+
if !shouldProcess {
124+
return nil, fmt.Errorf(errClusterStoreMismatch, store.GetName(), namespace)
125+
}
126+
126127
if m.enableFloodgate {
127128
err := assertStoreIsUsable(store)
128129
if err != nil {
@@ -155,7 +156,7 @@ func (m *Manager) getStoredClient(ctx context.Context, storeProvider esv1beta1.P
155156
m.log.V(1).Info("cleaning up client",
156157
"provider", fmt.Sprintf("%T", storeProvider),
157158
"store", storeName)
158-
// if we have a client but it points to a different store
159+
// if we have a client, but it points to a different store
159160
// we must clean it up
160161
val.client.Close(ctx)
161162
delete(m.clientMap, idx)
@@ -216,29 +217,32 @@ func (m *Manager) shouldProcessSecret(store esv1beta1.GenericStore, ns string) (
216217
return true, nil
217218
}
218219

219-
namespaceList := &v1.NamespaceList{}
220+
namespace := v1.Namespace{}
221+
if err := m.client.Get(context.Background(), client.ObjectKey{Name: ns}, &namespace); err != nil {
222+
return false, fmt.Errorf("failed to get a namespace %q: %w", ns, err)
223+
}
220224

225+
nsLabels := labels.Set(namespace.GetLabels())
221226
for _, condition := range store.GetSpec().Conditions {
227+
var labelSelectors []*metav1.LabelSelector
222228
if condition.NamespaceSelector != nil {
223-
namespaceSelector, err := metav1.LabelSelectorAsSelector(condition.NamespaceSelector)
224-
if err != nil {
225-
return false, err
226-
}
227-
228-
if err := m.client.List(context.Background(), namespaceList, client.MatchingLabelsSelector{Selector: namespaceSelector}); err != nil {
229-
return false, err
230-
}
231-
232-
for _, namespace := range namespaceList.Items {
233-
if namespace.GetName() == ns {
234-
return true, nil // namespace matches the labelselector
235-
}
236-
}
229+
labelSelectors = append(labelSelectors, condition.NamespaceSelector)
230+
}
231+
for _, n := range condition.Namespaces {
232+
labelSelectors = append(labelSelectors, &metav1.LabelSelector{
233+
MatchLabels: map[string]string{
234+
"kubernetes.io/metadata.name": n,
235+
},
236+
})
237237
}
238238

239-
if condition.Namespaces != nil {
240-
if slices.Contains(condition.Namespaces, ns) {
241-
return true, nil // namespace in the namespaces list
239+
for _, ls := range labelSelectors {
240+
selector, err := metav1.LabelSelectorAsSelector(ls)
241+
if err != nil {
242+
return false, fmt.Errorf("failed to convert label selector into selector %v: %w", ls, err)
243+
}
244+
if selector.Matches(nsLabels) {
245+
return true, nil
242246
}
243247
}
244248
}

0 commit comments

Comments
 (0)