|
| 1 | +/* |
| 2 | +Copyright 2024 Feast Community. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + |
| 24 | + "github.com/feast-dev/feast/infra/feast-operator/internal/controller/handler" |
| 25 | + "github.com/feast-dev/feast/infra/feast-operator/internal/controller/services" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 27 | + |
| 28 | + feastdevv1alpha1 "github.com/feast-dev/feast/infra/feast-operator/api/v1alpha1" |
| 29 | + . "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + appsv1 "k8s.io/api/apps/v1" |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | + "k8s.io/apimachinery/pkg/api/errors" |
| 34 | + "k8s.io/apimachinery/pkg/types" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = Describe("FeatureStore Controller - Deployment Volumes and VolumeMounts", func() { |
| 38 | + Context("When deploying featureStore Spec we should have an option do Volumes and VolumeMounts", func() { |
| 39 | + const resourceName = "services-ephemeral" |
| 40 | + const offlineType = "duckdb" |
| 41 | + var pullPolicy = corev1.PullAlways |
| 42 | + |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + typeNamespacedName := types.NamespacedName{ |
| 46 | + Name: resourceName, |
| 47 | + Namespace: "default", |
| 48 | + } |
| 49 | + featurestore := &feastdevv1alpha1.FeatureStore{} |
| 50 | + onlineStorePath := "/data/online.db" |
| 51 | + registryPath := "/data/registry.db" |
| 52 | + |
| 53 | + BeforeEach(func() { |
| 54 | + By("creating the custom resource for the Kind FeatureStore") |
| 55 | + err := k8sClient.Get(ctx, typeNamespacedName, featurestore) |
| 56 | + if err != nil && errors.IsNotFound(err) { |
| 57 | + resource := createFeatureStoreVolumeResource(resourceName, image, pullPolicy) |
| 58 | + resource.Spec.Services.OfflineStore.Persistence = &feastdevv1alpha1.OfflineStorePersistence{ |
| 59 | + FilePersistence: &feastdevv1alpha1.OfflineStoreFilePersistence{ |
| 60 | + Type: offlineType, |
| 61 | + }, |
| 62 | + } |
| 63 | + resource.Spec.Services.OnlineStore.Persistence = &feastdevv1alpha1.OnlineStorePersistence{ |
| 64 | + FilePersistence: &feastdevv1alpha1.OnlineStoreFilePersistence{ |
| 65 | + Path: onlineStorePath, |
| 66 | + }, |
| 67 | + } |
| 68 | + resource.Spec.Services.Registry = &feastdevv1alpha1.Registry{ |
| 69 | + Local: &feastdevv1alpha1.LocalRegistryConfig{ |
| 70 | + Persistence: &feastdevv1alpha1.RegistryPersistence{ |
| 71 | + FilePersistence: &feastdevv1alpha1.RegistryFilePersistence{ |
| 72 | + Path: registryPath, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + Expect(k8sClient.Create(ctx, resource)).To(Succeed()) |
| 78 | + } |
| 79 | + }) |
| 80 | + AfterEach(func() { |
| 81 | + resource := &feastdevv1alpha1.FeatureStore{} |
| 82 | + err := k8sClient.Get(ctx, typeNamespacedName, resource) |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + |
| 85 | + By("Cleanup the specific resource instance FeatureStore") |
| 86 | + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) |
| 87 | + |
| 88 | + }) |
| 89 | + |
| 90 | + It("should successfully reconcile the resource and volumes and volumeMounts should be available", func() { |
| 91 | + By("Reconciling the created resource") |
| 92 | + controllerReconciler := &FeatureStoreReconciler{ |
| 93 | + Client: k8sClient, |
| 94 | + Scheme: k8sClient.Scheme(), |
| 95 | + } |
| 96 | + |
| 97 | + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ |
| 98 | + NamespacedName: typeNamespacedName, |
| 99 | + }) |
| 100 | + Expect(err).NotTo(HaveOccurred()) |
| 101 | + |
| 102 | + resource := &feastdevv1alpha1.FeatureStore{} |
| 103 | + err = k8sClient.Get(ctx, typeNamespacedName, resource) |
| 104 | + Expect(err).NotTo(HaveOccurred()) |
| 105 | + |
| 106 | + feast := services.FeastServices{ |
| 107 | + Handler: handler.FeastHandler{ |
| 108 | + Client: controllerReconciler.Client, |
| 109 | + Context: ctx, |
| 110 | + Scheme: controllerReconciler.Scheme, |
| 111 | + FeatureStore: resource, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + deploy := &appsv1.Deployment{} |
| 116 | + objMeta := feast.GetObjectMeta() |
| 117 | + err = k8sClient.Get(ctx, types.NamespacedName{ |
| 118 | + Name: objMeta.Name, |
| 119 | + Namespace: objMeta.Namespace, |
| 120 | + }, deploy) |
| 121 | + |
| 122 | + Expect(err).NotTo(HaveOccurred()) |
| 123 | + |
| 124 | + // Extract the PodSpec from DeploymentSpec |
| 125 | + podSpec := deploy.Spec.Template.Spec |
| 126 | + |
| 127 | + // Validate Volumes |
| 128 | + // Validate Volumes - Check if our test volume exists among multiple |
| 129 | + Expect(podSpec.Volumes).To(ContainElement(WithTransform(func(v corev1.Volume) string { |
| 130 | + return v.Name |
| 131 | + }, Equal("test-volume"))), "Expected volume 'test-volume' to be present") |
| 132 | + |
| 133 | + // Ensure 'online' container has the test volume mount |
| 134 | + var onlineContainer *corev1.Container |
| 135 | + for i, container := range podSpec.Containers { |
| 136 | + if container.Name == "online" { |
| 137 | + onlineContainer = &podSpec.Containers[i] |
| 138 | + break |
| 139 | + } |
| 140 | + } |
| 141 | + Expect(onlineContainer).ToNot(BeNil(), "Expected to find container 'online'") |
| 142 | + |
| 143 | + // Validate that 'online' container has the test-volume mount |
| 144 | + Expect(onlineContainer.VolumeMounts).To(ContainElement(WithTransform(func(vm corev1.VolumeMount) string { |
| 145 | + return vm.Name |
| 146 | + }, Equal("test-volume"))), "Expected 'online' container to have volume mount 'test-volume'") |
| 147 | + |
| 148 | + // Ensure all other containers do NOT have the test volume mount |
| 149 | + for _, container := range podSpec.Containers { |
| 150 | + if container.Name != "online" { |
| 151 | + Expect(container.VolumeMounts).ToNot(ContainElement(WithTransform(func(vm corev1.VolumeMount) string { |
| 152 | + return vm.Name |
| 153 | + }, Equal("test-volume"))), "Unexpected volume mount 'test-volume' found in container "+container.Name) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + }) |
| 158 | + }) |
| 159 | +}) |
| 160 | + |
| 161 | +func createFeatureStoreVolumeResource(resourceName string, image string, pullPolicy corev1.PullPolicy) *feastdevv1alpha1.FeatureStore { |
| 162 | + volume := corev1.Volume{ |
| 163 | + Name: "test-volume", |
| 164 | + VolumeSource: corev1.VolumeSource{ |
| 165 | + EmptyDir: &corev1.EmptyDirVolumeSource{}, |
| 166 | + }, |
| 167 | + } |
| 168 | + volumeMount := corev1.VolumeMount{ |
| 169 | + Name: "test-volume", |
| 170 | + MountPath: "/data", |
| 171 | + } |
| 172 | + |
| 173 | + return &feastdevv1alpha1.FeatureStore{ |
| 174 | + ObjectMeta: metav1.ObjectMeta{ |
| 175 | + Name: resourceName, |
| 176 | + Namespace: "default", |
| 177 | + }, |
| 178 | + Spec: feastdevv1alpha1.FeatureStoreSpec{ |
| 179 | + FeastProject: feastProject, |
| 180 | + Services: &feastdevv1alpha1.FeatureStoreServices{ |
| 181 | + Volumes: []corev1.Volume{volume}, |
| 182 | + OfflineStore: &feastdevv1alpha1.OfflineStore{ |
| 183 | + ServerConfigs: feastdevv1alpha1.ServerConfigs{ |
| 184 | + ContainerConfigs: feastdevv1alpha1.ContainerConfigs{}, |
| 185 | + }, |
| 186 | + }, |
| 187 | + OnlineStore: &feastdevv1alpha1.OnlineStore{ |
| 188 | + ServerConfigs: feastdevv1alpha1.ServerConfigs{ |
| 189 | + VolumeMounts: []corev1.VolumeMount{volumeMount}, |
| 190 | + ContainerConfigs: feastdevv1alpha1.ContainerConfigs{ |
| 191 | + DefaultCtrConfigs: feastdevv1alpha1.DefaultCtrConfigs{ |
| 192 | + Image: &image, |
| 193 | + }, |
| 194 | + OptionalCtrConfigs: feastdevv1alpha1.OptionalCtrConfigs{ |
| 195 | + ImagePullPolicy: &pullPolicy, |
| 196 | + Resources: &corev1.ResourceRequirements{}, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + UI: &feastdevv1alpha1.ServerConfigs{ |
| 202 | + ContainerConfigs: feastdevv1alpha1.ContainerConfigs{ |
| 203 | + DefaultCtrConfigs: feastdevv1alpha1.DefaultCtrConfigs{ |
| 204 | + Image: &image, |
| 205 | + }, |
| 206 | + OptionalCtrConfigs: feastdevv1alpha1.OptionalCtrConfigs{ |
| 207 | + ImagePullPolicy: &pullPolicy, |
| 208 | + Resources: &corev1.ResourceRequirements{}, |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + } |
| 215 | +} |
0 commit comments