Exemplo n.º 1
0
    /// <inheritdoc />
    public async Task <TResource?> Get <TResource>(
        string name,
        string? @namespace = null)
        where TResource : class, IKubernetesObject <V1ObjectMeta>
    {
        var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition <TResource>();

        try
        {
            var result = await(string.IsNullOrWhiteSpace(@namespace)
                ? ApiClient.GetClusterCustomObjectAsync(crd.Group, crd.Version, crd.Plural, name)
                : ApiClient.GetNamespacedCustomObjectAsync(
                                   crd.Group,
                                   crd.Version,
                                   @namespace,
                                   crd.Plural,
                                   name));

            if (result is JsonElement element)
            {
                return(Deserialize <TResource>(element));
            }

            return(null);
        }
        catch (HttpOperationException e) when(e.Response.StatusCode == HttpStatusCode.NotFound)
        {
            return(null);
        }
    }
Exemplo n.º 2
0
    /// <inheritdoc />
    public async Task <IList <TResource> > List <TResource>(
        string? @namespace   = null,
        string?labelSelector = null)
        where TResource : IKubernetesObject <V1ObjectMeta>
    {
        var crd    = CustomEntityDefinitionExtensions.CreateResourceDefinition <TResource>();
        var result = await(string.IsNullOrWhiteSpace(@namespace)
            ? ApiClient.ListClusterCustomObjectAsync(
                               crd.Group,
                               crd.Version,
                               crd.Plural,
                               labelSelector: labelSelector)
            : ApiClient.ListNamespacedCustomObjectAsync(
                               crd.Group,
                               crd.Version,
                               @namespace,
                               crd.Plural,
                               labelSelector: labelSelector));

        if (result is JsonElement element)
        {
            var list = Deserialize <EntityList <TResource> >(element);
            return(list?.Items ?? throw new ArgumentException("Could not parse result"));
        }

        throw new ArgumentException("Could not parse result");
    }
        public ResourceCacheMetrics(OperatorSettings settings)
        {
            var crd         = CustomEntityDefinitionExtensions.CreateResourceDefinition <TEntity>();
            var labelValues = new[]
            {
                settings.Name,
                crd.Kind,
                crd.Group,
                crd.Version,
                crd.Scope.ToString(),
            };

            CachedItemsSummary = Metrics
                                 .CreateSummary(
                "operator_resource_cached_items_size",
                "Summary of the cached items count over the last 10 minutes",
                Labels)
                                 .WithLabels(labelValues);

            CachedItemsSize = Metrics
                              .CreateGauge(
                "operator_resource_cached_items_count",
                "Total number of cached items in this resource cache",
                Labels)
                              .WithLabels(labelValues);
        }
Exemplo n.º 4
0
        public Task <Watcher <TResource> > Watch <TResource>(
            TimeSpan timeout,
            Action <WatchEventType, TResource> onEvent,
            Action <Exception>?onError          = null,
            Action?onClose                      = null,
            string? @namespace                  = null,
            CancellationToken cancellationToken = default)
            where TResource : IKubernetesObject <V1ObjectMeta>
        {
            var crd    = CustomEntityDefinitionExtensions.CreateResourceDefinition <TResource>();
            var result = string.IsNullOrWhiteSpace(@namespace)
                ? ApiClient.ListClusterCustomObjectWithHttpMessagesAsync(
                crd.Group,
                crd.Version,
                crd.Plural,
                timeoutSeconds: (int)timeout.TotalSeconds,
                watch: true,
                cancellationToken: cancellationToken)
                : ApiClient.ListNamespacedCustomObjectWithHttpMessagesAsync(
                crd.Group,
                crd.Version,
                @namespace,
                crd.Plural,
                timeoutSeconds: (int)timeout.TotalSeconds,
                watch: true,
                cancellationToken: cancellationToken);

            return(Task.FromResult(
                       result.Watch(
                           onEvent,
                           onError,
                           onClose)));
        }
        public void Should_Correctly_Translate_CRD()
        {
            var crd = EntityToCrdExtensions.CreateCrd(_testSpecEntity).Convert();
            var ced = CustomEntityDefinitionExtensions.CreateResourceDefinition(_testSpecEntity);

            crd.Kind.Should().Be(V1CustomResourceDefinition.KubeKind);
            crd.Metadata.Name.Should().Be($"{ced.Plural}.{ced.Group}");
            crd.Spec.Names.Kind.Should().Be(ced.Kind);
            crd.Spec.Names.ListKind.Should().Be(ced.ListKind);
            crd.Spec.Names.Singular.Should().Be(ced.Singular);
            crd.Spec.Names.Plural.Should().Be(ced.Plural);
            crd.Spec.Scope.Should().Be(ced.Scope.ToString());
        }
Exemplo n.º 6
0
        public async Task Delete <TResource>(string name, string? @namespace = null)
            where TResource : IKubernetesObject <V1ObjectMeta>
        {
            var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition <TResource>();

            await(string.IsNullOrWhiteSpace(@namespace)
                ? ApiClient.DeleteClusterCustomObjectAsync(
                      crd.Group,
                      crd.Version,
                      crd.Plural,
                      name)
                : ApiClient.DeleteNamespacedCustomObjectAsync(
                      crd.Group,
                      crd.Version,
                      @namespace,
                      crd.Plural,
                      name));
        }
        public ResourceWatcherMetrics(OperatorSettings settings)
        {
            var crd         = CustomEntityDefinitionExtensions.CreateResourceDefinition <TEntity>();
            var labelValues = new[]
            {
                settings.Name,
                crd.Kind,
                crd.Group,
                crd.Version,
                crd.Scope.ToString(),
            };

            Running = Metrics
                      .CreateGauge(
                "operator_resource_watcher_running",
                "Determines if the resource watcher is up and running (1 == Running, 0 == Stopped)",
                Labels)
                      .WithLabels(labelValues);

            WatchedEvents = Metrics
                            .CreateCounter(
                "operator_resource_watcher_events",
                "The count of totally watched events from the resource watcher",
                Labels)
                            .WithLabels(labelValues);

            WatcherExceptions = Metrics
                                .CreateCounter(
                "operator_resource_watcher_exceptions",
                "The count of observed thrown exceptions from the resource watcher",
                Labels)
                                .WithLabels(labelValues);

            WatcherClosed = Metrics
                            .CreateCounter(
                "operator_resource_watcher_closed",
                "The count of observed 'close' events of the resource watcher",
                Labels)
                            .WithLabels(labelValues);
        }
Exemplo n.º 8
0
    /// <inheritdoc />
    public async Task Delete <TResource>(string name, string? @namespace = null)
        where TResource : IKubernetesObject <V1ObjectMeta>
    {
        var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition <TResource>();

        try
        {
            await(string.IsNullOrWhiteSpace(@namespace)
                ? ApiClient.DeleteClusterCustomObjectAsync(
                      crd.Group,
                      crd.Version,
                      crd.Plural,
                      name)
                : ApiClient.DeleteNamespacedCustomObjectAsync(
                      crd.Group,
                      crd.Version,
                      @namespace,
                      crd.Plural,
                      name));
        }
        catch (HttpOperationException e) when(e.Response.StatusCode == HttpStatusCode.NotFound)
        {
        }
    }
        public ResourceEventQueueMetrics(OperatorSettings settings)
        {
            var crd         = CustomEntityDefinitionExtensions.CreateResourceDefinition <TEntity>();
            var labelValues = new[]
            {
                settings.Name,
                crd.Kind,
                crd.Group,
                crd.Version,
                crd.Scope.ToString(),
            };

            Running = Metrics
                      .CreateGauge(
                "operator_resource_event_queue_running",
                "Determines if the resource event queue is up and running (1 == Running, 0 == Stopped)",
                Labels)
                      .WithLabels(labelValues);

            ReadQueueEvents = Metrics
                              .CreateCounter(
                "operator_resource_event_queue_read_events",
                "The count of totally read events from the queue",
                Labels)
                              .WithLabels(labelValues);

            WrittenQueueEvents = Metrics
                                 .CreateCounter(
                "operator_resource_event_queue_write_events",
                "The count of totally written events from the queue",
                Labels)
                                 .WithLabels(labelValues);

            QueueSizeSummary = Metrics
                               .CreateSummary(
                "operator_resource_event_queue_size",
                "Summary of the event queue size over the last 10 minutes",
                Labels)
                               .WithLabels(labelValues);

            QueueSize = Metrics
                        .CreateGauge(
                "operator_resource_event_queue_count",
                "Size of the entity queue",
                Labels)
                        .WithLabels(labelValues);

            CreatedEvents = Metrics
                            .CreateCounter(
                "operator_resource_event_queue_created_events",
                "The count of total 'created' events from the queue",
                Labels)
                            .WithLabels(labelValues);

            UpdatedEvents = Metrics
                            .CreateCounter(
                "operator_resource_event_queue_updated_events",
                "The count of total 'updated' events from the queue",
                Labels)
                            .WithLabels(labelValues);

            NotModifiedEvents = Metrics
                                .CreateCounter(
                "operator_resource_event_queue_not_modified_events",
                "The count of total 'not modified' events from the queue",
                Labels)
                                .WithLabels(labelValues);

            StatusUpdatedEvents = Metrics
                                  .CreateCounter(
                "operator_resource_event_queue_status_updated_events",
                "The count of total 'status updated' events from the queue",
                Labels)
                                  .WithLabels(labelValues);

            FinalizingEvents = Metrics
                               .CreateCounter(
                "operator_resource_event_queue_finalized_events",
                "The count of total 'finalized' events from the queue",
                Labels)
                               .WithLabels(labelValues);

            DeletedEvents = Metrics
                            .CreateCounter(
                "operator_resource_event_queue_deleted_events",
                "The count of total 'deleted' events from the queue",
                Labels)
                            .WithLabels(labelValues);

            RequeuedEvents = Metrics
                             .CreateCounter(
                "operator_resource_event_queue_requeued_events",
                "The count of total events that were requeued by request of the reconciler",
                Labels)
                             .WithLabels(labelValues);

            DelayedQueueSizeSummary = Metrics
                                      .CreateSummary(
                "operator_resource_event_delayed_queue_size",
                "Summary of the delayed (requeue) event queue size over the last 10 minutes",
                Labels)
                                      .WithLabels(labelValues);

            DelayedQueueSize = Metrics
                               .CreateGauge(
                "operator_resource_event_delayed_queue_count",
                "Size of the entity delayed (requeue) queue",
                Labels)
                               .WithLabels(labelValues);

            ErroredEvents = Metrics
                            .CreateCounter(
                "operator_resource_event_queue_errored_events",
                "The count of total events threw an error during reconciliation",
                Labels)
                            .WithLabels(labelValues);

            ErrorQueueSizeSummary = Metrics
                                    .CreateSummary(
                "operator_resource_event_errored_queue_size",
                "Summary of the error queue size over the last 10 minutes",
                Labels)
                                    .WithLabels(labelValues);

            ErrorQueueSize = Metrics
                             .CreateGauge(
                "operator_resource_event_errored_queue_count",
                "Size of the error queue",
                Labels)
                             .WithLabels(labelValues);
        }
        public ResourceControllerMetrics(OperatorSettings settings)
        {
            var crd         = CustomEntityDefinitionExtensions.CreateResourceDefinition <TEntity>();
            var labelValues = new[]
            {
                settings.Name,
                crd.Kind,
                crd.Group,
                crd.Version,
                crd.Scope.ToString(),
            };

            Running = Metrics
                      .CreateGauge(
                "operator_resource_controller_running",
                "Determines if the resource watcher is up and running (1 == Running, 0 == Stopped)",
                Labels)
                      .WithLabels(labelValues);

            EventsFromWatcher = Metrics
                                .CreateCounter(
                "operator_resource_controller_watched_events",
                "The count of totally watched events from the resource watcher",
                Labels)
                                .WithLabels(labelValues);

            RequeuedEvents = Metrics
                             .CreateCounter(
                "operator_resource_controller_requeued_events",
                "The count of totally requeued resources (normal requeue, without errors)",
                Labels)
                             .WithLabels(labelValues);

            ErroredEvents = Metrics
                            .CreateCounter(
                "operator_resource_controller_errored_events",
                "The count of totally errored reconciliation attempts.",
                Labels)
                            .WithLabels(labelValues);

            CreatedEvents = Metrics
                            .CreateCounter(
                "operator_resource_controller_created_events",
                "The count of total 'created' events reconciled by the controller",
                Labels)
                            .WithLabels(labelValues);

            UpdatedEvents = Metrics
                            .CreateCounter(
                "operator_resource_controller_updated_events",
                "The count of total 'updated' events reconciled by the controller",
                Labels)
                            .WithLabels(labelValues);

            NotModifiedEvents = Metrics
                                .CreateCounter(
                "operator_resource_controller_not_modified_events",
                "The count of total 'not modified' events reconciled by the controller",
                Labels)
                                .WithLabels(labelValues);

            StatusUpdatedEvents = Metrics
                                  .CreateCounter(
                "operator_resource_controller_status_updated_events",
                "The count of total 'status updated' events reconciled by the controller",
                Labels)
                                  .WithLabels(labelValues);

            FinalizingEvents = Metrics
                               .CreateCounter(
                "operator_resource_controller_finalized_events",
                "The count of total 'finalized' events reconciled by the controller",
                Labels)
                               .WithLabels(labelValues);

            DeletedEvents = Metrics
                            .CreateCounter(
                "operator_resource_controller_deleted_events",
                "The count of total 'deleted' events reconciled by the controller",
                Labels)
                            .WithLabels(labelValues);
        }