|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strconv" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/golang/glog" |
| 27 | + "github.com/spf13/pflag" |
| 28 | + |
| 29 | + apiv1 "k8s.io/api/core/v1" |
| 30 | + |
| 31 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/class" |
| 32 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/parser" |
| 33 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller" |
| 34 | + ing_net "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/net" |
| 35 | +) |
| 36 | + |
| 37 | +func parseFlags() (bool, *controller.Configuration, error) { |
| 38 | + var ( |
| 39 | + flags = pflag.NewFlagSet("", pflag.ExitOnError) |
| 40 | + |
| 41 | + apiserverHost = flags.String("apiserver-host", "", |
| 42 | + `Address of the Kubernetes API server. |
| 43 | +Takes the form "protocol://address:port". If not specified, it is assumed the |
| 44 | +program runs inside a Kubernetes cluster and local discovery is attempted.`) |
| 45 | + |
| 46 | + kubeConfigFile = flags.String("kubeconfig", "", |
| 47 | + `Path to a kubeconfig file containing authorization and API server information.`) |
| 48 | + |
| 49 | + ingressClass = flags.String("ingress-class", "", |
| 50 | + `Name of the ingress class this controller satisfies. |
| 51 | +The class of an Ingress object is set using the annotation "kubernetes.io/ingress.class". |
| 52 | +All ingress classes are satisfied if this parameter is left empty.`) |
| 53 | + |
| 54 | + // configMap = flags.String("configmap", "", |
| 55 | + // `Name of the ConfigMap containing custom global configurations for the controller.`) |
| 56 | + |
| 57 | + resyncPeriod = flags.Duration("sync-period", 30*time.Second, |
| 58 | + `Period at which the controller forces the repopulation of its local object stores.`) |
| 59 | + |
| 60 | + watchNamespace = flags.String("watch-namespace", apiv1.NamespaceAll, |
| 61 | + `Namespace the controller watches for updates to Kubernetes objects. |
| 62 | +This includes Ingresses, Services and all configuration resources. All |
| 63 | +namespaces are watched if this parameter is left empty.`) |
| 64 | + |
| 65 | + profiling = flags.Bool("profiling", true, |
| 66 | + `Enable profiling via web interface host:port/debug/pprof/`) |
| 67 | + |
| 68 | + defHealthzURL = flags.String("health-check-path", "/healthz", |
| 69 | + `URL path of the health check endpoint. |
| 70 | +Configured inside the NGINX status server. All requests received on the port |
| 71 | +defined by the healthz-port parameter are forwarded internally to this path.`) |
| 72 | + |
| 73 | + electionID = flags.String("election-id", "ingress-controller-leader", |
| 74 | + `Election id to use for Ingress status updates.`) |
| 75 | + |
| 76 | + showVersion = flags.Bool("version", false, |
| 77 | + `Show release information about the NGINX Ingress controller and exit.`) |
| 78 | + |
| 79 | + annotationsPrefix = flags.String("annotations-prefix", "nginx.ingress.kubernetes.io", |
| 80 | + `Prefix of the Ingress annotations specific to the NGINX controller.`) |
| 81 | + |
| 82 | + syncRateLimit = flags.Float32("sync-rate-limit", 0.3, |
| 83 | + `Define the sync frequency upper limit`) |
| 84 | + |
| 85 | + clusterName = flags.String("cluster-name", "", |
| 86 | + `Kubernetes cluster name (required)`) |
| 87 | + |
| 88 | + albNamePrefix = flags.String("alb-name-prefix", "", |
| 89 | + `Prefix to add to ALB resources (11 alphanumeric characters or less)`) |
| 90 | + |
| 91 | + restrictScheme = flags.Bool("restrict-scheme", false, |
| 92 | + `Restrict the scheme to internal except for whitelisted namespaces`) |
| 93 | + |
| 94 | + restrictSchemeNamespace = flags.String("restrict-scheme-namespace", "default", |
| 95 | + `The namespace with the ConfigMap containing the allowed ingresses. Only respected when restrict-scheme is true.`) |
| 96 | + |
| 97 | + awsSyncPeriod = flags.Duration("aws-sync-period", 60*time.Minute, |
| 98 | + `Period at which the controller refreshes the state from AWS.`) |
| 99 | + |
| 100 | + awsAPIMaxRetries = flags.Int("aws-max-retries", 20, |
| 101 | + `Maximum number of times to retry the AWS API.`) |
| 102 | + |
| 103 | + awsAPIDebug = flags.Bool("aws-api-debug", false, |
| 104 | + `Enable debug logging of AWS API`) |
| 105 | + healthzPort = flags.Int("healthz-port", 10254, "Port to use for the healthz endpoint.") |
| 106 | + ) |
| 107 | + |
| 108 | + flag.Set("logtostderr", "true") |
| 109 | + |
| 110 | + flags.AddGoFlagSet(flag.CommandLine) |
| 111 | + flags.Parse(os.Args) |
| 112 | + |
| 113 | + // Workaround for this issue: |
| 114 | + // https://github.com/kubernetes/kubernetes/issues/17162 |
| 115 | + flag.CommandLine.Parse([]string{}) |
| 116 | + |
| 117 | + pflag.VisitAll(func(flag *pflag.Flag) { |
| 118 | + glog.V(2).Infof("FLAG: --%s=%q", flag.Name, flag.Value) |
| 119 | + }) |
| 120 | + |
| 121 | + if *showVersion { |
| 122 | + return true, nil, nil |
| 123 | + } |
| 124 | + |
| 125 | + if *ingressClass != "" { |
| 126 | + glog.Infof("Watching for Ingress class: %s", *ingressClass) |
| 127 | + |
| 128 | + if *ingressClass != class.DefaultClass { |
| 129 | + glog.Warningf("Only Ingresses with class %q will be processed by this Ingress controller", *ingressClass) |
| 130 | + } |
| 131 | + |
| 132 | + class.IngressClass = *ingressClass |
| 133 | + } |
| 134 | + |
| 135 | + parser.AnnotationsPrefix = *annotationsPrefix |
| 136 | + |
| 137 | + // check port collisions |
| 138 | + if !ing_net.IsPortAvailable(*healthzPort) { |
| 139 | + return false, nil, fmt.Errorf("Port %v is already in use. Please check the flag --healthz-port", *healthzPort) |
| 140 | + } |
| 141 | + |
| 142 | + // Deal with legacy environment variable configuration options |
| 143 | + if s, ok := os.LookupEnv("CLUSTER_NAME"); ok { |
| 144 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --cluster-name flag.") |
| 145 | + clusterName = &s |
| 146 | + } |
| 147 | + if s, ok := os.LookupEnv("ALB_PREFIX"); ok { |
| 148 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --alb-name-prefix flag.") |
| 149 | + albNamePrefix = &s |
| 150 | + } |
| 151 | + if s, ok := os.LookupEnv("ALB_CONTROLLER_RESTRICT_SCHEME"); ok { |
| 152 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --restrict-scheme flag.") |
| 153 | + v, err := strconv.ParseBool(s) |
| 154 | + if err != nil { |
| 155 | + return false, nil, fmt.Errorf("ALB_CONTROLLER_RESTRICT_SCHEME environment variable must be either true or false. Value was: %s", s) |
| 156 | + } |
| 157 | + restrictScheme = &v |
| 158 | + } |
| 159 | + if s, ok := os.LookupEnv("ALB_CONTROLLER_RESTRICT_SCHEME_CONFIG_NAMESPACE"); ok { |
| 160 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --restrict-scheme-namespace flag.") |
| 161 | + restrictSchemeNamespace = &s |
| 162 | + } |
| 163 | + if s, ok := os.LookupEnv("ALB_SYNC_INTERVAL"); ok { |
| 164 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --aws-resync-period flag.") |
| 165 | + v, err := time.ParseDuration(s) |
| 166 | + if err != nil { |
| 167 | + return false, nil, fmt.Errorf("Failed to parse duration from ALB_SYNC_INTERVAL value of '%s'", s) |
| 168 | + } |
| 169 | + awsSyncPeriod = &v |
| 170 | + } |
| 171 | + if s, ok := os.LookupEnv("AWS_MAX_RETRIES"); ok { |
| 172 | + glog.Warningf("Environment variable configuration is deprecated, switch to the --aws-max-retries flag.") |
| 173 | + v, err := strconv.ParseInt(s, 0, 32) |
| 174 | + if err != nil { |
| 175 | + return false, nil, fmt.Errorf("AWS_MAX_RETRIES environment variable must be an integer. Value was: %s", s) |
| 176 | + } |
| 177 | + i := int(v) |
| 178 | + awsAPIMaxRetries = &i |
| 179 | + } |
| 180 | + |
| 181 | + config := &controller.Configuration{ |
| 182 | + ClusterName: *clusterName, |
| 183 | + ALBNamePrefix: *albNamePrefix, |
| 184 | + RestrictScheme: *restrictScheme, |
| 185 | + RestrictSchemeNamespace: *restrictSchemeNamespace, |
| 186 | + AWSSyncPeriod: *awsSyncPeriod, |
| 187 | + AWSAPIMaxRetries: *awsAPIMaxRetries, |
| 188 | + AWSAPIDebug: *awsAPIDebug, |
| 189 | + |
| 190 | + APIServerHost: *apiserverHost, |
| 191 | + KubeConfigFile: *kubeConfigFile, |
| 192 | + ElectionID: *electionID, |
| 193 | + EnableProfiling: *profiling, |
| 194 | + ResyncPeriod: *resyncPeriod, |
| 195 | + Namespace: *watchNamespace, |
| 196 | + // ConfigMapName: *configMap, |
| 197 | + DefaultHealthzURL: *defHealthzURL, |
| 198 | + SyncRateLimit: *syncRateLimit, |
| 199 | + HealthzPort: *healthzPort, |
| 200 | + } |
| 201 | + |
| 202 | + return false, config, nil |
| 203 | +} |
0 commit comments