-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add http proxy support in apibuilder
- Loading branch information
1 parent
8f5db6e
commit 559a19d
Showing
14 changed files
with
330 additions
and
18 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
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,29 @@ | ||
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 restartContainerdCommands = []string{ | ||
"sudo systemctl daemon-reload", | ||
"sudo systemctl restart containerd", | ||
} | ||
|
||
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 | ||
} |
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,75 @@ | ||
package clusterapi_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
"github.com/aws/eks-anywhere/pkg/clusterapi" | ||
) | ||
|
||
var kubeadmCommandsTests = []struct { | ||
name string | ||
cluster v1alpha1.ClusterSpec | ||
want []string | ||
}{ | ||
{ | ||
name: "registry mirror and proxy config both exist", | ||
cluster: v1alpha1.ClusterSpec{ | ||
RegistryMirrorConfiguration: nil, | ||
ProxyConfiguration: nil, | ||
}, | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "registry mirror nil", | ||
cluster: v1alpha1.ClusterSpec{ | ||
RegistryMirrorConfiguration: nil, | ||
ProxyConfiguration: &v1alpha1.ProxyConfiguration{ | ||
HttpProxy: "1.2.3.4:8888", | ||
HttpsProxy: "1.2.3.4:8888", | ||
NoProxy: []string{ | ||
"1.2.3.4/0", | ||
}, | ||
}, | ||
}, | ||
want: restartContainerdCommands, | ||
}, | ||
{ | ||
name: "proxy config nil", | ||
cluster: v1alpha1.ClusterSpec{ | ||
RegistryMirrorConfiguration: &v1alpha1.RegistryMirrorConfiguration{ | ||
Endpoint: "1.2.3.4", | ||
}, | ||
ProxyConfiguration: nil, | ||
}, | ||
want: restartContainerdCommands, | ||
}, | ||
} | ||
|
||
func TestRestartContainerdInKubeadmControlPlane(t *testing.T) { | ||
for _, tt := range kubeadmCommandsTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := newApiBuilerTest(t) | ||
got := wantKubeadmControlPlane() | ||
clusterapi.RestartContainerdInKubeadmControlPlane(got, tt.cluster) | ||
want := wantKubeadmControlPlane() | ||
want.Spec.KubeadmConfigSpec.PreKubeadmCommands = tt.want | ||
g.Expect(got).To(Equal(want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestRestartContainerdInKubeadmConfigTemplate(t *testing.T) { | ||
for _, tt := range kubeadmCommandsTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := newApiBuilerTest(t) | ||
got := wantKubeadmConfigTemplate() | ||
clusterapi.RestartContainerdInKubeadmConfigTemplate(got, tt.cluster) | ||
want := wantKubeadmConfigTemplate() | ||
want.Spec.Template.Spec.PreKubeadmCommands = tt.want | ||
g.Expect(got).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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 | ||
|
||
var NoProxyDefaults = []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,75 @@ | ||
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 | ||
wantCmd []string | ||
}{ | ||
{ | ||
name: "proxy config nil", | ||
proxy: nil, | ||
wantFiles: []bootstrapv1.File{}, | ||
wantCmd: []string{}, | ||
}, | ||
{ | ||
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"`, | ||
}, | ||
}, | ||
wantCmd: restartContainerdCommands, | ||
}, | ||
} | ||
|
||
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)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.