diff --git a/main.go b/main.go index b1660b28a2..d2009b1046 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,9 @@ package main import ( "flag" + v1 "k8s.io/api/apps/v1" "os" + "sigs.k8s.io/controller-runtime/pkg/client" "time" networkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" @@ -107,6 +109,19 @@ func main() { HealthProbeBindAddress: flagVar.probeAddr, LeaderElection: flagVar.enableLeaderElection, LeaderElectionID: "76223278.kyma-project.io", + Client: client.Options{ + Cache: &client.CacheOptions{ + // The cache is disabled for these objects to avoid huge memory usage. + // Having the cache enabled had previously caused memory usage + //to have a significant peak when sidecar restart was triggered. + DisableFor: []client.Object{ + &v1.DaemonSet{}, + &v1.Deployment{}, + &v1.StatefulSet{}, + &v1.ReplicaSet{}, + }, + }, + }, }) if err != nil { setupLog.Error(err, "unable to start manager") diff --git a/pkg/lib/sidecars/pods/get.go b/pkg/lib/sidecars/pods/get.go index a5c80f1a7c..61257da214 100644 --- a/pkg/lib/sidecars/pods/get.go +++ b/pkg/lib/sidecars/pods/get.go @@ -51,32 +51,32 @@ func getAllRunningPods(ctx context.Context, c client.Client) (*v1.PodList, error return podList, nil } -func GetPodsToRestart(ctx context.Context, c client.Client, expectedImage SidecarImage, expectedResources v1.ResourceRequirements, predicates []filter.SidecarProxyPredicate, logger *logr.Logger) (outputPodsList v1.PodList, err error) { +func GetPodsToRestart(ctx context.Context, c client.Client, expectedImage SidecarImage, expectedResources v1.ResourceRequirements, predicates []filter.SidecarProxyPredicate, logger *logr.Logger) (outputPodsList *v1.PodList, err error) { podList, err := getAllRunningPods(ctx, c) if err != nil { - return outputPodsList, err + return nil, err } - podList.DeepCopyInto(&outputPodsList) - outputPodsList.Items = []v1.Pod{} - //Add predicate for image version and resources configuration predicates = append(predicates, NewRestartProxyPredicate(expectedImage, expectedResources)) for _, predicate := range predicates { evaluator, err := predicate.NewProxyRestartEvaluator(ctx) if err != nil { - return v1.PodList{}, err + return &v1.PodList{}, err } + outputPodsList = &v1.PodList{} for _, pod := range podList.Items { if evaluator.RequiresProxyRestart(pod) { - outputPodsList.Items = append(outputPodsList.Items, *pod.DeepCopy()) + outputPodsList.Items = append(outputPodsList.Items, pod) } } } - logger.Info("Pods to restart", "number of pods", len(outputPodsList.Items)) + if outputPodsList != nil { + logger.Info("Pods to restart", "number of pods", len(outputPodsList.Items)) + } return outputPodsList, nil } @@ -97,6 +97,7 @@ func containsSidecar(pod v1.Pod) bool { func GetAllInjectedPods(ctx context.Context, k8sclient client.Client) (outputPodList *v1.PodList, err error) { podList := &v1.PodList{} outputPodList = &v1.PodList{} + outputPodList.Items = make([]v1.Pod, len(podList.Items)) err = retry.RetryOnError(retry.DefaultRetry, func() error { return k8sclient.List(ctx, podList, &client.ListOptions{}) diff --git a/pkg/lib/sidecars/remove/remove.go b/pkg/lib/sidecars/remove/remove.go index 770f2e3a1c..16aede487f 100644 --- a/pkg/lib/sidecars/remove/remove.go +++ b/pkg/lib/sidecars/remove/remove.go @@ -15,5 +15,5 @@ func RemoveSidecars(ctx context.Context, k8sclient client.Client, logger *logr.L return nil, err } - return restart.Restart(ctx, k8sclient, *toRestart, logger) + return restart.Restart(ctx, k8sclient, toRestart, logger) } diff --git a/pkg/lib/sidecars/restart/restart.go b/pkg/lib/sidecars/restart/restart.go index 0570b1f4a9..fbf2c71916 100644 --- a/pkg/lib/sidecars/restart/restart.go +++ b/pkg/lib/sidecars/restart/restart.go @@ -26,7 +26,7 @@ func newRestartWarning(o actionObject, message string) RestartWarning { } } -func Restart(ctx context.Context, c client.Client, podList v1.PodList, logger *logr.Logger) ([]RestartWarning, error) { +func Restart(ctx context.Context, c client.Client, podList *v1.PodList, logger *logr.Logger) ([]RestartWarning, error) { warnings := make([]RestartWarning, 0) processedActionObjects := make(map[string]bool) diff --git a/pkg/lib/sidecars/restart/restart_test.go b/pkg/lib/sidecars/restart/restart_test.go index fc6b329661..602d17a25d 100644 --- a/pkg/lib/sidecars/restart/restart_test.go +++ b/pkg/lib/sidecars/restart/restart_test.go @@ -46,7 +46,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -67,7 +67,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -88,7 +88,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -109,7 +109,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -134,7 +134,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -158,7 +158,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -181,7 +181,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -204,7 +204,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -230,7 +230,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -256,7 +256,7 @@ var _ = Describe("Restart Pods", func() { c := fakeClient(&pod) // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -291,7 +291,7 @@ var _ = Describe("Restart Pods", func() { }}) // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -316,7 +316,7 @@ var _ = Describe("Restart Pods", func() { } // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred()) @@ -351,7 +351,7 @@ var _ = Describe("Restart Pods", func() { }}) // when - warnings, err := restart.Restart(ctx, c, podList, &logger) + warnings, err := restart.Restart(ctx, c, &podList, &logger) // then Expect(err).NotTo(HaveOccurred())