コード例 #1
0
    public void Update(WatchEventType eventType, V1Ingress ingress)
    {
        if (ingress is null)
        {
            throw new ArgumentNullException(nameof(ingress));
        }

        var serviceNames = ImmutableList <string> .Empty;

        if (eventType == WatchEventType.Added || eventType == WatchEventType.Modified)
        {
            // If the ingress exists, list out the related services
            var spec           = ingress.Spec;
            var defaultBackend = spec?.DefaultBackend;
            var defaultService = defaultBackend?.Service;
            if (!string.IsNullOrEmpty(defaultService?.Name))
            {
                serviceNames = serviceNames.Add(defaultService.Name);
            }

            foreach (var rule in spec.Rules ?? Enumerable.Empty <V1IngressRule>())
            {
                var http = rule.Http;
                foreach (var path in http.Paths ?? Enumerable.Empty <V1HTTPIngressPath>())
                {
                    var backend = path.Backend;
                    var service = backend.Service;

                    if (!serviceNames.Contains(service.Name))
                    {
                        serviceNames = serviceNames.Add(service.Name);
                    }
                }
            }
        }

        var ingressName = ingress.Name();

        lock (_sync)
        {
            var serviceNamesPrevious = ImmutableList <string> .Empty;
            if (eventType == WatchEventType.Added || eventType == WatchEventType.Modified)
            {
                // If the ingress exists then remember details

                _ingressData[ingressName] = new IngressData(ingress);

                if (_ingressToServiceNames.TryGetValue(ingressName, out serviceNamesPrevious))
                {
                    _ingressToServiceNames[ingressName] = serviceNames;
                }
                else
                {
                    serviceNamesPrevious = ImmutableList <string> .Empty;
                    _ingressToServiceNames.Add(ingressName, serviceNames);
                }
            }
            else if (eventType == WatchEventType.Deleted)
            {
                // otherwise clear out details

                _ingressData.Remove(ingressName);

                if (_ingressToServiceNames.TryGetValue(ingressName, out serviceNamesPrevious))
                {
                    _ingressToServiceNames.Remove(ingressName);
                }
            }

            // update cross-reference for new ingress-to-services linkage not previously known
            foreach (var serviceName in serviceNames)
            {
                if (!serviceNamesPrevious.Contains(serviceName))
                {
                    if (_serviceToIngressNames.TryGetValue(serviceName, out var ingressNamesPrevious))
                    {
                        _serviceToIngressNames[serviceName] = _serviceToIngressNames[serviceName].Add(ingressName);
                    }
                    else
                    {
                        _serviceToIngressNames.Add(serviceName, ImmutableList <string> .Empty.Add(ingressName));
                    }
                }
            }

            // remove cross-reference for previous ingress-to-services linkage no longer present
            foreach (var serviceName in serviceNamesPrevious)
            {
                if (!serviceNames.Contains(serviceName))
                {
                    _serviceToIngressNames[serviceName] = _serviceToIngressNames[serviceName].Remove(ingressName);
                }
            }
        }
    }
コード例 #2
0
 public bool IngressExists(V1Ingress ingress)
 {
     return(_ingressData.ContainsKey(ingress.Name()));
 }