Beispiel #1
0
        private static void HandleIngressAdded(Extensionsv1beta1Ingress item, NodesInfo nodesInfo)
        {
            item.Spec.Rules.ToList().ForEach(rule =>
            {
                rule.Http.Paths.ToList().ForEach(path =>
                {
                    var targetPort = LookupPort(path, item.Metadata.NamespaceProperty);

                    var addRule = new Rule
                    {
                        Name     = item.Metadata.Name,
                        Host     = rule.Host,
                        Port     = 443,
                        Protocol = "https" // look up from annotation
                    };

                    addRule.Targets.AddRange(nodesInfo.NodeAddresses.Select(nodeAddress => $"{nodeAddress}:{targetPort}"));

                    try
                    {
                        var pushRule = _client.PushRule(addRule);
                        Console.WriteLine($"Rule pushed {pushRule.Accepted}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                });
            });
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            var appConfig = new ConfigurationBuilder()
                            .AddEnvironmentVariables()
                            .Build();

            var loadBalancerTarget = appConfig["LOAD_BALANCER_TARGET"];

            var channel = new Channel(loadBalancerTarget, ChannelCredentials.Insecure);

            _client = new ConfigRemoteClient(new ConfigRemote.ConfigRemoteClient(channel));

            var config = KubernetesClientConfiguration.IsInCluster()
                ? KubernetesClientConfiguration.InClusterConfig()
                : KubernetesClientConfiguration.BuildDefaultConfig();

            _k8sClient = new Kubernetes(config);

            Console.WriteLine($"Current context {config.CurrentContext}");
            Console.WriteLine($"Connecting to {_k8sClient.BaseUri}");

            var listNodeAsync = _k8sClient.ListNodeAsync();

            var nodesInfo = new NodesInfo(listNodeAsync.Result);

            var ingressLister = _k8sClient.ListIngressForAllNamespacesWithHttpMessagesAsync(watch: true);

            using (ingressLister.Watch <Extensionsv1beta1Ingress, Extensionsv1beta1IngressList>((type, item) =>
            {
                switch (type)
                {
                case WatchEventType.Added:
                    HandleIngressAdded(item, nodesInfo);
                    break;

                default:
                    Console.WriteLine($"Unhandled event for ingress {item.Metadata.Name}");
                    break;
                }
            }))
            {
                Console.WriteLine("Watching for ingress changes...");
                Console.ReadLine();
            }
        }