Skip to content

Commit

Permalink
Add proxy port to CNP if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Marie Roque committed Feb 14, 2024
1 parent da81b1c commit 60cda30
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add proxy port to CiliumNetworkPolicy if needed.

## [4.67.3] - 2024-02-13

### Added
Expand Down
1 change: 1 addition & 0 deletions service/controller/clusterapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func New(config Config) ([]resource.Interface, error) {
{
c := ciliumnetpol.Config{
DynamicK8sClient: config.DynamicK8sClient,
Proxy: config.Proxy,
Logger: config.Logger,
}

Expand Down
1 change: 1 addition & 0 deletions service/controller/managementcluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) {
{
c := ciliumnetpol.Config{
DynamicK8sClient: config.DynamicK8sClient,
Proxy: config.Proxy,
Logger: config.Logger,
}

Expand Down
57 changes: 38 additions & 19 deletions service/controller/resource/ciliumnetpol/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ciliumnetpol

import (
"net/url"
"os"
"reflect"

"github.com/giantswarm/microerror"
Expand All @@ -19,6 +21,7 @@ const (

type Config struct {
DynamicK8sClient dynamic.Interface
Proxy func(reqURL *url.URL) (*url.URL, error)
Logger micrologger.Logger
}

Expand Down Expand Up @@ -46,6 +49,34 @@ func toCiliumNetworkPolicy(v interface{}) (*unstructured.Unstructured, error) {
return nil, microerror.Mask(err)
}

ports := []map[string]string{
{
"port": "443",
},
// Grafana cloud mimir port
{
"port": "6443",
},
// Grafana cloud squid proxy port
{
"port": "3128",
},
}
// We need to retrieve the proxy port from the environment variables
// and add it to the CiliumNetworkPolicy.
proxyURL := os.Getenv("HTTP_PROXY")
if proxyURL != "" {
proxyURL, err := url.Parse(proxyURL)
if err != nil {
return nil, microerror.Mask(err)
}
proxyPort := proxyURL.Port()
if proxyPort == "" {
proxyPort = "80"
}
ports = append(ports, map[string]string{"port": proxyPort})
}

ciliumNetworkPolicy := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "cilium.io/v2",
Expand All @@ -64,44 +95,32 @@ func toCiliumNetworkPolicy(v interface{}) (*unstructured.Unstructured, error) {
},
},
"egress": []map[string]interface{}{
map[string]interface{}{
{
"toEntities": []string{
"kube-apiserver",
"cluster",
},
},
map[string]interface{}{
{
"toEntities": []string{
"world",
},
"toPorts": []map[string]interface{}{
map[string]interface{}{
"ports": []map[string]string{
map[string]string{
"port": "443",
},
// Grafana cloud mimir port
map[string]string{
"port": "6443",
},
// Grafana cloud squid proxy port
map[string]string{
"port": "3128",
},
},
{
"ports": ports,
},
},
},
},
"ingress": []map[string]interface{}{
map[string]interface{}{
{
"fromEntities": []string{
"cluster",
},
"toPorts": []map[string]interface{}{
map[string]interface{}{
{
"ports": []map[string]string{
map[string]string{
{
"port": "9090",
},
},
Expand Down

0 comments on commit 60cda30

Please sign in to comment.