Skip to content

Commit

Permalink
fix awaits for start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
cruikshj committed Apr 27, 2024
1 parent 322508e commit adeaa64
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,43 @@ ServerStatus GetStatus(int? replicas, int? readyReplicas)
}
}

public Task StartServerAsync(string identifier, CancellationToken cancellationToken = default)
public async Task StartServerAsync(string identifier, CancellationToken cancellationToken = default)
{
return SetServerReplicasAsync(identifier, 1, cancellationToken);
await SetServerReplicasAsync(identifier, 1, cancellationToken);
}

public Task StopServerAsync(string identifier, CancellationToken cancellationToken = default)
public async Task StopServerAsync(string identifier, CancellationToken cancellationToken = default)
{
return SetServerReplicasAsync(identifier, 0, cancellationToken);
await SetServerReplicasAsync(identifier, 0, cancellationToken);
}

private Task SetServerReplicasAsync(string identifier, int replicas, CancellationToken cancellationToken)
private async Task SetServerReplicasAsync(string identifier, int replicas, CancellationToken cancellationToken)
{
var metadata = ParseIdentifier(identifier);

using var client = new Kubernetes(KubeConfig);

var patch = new V1Patch($@"{{ ""spec"": {{ ""replicas"": {replicas} }} }}", V1Patch.PatchType.MergePatch);

return metadata.Kind switch
switch (metadata.Kind)
{
"Deployment" => client.PatchNamespacedDeploymentScaleAsync(
patch,
metadata.Name,
metadata.Namespace,
cancellationToken: cancellationToken),
"StatefulSet" => client.PatchNamespacedStatefulSetScaleAsync(
patch,
metadata.Name,
metadata.Namespace,
cancellationToken: cancellationToken),
_ => throw new NotSupportedException($"Kind {metadata.Kind} is not supported.")
};
case "Deployment":
await client.PatchNamespacedDeploymentScaleAsync(
patch,
metadata.Name,
metadata.Namespace,
cancellationToken: cancellationToken);
break;
case "StatefulSet":
await client.PatchNamespacedStatefulSetScaleAsync(
patch,
metadata.Name,
metadata.Namespace,
cancellationToken: cancellationToken);
break;
default:
throw new NotSupportedException($"Kind {metadata.Kind} is not supported.");
}
}

public async Task<IDictionary<string, Stream>> GetServerLogsAsync(string identifier, CancellationToken cancellationToken = default)
Expand Down

0 comments on commit adeaa64

Please sign in to comment.