Skip to content

Commit 582e5fa

Browse files
authored
Make k8s access explicitly read only (#1101)
* Repleace client.Client with client.Reader This is more aligned with the actual use abd prevents accidental mutation of k8s objects. Signed-off-by: Etai Lev Ran <[email protected]> * Replace client.Client with client.Reader While this is more aligned with the intent, in some cases the hepler calls unto gateway API code which is expecting a full fledged client. In those cases, the original parameter type has been retained (instead of type casting in the call to Gateway API code) Signed-off-by: Etai Lev Ran <[email protected]> --------- Signed-off-by: Etai Lev Ran <[email protected]>
1 parent daf010d commit 582e5fa

File tree

7 files changed

+21
-21
lines changed

7 files changed

+21
-21
lines changed

conformance/utils/kubernetes/helpers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func checkCondition(t *testing.T, conditions []metav1.Condition, expectedConditi
6969
// InferencePoolMustHaveCondition waits for the specified InferencePool resource
7070
// to exist and report the expected status condition within one of its parent statuses.
7171
// It polls the InferencePool's status until the condition is met or the timeout occurs.
72-
func InferencePoolMustHaveCondition(t *testing.T, c client.Client, poolNN types.NamespacedName, expectedCondition metav1.Condition) {
72+
func InferencePoolMustHaveCondition(t *testing.T, c client.Reader, poolNN types.NamespacedName, expectedCondition metav1.Condition) {
7373
t.Helper() // Marks this function as a test helper
7474

7575
var timeoutConfig config.InferenceExtensionTimeoutConfig = config.DefaultInferenceExtensionTimeoutConfig()
@@ -159,7 +159,7 @@ func InferencePoolMustHaveCondition(t *testing.T, c client.Client, poolNN types.
159159
// InferencePoolMustHaveNoParents waits for the specified InferencePool resource
160160
// to exist and report that it has no parent references in its status.
161161
// This typically indicates it is no longer referenced by any Gateway API resources.
162-
func InferencePoolMustHaveNoParents(t *testing.T, c client.Client, poolNN types.NamespacedName) {
162+
func InferencePoolMustHaveNoParents(t *testing.T, c client.Reader, poolNN types.NamespacedName) {
163163
t.Helper()
164164

165165
var lastObservedPool *inferenceapi.InferencePool
@@ -242,7 +242,7 @@ func HTTPRouteMustBeAcceptedAndResolved(t *testing.T, c client.Client, timeoutCo
242242
// InferencePoolMustBeAcceptedByParent waits for the specified InferencePool
243243
// to report an Accepted condition with status True and reason "Accepted"
244244
// from at least one of its parent Gateways.
245-
func InferencePoolMustBeAcceptedByParent(t *testing.T, c client.Client, poolNN types.NamespacedName) {
245+
func InferencePoolMustBeAcceptedByParent(t *testing.T, c client.Reader, poolNN types.NamespacedName) {
246246
t.Helper()
247247

248248
acceptedByParentCondition := metav1.Condition{
@@ -259,7 +259,7 @@ func InferencePoolMustBeAcceptedByParent(t *testing.T, c client.Client, poolNN t
259259
// InferencePoolMustBeRouteAccepted waits for the specified InferencePool resource
260260
// to exist and report an Accepted condition with Type=RouteConditionAccepted,
261261
// Status=True, and Reason=RouteReasonAccepted within one of its parent statuses.
262-
func InferencePoolMustBeRouteAccepted(t *testing.T, c client.Client, poolNN types.NamespacedName) {
262+
func InferencePoolMustBeRouteAccepted(t *testing.T, c client.Reader, poolNN types.NamespacedName) {
263263
t.Helper()
264264

265265
expectedPoolCondition := metav1.Condition{
@@ -310,7 +310,7 @@ func GetGatewayEndpoint(t *testing.T, k8sClient client.Client, timeoutConfig gat
310310

311311
// GetPodsWithLabel retrieves a list of Pods.
312312
// It finds pods matching the given labels in a specific namespace.
313-
func GetPodsWithLabel(t *testing.T, c client.Client, namespace string, labels map[string]string, timeConfig gatewayapiconfig.TimeoutConfig) ([]corev1.Pod, error) {
313+
func GetPodsWithLabel(t *testing.T, c client.Reader, namespace string, labels map[string]string, timeConfig gatewayapiconfig.TimeoutConfig) ([]corev1.Pod, error) {
314314
t.Helper()
315315

316316
pods := &corev1.PodList{}

pkg/epp/controller/inferencemodel_reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
type InferenceModelReconciler struct {
37-
client.Client
37+
client.Reader
3838
Record record.EventRecorder
3939
Datastore datastore.Datastore
4040
PoolNamespacedName types.NamespacedName
@@ -88,7 +88,7 @@ func (c *InferenceModelReconciler) handleModelDeleted(ctx context.Context, req t
8888
logger.Info("InferenceModel removed from datastore", "poolRef", existing.Spec.PoolRef, "modelName", existing.Spec.ModelName)
8989

9090
// TODO(#409): replace this backfill logic with one that is based on InferenceModel Ready conditions once those are set by an external controller.
91-
updated, err := c.Datastore.ModelResync(ctx, c.Client, existing.Spec.ModelName)
91+
updated, err := c.Datastore.ModelResync(ctx, c.Reader, existing.Spec.ModelName)
9292
if err != nil {
9393
return err
9494
}

pkg/epp/controller/inferencemodel_reconciler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestInferenceModelReconciler(t *testing.T) {
201201
}
202202
_ = ds.PoolSet(context.Background(), fakeClient, pool)
203203
reconciler := &InferenceModelReconciler{
204-
Client: fakeClient,
204+
Reader: fakeClient,
205205
Record: record.NewFakeRecorder(10),
206206
Datastore: ds,
207207
PoolNamespacedName: types.NamespacedName{Name: pool.Name, Namespace: pool.Namespace},

pkg/epp/controller/inferencepool_reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// This implementation is just used for reading & maintaining data sync. The Gateway implementation
3434
// will have the proper controller that will create/manage objects on behalf of the server pool.
3535
type InferencePoolReconciler struct {
36-
client.Client
36+
client.Reader
3737
Record record.EventRecorder
3838
Datastore datastore.Datastore
3939
}
@@ -60,7 +60,7 @@ func (c *InferencePoolReconciler) Reconcile(ctx context.Context, req ctrl.Reques
6060
return ctrl.Result{}, nil
6161
}
6262
// update pool in datastore
63-
if err := c.Datastore.PoolSet(ctx, c.Client, infPool); err != nil {
63+
if err := c.Datastore.PoolSet(ctx, c.Reader, infPool); err != nil {
6464
logger.Error(err, "Failed to update datastore")
6565
return ctrl.Result{}, err
6666
}

pkg/epp/controller/inferencepool_reconciler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestInferencePoolReconciler(t *testing.T) {
9696

9797
pmf := backendmetrics.NewPodMetricsFactory(&backendmetrics.FakePodMetricsClient{}, time.Second)
9898
datastore := datastore.NewDatastore(ctx, pmf)
99-
inferencePoolReconciler := &InferencePoolReconciler{Client: fakeClient, Datastore: datastore}
99+
inferencePoolReconciler := &InferencePoolReconciler{Reader: fakeClient, Datastore: datastore}
100100

101101
// Step 1: Inception, only ready pods matching pool1 are added to the store.
102102
if _, err := inferencePoolReconciler.Reconcile(ctx, req); err != nil {

pkg/epp/datastore/datastore.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Datastore interface {
4848
// PoolSet sets the given pool in datastore. If the given pool has different label selector than the previous pool
4949
// that was stored, the function triggers a resync of the pods to keep the datastore updated. If the given pool
5050
// is nil, this call triggers the datastore.Clear() function.
51-
PoolSet(ctx context.Context, client client.Client, pool *v1alpha2.InferencePool) error
51+
PoolSet(ctx context.Context, reader client.Reader, pool *v1alpha2.InferencePool) error
5252
PoolGet() (*v1alpha2.InferencePool, error)
5353
PoolHasSynced() bool
5454
PoolLabelsMatch(podLabels map[string]string) bool
@@ -57,7 +57,7 @@ type Datastore interface {
5757
ModelSetIfOlder(infModel *v1alpha2.InferenceModel) bool
5858
ModelGet(modelName string) *v1alpha2.InferenceModel
5959
ModelDelete(namespacedName types.NamespacedName) *v1alpha2.InferenceModel
60-
ModelResync(ctx context.Context, ctrlClient client.Client, modelName string) (bool, error)
60+
ModelResync(ctx context.Context, reader client.Reader, modelName string) (bool, error)
6161
ModelGetAll() []*v1alpha2.InferenceModel
6262

6363
// PodMetrics operations
@@ -110,7 +110,7 @@ func (ds *datastore) Clear() {
110110
}
111111

112112
// /// InferencePool APIs ///
113-
func (ds *datastore) PoolSet(ctx context.Context, client client.Client, pool *v1alpha2.InferencePool) error {
113+
func (ds *datastore) PoolSet(ctx context.Context, reader client.Reader, pool *v1alpha2.InferencePool) error {
114114
if pool == nil {
115115
ds.Clear()
116116
return nil
@@ -129,7 +129,7 @@ func (ds *datastore) PoolSet(ctx context.Context, client client.Client, pool *v1
129129
// 2) If the selector on the pool was updated, then we will not get any pod events, and so we need
130130
// to resync the whole pool: remove pods in the store that don't match the new selector and add
131131
// the ones that may have existed already to the store.
132-
if err := ds.podResyncAll(ctx, client); err != nil {
132+
if err := ds.podResyncAll(ctx, reader); err != nil {
133133
return fmt.Errorf("failed to update pods according to the pool selector - %w", err)
134134
}
135135
}
@@ -182,12 +182,12 @@ func (ds *datastore) ModelSetIfOlder(infModel *v1alpha2.InferenceModel) bool {
182182
return true
183183
}
184184

185-
func (ds *datastore) ModelResync(ctx context.Context, c client.Client, modelName string) (bool, error) {
185+
func (ds *datastore) ModelResync(ctx context.Context, reader client.Reader, modelName string) (bool, error) {
186186
ds.poolAndModelsMu.Lock()
187187
defer ds.poolAndModelsMu.Unlock()
188188

189189
var models v1alpha2.InferenceModelList
190-
if err := c.List(ctx, &models, client.MatchingFields{ModelNameIndexKey: modelName}, client.InNamespace(ds.pool.Namespace)); err != nil {
190+
if err := reader.List(ctx, &models, client.MatchingFields{ModelNameIndexKey: modelName}, client.InNamespace(ds.pool.Namespace)); err != nil {
191191
return false, fmt.Errorf("listing models that match the modelName %s: %w", modelName, err)
192192
}
193193
if len(models.Items) == 0 {
@@ -288,10 +288,10 @@ func (ds *datastore) PodDelete(namespacedName types.NamespacedName) {
288288
}
289289
}
290290

291-
func (ds *datastore) podResyncAll(ctx context.Context, ctrlClient client.Client) error {
291+
func (ds *datastore) podResyncAll(ctx context.Context, reader client.Reader) error {
292292
logger := log.FromContext(ctx)
293293
podList := &corev1.PodList{}
294-
if err := ctrlClient.List(ctx, podList, &client.ListOptions{
294+
if err := reader.List(ctx, podList, &client.ListOptions{
295295
LabelSelector: selectorFromInferencePoolSelector(ds.pool.Spec.Selector),
296296
Namespace: ds.pool.Namespace,
297297
}); err != nil {

pkg/epp/server/runserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ func (r *ExtProcServerRunner) SetupWithManager(ctx context.Context, mgr ctrl.Man
9393
// Create the controllers and register them with the manager
9494
if err := (&controller.InferencePoolReconciler{
9595
Datastore: r.Datastore,
96-
Client: mgr.GetClient(),
96+
Reader: mgr.GetClient(),
9797
Record: mgr.GetEventRecorderFor("InferencePool"),
9898
}).SetupWithManager(mgr); err != nil {
9999
return fmt.Errorf("failed setting up InferencePoolReconciler: %w", err)
100100
}
101101

102102
if err := (&controller.InferenceModelReconciler{
103103
Datastore: r.Datastore,
104-
Client: mgr.GetClient(),
104+
Reader: mgr.GetClient(),
105105
PoolNamespacedName: r.PoolNamespacedName,
106106
Record: mgr.GetEventRecorderFor("InferenceModel"),
107107
}).SetupWithManager(ctx, mgr); err != nil {

0 commit comments

Comments
 (0)