/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package informer import ( "context" "time" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "knative.dev/pkg/logging" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/aws/karpenter-core/pkg/controllers/state" corecontroller "github.com/aws/karpenter-core/pkg/operator/controller" ) var stateRetryPeriod = 1 * time.Minute // PodController reconciles pods for the purpose of maintaining state regarding pods that is expensive to compute. type PodController struct { kubeClient client.Client cluster *state.Cluster } func NewPodController(kubeClient client.Client, cluster *state.Cluster) corecontroller.Controller { return &PodController{ kubeClient: kubeClient, cluster: cluster, } } func (c *PodController) Name() string { return "pod_state" } func (c *PodController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { ctx = logging.WithLogger(ctx, logging.FromContext(ctx).Named(c.Name()).With("pod", req.NamespacedName)) pod := &v1.Pod{} if err := c.kubeClient.Get(ctx, req.NamespacedName, pod); err != nil { if errors.IsNotFound(err) { // notify cluster state of the node deletion c.cluster.DeletePod(req.NamespacedName) } return reconcile.Result{}, client.IgnoreNotFound(err) } if err := c.cluster.UpdatePod(ctx, pod); err != nil { // We requeue here since the NotFound error is from finding the node for the binding if errors.IsNotFound(err) { return reconcile.Result{Requeue: true}, nil } return reconcile.Result{}, err } return reconcile.Result{RequeueAfter: stateRetryPeriod}, nil } func (c *PodController) Builder(_ context.Context, m manager.Manager) corecontroller.Builder { return corecontroller.Adapt(controllerruntime. NewControllerManagedBy(m). For(&v1.Pod{}). WithOptions(controller.Options{MaxConcurrentReconciles: 10})) }