Skip to content

Commit 4e5ff76

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

File tree

3 files changed

+129
-9
lines changed

3 files changed

+129
-9
lines changed

exp/runtime/internal/controllers/extensionconfig_controller.go

Lines changed: 12 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,18 @@ 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+
result = append(result, ctrl.Request{NamespacedName: client.ObjectKey{Name: ext.Name}})
196199
}
197200

198201
return result
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"context"
21+
"fmt"
22+
23+
"github.com/pkg/errors"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
27+
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
28+
)
29+
30+
const (
31+
// injectCAFromSecretAnnotationField is used by the Extension controller for indexing ExtensionConfigs
32+
// which have the InjectCAFromSecretAnnotation set.
33+
injectCAFromSecretAnnotationField = "metadata.annotations[" + runtimev1.InjectCAFromSecretAnnotation + "]"
34+
)
35+
36+
// indexByExtensionInjectCAFromSecretName adds the index by InjectCAFromSecretAnnotation to the
37+
// managers cache.
38+
func indexByExtensionInjectCAFromSecretName(ctx context.Context, mgr ctrl.Manager) error {
39+
if err := mgr.GetCache().IndexField(ctx, &runtimev1.ExtensionConfig{},
40+
injectCAFromSecretAnnotationField,
41+
extensionConfigByInjectCAFromSecretName,
42+
); err != nil {
43+
return errors.Wrap(err, "error setting index field for InjectCAFromSecretAnnotation")
44+
}
45+
return nil
46+
}
47+
48+
func extensionConfigByInjectCAFromSecretName(o client.Object) []string {
49+
extensionConfig, ok := o.(*runtimev1.ExtensionConfig)
50+
if !ok {
51+
panic(fmt.Sprintf("Expected ExtensionConfig but got a %T", o))
52+
}
53+
if value, ok := extensionConfig.Annotations[runtimev1.InjectCAFromSecretAnnotation]; ok {
54+
return []string{value}
55+
}
56+
return nil
57+
}
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)