Skip to content

Commit

Permalink
Fix #756: Service re-apply error happening during k8s:watch
Browse files Browse the repository at this point in the history
Append ClusterIP fetched from Service from Kubernetes API to the
Service loaded from resource manifests in order to avoid patch
error

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 2, 2021
1 parent 798aec3 commit 00c878f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Usage:
* Fix #701: Update Fabric8 Kubernetes Client to 5.4.0
* Fix #425: Multi-layer support for Container Images
* Fix #751: QuarkusGenerator: Multi-layer images for the different Quarkus packaging modes
* Fix #756: Service re-apply error happening during `k8s:watch`

### 1.3.0 (2021-05-18)
* Fix #497: Assembly descriptor removed but still in documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ public class ApplyService {
private static final Set<String> projectsCreated = new HashSet<>();

public ApplyService(KubernetesClient kubernetesClient, KitLogger log) {
this(kubernetesClient, new PatchService(kubernetesClient, log), log);
}

ApplyService(KubernetesClient kubernetesClient, PatchService patchService, KitLogger log) {
this.kubernetesClient = kubernetesClient;
this.patchService = new PatchService(kubernetesClient, log);
this.patchService = patchService;
this.log = log;
}

Expand Down Expand Up @@ -865,6 +869,9 @@ public void applyService(Service service, String sourceName) {
kubernetesClient.services().inNamespace(currentNamespace).withName(id).delete();
doCreateService(service, currentNamespace, sourceName);
} else {
if (service.getSpec().getClusterIP() == null) {
service.getSpec().setClusterIP(old.getSpec().getClusterIP());
}
doPatchEntity(old, service, currentNamespace, sourceName);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.config.service;

import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Verifications;
import org.eclipse.jkube.kit.common.KitLogger;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.Objects;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class ApplyServiceWithoutMockServerTest {
private ApplyService applyService;

@Mocked
private KubernetesClient client;

@Mocked
private KitLogger log;

@Mocked
private PatchService patchService;

@Before
public void setUp() {
patchService = new PatchService(client, log);
applyService = new ApplyService(client, patchService, log);
applyService.setNamespace("default");
}

@Test
public void testServiceRedeploy() {
Service serviceManifest = createServiceBuilder()
.editSpec().withType("ClusterIP").endSpec()
.build();
Service serviceFromServer = createServiceBuilder()
.editSpec().withClusterIP("12.23.43.12").endSpec()
.build();
new Expectations() {{
client.services().inNamespace("default").withName("foo-svc").get();
result = serviceFromServer;
}};

applyService.applyService(serviceManifest, "test");

new Verifications() {{
Service finalMergedSvc = null;
patchService.compareAndPatchEntity("default", finalMergedSvc = withCapture(), serviceFromServer);

assertThat(finalMergedSvc).isNotNull()
.hasFieldOrPropertyWithValue("metadata.name", "foo-svc")
.hasFieldOrPropertyWithValue("spec.clusterIP", "12.23.43.12")
.hasFieldOrPropertyWithValue("spec.type", "ClusterIP");
}};
}

private ServiceBuilder createServiceBuilder() {
return new ServiceBuilder()
.withNewMetadata().withName("foo-svc").endMetadata()
.withNewSpec()
.addToSelector(Collections.singletonMap("foo", "bar"))
.addNewPort()
.withProtocol("TCP")
.withPort(80)
.withTargetPort(new IntOrString(9376))
.endPort()
.endSpec();
}
}

0 comments on commit 00c878f

Please sign in to comment.