Exemplo n.º 1
0
        /// <summary>
        /// Build a Kubernetes field selector for <see cref="EventV1.InvolvedObject"/> that targets the specified resource.
        /// </summary>
        /// <param name="resource">The <see cref="KubeResourceV1"/>.</param>
        /// <returns>The field selector.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="resource"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="resource"/> has <c>null</c> <see cref="KubeResourceV1.Metadata"/>.
        /// </exception>
        static string BuildInvolvedObjectFieldSelector(KubeResourceV1 resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (resource.Metadata == null)
            {
                throw new ArgumentException($"{resource.GetType().Name} has null metadata.", nameof(resource));
            }

            StringBuilder fieldSelector = new StringBuilder()
                                          .AppendFormat("involvedObject.kind={0}", resource.Kind)
                                          .Append(',')
                                          .AppendFormat("involvedObject.apiVersion={0}", resource.ApiVersion)
                                          .Append(',')
                                          .AppendFormat("involvedObject.name={0}", resource.Metadata.Name);

            if (!String.IsNullOrWhiteSpace(resource.Metadata.Uid))
            {
                fieldSelector
                .Append(',')
                .AppendFormat("involvedObject.uid={0}", resource.Metadata.Uid);
            }

            if (!String.IsNullOrWhiteSpace(resource.Metadata.Namespace))
            {
                fieldSelector
                .Append(',')
                .AppendFormat("involvedObject.namespace={0}", resource.Metadata.Namespace);
            }

            return(fieldSelector.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempt to resolve the related resource for the specified event.
        /// </summary>
        /// <param name="client">The Kubernetes API client.</param>
        /// <param name="eventResource">The <see cref="EventV1"/> to process.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        static async Task ResolveRelatedResource(IKubeApiClient client, EventV1 eventResource)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (eventResource == null)
            {
                throw new ArgumentNullException(nameof(eventResource));
            }

            KubeResourceV1 involvedResource = await client.Dynamic().Get(eventResource.InvolvedObject);

            if (involvedResource != null)
            {
                Log.Information("\tResolved related {ResourceModelName} resource for event {EventName}.",
                                involvedResource.GetType().Name,
                                eventResource.Metadata.Name,
                                eventResource.Metadata.Namespace
                                );
            }
            else
            {
                Log.Information("\tFailed to resolve related resource for event {EventName}.",
                                eventResource.Metadata.Name,
                                eventResource.Metadata.Namespace
                                );
            }
        }
        void OnResourceDeleted(KubeResourceV1 resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            Action <KubeResourceV1> handler;

            Type resourceType = resource.GetType();

            if (!_deleteHandlers.TryGetValue(resourceType, out handler))
            {
                Log.LogWarning("No deletion event handler registered for '{ResourceType}' resources.", resourceType.FullName);

                return;
            }

            try
            {
                handler(resource);
            }
            catch (Exception handlerError)
            {
                Log.LogError(handlerError, "Unexpected error encountered by deletion event handler for '{HandlerType}' resource events.", resourceType.FullName);
            }
        }