Ejemplo n.º 1
0
        private async Task <V1alpha1Ticket> PatchTicketStatusAsync(V1alpha1Ticket ticket, CancellationToken cancellationToken)
        {
            var policy = Policy
                         .Handle <HttpOperationException>(ex => ex.Response.StatusCode == HttpStatusCode.NotFound)
                         .WaitAndRetryAsync(3, attempt => TimeSpan.FromMilliseconds(25 * Math.Pow(2, attempt)));

            var status = new V1alpha1TicketStatus
            {
                UniqueId      = Guid.NewGuid().ToString("n", CultureInfo.InvariantCulture),
                WorkflowState = "Created",
                ContactInfo   = "*****@*****.**",
            };

            return(await policy.ExecuteAsync(async() =>
            {
                var result = await _client.PatchNamespacedCustomObjectAsync(
                    new V1alpha1Ticket {
                    Status = status
                },
                    group: V1alpha1Ticket.KubeGroup,
                    version: V1alpha1Ticket.KubeApiVersion,
                    namespaceParameter: ticket.Namespace(),
                    plural: "tickets",
                    name: ticket.Name(),
                    cancellationToken: cancellationToken);

                return _serializers.Convert <V1alpha1Ticket>(result);
            }));
        }
Ejemplo n.º 2
0
    public TestCluster(IOptions <TestClusterOptions> options, IResourceSerializers serializers)
    {
        if (options is null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        _serializers = serializers ?? throw new ArgumentNullException(nameof(serializers));

        foreach (var resource in options.Value.InitialResources)
        {
            Resources.Add(_serializers.Convert <ResourceObject>(resource));
        }
    }
Ejemplo n.º 3
0
        public static async Task <TResource> CreateOrReplaceClusterCustomObjectAsync <TResource>(
            this IKubernetes client,
            string group,
            string version,
            string plural,
            TResource resource,
            CancellationToken cancellationToken) where TResource : class, IKubernetesObject <V1ObjectMeta>, new()
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentException($"'{nameof(group)}' cannot be null or empty", nameof(group));
            }

            if (string.IsNullOrEmpty(version))
            {
                throw new ArgumentException($"'{nameof(version)}' cannot be null or empty", nameof(version));
            }

            if (string.IsNullOrEmpty(plural))
            {
                throw new ArgumentException($"'{nameof(plural)}' cannot be null or empty", nameof(plural));
            }

            if (resource is null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            var list = _serializers.Convert <KubernetesList <TResource> >(await client.ListClusterCustomObjectAsync(
                                                                              group: group,
                                                                              version: version,
                                                                              plural: plural,
                                                                              fieldSelector: $"metadata.name={resource.Name()}",
                                                                              cancellationToken: cancellationToken));

            var resourceExisting = list.Items.SingleOrDefault();

            if (resourceExisting != null)
            {
                resource.Metadata.ResourceVersion = resourceExisting.ResourceVersion();

                return(_serializers.Convert <TResource>(await client.ReplaceClusterCustomObjectAsync(
                                                            body: resource,
                                                            group: group,
                                                            version: version,
                                                            plural: plural,
                                                            name: resource.Name(),
                                                            cancellationToken: cancellationToken)));
            }
            else
            {
                return(_serializers.Convert <TResource>(await client.CreateClusterCustomObjectAsync(
                                                            body: resource,
                                                            group: group,
                                                            version: version,
                                                            plural: plural,
                                                            cancellationToken: cancellationToken)));
            }
        }