private async Task <bool> DeleteJobAsync(WebPingerArchive archive)
        {
            var name = $"wpa-{archive.Metadata.Name}-{archive.Metadata.CreationTimestamp:yyMMdd-HHmm}";
            var jobs = await _kubernetes.ListNamespacedJobAsync(
                Program.NamespaceName,
                fieldSelector : $"metadata.name={name}");

            if (jobs.Items.Any())
            {
                await _kubernetes.DeleteNamespacedJobAsync(name, Program.NamespaceName);

                Console.WriteLine($"** Deleted Job: {name}, in namespace: {Program.NamespaceName}");
                return(true);
            }
            else
            {
                Console.WriteLine($"** No Job to delete: {name}, in namespace: {Program.NamespaceName}");
                return(false);
            }
        }
Beispiel #2
0
        public static async Task Handle(WatchEventType type, WebPingerArchive archive)
        {
            switch (type)
            {
            case WatchEventType.Added:
                var addedhandler = _ServiceProvider.GetService <WebPingerArchiveAddedHandler>();
                await addedhandler.HandleAsync(archive);

                Console.WriteLine($"* Handled event: {type}, for: {archive.Metadata.Name}");
                break;

            case WatchEventType.Deleted:
                var deletedHandler = _ServiceProvider.GetService <WebPingerArchiveDeletedHandler>();
                await deletedHandler.HandleAsync(archive);

                Console.WriteLine($"* Handled event: {type}, for: {archive.Metadata.Name}");
                break;

            default:
                Console.WriteLine($"* Ignored event: {type}, for: {archive.Metadata.Name}");
                break;
            }
        }
        public async Task <bool> HandleAsync(WebPingerArchive archive)
        {
            await DeleteJobAsync(archive);

            return(true);
        }
        private async Task <bool> CreateJobAsync(WebPingerArchive archive)
        {
            // check a pinger exists to archive:
            var services = await _kubernetes.ListNamespacedServiceAsync(
                Program.NamespaceName,
                labelSelector : $"app=web-ping,target={archive.Spec.Target}");

            if (!services.Items.Any())
            {
                Console.WriteLine($"** No WebPinger Service exists for target: {archive.Spec.Target}, in namespace: {Program.NamespaceName}");
                return(false);
            }

            var pingerServiceName = services.Items.First().Metadata.Name;
            var name = $"wpa-{archive.Metadata.Name}-{archive.Metadata.CreationTimestamp:yyMMdd-HHmm}";
            var jobs = await _kubernetes.ListNamespacedJobAsync(
                Program.NamespaceName,
                fieldSelector : $"metadata.name={name}");

            if (!jobs.Items.Any())
            {
                var job = new V1Job
                {
                    Metadata = new V1ObjectMeta
                    {
                        Name   = name,
                        Labels = new Dictionary <string, string>()
                        {
                            { "kiamol", "ch20" },
                        }
                    },
                    Spec = new V1JobSpec
                    {
                        Completions = 1,
                        Template    = new V1PodTemplateSpec
                        {
                            Metadata = new V1ObjectMeta
                            {
                                Labels = new Dictionary <string, string>()
                                {
                                    { "app", "web-ping-archive" },
                                    { "target", archive.Spec.Target }
                                }
                            },
                            Spec = new V1PodSpec
                            {
                                AutomountServiceAccountToken = false,
                                RestartPolicy = "Never",
                                Containers    = new List <V1Container>
                                {
                                    new V1Container
                                    {
                                        Name  = "archiver",
                                        Image = "kiamol/ch20-web-ping-archiver",
                                        Env   = new List <V1EnvVar>
                                        {
                                            new V1EnvVar
                                            {
                                                Name  = "WEB_PING_URL",
                                                Value = $"http://{pingerServiceName}:8080/archive"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                await _kubernetes.CreateNamespacedJobAsync(job, Program.NamespaceName);

                Console.WriteLine($"** Created Job: {name}, in namespace: {Program.NamespaceName}");
                return(true);
            }
            else
            {
                Console.WriteLine($"** Job exists: {name}, in namespace: {Program.NamespaceName}");
                return(false);
            }
        }