Skip to content
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

Feature/mode env flag #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -51,6 +55,9 @@ type Config struct {
DefaultListener DefaultListenerConfig

BGP BGPConfig

FlagModeEnv bool
EnvFileLocation string
}

func (c *Config) Invalid() error {
Expand Down Expand Up @@ -198,7 +205,6 @@ func (i *IPVSConfig) WriteToNode() error {
for n := 0; n < reflectVal.NumField(); n++ {
// create reflect.Values and extract the name of field, ipvsTag
_, _, _, tag, value := processReflection(reflectVal, n)
fmt.Printf("setting value %s=%s\n", tag, value.String())
err := i.SetSysctl(tag, value.String())
if err != nil {
return err
Expand Down Expand Up @@ -254,6 +260,64 @@ func processReflection(v reflect.Value, i int) (string, string, string, string,
return name, typ, defaultVal, tag, value
}

func (c *Config) retrieveNodeConfig() error {
if c.FlagModeEnv && c.EnvFileLocation == "" {
return fmt.Errorf("in flag-mode-env, you need to specify the location that the file etc/environment is located in the deployment volume mount")
}

env := map[string]string{}

envFile, err := ioutil.ReadFile(c.EnvFileLocation)
if err != nil {
return fmt.Errorf("error retrieving environment variables from node: %v", err)
}

envFileLines := bytes.Split(envFile, []byte("\n"))

for _, line := range envFileLines {
if len(line) == 0 {
continue
}

lineSpl := strings.Split(string(line), "=")
if len(lineSpl) < 2 {
// ignore lines without = present
continue
}

// this case accounts for vars that may be in the format VAR=foo=bar=zarb
// and recreates them in this format in the map
env[lineSpl[0]] = strings.Join(lineSpl[1:], "=")
}

// now set the variables on the config. The c.Validate() method is called
// after this and will verify them outside of this method
// these use the same environment var names found in systemd files for RS
c.NodeName = env["HOST_IP"]
c.ConfigKey = env["SUBNET_KEY"]
c.Net.PrimaryIP = env["HOST_IP"]
c.Net.Gateway = env["HOST_GATEWAY"]
c.DefaultListener.Service = fmt.Sprintf("rdei-system/unicorns-%s:http", env["HOST_SECURITY_ZONE"])

// lastly, fetch the primary interface name which is not provided in env
// but fetched at rkt init on compute nodes via ip route get 1 | head -1 | cut -d' ' -f5
// running these pods with hostnetwork=true so we don't get a container virtual interface
cmd := exec.CommandContext(context.Background(), "ip", "route", "get", "1")
b, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to fetch primary interface: %v", err.Error())
}

s := bytes.Split(b, []byte(" "))
if len(s) < 5 {
return fmt.Errorf("not enough fields found for ip command: expected at least 5, saw %d", len(s))
}

c.Net.Interface = string(s[4])

return nil
}

// WARNING: This will panic if we have any non-string fields in the IPVS struct
// because we control the inputs (the type of fields on the struct and the tags themselves) this sort of risky business is fine
func setValue(name string, valueOR string, reflectVal reflect.Value) {
Expand Down Expand Up @@ -334,5 +398,8 @@ func NewConfig(flags *pflag.FlagSet) *Config {

config.BGP.Binary = viper.GetString("bgp-bin")

config.FlagModeEnv = viper.GetBool("flag-mode-env")
config.EnvFileLocation = viper.GetString("env-file-location")

return config
}
4 changes: 4 additions & 0 deletions cmd/kube2ipvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ func init() {
rootCmd.PersistentFlags().String("nodename", "", "required field. the ip address of the node; its identity from kubernetes' standpoint.")
rootCmd.PersistentFlags().String("kubeconfig", "", "the path to the kubeconfig file containing a crt and key.")
rootCmd.PersistentFlags().String("primary-ip", "", "The primary IP of the server this is running on.")
rootCmd.PersistentFlags().String("env-file-location", "/host-etc/environment", "Location of env file used to collect info if --flag-mode-env is enabled")

rootCmd.PersistentFlags().Bool("cleanup-master", false, "Cleanup IPVS master on shutdown")
rootCmd.PersistentFlags().String("pod-cidr-masq", "", "Pod CIDR used to exclude pod network from RDEI-MASQ rules")
rootCmd.PersistentFlags().Bool("forced-reconfigure", false, "Reconfigure happens every 10 minutes")
rootCmd.PersistentFlags().Bool("ipvs-weight-override", false, "set all IPVS wrr weights to 1 regardless")
rootCmd.PersistentFlags().Bool("ipvs-ignore-node-cordon", false, "ignore cordoned flag when determining whether a node is an eligible backend")
rootCmd.PersistentFlags().Bool("flag-mode-env", false, "for a realserver deployment only. retrieve the values of the following flags from the node environment: --nodename, --compute-iface, --config-key, --primary-ip, --gateway, --auto-configure-service. If these values are provided via an argument, they will be OVERWRITTEN unless the env value on the node is empty. This requires the volume mount /etc present on the deployment")

rootCmd.PersistentFlags().String("iptables-chain", "RAVEL", "The name of the iptables chain to use.")
rootCmd.PersistentFlags().Int("failover-timeout", 1, "number of seconds for the realserver to wait before reconfiguring itself")
Expand Down Expand Up @@ -148,6 +150,8 @@ Mode "ipvs" will result in pod ip addresses being added to the ipvs configuraton
viper.BindPFlag("forced-reconfigure", rootCmd.PersistentFlags().Lookup("forced-reconfigure"))
viper.BindPFlag("ipvs-weight-override", rootCmd.PersistentFlags().Lookup("ipvs-weight-override"))
viper.BindPFlag("ipvs-ignore-node-cordon", rootCmd.PersistentFlags().Lookup("ipvs-ignore-node-cordon"))
viper.BindPFlag("flag-mode-env", rootCmd.PersistentFlags().Lookup("flag-mode-env"))
viper.BindPFlag("env-file-location", rootCmd.PersistentFlags().Lookup("env-file-location"))
}

func main() {
Expand Down
6 changes: 6 additions & 0 deletions cmd/realserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ are missing from the configuration.`,
config := NewConfig(cmd.Flags())
logger.Debugf("got config %+v", config)

if config.FlagModeEnv {
if err := config.retrieveNodeConfig(); err != nil {
return err
}
}

// validate flags
if err := config.Invalid(); err != nil {
return err
Expand Down