Skip to content

Commit 113f855

Browse files
committed
RuntimeSDK: add index for ExtensionConfigs having ca injection annotation set
1 parent eeca559 commit 113f855

File tree

3 files changed

+131
-9
lines changed

3 files changed

+131
-9
lines changed

exp/runtime/internal/controllers/extensionconfig_controller.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
7474
return errors.Wrap(err, "failed setting up with a controller manager")
7575
}
7676

77+
if err := indexByExtensionInjectCAFromSecretName(ctx, mgr); err != nil {
78+
return err
79+
}
80+
7781
// warmupRunnable will attempt to sync the RuntimeSDK registry with existing ExtensionConfig objects to ensure extensions
7882
// are discovered before controllers begin reconciling.
7983
err = mgr.Add(&warmupRunnable{
@@ -180,19 +184,19 @@ func (r *Reconciler) secretToExtensionConfig(secret client.Object) []reconcile.R
180184
result := []ctrl.Request{}
181185

182186
extensionConfigs := runtimev1.ExtensionConfigList{}
183-
if err := r.Client.List(context.Background(), &extensionConfigs); err != nil {
187+
indexKey := secret.GetNamespace() + "/" + secret.GetName()
188+
189+
if err := r.Client.List(
190+
context.Background(),
191+
&extensionConfigs,
192+
client.MatchingFields{injectCAFromSecretAnnotationField: indexKey},
193+
); err != nil {
184194
return nil
185195
}
186196

187197
for _, ext := range extensionConfigs.Items {
188-
if secretNameRaw, ok := ext.GetAnnotations()[runtimev1.InjectCAFromSecretAnnotation]; ok {
189-
secretName := splitNamespacedName(secretNameRaw)
190-
// append all extensions to the result which refer the object as secret
191-
if secretName.Namespace == secret.GetNamespace() && secretName.Name == secret.GetName() {
192-
name := client.ObjectKey{Namespace: ext.GetNamespace(), Name: ext.GetName()}
193-
result = append(result, ctrl.Request{NamespacedName: name})
194-
}
195-
}
198+
name := client.ObjectKey{Namespace: ext.GetNamespace(), Name: ext.GetName()}
199+
result = append(result, ctrl.Request{NamespacedName: name})
196200
}
197201

198202
return result
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package index provides indexes for the api.
18+
package controllers
19+
20+
import (
21+
"context"
22+
"fmt"
23+
24+
"github.com/pkg/errors"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
29+
)
30+
31+
const (
32+
// injectCAFromSecretAnnotationField is used by the Extension controller for indexing ExtensionConfigs
33+
// which have the InjectCAFromSecretAnnotation set.
34+
injectCAFromSecretAnnotationField = "metadata.annotations[" + runtimev1.InjectCAFromSecretAnnotation + "]"
35+
)
36+
37+
// indexByExtensionInjectCAFromSecretName adds the index by InjectCAFromSecretAnnotation to the
38+
// managers cache.
39+
func indexByExtensionInjectCAFromSecretName(ctx context.Context, mgr ctrl.Manager) error {
40+
if err := mgr.GetCache().IndexField(ctx, &runtimev1.ExtensionConfig{},
41+
injectCAFromSecretAnnotationField,
42+
extensionConfigByInjectCAFromSecretName,
43+
); err != nil {
44+
return errors.Wrap(err, "error setting index field for InjectCAFromSecretAnnotation")
45+
}
46+
return nil
47+
}
48+
49+
func extensionConfigByInjectCAFromSecretName(o client.Object) []string {
50+
extensionConfig, ok := o.(*runtimev1.ExtensionConfig)
51+
if !ok {
52+
panic(fmt.Sprintf("Expected Cluster but got a %T", o))
53+
}
54+
if value, ok := extensionConfig.Annotations[runtimev1.InjectCAFromSecretAnnotation]; ok {
55+
return []string{value}
56+
}
57+
return nil
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
26+
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
27+
)
28+
29+
func TestExtensionConfigByInjectCAFromSecretName(t *testing.T) {
30+
testCases := []struct {
31+
name string
32+
object client.Object
33+
expected []string
34+
}{
35+
{
36+
name: "when extensionConfig has no inject annotation",
37+
object: &runtimev1.ExtensionConfig{},
38+
expected: nil,
39+
},
40+
{
41+
name: "when cluster has a valid Topology",
42+
object: &runtimev1.ExtensionConfig{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Annotations: map[string]string{
45+
runtimev1.InjectCAFromSecretAnnotation: "foo/bar",
46+
},
47+
},
48+
},
49+
expected: []string{"foo/bar"},
50+
},
51+
}
52+
53+
for _, test := range testCases {
54+
t.Run(test.name, func(t *testing.T) {
55+
g := NewWithT(t)
56+
got := extensionConfigByInjectCAFromSecretName(test.object)
57+
g.Expect(got).To(Equal(test.expected))
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)