|
6 | 6 |
|
7 | 7 | "github.com/google/go-cmp/cmp" |
8 | 8 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
9 | 10 | "k8s.io/utils/ptr" |
10 | 11 |
|
11 | 12 | fleetv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1" |
@@ -1189,3 +1190,150 @@ func TestIsDiffedResourcePlacementEqual(t *testing.T) { |
1189 | 1190 | }) |
1190 | 1191 | } |
1191 | 1192 | } |
| 1193 | + |
| 1194 | +func TestShouldPropagateObj_PodAndReplicaSet(t *testing.T) { |
| 1195 | + tests := []struct { |
| 1196 | + name string |
| 1197 | + obj map[string]interface{} |
| 1198 | + ownerReferences []metav1.OwnerReference |
| 1199 | + want bool |
| 1200 | + }{ |
| 1201 | + { |
| 1202 | + name: "standalone replicaset without ownerReferences should propagate", |
| 1203 | + obj: map[string]interface{}{ |
| 1204 | + "apiVersion": "apps/v1", |
| 1205 | + "kind": "ReplicaSet", |
| 1206 | + "metadata": map[string]interface{}{ |
| 1207 | + "name": "standalone-rs", |
| 1208 | + "namespace": "default", |
| 1209 | + }, |
| 1210 | + }, |
| 1211 | + ownerReferences: nil, |
| 1212 | + want: true, |
| 1213 | + }, |
| 1214 | + { |
| 1215 | + name: "standalone pod without ownerReferences should propagate", |
| 1216 | + obj: map[string]interface{}{ |
| 1217 | + "apiVersion": "v1", |
| 1218 | + "kind": "Pod", |
| 1219 | + "metadata": map[string]interface{}{ |
| 1220 | + "name": "standalone-pod", |
| 1221 | + "namespace": "default", |
| 1222 | + }, |
| 1223 | + }, |
| 1224 | + ownerReferences: nil, |
| 1225 | + want: true, |
| 1226 | + }, |
| 1227 | + { |
| 1228 | + name: "replicaset with deployment owner should NOT propagate", |
| 1229 | + obj: map[string]interface{}{ |
| 1230 | + "apiVersion": "apps/v1", |
| 1231 | + "kind": "ReplicaSet", |
| 1232 | + "metadata": map[string]interface{}{ |
| 1233 | + "name": "test-deploy-abc123", |
| 1234 | + "namespace": "default", |
| 1235 | + }, |
| 1236 | + }, |
| 1237 | + ownerReferences: []metav1.OwnerReference{ |
| 1238 | + { |
| 1239 | + APIVersion: "apps/v1", |
| 1240 | + Kind: "Deployment", |
| 1241 | + Name: "test-deploy", |
| 1242 | + UID: "12345", |
| 1243 | + }, |
| 1244 | + }, |
| 1245 | + want: false, |
| 1246 | + }, |
| 1247 | + { |
| 1248 | + name: "pod owned by replicaset - passes ShouldPropagateObj but filtered by resource config", |
| 1249 | + obj: map[string]interface{}{ |
| 1250 | + "apiVersion": "v1", |
| 1251 | + "kind": "Pod", |
| 1252 | + "metadata": map[string]interface{}{ |
| 1253 | + "name": "test-deploy-abc123-xyz", |
| 1254 | + "namespace": "default", |
| 1255 | + }, |
| 1256 | + }, |
| 1257 | + ownerReferences: []metav1.OwnerReference{ |
| 1258 | + { |
| 1259 | + APIVersion: "apps/v1", |
| 1260 | + Kind: "ReplicaSet", |
| 1261 | + Name: "test-deploy-abc123", |
| 1262 | + UID: "67890", |
| 1263 | + }, |
| 1264 | + }, |
| 1265 | + want: true, // ShouldPropagateObj doesn't filter Pods - they're filtered by NewResourceConfig |
| 1266 | + }, |
| 1267 | + { |
| 1268 | + name: "controllerrevision owned by daemonset should NOT propagate", |
| 1269 | + obj: map[string]interface{}{ |
| 1270 | + "apiVersion": "apps/v1", |
| 1271 | + "kind": "ControllerRevision", |
| 1272 | + "metadata": map[string]interface{}{ |
| 1273 | + "name": "test-ds-7b9848797f", |
| 1274 | + "namespace": "default", |
| 1275 | + }, |
| 1276 | + }, |
| 1277 | + ownerReferences: []metav1.OwnerReference{ |
| 1278 | + { |
| 1279 | + APIVersion: "apps/v1", |
| 1280 | + Kind: "DaemonSet", |
| 1281 | + Name: "test-ds", |
| 1282 | + UID: "abcdef", |
| 1283 | + }, |
| 1284 | + }, |
| 1285 | + want: false, |
| 1286 | + }, |
| 1287 | + { |
| 1288 | + name: "controllerrevision owned by statefulset should NOT propagate", |
| 1289 | + obj: map[string]interface{}{ |
| 1290 | + "apiVersion": "apps/v1", |
| 1291 | + "kind": "ControllerRevision", |
| 1292 | + "metadata": map[string]interface{}{ |
| 1293 | + "name": "test-ss-7878b4b446", |
| 1294 | + "namespace": "default", |
| 1295 | + }, |
| 1296 | + }, |
| 1297 | + ownerReferences: []metav1.OwnerReference{ |
| 1298 | + { |
| 1299 | + APIVersion: "apps/v1", |
| 1300 | + Kind: "StatefulSet", |
| 1301 | + Name: "test-ss", |
| 1302 | + UID: "fedcba", |
| 1303 | + }, |
| 1304 | + }, |
| 1305 | + want: false, |
| 1306 | + }, |
| 1307 | + { |
| 1308 | + name: "standalone controllerrevision without owner should propagate", |
| 1309 | + obj: map[string]interface{}{ |
| 1310 | + "apiVersion": "apps/v1", |
| 1311 | + "kind": "ControllerRevision", |
| 1312 | + "metadata": map[string]interface{}{ |
| 1313 | + "name": "custom-revision", |
| 1314 | + "namespace": "default", |
| 1315 | + }, |
| 1316 | + }, |
| 1317 | + ownerReferences: nil, |
| 1318 | + want: true, |
| 1319 | + }, |
| 1320 | + } |
| 1321 | + |
| 1322 | + for _, tt := range tests { |
| 1323 | + t.Run(tt.name, func(t *testing.T) { |
| 1324 | + uObj := &unstructured.Unstructured{Object: tt.obj} |
| 1325 | + if tt.ownerReferences != nil { |
| 1326 | + uObj.SetOwnerReferences(tt.ownerReferences) |
| 1327 | + } |
| 1328 | + |
| 1329 | + got, err := ShouldPropagateObj(nil, uObj) |
| 1330 | + if err != nil { |
| 1331 | + t.Errorf("ShouldPropagateObj() error = %v", err) |
| 1332 | + return |
| 1333 | + } |
| 1334 | + if got != tt.want { |
| 1335 | + t.Errorf("ShouldPropagateObj() = %v, want %v", got, tt.want) |
| 1336 | + } |
| 1337 | + }) |
| 1338 | + } |
| 1339 | +} |
0 commit comments