<commit_before>/*-
 * Copyright (c) 2017,2018,2019 F5 Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"

	"github.com/F5Networks/k8s-bigip-ctlr/pkg/appmanager"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/health"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/pollers"
	bigIPPrometheus "github.com/F5Networks/k8s-bigip-ctlr/pkg/prometheus"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/vxlan"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/writer"

	log "github.com/F5Networks/k8s-bigip-ctlr/pkg/vlogger"
	clog "github.com/F5Networks/k8s-bigip-ctlr/pkg/vlogger/console"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/spf13/pflag"
	"golang.org/x/crypto/ssh/terminal"

	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"

	"k8s.io/apimachinery/pkg/labels"

	routeclient "github.com/openshift/origin/pkg/client"
	// The import below is required to register the RouteList and Route types.
	_ "github.com/openshift/origin/pkg/route/api/install"
)

type globalSection struct {
	LogLevel       string `json:"log-level,omitempty"`
	VerifyInterval int    `json:"verify-interval,omitempty"`
	VXLANPartition string `json:"vxlan-partition,omitempty"`
}

type bigIPSection struct {
	BigIPUsername   string   `json:"username,omitempty"`
	BigIPPassword   string   `json:"password,omitempty"`
	BigIPURL        string   `json:"url,omitempty"`
	BigIPPartitions []string `json:"partitions,omitempty"`
}

var (
	// To be set by build
	version   string
	buildInfo string

	// Flag sets and supported flags
	flags        *pflag.FlagSet
	globalFlags  *pflag.FlagSet
	bigIPFlags   *pflag.FlagSet
	kubeFlags    *pflag.FlagSet
	vxlanFlags   *pflag.FlagSet
	osRouteFlags *pflag.FlagSet

	pythonBaseDir    *string
	logLevel         *string
	verifyInterval   *int
	nodePollInterval *int
	printVersion     *bool
	httpAddress      *string

	namespaces        *[]string
	useNodeInternal   *bool
	poolMemberType    *string
	inCluster         *bool
	kubeConfig        *string
	namespaceLabel    *string
	manageRoutes      *bool
	manageConfigMaps  *bool
	nodeLabelSelector *string
	resolveIngNames   *string
	defaultIngIP      *string
	vsSnatPoolName    *string
	useSecrets        *bool
	schemaLocal       *string

	bigIPURL           *string
	bigIPUsername      *string
	bigIPPassword      *string
	bigIPPartitions    *[]string
	credsDir           *string
	as3Validation      *bool
	sslInsecure        *bool
	trustedCertsCfgmap *string

	vxlanMode        string
	openshiftSDNName *string
	flannelName      *string

	routeVserverAddr *string
	routeLabel       *string
	routeHttpVs      *string
	routeHttpsVs     *string
	clientSSL        *string
	serverSSL        *string

	// package variables
	isNodePort         bool
	watchAllNamespaces bool
	vxlanName          string
)

func _init() {
	flags = pflag.NewFlagSet("main", pflag.ContinueOnError)
	globalFlags = pflag.NewFlagSet("Global", pflag.ContinueOnError)
	bigIPFlags = pflag.NewFlagSet("BigIP", pflag.ContinueOnError)
	kubeFlags = pflag.NewFlagSet("Kubernetes", pflag.ContinueOnError)
	vxlanFlags = pflag.NewFlagSet("VXLAN", pflag.ContinueOnError)
	osRouteFlags = pflag.NewFlagSet("OpenShift Routes", pflag.ContinueOnError)

	// Flag wrapping
	var err error
	var width int
	fd := int(os.Stdout.Fd())
	if terminal.IsTerminal(fd) {
		width, _, err = terminal.GetSize(fd)
		if nil != err {
			width = 0
		}
	}

	// Global flags
	pythonBaseDir = globalFlags.String("python-basedir", "",
		"DEPRECATED: Optional, directory location of python utilities")
	logLevel = globalFlags.String("log-level", "INFO",
		"Optional, logging level")
	verifyInterval = globalFlags.Int("verify-interval", 30,
		"Optional, interval (in seconds) at which to verify the BIG-IP configuration.")
	nodePollInterval = globalFlags.Int("node-poll-interval", 30,
		"Optional, interval (in seconds) at which to poll for cluster nodes.")
	printVersion = globalFlags.Bool("version", false,
		"Optional, print version and exit.")
	httpAddress = globalFlags.String("http-listen-address", "0.0.0.0:8080",
		"Optional, address to serve http based informations (/metrics and /health).")

	globalFlags.Usage = func() {
		fmt.Fprintf(os.Stderr, "  Global:\n%s\n", globalFlags.FlagUsagesWrapped(width))
	}

	// BigIP flags
	bigIPURL = bigIPFlags.String("bigip-url", "",
		"Required, URL for the Big-IP")
	bigIPUsername = bigIPFlags.String("bigip-username", "",
		"Required, user name for the Big-IP user account.")
	bigIPPassword = bigIPFlags.String("bigip-password", "",
		"Required, password for the Big-IP user account.")
	bigIPPartitions = bigIPFlags.StringArray("bigip-partition", []string{},
		"Required, partition(s) for the Big-IP kubernetes objects.")
	credsDir = bigIPFlags.String("credentials-directory", "",
		"Optional, directory that contains the BIG-IP username, password, and/or "+
			"url files. To be used instead of username, password, and/or url arguments.")
	as3Validation = bigIPFlags.Bool("as3-validation", true,
		"Optional, when set to false, disables as3 template validation on the controller.")
	sslInsecure = bigIPFlags.Bool("insecure", false,
		"Optional, when set to true, enable insecure SSL communication to BIGIP.")
	trustedCertsCfgmap = bigIPFlags.String("trusted-certs-cfgmap", "",
		"Optional, when certificates are provided, enable secure SSL communication to BIGIP.")

	bigIPFlags.Usage = func() {
		fmt.Fprintf(os.Stderr, "  BigIP:\n%s\n", bigIPFlags.FlagUsagesWrapped(width))
	}

	// Kubernetes flags
	namespaces = kubeFlags.StringArray("namespace", []string{},
		"Optional, Kubernetes namespace(s) to watch."+<commit_msg>Description changes for trusted-certs-cfgmap option<commit_after>/*-
 * Copyright (c) 2017,2018,2019 F5 Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"

	"github.com/F5Networks/k8s-bigip-ctlr/pkg/appmanager"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/health"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/pollers"
	bigIPPrometheus "github.com/F5Networks/k8s-bigip-ctlr/pkg/prometheus"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/vxlan"
	"github.com/F5Networks/k8s-bigip-ctlr/pkg/writer"

	log "github.com/F5Networks/k8s-bigip-ctlr/pkg/vlogger"
	clog "github.com/F5Networks/k8s-bigip-ctlr/pkg/vlogger/console"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/spf13/pflag"
	"golang.org/x/crypto/ssh/terminal"

	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"

	"k8s.io/apimachinery/pkg/labels"

	routeclient "github.com/openshift/origin/pkg/client"
	// The import below is required to register the RouteList and Route types.
	_ "github.com/openshift/origin/pkg/route/api/install"
)

type globalSection struct {
	LogLevel       string `json:"log-level,omitempty"`
	VerifyInterval int    `json:"verify-interval,omitempty"`
	VXLANPartition string `json:"vxlan-partition,omitempty"`
}

type bigIPSection struct {
	BigIPUsername   string   `json:"username,omitempty"`
	BigIPPassword   string   `json:"password,omitempty"`
	BigIPURL        string   `json:"url,omitempty"`
	BigIPPartitions []string `json:"partitions,omitempty"`
}

var (
	// To be set by build
	version   string
	buildInfo string

	// Flag sets and supported flags
	flags        *pflag.FlagSet
	globalFlags  *pflag.FlagSet
	bigIPFlags   *pflag.FlagSet
	kubeFlags    *pflag.FlagSet
	vxlanFlags   *pflag.FlagSet
	osRouteFlags *pflag.FlagSet

	pythonBaseDir    *string
	logLevel         *string
	verifyInterval   *int
	nodePollInterval *int
	printVersion     *bool
	httpAddress      *string

	namespaces        *[]string
	useNodeInternal   *bool
	poolMemberType    *string
	inCluster         *bool
	kubeConfig        *string
	namespaceLabel    *string
	manageRoutes      *bool
	manageConfigMaps  *bool
	nodeLabelSelector *string
	resolveIngNames   *string
	defaultIngIP      *string
	vsSnatPoolName    *string
	useSecrets        *bool
	schemaLocal       *string

	bigIPURL           *string
	bigIPUsername      *string
	bigIPPassword      *string
	bigIPPartitions    *[]string
	credsDir           *string
	as3Validation      *bool
	sslInsecure        *bool
	trustedCertsCfgmap *string

	vxlanMode        string
	openshiftSDNName *string
	flannelName      *string

	routeVserverAddr *string
	routeLabel       *string
	routeHttpVs      *string
	routeHttpsVs     *string
	clientSSL        *string
	serverSSL        *string

	// package variables
	isNodePort         bool
	watchAllNamespaces bool
	vxlanName          string
)

func _init() {
	flags = pflag.NewFlagSet("main", pflag.ContinueOnError)
	globalFlags = pflag.NewFlagSet("Global", pflag.ContinueOnError)
	bigIPFlags = pflag.NewFlagSet("BigIP", pflag.ContinueOnError)
	kubeFlags = pflag.NewFlagSet("Kubernetes", pflag.ContinueOnError)
	vxlanFlags = pflag.NewFlagSet("VXLAN", pflag.ContinueOnError)
	osRouteFlags = pflag.NewFlagSet("OpenShift Routes", pflag.ContinueOnError)

	// Flag wrapping
	var err error
	var width int
	fd := int(os.Stdout.Fd())
	if terminal.IsTerminal(fd) {
		width, _, err = terminal.GetSize(fd)
		if nil != err {
			width = 0
		}
	}

	// Global flags
	pythonBaseDir = globalFlags.String("python-basedir", "",
		"DEPRECATED: Optional, directory location of python utilities")
	logLevel = globalFlags.String("log-level", "INFO",
		"Optional, logging level")
	verifyInterval = globalFlags.Int("verify-interval", 30,
		"Optional, interval (in seconds) at which to verify the BIG-IP configuration.")
	nodePollInterval = globalFlags.Int("node-poll-interval", 30,
		"Optional, interval (in seconds) at which to poll for cluster nodes.")
	printVersion = globalFlags.Bool("version", false,
		"Optional, print version and exit.")
	httpAddress = globalFlags.String("http-listen-address", "0.0.0.0:8080",
		"Optional, address to serve http based informations (/metrics and /health).")

	globalFlags.Usage = func() {
		fmt.Fprintf(os.Stderr, "  Global:\n%s\n", globalFlags.FlagUsagesWrapped(width))
	}

	// BigIP flags
	bigIPURL = bigIPFlags.String("bigip-url", "",
		"Required, URL for the Big-IP")
	bigIPUsername = bigIPFlags.String("bigip-username", "",
		"Required, user name for the Big-IP user account.")
	bigIPPassword = bigIPFlags.String("bigip-password", "",
		"Required, password for the Big-IP user account.")
	bigIPPartitions = bigIPFlags.StringArray("bigip-partition", []string{},
		"Required, partition(s) for the Big-IP kubernetes objects.")
	credsDir = bigIPFlags.String("credentials-directory", "",
		"Optional, directory that contains the BIG-IP username, password, and/or "+
			"url files. To be used instead of username, password, and/or url arguments.")
	as3Validation = bigIPFlags.Bool("as3-validation", true,
		"Optional, when set to false, disables as3 template validation on the controller.")
	sslInsecure = bigIPFlags.Bool("insecure", false,
		"Optional, when set to true, enable insecure SSL communication to BIGIP.")
	trustedCertsCfgmap = bigIPFlags.String("trusted-certs-cfgmap", "",
		"Optional, when certificates are provided, adds them to controller’s trusted certificate store.")

	bigIPFlags.Usage = func() {
		fmt.Fprintf(os.Stderr, "  BigIP:\n%s\n", bigIPFlags.FlagUsagesWrapped(width))
	}

	// Kubernetes flags
	namespaces = kubeFlags.StringArray("namespace", []string{},
		"Optional, Kubernetes namespace(s) to watch."+
{'fraction_non_alphanumeric': 0.11189202140404121, 'fraction_numerical': 0.008944972446290233, 'mean_word_length': 4.045124899274779, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1521778', 'n_tokens_mistral': 4304, 'n_tokens_neox': 4092, 'n_words': 996}