Skip to content

Commit

Permalink
Add support for proxy for Docker, curl, and apt-get
Browse files Browse the repository at this point in the history
  • Loading branch information
xmudrii committed Feb 14, 2019
1 parent c09ea6c commit f677a4a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ backup:
#machine_controller:
# deploy: false

# Proxy is used to configure HTTP_PROXY, HTTPS_PROXY and NO_PROXY
# for Docker daemon and kubelet, and to be used when provisioning cluster
# (e.g. for curl, apt-get..).
# proxy:
# http_proxy: 'http://1.2.3.4'
# https_proxy: 'https://1.2.3.4'
# no_proxy: '1.2.3.4'

# KubeOne can automatically create MachineDeployments to create
# worker nodes in your cluster. Each element in this "workers"
# list is a single deployment and must have a unique name.
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Cluster struct {
Provider ProviderConfig `json:"provider"`
Versions VersionConfig `json:"versions"`
Network NetworkConfig `json:"network"`
Proxy ProxyConfig `json:"proxy"`
Workers []WorkerConfig `json:"workers"`
Backup BackupConfig `json:"backup"`
MachineController MachineControllerConfig `json:"machine_controller"`
Expand Down Expand Up @@ -157,6 +158,12 @@ type APIServerConfig struct {
Address string `json:"address"`
}

type ProxyConfig struct {
HTTPProxy string `json:"http_proxy"`
HTTPSProxy string `json:"https_proxy"`
NoProxy string `json:"no_proxy"`
}

// ProviderName represents the name of an provider
type ProviderName string

Expand Down
55 changes: 55 additions & 0 deletions pkg/installer/installation/prerequisites.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func installPrerequisitesOnNode(ctx *util.Context, node *config.HostConfig, conn
return fmt.Errorf("failed to determine hostname: %v", err)
}

err = createEnvironmentFile(ctx)
if err != nil {
return fmt.Errorf("failed to create environment file: %v", err)
}

node.Hostname = hostname

logger := ctx.Logger.WithField("os", os)
Expand All @@ -60,6 +65,11 @@ func installPrerequisitesOnNode(ctx *util.Context, node *config.HostConfig, conn
return fmt.Errorf("failed to install kubeadm: %v", err)
}

err = configureDockerDaemonProxy(ctx)
if err != nil {
return fmt.Errorf("failed to configure proxy for docker daemon: %v", err)
}

logger.Infoln("Deploying configuration files…")
err = deployConfigurationFiles(ctx)
if err != nil {
Expand All @@ -80,6 +90,24 @@ func determineHostname(ctx *util.Context, _ *config.HostConfig) (string, error)
return stdout, err
}

func createEnvironmentFile(ctx *util.Context) error {
_, _, err := ctx.Runner.Run(environmentFileCommand, util.TemplateVariables{
"HTTP_PROXY": ctx.Cluster.Proxy.HTTPProxy,
"HTTPS_PROXY": ctx.Cluster.Proxy.HTTPSProxy,
"NO_PROXY": ctx.Cluster.Proxy.NoProxy,
})

return err
}

const environmentFileCommand = `
cat <<EOF |sudo tee /etc/environment/kubeone
{{ if .HTTP_PROXY }}HTTP_PROXY="{{ .HTTP_PROXY }}"{{ end }}
{{ if .HTTPS_PROXY }}HTTPS_PROXY="{{ .HTTPS_PROXY }}"{{ end }}
{{ if .NO_PROXY }}NO_PROXY="{{ .NO_PROXY }}"{{ end }}
EOF
`

func installKubeadm(ctx *util.Context, node *config.HostConfig) error {
var err error

Expand Down Expand Up @@ -114,6 +142,7 @@ sudo swapoff -a
sudo sed -i '/.*swap.*/d' /etc/fstab
source /etc/os-release
source /etc/environment/kubeone
# Short-Circuit the installation if it was arleady executed
if type docker &>/dev/null && type kubelet &>/dev/null; then exit 0; fi
Expand Down Expand Up @@ -163,6 +192,8 @@ sudo sed -i '/.*swap.*/d' /etc/fstab
sudo setenforce 0
sudo sed -i s/SELINUX=enforcing/SELINUX=permissive/g /etc/sysconfig/selinux
source /etc/environment/kubeone
# Short-Circuit the installation if it was arleady executed
if type docker &>/dev/null && type kubelet &>/dev/null; then exit 0; fi
Expand Down Expand Up @@ -207,6 +238,8 @@ func installKubeadmCoreOS(ctx *util.Context) error {
}

const kubeadmCoreOSCommand = `
source /etc/environment/kubeone
sudo mkdir -p /opt/cni/bin /etc/kubernetes/pki /etc/kubernetes/manifests
curl -L "https://github.com/containernetworking/plugins/releases/download/{{ .CNI_VERSION }}/cni-plugins-amd64-{{ .CNI_VERSION }}.tgz" | \
sudo tar -C /opt/cni/bin -xz
Expand Down Expand Up @@ -251,3 +284,25 @@ sudo chmod 600 /etc/kubernetes/cloud-config

return err
}

func configureDockerDaemonProxy(ctx *util.Context) error {
if ctx.Cluster.Proxy.HTTPProxy == "" && ctx.Cluster.Proxy.HTTPSProxy == "" && ctx.Cluster.Proxy.NoProxy == "" {
return nil
}

ctx.Logger.Infoln("Configuring docker proxy…")
_, _, err := ctx.Runner.Run(dockerDaemonProxy, util.TemplateVariables{})

return err
}

const dockerDaemonProxy = `
# Configure HTTP/HTTPS proxy for Docker
sudo mkdir -p /etc/systemd/system/docker.service.d
cat <<EOF |sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
EnvironmentFile=/etc/environment/kubeone
EOF
sudo systemctl daemon-reload
if sudo systemctl status docker &>/dev/null; then sudo systemctl restart docker; fi
`

0 comments on commit f677a4a

Please sign in to comment.