public void JustNameFromClusterResource()
    {
        var resource = new V1ClusterRole(
            apiVersion: V1ClusterRole.KubeApiVersion,
            kind: V1ClusterRole.KubeKind,
            metadata: new V1ObjectMeta(
                name: "the-name"));

        var nn = NamespacedName.From(resource);

        Assert.Equal("the-name", nn.Name);
        Assert.Null(nn.Namespace);
    }
    public void NamespaceAndNameFromResource()
    {
        var resource = new V1ConfigMap(
            apiVersion: V1ConfigMap.KubeApiVersion,
            kind: V1ConfigMap.KubeKind,
            metadata: new V1ObjectMeta(
                name: "the-name",
                namespaceProperty: "the-namespace"));

        var nn = NamespacedName.From(resource);

        Assert.Equal("the-name", nn.Name);
        Assert.Equal("the-namespace", nn.Namespace);
    }
Esempio n. 3
0
        private void OnPrimaryResourceWatchEvent(WatchEventType watchEventType, TResource resource)
        {
            var key = NamespacedName.From(resource);

            _cache.UpdateWorkItem(key, workItem =>
            {
                if (watchEventType == WatchEventType.Added || watchEventType == WatchEventType.Modified)
                {
                    workItem = workItem.SetResource(resource);
                }
                else if (watchEventType == WatchEventType.Deleted)
                {
                    workItem = workItem.SetResource(default);
                }
Esempio n. 4
0
        private void OnEvent(WatchEventType watchEventType, TResource item)
        {
            if (watchEventType != WatchEventType.Modified || item.Kind != "ConfigMap")
            {
                Logger.LogDebug(
                    EventId(EventType.InformerWatchEvent),
                    "Informer {ResourceType} received {WatchEventType} notification for {ItemKind}/{ItemName}.{ItemNamespace} at resource version {ResourceVersion}",
                    typeof(TResource).Name,
                    watchEventType,
                    item.Kind,
                    item.Name(),
                    item.Namespace(),
                    item.ResourceVersion());
            }

            if (watchEventType == WatchEventType.Added ||
                watchEventType == WatchEventType.Modified)
            {
                // BUGBUG: log warning if cache was not in expected state
                _cache[NamespacedName.From(item)] = item.Metadata?.OwnerReferences;
            }

            if (watchEventType == WatchEventType.Deleted)
            {
                _cache.Remove(NamespacedName.From(item));
            }

            if (watchEventType == WatchEventType.Added ||
                watchEventType == WatchEventType.Modified ||
                watchEventType == WatchEventType.Deleted ||
                watchEventType == WatchEventType.Bookmark)
            {
                _lastResourceVersion = item.ResourceVersion();
            }

            if (watchEventType == WatchEventType.Added ||
                watchEventType == WatchEventType.Modified ||
                watchEventType == WatchEventType.Deleted)
            {
                InvokeRegistrationCallbacks(watchEventType, item);
            }
        }
 /// <summary>
 /// Called by the informer with real-time resource updates.
 /// </summary>
 /// <param name="eventType">Indicates if the resource new, updated, or deleted.</param>
 /// <param name="resource">The information as provided by the Kubernets API server.</param>
 private void Notification(WatchEventType eventType, V1Ingress resource)
 {
     _cache.Update(eventType, resource);
     _queue.Add(new QueueItem(NamespacedName.From(resource), null));
 }
Esempio n. 6
0
        private async Task ListAsync(CancellationToken cancellationToken)
        {
            var previousCache = _cache;

            _cache = new Dictionary <NamespacedName, IList <V1OwnerReference> >();

            Logger.LogInformation(
                EventId(EventType.SynchronizeStarted),
                "Started synchronizing {ResourceType} resources from API server.",
                typeof(TResource).Name);

            string continueParameter = null;

            do
            {
                cancellationToken.ThrowIfCancellationRequested();

                // request next page of items
                using var listWithHttpMessage = await _client.ListClusterAnyResourceKindWithHttpMessagesAsync <TResource>(
                          _names.Group,
                          _names.ApiVersion,
                          _names.PluralName,
                          continueParameter : continueParameter,
                          cancellationToken : cancellationToken);

                var list = listWithHttpMessage.Body;
                foreach (var item in list.Items)
                {
                    // These properties are not already set on items while listing
                    // assigned here for consistency
                    item.ApiVersion = _names.GroupApiVersion;
                    item.Kind       = _names.Kind;

                    var key = NamespacedName.From(item);
                    _cache[key] = item?.Metadata?.OwnerReferences;

                    var watchEventType = WatchEventType.Added;
                    if (previousCache.Remove(key))
                    {
                        // an already-known key is provided as a modification for re-sync purposes
                        watchEventType = WatchEventType.Modified;
                    }

                    InvokeRegistrationCallbacks(watchEventType, item);
                }

                foreach (var(key, value) in previousCache)
                {
                    // for anything which was previously known but not part of list
                    // send a deleted notification to clear any observer caches
                    var item = new TResource()
                    {
                        ApiVersion = _names.GroupApiVersion,
                        Kind       = _names.Kind,
                        Metadata   = new V1ObjectMeta(
                            name: key.Name,
                            namespaceProperty: key.Namespace,
                            ownerReferences: value),
                    };

                    InvokeRegistrationCallbacks(WatchEventType.Deleted, item);
                }

                // keep track of values needed for next page and to start watching
                _lastResourceVersion = list.ResourceVersion();
                continueParameter    = list.Continue();
            }while (!string.IsNullOrEmpty(continueParameter));

            Logger.LogInformation(
                EventId(EventType.SynchronizeComplete),
                "Completed synchronizing {ResourceType} resources from API server.",
                typeof(TResource).Name);
        }