Skip to content

Commit 317b48e

Browse files
fix: use non-caching client to get pod in getPodPrivateIP in ENI controller (#21)
This removes the requirement to have list/watch permissions on pods (was missing in the ClusterRole in the Helm chart) and it doesn't cache all pods in the cluster anymore (should reduce memory usage).
1 parent 3a8013a commit 317b48e

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

controllers/eni_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ import (
3838
// ENIReconciler reconciles a ENI object
3939
type ENIReconciler struct {
4040
client.Client
41-
Log logr.Logger
42-
EC2 *ec2.EC2
41+
NonCachingClient client.Client
42+
Log logr.Logger
43+
EC2 *ec2.EC2
4344
}
4445

4546
// +kubebuilder:rbac:groups=aws.k8s.logmein.com,resources=enis,verbs=get;list;watch;create;update;patch;delete
@@ -278,7 +279,8 @@ func (r *ENIReconciler) getSecurityGroupIDs(securityGroups []string) ([]*string,
278279

279280
func (r *ENIReconciler) getPodPrivateIP(namespace, podName string) (string, error) {
280281
pod := &corev1.Pod{}
281-
if err := r.Client.Get(context.Background(), client.ObjectKey{
282+
// we use a non-caching client here as otherwise we would need to cache all pods (would increase memory usage) in the cluster and require list/watch permissions
283+
if err := r.NonCachingClient.Get(context.Background(), client.ObjectKey{
282284
Namespace: namespace,
283285
Name: podName,
284286
}, pod); err != nil {
@@ -291,7 +293,7 @@ func (r *ENIReconciler) getPodPrivateIP(namespace, podName string) (string, erro
291293
func (r *ENIReconciler) findENI(privateIP string) (*ec2.NetworkInterface, error) {
292294
if resp, err := r.EC2.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
293295
Filters: []*ec2.Filter{
294-
&ec2.Filter{
296+
{
295297
Name: aws.String("addresses.private-ip-address"),
296298
Values: []*string{
297299
aws.String(privateIP),

main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/runtime"
2929
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3030
ctrl "sigs.k8s.io/controller-runtime"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
3132
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3233
// +kubebuilder:scaffold:imports
3334
)
@@ -82,8 +83,15 @@ func main() {
8283
os.Exit(1)
8384
}
8485

86+
cachingClient := mgr.GetClient()
87+
nonCachingClient, err := client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()})
88+
if err != nil {
89+
setupLog.Error(err, "unable to get non-caching client")
90+
os.Exit(1)
91+
}
92+
8593
err = (&controllers.EIPReconciler{
86-
Client: mgr.GetClient(),
94+
Client: cachingClient,
8795
Log: ctrl.Log.WithName("controllers").WithName("EIP"),
8896
EC2: ec2,
8997
}).SetupWithManager(mgr)
@@ -92,9 +100,10 @@ func main() {
92100
os.Exit(1)
93101
}
94102
err = (&controllers.ENIReconciler{
95-
Client: mgr.GetClient(),
96-
Log: ctrl.Log.WithName("controllers").WithName("ENI"),
97-
EC2: ec2,
103+
Client: cachingClient,
104+
NonCachingClient: nonCachingClient,
105+
Log: ctrl.Log.WithName("controllers").WithName("ENI"),
106+
EC2: ec2,
98107
}).SetupWithManager(mgr)
99108
if err != nil {
100109
setupLog.Error(err, "unable to create controller", "controller", "ENI")

0 commit comments

Comments
 (0)