-
Notifications
You must be signed in to change notification settings - Fork 288
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
Add proxy config setup when generating CAPI objects in apibuilder #2262
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
559a19d
Add http proxy support in apibuilder
jiayiwang7 128b272
Merge branch 'main' into snow-proxy
jiayiwang7 837300b
Make NoProxyDefaults func
jiayiwang7 8e4e1fc
Merge branch 'main' into snow-proxy
jiayiwang7 437421f
Move OS specific cmds to snow provider
jiayiwang7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment here about different OS
I believe this works differently for Bottlerocket (for example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proxy config setup in provider now