Skip to content

Commit 1bac304

Browse files
committed
feat: Support infra-specific httpproxy patches
Signed-off-by: Daniel Lipovetsky <[email protected]>
1 parent c76e4b1 commit 1bac304

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

pkg/handlers/httpproxy/inject.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import (
3131
const (
3232
// HandlerNamePatch is the name of the inject handler.
3333
HandlerNamePatch = "HTTPProxyPatch"
34+
35+
// instanceMetadataIP is the IPv4 address used to retrieve
36+
// instance metadata in AWS, Azure, OpenStack, etc.
37+
instanceMetadataIP = "169.254.169.254"
3438
)
3539

3640
type httpProxyPatchHandler struct {
@@ -201,5 +205,34 @@ func generateNoProxy(cluster *capiv1.Cluster) []string {
201205
fmt.Sprintf(".svc.%s", strings.TrimLeft(serviceDomain, ".")),
202206
)
203207

208+
if cluster.Spec.InfrastructureRef == nil {
209+
return noProxy
210+
}
211+
212+
// Add infra-specific entries
213+
switch cluster.Spec.InfrastructureRef.Kind {
214+
case "AWSCluster", "AWSManagedCluster":
215+
noProxy = append(noProxy,
216+
// Exclude the instance metadata service
217+
instanceMetadataIP,
218+
// Exclude the control plane endpoint
219+
".elb.amazonaws.com",
220+
)
221+
case "AzureCluster", "AzureManagedControlPlane":
222+
noProxy = append(noProxy,
223+
// Exclude the instance metadata service
224+
instanceMetadataIP)
225+
case "GCPCluster":
226+
noProxy = append(noProxy,
227+
// Exclude the instance metadata service
228+
instanceMetadataIP,
229+
// Exclude aliases for instance metadata service.
230+
// See https://cloud.google.com/vpc/docs/special-configurations
231+
"metadata",
232+
"metadata.google.internal",
233+
)
234+
default:
235+
// Unknown infrastructure. Do nothing.
236+
}
204237
return noProxy
205238
}

pkg/handlers/httpproxy/inject_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
. "github.com/onsi/gomega"
10+
v1 "k8s.io/api/core/v1"
1011
"k8s.io/apiserver/pkg/storage/names"
1112
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1213
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -134,6 +135,90 @@ func TestGenerateNoProxy(t *testing.T) {
134135
"kubernetes.default", ".svc", ".svc.cluster.local",
135136
},
136137
},
138+
{
139+
name: "Unknown infrastructure cluster",
140+
cluster: &capiv1.Cluster{
141+
Spec: capiv1.ClusterSpec{
142+
InfrastructureRef: &v1.ObjectReference{
143+
Kind: "SomeFakeInfrastructureCluster",
144+
},
145+
},
146+
},
147+
expectedNoProxy: []string{
148+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
149+
".svc", ".svc.cluster.local",
150+
},
151+
},
152+
{
153+
name: "AWS cluster",
154+
cluster: &capiv1.Cluster{
155+
Spec: capiv1.ClusterSpec{
156+
InfrastructureRef: &v1.ObjectReference{
157+
Kind: "AWSCluster",
158+
},
159+
},
160+
},
161+
expectedNoProxy: []string{
162+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
163+
".svc", ".svc.cluster.local", "169.254.169.254", ".elb.amazonaws.com",
164+
},
165+
},
166+
{
167+
name: "AWS managed (EKS) cluster",
168+
cluster: &capiv1.Cluster{
169+
Spec: capiv1.ClusterSpec{
170+
InfrastructureRef: &v1.ObjectReference{
171+
Kind: "AWSManagedCluster",
172+
},
173+
},
174+
},
175+
expectedNoProxy: []string{
176+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
177+
".svc", ".svc.cluster.local", "169.254.169.254", ".elb.amazonaws.com",
178+
},
179+
},
180+
{
181+
name: "Azure cluster",
182+
cluster: &capiv1.Cluster{
183+
Spec: capiv1.ClusterSpec{
184+
InfrastructureRef: &v1.ObjectReference{
185+
Kind: "AzureCluster",
186+
},
187+
},
188+
},
189+
expectedNoProxy: []string{
190+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
191+
".svc", ".svc.cluster.local", "169.254.169.254",
192+
},
193+
},
194+
{
195+
name: "Azure managed (AKS) cluster",
196+
cluster: &capiv1.Cluster{
197+
Spec: capiv1.ClusterSpec{
198+
InfrastructureRef: &v1.ObjectReference{
199+
Kind: "AzureCluster",
200+
},
201+
},
202+
},
203+
expectedNoProxy: []string{
204+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
205+
".svc", ".svc.cluster.local", "169.254.169.254",
206+
},
207+
},
208+
{
209+
name: "GCP cluster",
210+
cluster: &capiv1.Cluster{
211+
Spec: capiv1.ClusterSpec{
212+
InfrastructureRef: &v1.ObjectReference{
213+
Kind: "GCPCluster",
214+
},
215+
},
216+
},
217+
expectedNoProxy: []string{
218+
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
219+
".svc", ".svc.cluster.local", "169.254.169.254", "metadata", "metadata.google.internal",
220+
},
221+
},
137222
{
138223
name: "custom service network",
139224
cluster: &capiv1.Cluster{

0 commit comments

Comments
 (0)