Skip to content

Commit e6f294e

Browse files
aleoliadamjensenbot
authored andcommitted
golang http proxy
1 parent 7abcb94 commit e6f294e

File tree

11 files changed

+285
-94
lines changed

11 files changed

+285
-94
lines changed

.github/workflows/integration.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ jobs:
9999
id: set-architectures
100100
run: |
101101
ARCHITECTURES=${{ needs.configure.outputs.architectures }}
102-
if [ "${{ matrix.component }}" == "proxy" ]; then
103-
ARCHITECTURES=$(echo ${ARCHITECTURES} | sed 's/,linux\/arm\/v7//')
104-
fi
105102
echo "ARCHITECTURES=${ARCHITECTURES}" >> $GITHUB_ENV
106103
- name: Set up QEMU
107104
uses: docker/[email protected]

build/proxy/Dockerfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/proxy/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package proxy contains the logic for the Liqo proxy.
16+
package main

cmd/proxy/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"context"
19+
"flag"
20+
"os"
21+
22+
"k8s.io/klog/v2"
23+
24+
"github.com/liqotech/liqo/pkg/proxy"
25+
)
26+
27+
func main() {
28+
ctx := context.Background()
29+
30+
port := flag.Int("port", 8080, "port to listen on")
31+
allowedHosts := flag.String("allowed-hosts", "", "comma separated list of allowed hosts")
32+
forceHost := flag.String("force-host", "", "force the server Host to this value")
33+
34+
flag.Parse()
35+
36+
p := proxy.New(*allowedHosts, *port, *forceHost)
37+
38+
if err := p.Start(ctx); err != nil {
39+
klog.Error(err)
40+
os.Exit(1)
41+
}
42+
}

deployments/liqo/templates/liqo-proxy-configmap.yaml

Lines changed: 0 additions & 80 deletions
This file was deleted.

deployments/liqo/templates/liqo-proxy-deployment.yaml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,17 @@ spec:
3636
ports:
3737
- containerPort: {{ .Values.proxy.config.listeningPort }}
3838
resources: {{- toYaml .Values.proxy.pod.resources | nindent 12 }}
39-
volumeMounts:
40-
- mountPath: /etc/envoy/envoy.yaml
41-
name: config-volume
42-
subPath: config
43-
{{- if or .Values.common.extraArgs .Values.proxy.pod.extraArgs }}
4439
args:
40+
- --port={{ .Values.proxy.config.listeningPort }}
41+
- --force-host=kubernetes.default.svc:443
42+
{{- if or .Values.common.extraArgs .Values.proxy.pod.extraArgs }}
4543
{{- if .Values.common.extraArgs }}
4644
{{- toYaml .Values.common.extraArgs | nindent 10 }}
4745
{{- end }}
4846
{{- if .Values.proxy.pod.extraArgs }}
4947
{{- toYaml .Values.proxy.pod.extraArgs | nindent 10 }}
5048
{{- end }}
5149
{{- end }}
52-
volumes:
53-
- name: config-volume
54-
configMap:
55-
name: {{ include "liqo.prefixedName" $proxyConfig }}
5650
{{- if ((.Values.common).nodeSelector) }}
5751
nodeSelector:
5852
{{- toYaml .Values.common.nodeSelector | nindent 8 }}

docs/advanced/k8s-api-server-proxy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This feature is **internally** used by the [in-band peering](UsagePeeringInBand)
88
If you just need to peer two clusters without publicly exposing the Kubernetes API server, you can use the [in-band peering](UsagePeeringInBand).
99
```
1010

11-
The Kubernetes API Server Proxy is an Envoy HTTP server that accepts [HTTP Connect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT) requests and forwards them to the Kubernetes API Server of the local cluster.
11+
The Kubernetes API Server Proxy is an HTTP server that accepts [HTTP Connect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT) requests and forwards them to the Kubernetes API Server of the local cluster.
1212
It just proxy the requests to the API server and it has no permission on the local cluster.
1313
This means that, as usual, all the requesters must authenticate with the Kubernetes API Server to access the resources.
1414

pkg/proxy/connect.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package proxy
16+
17+
import (
18+
"bufio"
19+
"io"
20+
"net"
21+
"net/http"
22+
"time"
23+
24+
"k8s.io/klog/v2"
25+
)
26+
27+
func (p *Proxy) handleConnect(c net.Conn) {
28+
br := bufio.NewReader(c)
29+
req, err := http.ReadRequest(br)
30+
if err != nil {
31+
klog.Errorf("error reading request: %v", err)
32+
return
33+
}
34+
35+
if req.Method != http.MethodConnect {
36+
response := &http.Response{
37+
StatusCode: http.StatusMethodNotAllowed,
38+
ProtoMajor: 1,
39+
ProtoMinor: 1,
40+
}
41+
if err := response.Write(c); err != nil {
42+
klog.Errorf("error writing response: %v", err)
43+
}
44+
if err := c.Close(); err != nil {
45+
klog.Errorf("error closing connection: %v", err)
46+
}
47+
return
48+
}
49+
50+
if p.ForceHost == "" && !p.isAllowed(req.URL.Host) {
51+
klog.Infof("host %s is not allowed", req.URL.Host)
52+
53+
response := &http.Response{
54+
StatusCode: http.StatusForbidden,
55+
ProtoMajor: 1,
56+
ProtoMinor: 1,
57+
}
58+
if err := response.Write(c); err != nil {
59+
klog.Errorf("error writing response: %v", err)
60+
}
61+
return
62+
}
63+
64+
klog.Infof("handling CONNECT to %s", req.URL.Host)
65+
66+
response := &http.Response{
67+
StatusCode: 200,
68+
ProtoMajor: 1,
69+
ProtoMinor: 1,
70+
}
71+
if err := response.Write(c); err != nil {
72+
klog.Errorf("error writing response: %v", err)
73+
if err := c.Close(); err != nil {
74+
klog.Errorf("error closing connection: %v", err)
75+
}
76+
return
77+
}
78+
79+
destConn, err := net.DialTimeout("tcp", p.getHost(req), 30*time.Second)
80+
if err != nil {
81+
klog.Errorf("error dialing destination: %v", err)
82+
83+
response := &http.Response{
84+
StatusCode: http.StatusRequestTimeout,
85+
ProtoMajor: 1,
86+
ProtoMinor: 1,
87+
}
88+
if err := response.Write(c); err != nil {
89+
klog.Errorf("error writing response: %v", err)
90+
}
91+
return
92+
}
93+
94+
go transfer(destConn, c)
95+
go transfer(c, destConn)
96+
}
97+
98+
func (p *Proxy) getHost(req *http.Request) string {
99+
if p.ForceHost != "" {
100+
return p.ForceHost
101+
}
102+
return req.URL.Host
103+
}
104+
105+
func transfer(destination io.WriteCloser, source io.ReadCloser) {
106+
defer destination.Close()
107+
defer source.Close()
108+
_, err := io.Copy(destination, source)
109+
if err != nil {
110+
klog.Errorf("error copying data: %v", err)
111+
}
112+
}

pkg/proxy/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package proxy contains the logic for the Liqo proxy.
16+
package proxy

0 commit comments

Comments
 (0)