|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.kubernetes.operator.utils.bluegreen; |
| 19 | + |
| 20 | +import org.apache.flink.kubernetes.operator.api.FlinkBlueGreenDeployment; |
| 21 | +import org.apache.flink.kubernetes.operator.api.FlinkDeployment; |
| 22 | +import org.apache.flink.kubernetes.operator.api.bluegreen.BlueGreenDeploymentType; |
| 23 | +import org.apache.flink.kubernetes.operator.api.spec.ConfigObjectNode; |
| 24 | +import org.apache.flink.kubernetes.operator.api.spec.FlinkBlueGreenDeploymentSpec; |
| 25 | +import org.apache.flink.kubernetes.operator.api.spec.FlinkDeploymentSpec; |
| 26 | +import org.apache.flink.kubernetes.operator.api.spec.FlinkDeploymentTemplateSpec; |
| 27 | +import org.apache.flink.kubernetes.operator.api.spec.JobSpec; |
| 28 | +import org.apache.flink.kubernetes.operator.api.spec.UpgradeMode; |
| 29 | +import org.apache.flink.kubernetes.operator.api.status.FlinkBlueGreenDeploymentStatus; |
| 30 | +import org.apache.flink.kubernetes.operator.api.utils.SpecUtils; |
| 31 | +import org.apache.flink.kubernetes.operator.controller.bluegreen.BlueGreenContext; |
| 32 | + |
| 33 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.UUID; |
| 39 | + |
| 40 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 41 | + |
| 42 | +/** Tests for {@link BlueGreenUtils}. */ |
| 43 | +public class BlueGreenUtilsTest { |
| 44 | + |
| 45 | + private static final String TEST_NAMESPACE = "test-namespace"; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testPrepareFlinkDeploymentWithoutNameReplacement() { |
| 49 | + String parentDeploymentName = "my-app"; |
| 50 | + FlinkBlueGreenDeployment bgDeployment = |
| 51 | + buildBlueGreenDeployment(parentDeploymentName, TEST_NAMESPACE); |
| 52 | + |
| 53 | + // Add configuration that contains the deployment name in values |
| 54 | + Map<String, String> flinkConfig = |
| 55 | + bgDeployment.getSpec().getTemplate().getSpec().getFlinkConfiguration().asFlatMap(); |
| 56 | + flinkConfig.put( |
| 57 | + "high-availability.storageDir", |
| 58 | + "s3://" + parentDeploymentName + "/highavailability"); |
| 59 | + flinkConfig.put("metrics.scope.jm", parentDeploymentName + ".jm"); |
| 60 | + bgDeployment.getSpec().getTemplate().getSpec().setFlinkConfiguration(flinkConfig); |
| 61 | + |
| 62 | + BlueGreenContext context = createContext(bgDeployment); |
| 63 | + |
| 64 | + // Test: Prepare a BLUE deployment |
| 65 | + FlinkDeployment blueDeployment = |
| 66 | + BlueGreenUtils.prepareFlinkDeployment( |
| 67 | + context, |
| 68 | + BlueGreenDeploymentType.BLUE, |
| 69 | + null, |
| 70 | + true, |
| 71 | + bgDeployment.getMetadata()); |
| 72 | + |
| 73 | + // Verify child deployment name is correctly set in metadata |
| 74 | + String expectedChildName = parentDeploymentName + "-blue"; |
| 75 | + assertEquals(expectedChildName, blueDeployment.getMetadata().getName()); |
| 76 | + |
| 77 | + // Verify configuration values that contain the parent name are NOT replaced |
| 78 | + Map<String, String> resultFlinkConfig = |
| 79 | + blueDeployment.getSpec().getFlinkConfiguration().asFlatMap(); |
| 80 | + assertEquals( |
| 81 | + "s3://" + parentDeploymentName + "/highavailability", |
| 82 | + resultFlinkConfig.get("high-availability.storageDir")); |
| 83 | + assertEquals(parentDeploymentName + ".jm", resultFlinkConfig.get("metrics.scope.jm")); |
| 84 | + } |
| 85 | + |
| 86 | + private static FlinkBlueGreenDeployment buildBlueGreenDeployment( |
| 87 | + String name, String namespace) { |
| 88 | + var deployment = new FlinkBlueGreenDeployment(); |
| 89 | + deployment.setMetadata( |
| 90 | + new ObjectMetaBuilder() |
| 91 | + .withName(name) |
| 92 | + .withNamespace(namespace) |
| 93 | + .withUid(UUID.randomUUID().toString()) |
| 94 | + .build()); |
| 95 | + |
| 96 | + var flinkDeploymentSpec = |
| 97 | + FlinkDeploymentSpec.builder() |
| 98 | + .flinkConfiguration(new ConfigObjectNode()) |
| 99 | + .job(JobSpec.builder().upgradeMode(UpgradeMode.STATELESS).build()) |
| 100 | + .build(); |
| 101 | + |
| 102 | + var bgDeploymentSpec = |
| 103 | + new FlinkBlueGreenDeploymentSpec( |
| 104 | + new HashMap<>(), |
| 105 | + FlinkDeploymentTemplateSpec.builder().spec(flinkDeploymentSpec).build()); |
| 106 | + |
| 107 | + deployment.setSpec(bgDeploymentSpec); |
| 108 | + return deployment; |
| 109 | + } |
| 110 | + |
| 111 | + private BlueGreenContext createContext(FlinkBlueGreenDeployment bgDeployment) { |
| 112 | + FlinkBlueGreenDeploymentStatus status = new FlinkBlueGreenDeploymentStatus(); |
| 113 | + status.setLastReconciledSpec(SpecUtils.writeSpecAsJSON(bgDeployment.getSpec(), "spec")); |
| 114 | + bgDeployment.setStatus(status); |
| 115 | + |
| 116 | + return new BlueGreenContext(bgDeployment, status, null, null, null); |
| 117 | + } |
| 118 | +} |
0 commit comments