private async Task ProcessPreviewSaveRequestAsync()

in src/Azure.Deployments.Extensibility.Providers.Kubernetes/KubernetesProvider.cs [48:76]


        private async Task<ExtensibilityOperationResponse> ProcessPreviewSaveRequestAsync(ExtensibilityOperationRequest request, CancellationToken cancellationToken)
        {
            using var resource = await request.ProcessAsync(cancellationToken);

            if (resource.Namespace is not null)
            {
                try
                {
                    await resource.Kubernetes.CoreV1.ReadNamespaceAsync(resource.Namespace, cancellationToken: cancellationToken);
                }
                catch (HttpOperationException exception) when (exception.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    // For namespaced resources we have to handle a special case where the namespace is being created as part of the same
                    // template. When we do the dry-run request this will fail if the namespace does not yet exist. This isn't useful to us
                    // because if the namespace is being created as part of the template this would be a false positive. So for these cases
                    // we want to fall back to a "client" dry-run if the namespace has yet to be created. This is lower fidelity but it won't 
                    // block things that would work.
                    var metadata = resource.Properties.Metadata with { Namespace = resource.Namespace };
                    var patchedProperties = resource.Properties with { Metadata = metadata };
                    var patchedPropertiesElement = ExtensibilityJsonSerializer.Default.SerializeToElement(patchedProperties);

                    return new ExtensibilityOperationSuccessResponse(request.Resource with { Properties = patchedPropertiesElement });
                }
            }

            var properties = await resource.PatchAsync(dryRun: "All", cancellationToken);

            return new ExtensibilityOperationSuccessResponse(request.Resource with { Properties = properties });
        }