- 
                Notifications
    
You must be signed in to change notification settings  - Fork 133
 
Add New Environment Variables for Load Balancer Configuration #1052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -5,10 +5,12 @@ import ( | |||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| "k8s.io/klog/v2" | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||||||||||||||||||||||||||
| "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/envutil" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| 
        
          
        
         | 
    @@ -31,12 +33,20 @@ const ( | |||||||||||||||||||||||||
| hcloudNetworkDisableAttachedCheck = "HCLOUD_NETWORK_DISABLE_ATTACHED_CHECK" | ||||||||||||||||||||||||||
| hcloudNetworkRoutesEnabled = "HCLOUD_NETWORK_ROUTES_ENABLED" | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| hcloudLoadBalancersAlgorithmType = "HCLOUD_LOAD_BALANCERS_ALGORITHM_TYPE" | ||||||||||||||||||||||||||
| hcloudLoadBalancersDisableIPv6 = "HCLOUD_LOAD_BALANCERS_DISABLE_IPV6" | ||||||||||||||||||||||||||
| hcloudLoadBalancersDisablePrivateIngress = "HCLOUD_LOAD_BALANCERS_DISABLE_PRIVATE_INGRESS" | ||||||||||||||||||||||||||
| hcloudLoadBalancersDisablePublicNetwork = "HCLOUD_LOAD_BALANCERS_DISABLE_PUBLIC_NETWORK" | ||||||||||||||||||||||||||
| hcloudLoadBalancersEnabled = "HCLOUD_LOAD_BALANCERS_ENABLED" | ||||||||||||||||||||||||||
| hcloudLoadBalancersHealthCheckInterval = "HCLOUD_LOAD_BALANCERS_HEALTH_CHECK_INTERVAL" | ||||||||||||||||||||||||||
| hcloudLoadBalancersHealthCheckRetries = "HCLOUD_LOAD_BALANCERS_HEALTH_CHECK_RETRIES" | ||||||||||||||||||||||||||
| hcloudLoadBalancersHealthCheckTimeout = "HCLOUD_LOAD_BALANCERS_HEALTH_CHECK_TIMEOUT" | ||||||||||||||||||||||||||
| hcloudLoadBalancersLocation = "HCLOUD_LOAD_BALANCERS_LOCATION" | ||||||||||||||||||||||||||
| hcloudLoadBalancersNetworkZone = "HCLOUD_LOAD_BALANCERS_NETWORK_ZONE" | ||||||||||||||||||||||||||
| hcloudLoadBalancersDisablePrivateIngress = "HCLOUD_LOAD_BALANCERS_DISABLE_PRIVATE_INGRESS" | ||||||||||||||||||||||||||
| hcloudLoadBalancersPrivateSubnetIPRange = "HCLOUD_LOAD_BALANCERS_PRIVATE_SUBNET_IP_RANGE" | ||||||||||||||||||||||||||
| hcloudLoadBalancersType = "HCLOUD_LOAD_BALANCERS_TYPE" | ||||||||||||||||||||||||||
| hcloudLoadBalancersUsePrivateIP = "HCLOUD_LOAD_BALANCERS_USE_PRIVATE_IP" | ||||||||||||||||||||||||||
| hcloudLoadBalancersDisableIPv6 = "HCLOUD_LOAD_BALANCERS_DISABLE_IPV6" | ||||||||||||||||||||||||||
| hcloudLoadBalancersUsesProxyProtocol = "HCLOUD_LOAD_BALANCERS_USES_PROXYPROTOCOL" | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| hcloudMetricsEnabled = "HCLOUD_METRICS_ENABLED" | ||||||||||||||||||||||||||
| hcloudMetricsAddress = "HCLOUD_METRICS_ADDRESS" | ||||||||||||||||||||||||||
| 
          
            
          
           | 
    @@ -76,12 +86,20 @@ type InstanceConfiguration struct { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| type LoadBalancerConfiguration struct { | ||||||||||||||||||||||||||
| AlgorithmType hcloud.LoadBalancerAlgorithmType | ||||||||||||||||||||||||||
| DisablePublicNetwork *bool | ||||||||||||||||||||||||||
| Enabled bool | ||||||||||||||||||||||||||
| HealthCheckInterval time.Duration | ||||||||||||||||||||||||||
| HealthCheckRetries int | ||||||||||||||||||||||||||
| HealthCheckTimeout time.Duration | ||||||||||||||||||||||||||
| IPv6Enabled bool | ||||||||||||||||||||||||||
| Location string | ||||||||||||||||||||||||||
| NetworkZone string | ||||||||||||||||||||||||||
| PrivateIngressEnabled bool | ||||||||||||||||||||||||||
| PrivateIPEnabled bool | ||||||||||||||||||||||||||
| IPv6Enabled bool | ||||||||||||||||||||||||||
| PrivateSubnetIPRange string | ||||||||||||||||||||||||||
| ProxyProtocolEnabled *bool | ||||||||||||||||||||||||||
| Type string | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| type NetworkConfiguration struct { | ||||||||||||||||||||||||||
| 
          
            
          
           | 
    @@ -188,12 +206,66 @@ func Read() (HCCMConfiguration, error) { | |||||||||||||||||||||||||
| errs = append(errs, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| cfg.LoadBalancer.ProxyProtocolEnabled, err = getEnvBoolPtr(hcloudLoadBalancersUsesProxyProtocol) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| errs = append(errs, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| disableIPv6, err := getEnvBool(hcloudLoadBalancersDisableIPv6, false) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| errs = append(errs, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| cfg.LoadBalancer.IPv6Enabled = !disableIPv6 // Invert the logic, as the env var is prefixed with DISABLE_. | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if subnetRange, ok := os.LookupEnv(hcloudLoadBalancersPrivateSubnetIPRange); ok { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.PrivateSubnetIPRange = subnetRange | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if algorithmType, ok := os.LookupEnv(hcloudLoadBalancersAlgorithmType); ok { | ||||||||||||||||||||||||||
| alg, parseErr := parseLoadBalancerAlgorithmType(algorithmType) | ||||||||||||||||||||||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are mixing   | 
||||||||||||||||||||||||||
| if parseErr != nil { | ||||||||||||||||||||||||||
| errs = append(errs, fmt.Errorf("failed to parse %s: %w", hcloudLoadBalancersAlgorithmType, parseErr)) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.AlgorithmType = alg | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if interval, ok := os.LookupEnv(hcloudLoadBalancersHealthCheckInterval); ok { | ||||||||||||||||||||||||||
| d, parseErr := time.ParseDuration(interval) | ||||||||||||||||||||||||||
| if parseErr != nil { | ||||||||||||||||||||||||||
| errs = append(errs, fmt.Errorf("failed to parse %s: %w", hcloudLoadBalancersHealthCheckInterval, parseErr)) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.HealthCheckInterval = d | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
         
      Comment on lines
    
      +233
     to 
      +240
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
 We already have a util function for this. If   | 
||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if timeout, ok := os.LookupEnv(hcloudLoadBalancersHealthCheckTimeout); ok { | ||||||||||||||||||||||||||
| d, parseErr := time.ParseDuration(timeout) | ||||||||||||||||||||||||||
| if parseErr != nil { | ||||||||||||||||||||||||||
| errs = append(errs, fmt.Errorf("failed to parse %s: %w", hcloudLoadBalancersHealthCheckTimeout, parseErr)) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.HealthCheckTimeout = d | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
         
      Comment on lines
    
      +242
     to 
      +249
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
 Same here  | 
||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if retries, ok := os.LookupEnv(hcloudLoadBalancersHealthCheckRetries); ok { | ||||||||||||||||||||||||||
| v, parseErr := strconv.Atoi(retries) | ||||||||||||||||||||||||||
| if parseErr != nil { | ||||||||||||||||||||||||||
| errs = append(errs, fmt.Errorf("failed to parse %s: %w", hcloudLoadBalancersHealthCheckRetries, parseErr)) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.HealthCheckRetries = v | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
         
      Comment on lines
    
      +251
     to 
      +258
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
 Same here  | 
||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| cfg.LoadBalancer.DisablePublicNetwork, err = getEnvBoolPtr(hcloudLoadBalancersDisablePublicNetwork) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| errs = append(errs, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| if lbType, ok := os.LookupEnv(hcloudLoadBalancersType); ok { | ||||||||||||||||||||||||||
| cfg.LoadBalancer.Type = lbType | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
         
      Comment on lines
    
      +265
     to 
      +267
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
 
  | 
||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| cfg.Network.NameOrID = os.Getenv(hcloudNetwork) | ||||||||||||||||||||||||||
| disableAttachedCheck, err := getEnvBool(hcloudNetworkDisableAttachedCheck, false) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| 
          
            
          
           | 
    @@ -270,6 +342,22 @@ func getEnvBool(key string, defaultValue bool) (bool, error) { | |||||||||||||||||||||||||
| return b, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| // getEnvBoolPtr returns a pointer to the boolean parsed from the environment variable with the given key. | ||||||||||||||||||||||||||
| // Returns nil if the env var is unset. | ||||||||||||||||||||||||||
| func getEnvBoolPtr(key string) (*bool, error) { | ||||||||||||||||||||||||||
| v, ok := os.LookupEnv(key) | ||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||
| return nil, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| b, err := strconv.ParseBool(v) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| return nil, fmt.Errorf("failed to parse %s: %w", key, err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| return &b, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| // getEnvDuration returns the duration parsed from the environment variable with the given key and a potential error | ||||||||||||||||||||||||||
| // parsing the var. Returns false if the env var is unset. | ||||||||||||||||||||||||||
| func getEnvDuration(key string) (time.Duration, error) { | ||||||||||||||||||||||||||
| 
        
          
        
         | 
    @@ -285,3 +373,12 @@ func getEnvDuration(key string) (time.Duration, error) { | |||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| return b, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||
| func parseLoadBalancerAlgorithmType(value string) (hcloud.LoadBalancerAlgorithmType, error) { | ||||||||||||||||||||||||||
| v := strings.ToLower(strings.TrimSpace(value)) | ||||||||||||||||||||||||||
| alg := hcloud.LoadBalancerAlgorithmType(v) | ||||||||||||||||||||||||||
| if alg == hcloud.LoadBalancerAlgorithmTypeRoundRobin || alg == hcloud.LoadBalancerAlgorithmTypeLeastConnections { | ||||||||||||||||||||||||||
| return alg, nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return "", fmt.Errorf("unsupported value %q", value) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use
net.ParseCIDRin theValidatefunction to check if the format is correct.