Skip to content

Commit abb8651

Browse files
feat: Support infra-specific httpproxy patches (#141)
Co-authored-by: Jimmi Dyson <[email protected]>
1 parent c76e4b1 commit abb8651

File tree

2 files changed

+125
-6
lines changed

2 files changed

+125
-6
lines changed

pkg/handlers/httpproxy/inject.go

Lines changed: 40 additions & 6 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 {
@@ -189,17 +193,47 @@ func generateNoProxy(cluster *capiv1.Cluster) []string {
189193
serviceDomain = cluster.Spec.ClusterNetwork.ServiceDomain
190194
}
191195

192-
noProxy = append(noProxy, []string{
196+
noProxy = append(
197+
noProxy,
193198
"kubernetes",
194199
"kubernetes.default",
195200
".svc",
196-
}...)
197-
198-
// append .svc.<SERVICE_DOMAIN>
199-
noProxy = append(
200-
noProxy,
201+
// append .svc.<SERVICE_DOMAIN>
201202
fmt.Sprintf(".svc.%s", strings.TrimLeft(serviceDomain, ".")),
202203
)
203204

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

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)