Skip to content

Commit e9f5d4e

Browse files
CKS: don't fail when a node still has an ISO attached
attachIsoKubernetesVMs attaches the binaries ISO to every node before an upgrade, a scale-up or a start, and relied on the attach replacing whatever ISO the node had. Since an Instance can have several CD-ROMs, attachIso refuses an ISO that is already attached, and any ISO beyond the number of CD-ROMs, one by default. A node left holding the binaries ISO by an operation that did not finish, for example because the management server stopped, then made every later operation fail before it started. Leave the binaries ISO in place if the node has it already, and detach any other ISO from the node first, as the attach used to replace it. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent a95eb59 commit e9f5d4e

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterActionWorker.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import com.cloud.vm.dao.UserVmDao;
140140
import com.cloud.vm.dao.VMInstanceDao;
141141
import com.cloud.vm.dao.VMInstanceDetailsDao;
142+
import com.cloud.vm.dao.VmIsoMapDao;
142143

143144
import static com.cloud.kubernetes.cluster.KubernetesServiceHelper.KubernetesClusterNodeType.CONTROL;
144145
import static com.cloud.kubernetes.cluster.KubernetesServiceHelper.KubernetesClusterNodeType.ETCD;
@@ -196,6 +197,8 @@ public class KubernetesClusterActionWorker {
196197
@Inject
197198
protected UserVmDao userVmDao;
198199
@Inject
200+
protected VmIsoMapDao vmIsoMapDao;
201+
@Inject
199202
protected VMInstanceDetailsDao vmInstanceDetailsDao;
200203
@Inject
201204
protected UserVmService userVmService;
@@ -699,6 +702,18 @@ protected void attachIsoKubernetesVMs(List<UserVm> clusterVMs, final KubernetesS
699702
CallContext vmContext = CallContext.register(CallContext.current(), ApiCommandResourceType.VirtualMachine);
700703
vmContext.putContextParameter(VirtualMachine.class, vm.getUuid());
701704
try {
705+
// A node can still hold an ISO from an operation that did not finish. An Instance can not have the
706+
// same ISO attached twice, nor more ISOs than it has CD-ROMs.
707+
UserVmVO userVm = userVmDao.findById(vm.getId());
708+
Long attachedIsoId = userVm != null ? userVm.getIsoId() : null;
709+
if (Long.valueOf(iso.getId()).equals(attachedIsoId) || vmIsoMapDao.findByVmIdIsoId(vm.getId(), iso.getId()) != null) {
710+
logger.info("Binaries ISO is already attached to VM: {} in cluster: {}", vm, kubernetesCluster);
711+
continue;
712+
}
713+
if (attachedIsoId != null) {
714+
logger.info("Detaching ISO {} from VM: {} in cluster: {} to attach the binaries ISO", attachedIsoId, vm, kubernetesCluster);
715+
templateService.detachIso(vm.getId(), attachedIsoId, true);
716+
}
702717
templateService.attachIso(iso.getId(), vm.getId(), true);
703718
if (logger.isInfoEnabled()) {
704719
logger.info("Attached binaries ISO for VM: {} in cluster: {}", vm, kubernetesCluster);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.kubernetes.cluster.actionworkers;
18+
19+
import java.util.List;
20+
21+
import org.apache.cloudstack.api.ApiCommandResourceType;
22+
import org.apache.cloudstack.context.CallContext;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.MockedStatic;
28+
import org.mockito.Mockito;
29+
import org.mockito.junit.MockitoJUnitRunner;
30+
31+
import com.cloud.kubernetes.cluster.KubernetesCluster;
32+
import com.cloud.kubernetes.cluster.KubernetesClusterManagerImpl;
33+
import com.cloud.kubernetes.cluster.dao.KubernetesClusterDao;
34+
import com.cloud.kubernetes.version.KubernetesSupportedVersion;
35+
import com.cloud.storage.Storage;
36+
import com.cloud.storage.VMTemplateVO;
37+
import com.cloud.storage.dao.VMTemplateDao;
38+
import com.cloud.template.TemplateApiService;
39+
import com.cloud.template.VirtualMachineTemplate;
40+
import com.cloud.uservm.UserVm;
41+
import com.cloud.vm.UserVmVO;
42+
import com.cloud.vm.VmIsoMapVO;
43+
import com.cloud.vm.dao.UserVmDao;
44+
import com.cloud.vm.dao.VmIsoMapDao;
45+
46+
/**
47+
* Attaching the binaries ISO to the nodes of a cluster, some of which may still have an ISO attached.
48+
*/
49+
@RunWith(MockitoJUnitRunner.class)
50+
public class KubernetesClusterActionWorkerIsoTest {
51+
52+
private static final long BINARIES_ISO_ID = 10L;
53+
private static final long OTHER_ISO_ID = 11L;
54+
private static final long VM_ID = 2L;
55+
56+
@Mock
57+
KubernetesClusterManagerImpl kubernetesClusterManager;
58+
59+
@Mock
60+
KubernetesCluster kubernetesCluster;
61+
62+
@Mock
63+
TemplateApiService templateService;
64+
65+
@Mock
66+
VMTemplateDao templateDao;
67+
68+
@Mock
69+
UserVmDao userVmDao;
70+
71+
@Mock
72+
VmIsoMapDao vmIsoMapDao;
73+
74+
@Mock
75+
KubernetesSupportedVersion version;
76+
77+
@Mock
78+
UserVm vm;
79+
80+
KubernetesClusterActionWorker actionWorker;
81+
82+
@Before
83+
public void setUp() {
84+
kubernetesClusterManager.kubernetesClusterDao = Mockito.mock(KubernetesClusterDao.class);
85+
actionWorker = new KubernetesClusterActionWorker(kubernetesCluster, kubernetesClusterManager);
86+
actionWorker.templateService = templateService;
87+
actionWorker.templateDao = templateDao;
88+
actionWorker.userVmDao = userVmDao;
89+
actionWorker.vmIsoMapDao = vmIsoMapDao;
90+
Mockito.when(version.getIsoId()).thenReturn(BINARIES_ISO_ID);
91+
VMTemplateVO iso = Mockito.mock(VMTemplateVO.class);
92+
Mockito.when(iso.getId()).thenReturn(BINARIES_ISO_ID);
93+
Mockito.when(iso.getFormat()).thenReturn(Storage.ImageFormat.ISO);
94+
Mockito.when(iso.getState()).thenReturn(VirtualMachineTemplate.State.Active);
95+
Mockito.when(templateDao.findById(BINARIES_ISO_ID)).thenReturn(iso);
96+
Mockito.when(vm.getId()).thenReturn(VM_ID);
97+
}
98+
99+
private void attachBinariesIso(Long attachedIsoId, boolean binariesIsoInOtherCdrom) {
100+
UserVmVO userVm = Mockito.mock(UserVmVO.class);
101+
Mockito.when(userVm.getIsoId()).thenReturn(attachedIsoId);
102+
Mockito.when(userVmDao.findById(VM_ID)).thenReturn(userVm);
103+
Mockito.lenient().when(vmIsoMapDao.findByVmIdIsoId(VM_ID, BINARIES_ISO_ID)).thenReturn(binariesIsoInOtherCdrom ? Mockito.mock(VmIsoMapVO.class) : null);
104+
try (MockedStatic<CallContext> callContext = Mockito.mockStatic(CallContext.class)) {
105+
callContext.when(() -> CallContext.register(Mockito.any(), Mockito.any(ApiCommandResourceType.class))).thenReturn(Mockito.mock(CallContext.class));
106+
actionWorker.attachIsoKubernetesVMs(List.of(vm), version);
107+
}
108+
}
109+
110+
@Test
111+
public void testAttachIsoKubernetesVMsNoIsoAttached() {
112+
attachBinariesIso(null, false);
113+
Mockito.verify(templateService, Mockito.never()).detachIso(Mockito.anyLong(), Mockito.any(), Mockito.any());
114+
Mockito.verify(templateService).attachIso(BINARIES_ISO_ID, VM_ID, true);
115+
}
116+
117+
@Test
118+
public void testAttachIsoKubernetesVMsBinariesIsoAlreadyAttached() {
119+
attachBinariesIso(BINARIES_ISO_ID, false);
120+
Mockito.verifyNoInteractions(templateService);
121+
}
122+
123+
@Test
124+
public void testAttachIsoKubernetesVMsBinariesIsoAttachedToOtherCdrom() {
125+
attachBinariesIso(OTHER_ISO_ID, true);
126+
Mockito.verifyNoInteractions(templateService);
127+
}
128+
129+
@Test
130+
public void testAttachIsoKubernetesVMsOtherIsoAttached() {
131+
attachBinariesIso(OTHER_ISO_ID, false);
132+
Mockito.verify(templateService).detachIso(VM_ID, OTHER_ISO_ID, true);
133+
Mockito.verify(templateService).attachIso(BINARIES_ISO_ID, VM_ID, true);
134+
}
135+
}

0 commit comments

Comments
 (0)