예제 #1
0
        /// <summary>
        /// Validates the instance.
        /// </summary>
        /// <param name="context">The validation context.</param>
        public void Validate(TrafficValidationContext context)
        {
            Timeouts              = Timeouts ?? new TrafficTimeouts();
            Resolvers             = Resolvers ?? new List <TrafficResolver>();
            BridgeTargetAddresses = BridgeTargetAddresses ?? new List <IPAddress>();

            if (!Resolvers.Exists(r => r.Name == "docker"))
            {
                Resolvers.Add(
                    new TrafficResolver()
                {
                    Name        = "docker",
                    NameServers = new List <TrafficNameserver>()
                    {
                        new TrafficNameserver()
                        {
                            Name     = "docker0",
                            Endpoint = HiveConst.DockerDnsEndpoint
                        }
                    }
                });
            }

            if (!NetHelper.IsValidPort(ProxyPorts.PortRange.FirstPort) ||
                !NetHelper.IsValidPort(ProxyPorts.PortRange.LastPort) ||
                ProxyPorts.PortRange.LastPort <= ProxyPorts.PortRange.FirstPort + 1)
            {
                context.Error($"Load balancer port block [{ProxyPorts.PortRange.FirstPort}-{ProxyPorts.PortRange.LastPort}] range is not valid.");
            }

            if (MaxConnections <= 0)
            {
                context.Error($"Load balancer settings [{nameof(MaxConnections)}={MaxConnections}] is not positive.");
            }

            Timeouts.Validate(context);

            if (!Resolvers.Exists(r => r.Name == "docker"))
            {
                context.Error($"Load balancer settings [{nameof(Resolvers)}] must include a [docker] definition.");
            }

            foreach (var resolver in Resolvers)
            {
                resolver.Validate(context);
            }

            if (BridgeTargetCount < 0)
            {
                context.Error($"Load balancer settings [{nameof(BridgeTargetCount)}={BridgeTargetCount}] cannot be negative.");
            }

            if (BridgeTargetCount == 0 && BridgeTargetAddresses.Count == 0)
            {
                context.Error($"Load balancer settings no bridge targets are specified.");
            }
        }