forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_reconciler.go
99 lines (85 loc) · 3.16 KB
/
pod_reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright 2025 The Kubernetes Authors.
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 controller
import (
"context"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)
type PodReconciler struct {
client.Client
Datastore datastore.Datastore
Scheme *runtime.Scheme
Record record.EventRecorder
}
func (c *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
inferencePool, err := c.Datastore.PoolGet()
if err != nil {
logger.V(logutil.TRACE).Info("Skipping reconciling Pod because the InferencePool is not available yet", "error", err)
// When the inferencePool is initialized it lists the appropriate pods and populates the datastore, so no need to requeue.
return ctrl.Result{}, nil
} else if inferencePool.Namespace != req.Namespace {
return ctrl.Result{}, nil
}
logger.V(logutil.VERBOSE).Info("Pod being reconciled", "name", req.NamespacedName)
pod := &corev1.Pod{}
if err := c.Get(ctx, req.NamespacedName, pod); err != nil {
if apierrors.IsNotFound(err) {
c.Datastore.PodDelete(req.NamespacedName)
return ctrl.Result{}, nil
}
logger.V(logutil.DEFAULT).Error(err, "Unable to get pod", "name", req.NamespacedName)
return ctrl.Result{}, err
}
c.updateDatastore(logger, pod)
return ctrl.Result{}, nil
}
func (c *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
Complete(c)
}
func (c *PodReconciler) updateDatastore(logger logr.Logger, pod *corev1.Pod) {
namespacedName := types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}
if !pod.DeletionTimestamp.IsZero() || !c.Datastore.PoolLabelsMatch(pod.Labels) || !podIsReady(pod) {
logger.V(logutil.DEFAULT).Info("Pod removed or not added", "name", namespacedName)
c.Datastore.PodDelete(namespacedName)
} else {
if c.Datastore.PodUpdateOrAddIfNotExist(pod) {
logger.V(logutil.DEFAULT).Info("Pod added", "name", namespacedName)
} else {
logger.V(logutil.DEFAULT).Info("Pod already exists", "name", namespacedName)
}
}
}
func podIsReady(pod *corev1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodReady {
if condition.Status == corev1.ConditionTrue {
return true
}
break
}
}
return false
}