|
| 1 | +/* |
| 2 | +Copyright 2026 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 services |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + feastdevv1 "github.com/feast-dev/feast/infra/feast-operator/api/v1" |
| 25 | + "github.com/feast-dev/feast/infra/feast-operator/internal/controller/handler" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + migrationTestOperatorNamespace = "ml-platform" |
| 35 | + migrationTestStoreNamespace = "team-a" |
| 36 | + migrationTestStoreName = "fraud" |
| 37 | + migrationTestClientConfig = "feast-fraud-client" |
| 38 | +) |
| 39 | + |
| 40 | +func newDiscoveryMigrationServices(t *testing.T, featureStore *feastdevv1.FeatureStore, objects ...client.Object) (*FeastServices, client.Client) { |
| 41 | + t.Helper() |
| 42 | + t.Setenv("FEAST_OPERATOR_NAMESPACE", migrationTestOperatorNamespace) |
| 43 | + t.Setenv("POD_NAMESPACE", "") |
| 44 | + |
| 45 | + previousIsOpenShift := isOpenShift |
| 46 | + isOpenShift = false |
| 47 | + t.Cleanup(func() { |
| 48 | + isOpenShift = previousIsOpenShift |
| 49 | + }) |
| 50 | + |
| 51 | + scheme := runtime.NewScheme() |
| 52 | + if err := corev1.AddToScheme(scheme); err != nil { |
| 53 | + t.Fatalf("add core scheme: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build() |
| 57 | + feast := &FeastServices{ |
| 58 | + Handler: handler.FeastHandler{ |
| 59 | + Client: k8sClient, |
| 60 | + Context: context.Background(), |
| 61 | + FeatureStore: featureStore, |
| 62 | + }, |
| 63 | + } |
| 64 | + return feast, k8sClient |
| 65 | +} |
| 66 | + |
| 67 | +func namespaceRegistryConfigMap(t *testing.T, namespace string, data NamespaceRegistryData) *corev1.ConfigMap { |
| 68 | + t.Helper() |
| 69 | + encoded, err := json.Marshal(data) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("marshal namespace registry data: %v", err) |
| 72 | + } |
| 73 | + return &corev1.ConfigMap{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: NamespaceRegistryConfigMapName, |
| 76 | + Namespace: namespace, |
| 77 | + }, |
| 78 | + Data: map[string]string{NamespaceRegistryDataKey: string(encoded)}, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func openLineageConfigMap(t *testing.T, namespace string, data OpenLineageDiscoveryData) *corev1.ConfigMap { |
| 83 | + t.Helper() |
| 84 | + encoded, err := json.Marshal(data) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("marshal OpenLineage discovery data: %v", err) |
| 87 | + } |
| 88 | + return &corev1.ConfigMap{ |
| 89 | + ObjectMeta: metav1.ObjectMeta{ |
| 90 | + Name: OpenLineageDiscoveryConfigMapName, |
| 91 | + Namespace: namespace, |
| 92 | + }, |
| 93 | + Data: map[string]string{OpenLineageDiscoveryEndpointsKey: string(encoded)}, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func readNamespaceRegistry(t *testing.T, k8sClient client.Client, namespace string) NamespaceRegistryData { |
| 98 | + t.Helper() |
| 99 | + cm := &corev1.ConfigMap{} |
| 100 | + if err := k8sClient.Get(context.Background(), client.ObjectKey{ |
| 101 | + Name: NamespaceRegistryConfigMapName, |
| 102 | + Namespace: namespace, |
| 103 | + }, cm); err != nil { |
| 104 | + t.Fatalf("get namespace registry from %s: %v", namespace, err) |
| 105 | + } |
| 106 | + |
| 107 | + var data NamespaceRegistryData |
| 108 | + if err := json.Unmarshal([]byte(cm.Data[NamespaceRegistryDataKey]), &data); err != nil { |
| 109 | + t.Fatalf("unmarshal namespace registry from %s: %v", namespace, err) |
| 110 | + } |
| 111 | + return data |
| 112 | +} |
| 113 | + |
| 114 | +func readOpenLineageDiscovery(t *testing.T, k8sClient client.Client, namespace string) OpenLineageDiscoveryData { |
| 115 | + t.Helper() |
| 116 | + cm := &corev1.ConfigMap{} |
| 117 | + if err := k8sClient.Get(context.Background(), client.ObjectKey{ |
| 118 | + Name: OpenLineageDiscoveryConfigMapName, |
| 119 | + Namespace: namespace, |
| 120 | + }, cm); err != nil { |
| 121 | + t.Fatalf("get OpenLineage discovery from %s: %v", namespace, err) |
| 122 | + } |
| 123 | + |
| 124 | + var data OpenLineageDiscoveryData |
| 125 | + if err := json.Unmarshal([]byte(cm.Data[OpenLineageDiscoveryEndpointsKey]), &data); err != nil { |
| 126 | + t.Fatalf("unmarshal OpenLineage discovery from %s: %v", namespace, err) |
| 127 | + } |
| 128 | + return data |
| 129 | +} |
| 130 | + |
| 131 | +func TestNamespaceRegistryKeepsLegacyConfigMapCurrent(t *testing.T) { |
| 132 | + featureStore := &feastdevv1.FeatureStore{ |
| 133 | + ObjectMeta: metav1.ObjectMeta{ |
| 134 | + Name: migrationTestStoreName, |
| 135 | + Namespace: migrationTestStoreNamespace, |
| 136 | + }, |
| 137 | + Status: feastdevv1.FeatureStoreStatus{ |
| 138 | + ClientConfigMap: migrationTestClientConfig, |
| 139 | + }, |
| 140 | + } |
| 141 | + currentConfigMap := namespaceRegistryConfigMap(t, migrationTestOperatorNamespace, NamespaceRegistryData{ |
| 142 | + Namespaces: map[string][]string{"existing": {"current-client"}}, |
| 143 | + }) |
| 144 | + legacyConfigMap := namespaceRegistryConfigMap(t, DefaultKubernetesNamespace, NamespaceRegistryData{ |
| 145 | + Namespaces: map[string][]string{ |
| 146 | + "existing": {"legacy-client"}, |
| 147 | + migrationTestStoreNamespace: {migrationTestClientConfig}, |
| 148 | + }, |
| 149 | + }) |
| 150 | + feast, k8sClient := newDiscoveryMigrationServices(t, featureStore, currentConfigMap, legacyConfigMap) |
| 151 | + |
| 152 | + if err := feast.AddToNamespaceRegistry(); err != nil { |
| 153 | + t.Fatalf("add FeatureStore to namespace registry: %v", err) |
| 154 | + } |
| 155 | + for _, namespace := range []string{migrationTestOperatorNamespace, DefaultKubernetesNamespace} { |
| 156 | + data := readNamespaceRegistry(t, k8sClient, namespace) |
| 157 | + if got := data.Namespaces[migrationTestStoreNamespace]; len(got) != 1 || got[0] != migrationTestClientConfig { |
| 158 | + t.Errorf("registry in %s has %v, want the new client config", namespace, got) |
| 159 | + } |
| 160 | + if got := data.Namespaces["existing"]; len(got) != 1 { |
| 161 | + t.Errorf("registry in %s lost existing entries: %v", namespace, got) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if err := feast.RemoveFromNamespaceRegistry(); err != nil { |
| 166 | + t.Fatalf("remove FeatureStore from namespace registry: %v", err) |
| 167 | + } |
| 168 | + for _, namespace := range []string{migrationTestOperatorNamespace, DefaultKubernetesNamespace} { |
| 169 | + data := readNamespaceRegistry(t, k8sClient, namespace) |
| 170 | + if _, found := data.Namespaces[migrationTestStoreNamespace]; found { |
| 171 | + t.Errorf("registry in %s still contains the deleted FeatureStore", namespace) |
| 172 | + } |
| 173 | + if got := data.Namespaces["existing"]; len(got) != 1 { |
| 174 | + t.Errorf("registry in %s lost unrelated entries: %v", namespace, got) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestOpenLineageDiscoveryKeepsLegacyConfigMapCurrent(t *testing.T) { |
| 180 | + featureStore := &feastdevv1.FeatureStore{ |
| 181 | + ObjectMeta: metav1.ObjectMeta{ |
| 182 | + Name: migrationTestStoreName, |
| 183 | + Namespace: migrationTestStoreNamespace, |
| 184 | + }, |
| 185 | + Status: feastdevv1.FeatureStoreStatus{ |
| 186 | + Applied: feastdevv1.FeatureStoreSpec{ |
| 187 | + OpenLineage: &feastdevv1.OpenLineageConfig{ |
| 188 | + Consumer: &feastdevv1.OpenLineageConsumerConfig{ |
| 189 | + Enabled: true, |
| 190 | + LineageServer: &feastdevv1.LineageServerConfig{}, |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + } |
| 196 | + otherKey := "team-b/other" |
| 197 | + otherConsumer := OpenLineageEndpointInfo{ |
| 198 | + URL: "http://other.svc:6580", |
| 199 | + Endpoint: "api/v1/lineage", |
| 200 | + } |
| 201 | + storeKey := migrationTestStoreNamespace + "/" + migrationTestStoreName |
| 202 | + initialData := OpenLineageDiscoveryData{ |
| 203 | + Consumers: map[string]OpenLineageEndpointInfo{ |
| 204 | + otherKey: otherConsumer, |
| 205 | + storeKey: { |
| 206 | + URL: "http://old-fraud.svc:6580", |
| 207 | + Endpoint: "api/v1/lineage", |
| 208 | + }, |
| 209 | + }, |
| 210 | + } |
| 211 | + currentConfigMap := openLineageConfigMap(t, migrationTestOperatorNamespace, initialData) |
| 212 | + legacyConfigMap := openLineageConfigMap(t, DefaultKubernetesNamespace, initialData) |
| 213 | + feast, k8sClient := newDiscoveryMigrationServices(t, featureStore, currentConfigMap, legacyConfigMap) |
| 214 | + |
| 215 | + if err := feast.AddToOpenLineageDiscovery(); err != nil { |
| 216 | + t.Fatalf("add FeatureStore to OpenLineage discovery: %v", err) |
| 217 | + } |
| 218 | + for _, namespace := range []string{migrationTestOperatorNamespace, DefaultKubernetesNamespace} { |
| 219 | + data := readOpenLineageDiscovery(t, k8sClient, namespace) |
| 220 | + if got := data.Consumers[storeKey]; got.URL == "" || got.Endpoint != "api/v1/lineage" { |
| 221 | + t.Errorf("OpenLineage discovery in %s has no updated entry for %s: %+v", namespace, storeKey, got) |
| 222 | + } |
| 223 | + if got := data.Consumers[otherKey]; got != otherConsumer { |
| 224 | + t.Errorf("OpenLineage discovery in %s changed unrelated consumer: %+v", namespace, got) |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if err := feast.RemoveFromOpenLineageDiscovery(); err != nil { |
| 229 | + t.Fatalf("remove FeatureStore from OpenLineage discovery: %v", err) |
| 230 | + } |
| 231 | + for _, namespace := range []string{migrationTestOperatorNamespace, DefaultKubernetesNamespace} { |
| 232 | + data := readOpenLineageDiscovery(t, k8sClient, namespace) |
| 233 | + if _, found := data.Consumers[storeKey]; found { |
| 234 | + t.Errorf("OpenLineage discovery in %s still contains the deleted FeatureStore", namespace) |
| 235 | + } |
| 236 | + if got := data.Consumers[otherKey]; got != otherConsumer { |
| 237 | + t.Errorf("OpenLineage discovery in %s lost unrelated consumers: %+v", namespace, got) |
| 238 | + } |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +func TestDiscoveryConfigMapNamespacesSkipsAbsentLegacyMap(t *testing.T) { |
| 243 | + feast, _ := newDiscoveryMigrationServices(t, &feastdevv1.FeatureStore{}) |
| 244 | + namespaces, err := feast.discoveryConfigMapNamespaces(migrationTestOperatorNamespace, NamespaceRegistryConfigMapName) |
| 245 | + if err != nil { |
| 246 | + t.Fatalf("get discovery namespaces: %v", err) |
| 247 | + } |
| 248 | + if len(namespaces) != 1 || namespaces[0] != migrationTestOperatorNamespace { |
| 249 | + t.Fatalf("got namespaces %v, want only %q", namespaces, migrationTestOperatorNamespace) |
| 250 | + } |
| 251 | +} |
0 commit comments