Skip to content

feat: expose GenerateNoProxy func #594

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

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions api/v1alpha1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,6 @@ type Etcd struct {
Image *Image `json:"image,omitempty"`
}

// HTTPProxy required for providing proxy configuration.
type HTTPProxy struct {
// HTTP proxy value.
HTTP string `json:"http,omitempty"`

// HTTPS proxy value.
HTTPS string `json:"https,omitempty"`

// AdditionalNo Proxy list that will be added to the automatically calculated
// values that will apply no_proxy configuration for cluster internal network.
// Default values: localhost,127.0.0.1,<POD_NETWORK>,<SERVICE_NETWORK>,kubernetes
// ,kubernetes.default,.svc,.svc.<SERVICE_DOMAIN>
AdditionalNo []string `json:"additionalNo"`
}

type RegistryCredentials struct {
// A reference to the Secret containing the registry credentials and optional CA certificate
// using the keys `username`, `password` and `ca.crt`.
Expand Down
102 changes: 102 additions & 0 deletions api/v1alpha1/http_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"fmt"
"strings"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
// instanceMetadataIP is the IPv4 address used to retrieve
// instance metadata in AWS, Azure, OpenStack, etc.
instanceMetadataIP = "169.254.169.254"
)

// HTTPProxy required for providing proxy configuration.
type HTTPProxy struct {
// HTTP proxy value.
HTTP string `json:"http,omitempty"`

// HTTPS proxy value.
HTTPS string `json:"https,omitempty"`

// AdditionalNo Proxy list that will be added to the automatically calculated
// values that will apply no_proxy configuration for cluster internal network.
// Default values: localhost,127.0.0.1,<POD_NETWORK>,<SERVICE_NETWORK>,kubernetes
// ,kubernetes.default,.svc,.svc.<SERVICE_DOMAIN>
AdditionalNo []string `json:"additionalNo"`
}

// GenerateNoProxy creates default NO_PROXY values that should be applied on cluster
// in any environment and are preventing the use of proxy for cluster internal
// networking. It appends additional values from HTTPProxy.AdditionalNo.
func (p *HTTPProxy) GenerateNoProxy(cluster *clusterv1.Cluster) []string {
noProxy := []string{
"localhost",
"127.0.0.1",
}

if cluster.Spec.ClusterNetwork != nil &&
cluster.Spec.ClusterNetwork.Pods != nil {
noProxy = append(noProxy, cluster.Spec.ClusterNetwork.Pods.CIDRBlocks...)
}

if cluster.Spec.ClusterNetwork != nil &&
cluster.Spec.ClusterNetwork.Services != nil {
noProxy = append(noProxy, cluster.Spec.ClusterNetwork.Services.CIDRBlocks...)
}

serviceDomain := "cluster.local"
if cluster.Spec.ClusterNetwork != nil &&
cluster.Spec.ClusterNetwork.ServiceDomain != "" {
serviceDomain = cluster.Spec.ClusterNetwork.ServiceDomain
}

noProxy = append(
noProxy,
"kubernetes",
"kubernetes.default",
".svc",
// append .svc.<SERVICE_DOMAIN>
fmt.Sprintf(".svc.%s", strings.TrimLeft(serviceDomain, ".")),
)

if cluster.Spec.InfrastructureRef == nil {
return append(noProxy, p.AdditionalNo...)
}

// Add infra-specific entries
switch cluster.Spec.InfrastructureRef.Kind {
case "AWSCluster", "AWSManagedCluster":
noProxy = append(
noProxy,
// Exclude the instance metadata service
instanceMetadataIP,
// Exclude the control plane endpoint
".elb.amazonaws.com",
)
case "AzureCluster", "AzureManagedControlPlane":
noProxy = append(
noProxy,
// Exclude the instance metadata service
instanceMetadataIP,
)
case "GCPCluster":
noProxy = append(
noProxy,
// Exclude the instance metadata service
instanceMetadataIP,
// Exclude aliases for instance metadata service.
// See https://cloud.google.com/vpc/docs/special-configurations
"metadata",
"metadata.google.internal",
)
default:
// Unknown infrastructure. Do nothing.
}
return append(noProxy, p.AdditionalNo...)
}
195 changes: 195 additions & 0 deletions api/v1alpha1/http_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1_test

import (
"testing"

"github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
)

func TestGenerateNoProxy(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
cluster *clusterv1.Cluster
expectedNoProxy []string
additonalNo []string
}{{
name: "no networking config",
cluster: &clusterv1.Cluster{},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local",
},
}, {
name: "no networking config with additional no proxy",
cluster: &clusterv1.Cluster{},
additonalNo: []string{"example.com"},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "example.com",
},
}, {
name: "custom pod network",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
ClusterNetwork: &clusterv1.ClusterNetwork{
Pods: &clusterv1.NetworkRanges{
CIDRBlocks: []string{"10.0.0.0/24", "10.0.1.0/24"},
},
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "10.0.0.0/24", "10.0.1.0/24", "kubernetes",
"kubernetes.default", ".svc", ".svc.cluster.local",
},
}, {
name: "Unknown infrastructure cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "SomeFakeInfrastructureCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local",
},
}, {
name: "AWS cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "AWSCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "169.254.169.254", ".elb.amazonaws.com",
},
}, {
name: "AWS managed (EKS) cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "AWSManagedCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "169.254.169.254", ".elb.amazonaws.com",
},
}, {
name: "Azure cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "AzureCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "169.254.169.254",
},
}, {
name: "Azure managed (AKS) cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "AzureCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "169.254.169.254",
},
}, {
name: "GCP cluster",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &v1.ObjectReference{
Kind: "GCPCluster",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.cluster.local", "169.254.169.254", "metadata", "metadata.google.internal",
},
}, {
name: "custom service network",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
ClusterNetwork: &clusterv1.ClusterNetwork{
Services: &clusterv1.NetworkRanges{
CIDRBlocks: []string{"172.16.0.0/24", "172.16.1.0/24"},
},
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "172.16.0.0/24", "172.16.1.0/24", "kubernetes",
"kubernetes.default", ".svc", ".svc.cluster.local",
},
}, {
name: "custom servicedomain",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
ClusterNetwork: &clusterv1.ClusterNetwork{
ServiceDomain: "foo.bar",
},
},
},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "kubernetes", "kubernetes.default",
".svc", ".svc.foo.bar",
},
}, {
name: "all options",
cluster: &clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
ClusterNetwork: &clusterv1.ClusterNetwork{
Pods: &clusterv1.NetworkRanges{
CIDRBlocks: []string{"10.10.0.0/16"},
},
Services: &clusterv1.NetworkRanges{
CIDRBlocks: []string{"172.16.0.0/16"},
},
ServiceDomain: "foo.bar",
},
},
},
additonalNo: []string{"example.com"},
expectedNoProxy: []string{
"localhost", "127.0.0.1", "10.10.0.0/16", "172.16.0.0/16", "kubernetes",
"kubernetes.default", ".svc", ".svc.foo.bar", "example.com",
},
}}

for idx := range testCases {
tt := testCases[idx]

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

g := gomega.NewWithT(t)

g.Expect((&v1alpha1.HTTPProxy{
AdditionalNo: tt.additonalNo,
}).GenerateNoProxy(tt.cluster)).To(gomega.Equal(tt.expectedNoProxy))
})
}
}
Loading
Loading