-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proxy config setup when generating CAPI objects in apibuilder (#2262
) * Add http proxy support in apibuilder * Make NoProxyDefaults func * Move OS specific cmds to snow provider
- Loading branch information
1 parent
adf3fe1
commit ccc024f
Showing
15 changed files
with
577 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[Service] | ||
Environment="HTTP_PROXY={{.httpProxy}}" | ||
Environment="HTTPS_PROXY={{.httpsProxy}}" | ||
Environment="NO_PROXY={{ stringsJoin .noProxy "," }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package clusterapi | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
|
||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
|
||
"github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
"github.com/aws/eks-anywhere/pkg/templater" | ||
) | ||
|
||
//go:embed config/http-proxy.conf | ||
var proxyConfig string | ||
|
||
func NoProxyDefaults() []string { | ||
return []string{ | ||
"localhost", | ||
"127.0.0.1", | ||
".svc", | ||
} | ||
} | ||
|
||
func proxyConfigContent(cluster v1alpha1.ClusterSpec) (string, error) { | ||
capacity := len(cluster.ClusterNetwork.Pods.CidrBlocks) + | ||
len(cluster.ClusterNetwork.Services.CidrBlocks) + | ||
len(cluster.ProxyConfiguration.NoProxy) + 4 | ||
|
||
noProxyList := make([]string, 0, capacity) | ||
noProxyList = append(noProxyList, cluster.ClusterNetwork.Pods.CidrBlocks...) | ||
noProxyList = append(noProxyList, cluster.ClusterNetwork.Services.CidrBlocks...) | ||
noProxyList = append(noProxyList, cluster.ProxyConfiguration.NoProxy...) | ||
|
||
// Add no-proxy defaults | ||
noProxyList = append(noProxyList, NoProxyDefaults()...) | ||
noProxyList = append(noProxyList, cluster.ControlPlaneConfiguration.Endpoint.Host) | ||
|
||
val := values{ | ||
"httpProxy": cluster.ProxyConfiguration.HttpProxy, | ||
"httpsProxy": cluster.ProxyConfiguration.HttpsProxy, | ||
"noProxy": noProxyList, | ||
} | ||
|
||
config, err := templater.Execute(proxyConfig, val) | ||
if err != nil { | ||
return "", fmt.Errorf("building http-proxy.conf file: %v", err) | ||
} | ||
return string(config), nil | ||
} | ||
|
||
func proxyConfigFile(cluster v1alpha1.ClusterSpec) (bootstrapv1.File, error) { | ||
proxyConfig, err := proxyConfigContent(cluster) | ||
if err != nil { | ||
return bootstrapv1.File{}, err | ||
} | ||
|
||
return bootstrapv1.File{ | ||
Path: "/etc/systemd/system/containerd.service.d/http-proxy.conf", | ||
Owner: "root:root", | ||
Content: proxyConfig, | ||
}, nil | ||
} | ||
|
||
func SetProxyConfigInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, cluster v1alpha1.ClusterSpec) error { | ||
if cluster.ProxyConfiguration == nil { | ||
return nil | ||
} | ||
|
||
proxyConfigFile, err := proxyConfigFile(cluster) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kcp.Spec.KubeadmConfigSpec.Files = append(kcp.Spec.KubeadmConfigSpec.Files, proxyConfigFile) | ||
|
||
return nil | ||
} | ||
|
||
func SetProxyConfigInKubeadmConfigTemplate(kct *bootstrapv1.KubeadmConfigTemplate, cluster v1alpha1.ClusterSpec) error { | ||
if cluster.ProxyConfiguration == nil { | ||
return nil | ||
} | ||
|
||
proxyConfigFile, err := proxyConfigFile(cluster) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kct.Spec.Template.Spec.Files = append(kct.Spec.Template.Spec.Files, proxyConfigFile) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package clusterapi_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
|
||
"github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
"github.com/aws/eks-anywhere/pkg/clusterapi" | ||
) | ||
|
||
var proxyTests = []struct { | ||
name string | ||
proxy *v1alpha1.ProxyConfiguration | ||
wantFiles []bootstrapv1.File | ||
}{ | ||
{ | ||
name: "proxy config nil", | ||
proxy: nil, | ||
wantFiles: []bootstrapv1.File{}, | ||
}, | ||
{ | ||
name: "with proxy, pods cidr, service cidr, cp endpoint", | ||
proxy: &v1alpha1.ProxyConfiguration{ | ||
HttpProxy: "1.2.3.4:8888", | ||
HttpsProxy: "1.2.3.4:8888", | ||
NoProxy: []string{ | ||
"1.2.3.4/0", | ||
"1.2.3.5/0", | ||
}, | ||
}, | ||
wantFiles: []bootstrapv1.File{ | ||
{ | ||
Path: "/etc/systemd/system/containerd.service.d/http-proxy.conf", | ||
Owner: "root:root", | ||
Content: `[Service] | ||
Environment="HTTP_PROXY=1.2.3.4:8888" | ||
Environment="HTTPS_PROXY=1.2.3.4:8888" | ||
Environment="NO_PROXY=1.2.3.4/5,1.2.3.4/5,1.2.3.4/0,1.2.3.5/0,localhost,127.0.0.1,.svc,1.2.3.4"`, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestSetProxyConfigInKubeadmControlPlane(t *testing.T) { | ||
for _, tt := range proxyTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := newApiBuilerTest(t) | ||
got := wantKubeadmControlPlane() | ||
g.clusterSpec.Cluster.Spec.ProxyConfiguration = tt.proxy | ||
g.Expect(clusterapi.SetProxyConfigInKubeadmControlPlane(got, g.clusterSpec.Cluster.Spec)).To(Succeed()) | ||
want := wantKubeadmControlPlane() | ||
want.Spec.KubeadmConfigSpec.Files = tt.wantFiles | ||
g.Expect(got).To(Equal(want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestSetProxyConfigInKubeadmConfigTemplate(t *testing.T) { | ||
for _, tt := range proxyTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := newApiBuilerTest(t) | ||
got := wantKubeadmConfigTemplate() | ||
g.clusterSpec.Cluster.Spec.ProxyConfiguration = tt.proxy | ||
g.Expect(clusterapi.SetProxyConfigInKubeadmConfigTemplate(got, g.clusterSpec.Cluster.Spec)).To(Succeed()) | ||
want := wantKubeadmConfigTemplate() | ||
want.Spec.Template.Spec.Files = tt.wantFiles | ||
g.Expect(got).To(Equal(want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestNoProxyDefaults(t *testing.T) { | ||
g := NewWithT(t) | ||
want := []string{ | ||
"localhost", | ||
"127.0.0.1", | ||
".svc", | ||
} | ||
g.Expect(clusterapi.NoProxyDefaults()).To(Equal(want)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package clusterapi | ||
|
||
import ( | ||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
|
||
"github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
) | ||
|
||
var buildContainerdConfigCommands = []string{ | ||
"cat /etc/containerd/config_append.toml >> /etc/containerd/config.toml", | ||
} | ||
|
||
var restartContainerdCommands = []string{ | ||
"sudo systemctl daemon-reload", | ||
"sudo systemctl restart containerd", | ||
} | ||
|
||
func CreateContainerdConfigFileInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, cluster v1alpha1.ClusterSpec) { | ||
if cluster.RegistryMirrorConfiguration != nil { | ||
kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands, buildContainerdConfigCommands...) | ||
} | ||
} | ||
|
||
func CreateContainerdConfigFileInKubeadmConfigTemplate(kct *bootstrapv1.KubeadmConfigTemplate, cluster v1alpha1.ClusterSpec) { | ||
if cluster.RegistryMirrorConfiguration != nil { | ||
kct.Spec.Template.Spec.PreKubeadmCommands = append(kct.Spec.Template.Spec.PreKubeadmCommands, buildContainerdConfigCommands...) | ||
} | ||
} | ||
|
||
func RestartContainerdInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, cluster v1alpha1.ClusterSpec) { | ||
if restartContainerdNeeded(cluster) { | ||
kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands, restartContainerdCommands...) | ||
} | ||
} | ||
|
||
func RestartContainerdInKubeadmConfigTemplate(kct *bootstrapv1.KubeadmConfigTemplate, cluster v1alpha1.ClusterSpec) { | ||
if restartContainerdNeeded(cluster) { | ||
kct.Spec.Template.Spec.PreKubeadmCommands = append(kct.Spec.Template.Spec.PreKubeadmCommands, restartContainerdCommands...) | ||
} | ||
} | ||
|
||
func restartContainerdNeeded(cluster v1alpha1.ClusterSpec) bool { | ||
return cluster.RegistryMirrorConfiguration != nil || cluster.ProxyConfiguration != nil | ||
} |
Oops, something went wrong.