Skip to content

Commit a7c70e5

Browse files
authored
feat(kustomizer): add selective manifest resource reconciliation (#101)
Implements selective manifest resource reconciliation using a 3-step process: render → filter → apply. Adds `FilterExcludeKinds()` function to the deploy package that uses exclusion-based filtering for more intuitive resource management. Updates the controller to use centralized `reconcileManifestResources()` with exclude-based logic instead of separate render/apply cycles for each resource type. Approved-by: VaishnaviHire
1 parent 987937e commit a7c70e5

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed

controllers/llamastackdistribution_controller.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,50 @@ func (r *LlamaStackDistributionReconciler) fetchInstance(ctx context.Context, na
181181
return instance, nil
182182
}
183183

184+
// determineKindsToExclude returns a list of resource kinds that should be excluded
185+
// based on the instance specification.
186+
func (r *LlamaStackDistributionReconciler) determineKindsToExclude(instance *llamav1alpha1.LlamaStackDistribution) []string {
187+
var kinds []string
188+
189+
// Exclude PersistentVolumeClaim if storage is not configured
190+
if instance.Spec.Server.Storage == nil {
191+
kinds = append(kinds, "PersistentVolumeClaim")
192+
}
193+
194+
// Exclude NetworkPolicy if the feature is disabled
195+
if !r.EnableNetworkPolicy {
196+
kinds = append(kinds, "NetworkPolicy")
197+
}
198+
199+
// Exclude Service if no ports are defined
200+
if !instance.HasPorts() {
201+
kinds = append(kinds, "Service")
202+
}
203+
204+
return kinds
205+
}
206+
207+
// reconcileManifestResources applies resources that are managed by the operator
208+
// based on the instance specification.
209+
func (r *LlamaStackDistributionReconciler) reconcileManifestResources(ctx context.Context, instance *llamav1alpha1.LlamaStackDistribution) error {
210+
resMap, err := deploy.RenderManifest(filesys.MakeFsOnDisk(), manifestsBasePath, instance)
211+
if err != nil {
212+
return fmt.Errorf("failed to render manifests: %w", err)
213+
}
214+
215+
kindsToExclude := r.determineKindsToExclude(instance)
216+
filteredResMap, err := deploy.FilterExcludeKinds(resMap, kindsToExclude)
217+
if err != nil {
218+
return fmt.Errorf("failed to filter manifests: %w", err)
219+
}
220+
221+
if err := deploy.ApplyResources(ctx, r.Client, r.Scheme, instance, filteredResMap); err != nil {
222+
return fmt.Errorf("failed to apply manifests: %w", err)
223+
}
224+
225+
return nil
226+
}
227+
184228
// reconcileResources reconciles all resources for the LlamaStackDistribution instance.
185229
func (r *LlamaStackDistributionReconciler) reconcileResources(ctx context.Context, instance *llamav1alpha1.LlamaStackDistribution) error {
186230
// Reconcile the ConfigMap if specified by the user
@@ -190,17 +234,6 @@ func (r *LlamaStackDistributionReconciler) reconcileResources(ctx context.Contex
190234
}
191235
}
192236

193-
// Reconcile the PVC if storage is configured
194-
if instance.Spec.Server.Storage != nil {
195-
resMap, err := deploy.RenderManifest(filesys.MakeFsOnDisk(), manifestsBasePath, instance)
196-
if err != nil {
197-
return fmt.Errorf("failed to render PVC manifests: %w", err)
198-
}
199-
if err := deploy.ApplyResources(ctx, r.Client, r.Scheme, instance, resMap); err != nil {
200-
return fmt.Errorf("failed to apply PVC manifests: %w", err)
201-
}
202-
}
203-
204237
// Reconcile the NetworkPolicy
205238
if err := r.reconcileNetworkPolicy(ctx, instance); err != nil {
206239
return fmt.Errorf("failed to reconcile NetworkPolicy: %w", err)
@@ -217,6 +250,12 @@ func (r *LlamaStackDistributionReconciler) reconcileResources(ctx context.Contex
217250
return fmt.Errorf("failed to reconcile service: %w", err)
218251
}
219252
}
253+
254+
// Reconcile manifest-based resources
255+
if err := r.reconcileManifestResources(ctx, instance); err != nil {
256+
return err
257+
}
258+
220259
return nil
221260
}
222261

pkg/deploy/kustomizer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"path/filepath"
8+
"slices"
89

910
llamav1alpha1 "github.com/llamastack/llama-stack-k8s-operator/api/v1alpha1"
1011
"github.com/llamastack/llama-stack-k8s-operator/pkg/deploy/plugins"
@@ -220,3 +221,15 @@ func getStorageSize(instance *llamav1alpha1.LlamaStackDistribution) string {
220221
// Returning an empty string signals the field transformer to use the default value.
221222
return ""
222223
}
224+
225+
func FilterExcludeKinds(resMap *resmap.ResMap, kindsToExclude []string) (*resmap.ResMap, error) {
226+
filteredResMap := resmap.New()
227+
for _, res := range (*resMap).Resources() {
228+
if !slices.Contains(kindsToExclude, res.GetKind()) {
229+
if err := filteredResMap.Append(res); err != nil {
230+
return nil, fmt.Errorf("failed to append resource while filtering %s/%s: %w", res.GetKind(), res.GetName(), err)
231+
}
232+
}
233+
}
234+
return &filteredResMap, nil
235+
}

pkg/deploy/kustomizer_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,55 @@ func TestApplyResources_PVCImmutability(t *testing.T) {
478478
storageRequest := unchangedPVC.Spec.Resources.Requests[corev1.ResourceStorage]
479479
require.Equal(t, expStorageSize, storageRequest.String(), "PVC storage spec should remain unchanged")
480480
}
481+
482+
// TestFilterExcludeKinds tests the filtering functionality.
483+
func TestFilterExcludeKinds(t *testing.T) {
484+
t.Run("excludes specified kinds", func(t *testing.T) {
485+
pvc := newTestResource(t, "v1", "PersistentVolumeClaim", "test-pvc", "test-ns", nil)
486+
svc := newTestResource(t, "v1", "Service", "test-svc", "test-ns", nil)
487+
488+
resMap := resmap.New()
489+
require.NoError(t, resMap.Append(pvc))
490+
require.NoError(t, resMap.Append(svc))
491+
492+
filtered, err := FilterExcludeKinds(&resMap, []string{"PersistentVolumeClaim"})
493+
require.NoError(t, err)
494+
require.Equal(t, 1, (*filtered).Size())
495+
require.Equal(t, "Service", (*filtered).Resources()[0].GetKind())
496+
})
497+
498+
t.Run("includes all when no exclusions", func(t *testing.T) {
499+
svc := newTestResource(t, "v1", "Service", "test-svc", "test-ns", nil)
500+
resMap := resmap.New()
501+
require.NoError(t, resMap.Append(svc))
502+
503+
filtered, err := FilterExcludeKinds(&resMap, []string{})
504+
require.NoError(t, err)
505+
require.Equal(t, 1, (*filtered).Size())
506+
require.Equal(t, "Service", (*filtered).Resources()[0].GetKind())
507+
})
508+
509+
t.Run("handles empty inputs", func(t *testing.T) {
510+
emptyResMap := resmap.New()
511+
512+
filtered, err := FilterExcludeKinds(&emptyResMap, []string{"PersistentVolumeClaim"})
513+
require.NoError(t, err)
514+
require.Equal(t, 0, (*filtered).Size())
515+
})
516+
517+
t.Run("excludes multiple kinds", func(t *testing.T) {
518+
pvc := newTestResource(t, "v1", "PersistentVolumeClaim", "test-pvc", "test-ns", nil)
519+
svc := newTestResource(t, "v1", "Service", "test-svc", "test-ns", nil)
520+
deployment := newTestResource(t, "apps/v1", "Deployment", "test-deployment", "test-ns", nil)
521+
522+
resMap := resmap.New()
523+
require.NoError(t, resMap.Append(pvc))
524+
require.NoError(t, resMap.Append(svc))
525+
require.NoError(t, resMap.Append(deployment))
526+
527+
filtered, err := FilterExcludeKinds(&resMap, []string{"PersistentVolumeClaim", "Service"})
528+
require.NoError(t, err)
529+
require.Equal(t, 1, (*filtered).Size())
530+
require.Equal(t, "Deployment", (*filtered).Resources()[0].GetKind())
531+
})
532+
}

0 commit comments

Comments
 (0)