示例#1
0
        public async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            var error = false;
            var crds  = CrdGenerator.GenerateCrds().ToList();
            await app.Out.WriteLineAsync($"Found {crds.Count} CRD's.");

            await app.Out.WriteLineAsync($@"Starting install into cluster with url ""{_client.ApiClient.BaseUri}"".");

            foreach (var crd in crds)
            {
                await app.Out.WriteLineAsync(
                    $@"Install ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"" into the cluster");

                try
                {
                    try
                    {
                        await _client.Save(crd);
                    }
                    catch (HttpOperationException e) when(e.Response.StatusCode == HttpStatusCode.NotFound)
                    {
                        await _client.Save((V1beta1CustomResourceDefinition)crd);
                    }
                }
                catch (HttpOperationException e)
                {
                    await app.Out.WriteLineAsync(
                        $@"There was a http (api) error while installing ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"".");

                    await app.Out.WriteLineAsync(e.Message);

                    await app.Out.WriteLineAsync(e.Response.Content);

                    error = true;
                }
                catch (Exception e)
                {
                    await app.Out.WriteLineAsync(
                        $@"There was an error while installing ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"".");

                    await app.Out.WriteLineAsync(e.Message);

                    error = true;
                }
            }

            return(error ? ExitCodes.Error : ExitCodes.Success);
        }
        public async Task PublishAsync(
            IKubernetesObject <V1ObjectMeta> resource,
            string reason,
            string message,
            EventType type = EventType.Normal)
        {
            _logger.LogTrace(
                "Encoding event name with: {resourceName}.{resourceNamespace}.{reason}.{message}.{type}.",
                resource.Name(),
                resource.Namespace(),
                reason,
                message,
                type);
            var eventName =
                Base32.Rfc4648.Encode(
                    SHA512.HashData(
                        Encoding.UTF8.GetBytes($"{resource.Name()}.{resource.Namespace()}.{reason}.{message}.{type}")));

            _logger.LogTrace(@"Search or create event with name ""{name}"".", eventName);
            var @event = await _client.Get <Corev1Event>(eventName, resource.Namespace()) ??
                         new Corev1Event
            {
                Kind       = Corev1Event.KubeKind,
                ApiVersion = $"{Corev1Event.KubeGroup}/{Corev1Event.KubeApiVersion}",
                Metadata   = new V1ObjectMeta
                {
                    Name = eventName,
                    NamespaceProperty = resource.Namespace(),
                    Annotations       = new Dictionary <string, string>
                    {
                        { "nameHash", "sha512" },
                        { "nameEncoding", "Base32 / RFC 4648" },
                    },
                },
                Type               = type.ToString(),
                Reason             = reason,
                Message            = message,
                ReportingComponent = _settings.Name,
                ReportingInstance  = Environment.MachineName,
                Source             = new V1EventSource {
                    Component = _settings.Name
                },
                InvolvedObject = resource.MakeObjectReference(),
                FirstTimestamp = DateTime.UtcNow,
                LastTimestamp  = DateTime.UtcNow,
                Count          = 0,
            };

            @event.Count++;
            @event.LastTimestamp = DateTime.UtcNow;
            _logger.LogTrace(
                "Save event with new count {count} and last timestamp {timestamp}",
                @event.Count,
                @event.LastTimestamp);

            await _client.Save(@event);

            _logger.LogInformation(
                @"Created or updated event with name ""{name}"" to new count {count} on resource ""{kind}/{name}"".",
                eventName,
                @event.Count,
                resource.Kind,
                resource.Name());
        }