Skip to content

Commit 9f73a2e

Browse files
mgmt, aks avoid unnecessary PUT (Azure#28123)
1 parent 3070bf2 commit 9f73a2e

File tree

12 files changed

+2556
-2047
lines changed

12 files changed

+2556
-2047
lines changed

sdk/resourcemanager/azure-resourcemanager-containerservice/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- Supported disabling local accounts for `KubernetesCluster`.
1010
- Supported disk encryption set for `KubernetesCluster`.
1111

12+
### Bugs Fixed
13+
14+
- Fixed a bug that `orchestratorVersion` not initialized in agent pool.
15+
1216
### Other Changes
1317

1418
- Changed behavior that `KubernetesCluster` no longer retrieves admin and user KubeConfig during create, update, refresh.

sdk/resourcemanager/azure-resourcemanager-containerservice/src/main/java/com/azure/resourcemanager/containerservice/implementation/KubernetesClusterAgentPoolImpl.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,50 @@ public Map<String, String> tags() {
173173
: Collections.unmodifiableMap(innerModel().tags());
174174
}
175175

176+
// @Override
177+
// public void start() {
178+
// startAsync().block();
179+
// }
180+
//
181+
// @Override
182+
// public Mono<Void> startAsync() {
183+
// AgentPoolInner innerModel = this.getAgentPoolInner();
184+
// PowerState powerState = innerModel.powerState();
185+
// if (powerState == null) {
186+
// powerState = new PowerState();
187+
// innerModel.withPowerState(powerState);
188+
// }
189+
// powerState.withCode(Code.RUNNING);
190+
// return parent().manager().serviceClient().getAgentPools()
191+
// .createOrUpdateAsync(parent().resourceGroupName(), parent().name(), this.name(), innerModel)
192+
// .map(inner -> {
193+
// this.innerModel().withPowerState(inner.powerState());
194+
// return inner;
195+
// }).then();
196+
// }
197+
//
198+
// @Override
199+
// public void stop() {
200+
// stopAsync().block();
201+
// }
202+
//
203+
// @Override
204+
// public Mono<Void> stopAsync() {
205+
// AgentPoolInner innerModel = this.getAgentPoolInner();
206+
// PowerState powerState = innerModel.powerState();
207+
// if (powerState == null) {
208+
// powerState = new PowerState();
209+
// innerModel.withPowerState(powerState);
210+
// }
211+
// powerState.withCode(Code.STOPPED);
212+
// return parent().manager().serviceClient().getAgentPools()
213+
// .createOrUpdateAsync(parent().resourceGroupName(), parent().name(), this.name(), innerModel)
214+
// .map(inner -> {
215+
// this.innerModel().withPowerState(inner.powerState());
216+
// return inner;
217+
// }).then();
218+
// }
219+
176220
@Override
177221
public KubernetesClusterAgentPoolImpl withVirtualMachineSize(ContainerServiceVMSizeTypes vmSize) {
178222
this.innerModel().withVmSize(vmSize.toString());

sdk/resourcemanager/azure-resourcemanager-containerservice/src/main/java/com/azure/resourcemanager/containerservice/implementation/KubernetesClusterImpl.java

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
34
package com.azure.resourcemanager.containerservice.implementation;
45

56
import com.azure.core.http.rest.PagedFlux;
67
import com.azure.core.http.rest.PagedIterable;
78
import com.azure.core.http.rest.Response;
89
import com.azure.core.http.rest.SimpleResponse;
10+
import com.azure.core.management.serializer.SerializerFactory;
911
import com.azure.core.util.CoreUtils;
1012
import com.azure.core.util.logging.ClientLogger;
13+
import com.azure.core.util.serializer.SerializerAdapter;
14+
import com.azure.core.util.serializer.SerializerEncoding;
1115
import com.azure.resourcemanager.containerservice.ContainerServiceManager;
1216
import com.azure.resourcemanager.containerservice.fluent.models.ManagedClusterInner;
1317
import com.azure.resourcemanager.containerservice.fluent.models.PrivateEndpointConnectionInner;
@@ -39,11 +43,13 @@
3943
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
4044
import reactor.core.publisher.Mono;
4145

46+
import java.io.IOException;
4247
import java.util.ArrayList;
4348
import java.util.Collections;
4449
import java.util.HashMap;
4550
import java.util.List;
4651
import java.util.Map;
52+
import java.util.Set;
4753
import java.util.concurrent.ConcurrentHashMap;
4854
import java.util.stream.Collectors;
4955

@@ -58,6 +64,10 @@ public class KubernetesClusterImpl
5864
private List<CredentialResult> userKubeConfigs;
5965
private final Map<Format, List<CredentialResult>> formatUserKubeConfigsMap = new ConcurrentHashMap<>();
6066

67+
private ManagedClusterInner parameterSnapshotOnUpdate;
68+
private static final SerializerAdapter SERIALIZER_ADAPTER =
69+
SerializerFactory.createDefaultManagementSerializerAdapter();
70+
6171
protected KubernetesClusterImpl(String name, ManagedClusterInner innerObject, ContainerServiceManager manager) {
6272
super(name, innerObject, manager);
6373
if (this.innerModel().agentPoolProfiles() == null) {
@@ -300,23 +310,96 @@ protected Mono<ManagedClusterInner> getInnerAsync() {
300310
});
301311
}
302312

313+
@Override
314+
public KubernetesClusterImpl update() {
315+
parameterSnapshotOnUpdate = this.deepCopyInner();
316+
parameterSnapshotOnUpdate.withServicePrincipalProfile(null); // servicePrincipalProfile is null in update
317+
return super.update();
318+
}
319+
320+
boolean isClusterModifiedDuringUpdate(ManagedClusterInner parameter) {
321+
if (parameterSnapshotOnUpdate == null || parameter == null) {
322+
return true;
323+
} else {
324+
final List<ManagedClusterAgentPoolProfile> parameterSnapshotAgentPools =
325+
parameterSnapshotOnUpdate.agentPoolProfiles();
326+
final List<ManagedClusterAgentPoolProfile> parameterAgentPools = parameter.agentPoolProfiles();
327+
328+
// intersection of agent pool names
329+
Set<String> intersectAgentPoolNames = parameter.agentPoolProfiles().stream()
330+
.map(ManagedClusterAgentPoolProfile::name)
331+
.collect(Collectors.toSet());
332+
intersectAgentPoolNames.retainAll(parameterSnapshotOnUpdate.agentPoolProfiles().stream()
333+
.map(ManagedClusterAgentPoolProfile::name)
334+
.collect(Collectors.toSet()));
335+
336+
// compare the intersection, as add/delete is handled by REST API on AgentPoolsClient
337+
List<ManagedClusterAgentPoolProfile> agentPools = parameterSnapshotOnUpdate.agentPoolProfiles()
338+
.stream()
339+
.filter(p -> intersectAgentPoolNames.contains(p.name()))
340+
.collect(Collectors.toList());
341+
// will be reverted in finally block
342+
parameterSnapshotOnUpdate.withAgentPoolProfiles(agentPools);
343+
344+
agentPools = parameter.agentPoolProfiles()
345+
.stream()
346+
.filter(p -> intersectAgentPoolNames.contains(p.name()))
347+
.collect(Collectors.toList());
348+
// will be reverted in finally block
349+
parameter.withAgentPoolProfiles(agentPools);
350+
351+
try {
352+
String jsonStrSnapshot =
353+
SERIALIZER_ADAPTER.serialize(parameterSnapshotOnUpdate, SerializerEncoding.JSON);
354+
String jsonStr = SERIALIZER_ADAPTER.serialize(parameter, SerializerEncoding.JSON);
355+
return !jsonStr.equals(jsonStrSnapshot);
356+
} catch (IOException e) {
357+
// ignored, treat as modified
358+
return true;
359+
} finally {
360+
parameterSnapshotOnUpdate.withAgentPoolProfiles(parameterSnapshotAgentPools);
361+
parameter.withAgentPoolProfiles(parameterAgentPools);
362+
}
363+
}
364+
}
365+
366+
ManagedClusterInner deepCopyInner() {
367+
ManagedClusterInner updateParameter;
368+
try {
369+
// deep copy via json
370+
String jsonStr = SERIALIZER_ADAPTER.serialize(this.innerModel(), SerializerEncoding.JSON);
371+
updateParameter =
372+
SERIALIZER_ADAPTER.deserialize(jsonStr, ManagedClusterInner.class, SerializerEncoding.JSON);
373+
} catch (IOException e) {
374+
// ignored, null to signify not available
375+
updateParameter = null;
376+
}
377+
return updateParameter;
378+
}
379+
303380
@Override
304381
public Mono<KubernetesCluster> createResourceAsync() {
305382
final KubernetesClusterImpl self = this;
306383
if (!this.isInCreateMode()) {
307384
this.innerModel().withServicePrincipalProfile(null);
308385
}
309386

310-
return this
311-
.manager()
312-
.serviceClient()
313-
.getManagedClusters()
314-
.createOrUpdateAsync(self.resourceGroupName(), self.name(), self.innerModel())
315-
.map(inner -> {
316-
self.setInner(inner);
317-
clearKubeConfig();
318-
return self;
319-
});
387+
final boolean createOrModified = this.isInCreateMode() || this.isClusterModifiedDuringUpdate(this.innerModel());
388+
389+
if (createOrModified) {
390+
return this
391+
.manager()
392+
.serviceClient()
393+
.getManagedClusters()
394+
.createOrUpdateAsync(self.resourceGroupName(), self.name(), self.innerModel())
395+
.map(inner -> {
396+
self.setInner(inner);
397+
clearKubeConfig();
398+
return self;
399+
});
400+
} else {
401+
return Mono.just(this);
402+
}
320403
}
321404

322405
private void clearKubeConfig() {
@@ -387,8 +470,9 @@ public KubernetesClusterImpl withDnsPrefix(String dnsPrefix) {
387470

388471
@Override
389472
public KubernetesClusterAgentPoolImpl defineAgentPool(String name) {
390-
ManagedClusterAgentPoolProfile innerPoolProfile = new ManagedClusterAgentPoolProfile();
391-
innerPoolProfile.withName(name);
473+
ManagedClusterAgentPoolProfile innerPoolProfile = new ManagedClusterAgentPoolProfile()
474+
.withName(name)
475+
.withOrchestratorVersion(this.innerModel().kubernetesVersion());
392476
return new KubernetesClusterAgentPoolImpl(innerPoolProfile, this);
393477
}
394478

sdk/resourcemanager/azure-resourcemanager-containerservice/src/main/java/com/azure/resourcemanager/containerservice/models/KubernetesClusterAgentPool.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ public interface KubernetesClusterAgentPool
9696
*/
9797
Map<String, String> tags();
9898

99+
// /**
100+
// * Starts the agent pool.
101+
// */
102+
// void start();
103+
//
104+
// /**
105+
// * Starts the agent pool.
106+
// *
107+
// * @return A {@link Mono} that completes when a successful response is received.
108+
// */
109+
// Mono<Void> startAsync();
110+
//
111+
// /**
112+
// * Stops the agent pool.
113+
// */
114+
// void stop();
115+
//
116+
// /**
117+
// * Stops the agent pool.
118+
// *
119+
// * @return A {@link Mono} that completes when a successful response is received.
120+
// */
121+
// Mono<Void> stopAsync();
122+
99123
// Fluent interfaces
100124

101125
/**

0 commit comments

Comments
 (0)