예제 #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void registerMultipleDirectoriesForMonitoring() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void RegisterMultipleDirectoriesForMonitoring()
        {
            using (DefaultFileSystemWatcher watcher = new DefaultFileSystemWatcher(FileSystems.Default.newWatchService()))
            {
                File            directory1       = TestDirectory.directory("test1");
                File            directory2       = TestDirectory.directory("test2");
                WatchedResource watchedResource1 = watcher.Watch(directory1);
                WatchedResource watchedResource2 = watcher.Watch(directory2);
                assertNotSame(watchedResource1, watchedResource2);
            }
        }
예제 #2
0
        public void WatcherInitGivesEvents()
        {
            using var watch = new WatchedResource <V1Namespace, V1NamespaceList>((b, limit) =>
                                                                                 _k8S.ListNamespaceWithHttpMessagesAsync(watch: b, limit: limit));
            var events = new List <(WatchEventType type, V1Namespace entity)>();

            watch.EntityChanged += (type, entity) => { events.Add((type, (V1Namespace)entity)); };
            Thread.Sleep(5000);
            Assert.NotEmpty(events);
            var namespaces = watch.GetAll <V1Namespace>().ToList();

            Assert.Equal(namespaces.Count, events.Count);
            Assert.All(namespaces, entity => Assert.Contains(events, ev => ev.entity.IsEqualViaJson(entity)));
        }
예제 #3
0
        public async Task WatchingEmptyNamespaceWorks()
        {
            // make sure namespace "test" contains no configmaps
            var configmapsToDelete = await _k8S.ListNamespacedConfigMapAsync(TestNamespace);

            foreach (var configmap in configmapsToDelete.Items)
            {
                await _k8S.DeleteNamespacedConfigMapAsync(configmap.Name(), TestNamespace);
            }

            using var watch = new WatchedResource <V1ConfigMap, V1ConfigMapList>((b, limit) =>
                                                                                 _k8S.ListNamespacedConfigMapWithHttpMessagesAsync(TestNamespace, watch: b, limit: limit));
            var events = new List <(WatchEventType type, V1Namespace entity)>();

            watch.EntityChanged += (type, entity) => { events.Add((type, (V1Namespace)entity)); };
            Thread.Sleep(1000);
            Assert.Empty(events);
            var configmaps = watch.GetAll <V1ConfigMap>().ToList();

            Assert.Empty(configmaps);
        }
예제 #4
0
        // ReSharper disable once UnusedParameter.Local
        private static void Main(string[] args)
        {
            // Configure logging
            using var loggerFactory = LoggerFactory.Create(builder =>
                                                           builder.AddSimpleConsole(options =>
            {
                options.IncludeScopes   = true;
                options.SingleLine      = true;
                options.TimestampFormat = "hh:mm:ss ";
            }).SetMinimumLevel(LogLevel.Debug));
            var logger = loggerFactory.CreateLogger <WatchedResource <V1ConfigMap, V1ConfigMapList> >();

            // Configure Kubernetes client
            const string ns        = "test";
            var          config    = Config.Configuration();
            var          k8SConfig = config.GetSection("K8s").Get <K8SConfig>();

            var c   = KubernetesClientConfiguration.BuildConfigFromConfigFile(k8SConfig.KubeConfig);
            var k8S = new Kubernetes(c);

            // watch and log k8s events
            var watch = new WatchedResource <V1ConfigMap, V1ConfigMapList>((doWatch, limit) =>
                                                                           k8S.ListNamespacedConfigMapWithHttpMessagesAsync(ns, watch: doWatch, limit: limit), logger);

            watch.EntityChanged += (type, entity) =>
            {
                Console.WriteLine($"Entity changed event [{type}]: {entity.Metadata.Name}");
            };

            // wait for keypress
            Console.WriteLine("Press ESC to stop");
            do
            {
                while (!Console.KeyAvailable)
                {
                    // Do something
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
        }
예제 #5
0
        public async Task WatchingEmptyNamespaceGrowByTwoItemsWorks()
        {
            // make sure namespace "test" contains no configmaps
            var configmapsToDelete = await _k8S.ListNamespacedConfigMapAsync(TestNamespace);

            foreach (var configmap in configmapsToDelete.Items)
            {
                await _k8S.DeleteNamespacedConfigMapAsync(configmap.Name(), TestNamespace);
            }

            using var watch = new WatchedResource <V1ConfigMap, V1ConfigMapList>((b, limit) =>
                                                                                 _k8S.ListNamespacedConfigMapWithHttpMessagesAsync(TestNamespace, watch: b, limit: limit));
            var events = new List <(WatchEventType type, V1ConfigMap entity)>();

            watch.EntityChanged += (type, entity) =>
            {
                if (((V1ConfigMap)entity).Metadata.Name.StartsWith("testconfigmap"))
                {
                    events.Add((type, (V1ConfigMap)entity));
                }
            };
            Thread.Sleep(1000);
            Assert.Empty(events);
            var configmaps = watch.GetAll <V1ConfigMap>().ToList();

            Assert.Empty(configmaps);

            // Now create two configmaps
            var configMap1 = new V1ConfigMap
            {
                Metadata = new V1ObjectMeta
                {
                    Name = "testconfigmap1",
                    NamespaceProperty = TestNamespace
                },
                Data = new Dictionary <string, string>
                {
                    { "test1", "value1" },
                    { "test2", "value2" }
                }
            };
            var configMap2 = new V1ConfigMap
            {
                Metadata = new V1ObjectMeta
                {
                    Name = "testconfigmap2",
                    NamespaceProperty = TestNamespace
                },
                Data = new Dictionary <string, string>
                {
                    { "test3", "value3" },
                    { "test4", "value4" }
                }
            };
            await _k8S.CreateNamespacedConfigMapAsync(configMap1, TestNamespace);

            await _k8S.CreateNamespacedConfigMapAsync(configMap2, TestNamespace);

            Thread.Sleep(11000); // we need two loops and some extra time to detect the new entities
            Assert.Equal(2, events.Count);
            Assert.Contains(events, ev => ev.entity.Metadata.Name == configMap1.Name());
            Assert.Contains(events, ev => ev.entity.Metadata.Name == configMap2.Name());
        }