Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Hepto
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ACIDES
Hepto
Commits
dcb08fd2
Commit
dcb08fd2
authored
1 year ago
by
kaiyou
Browse files
Options
Downloads
Patches
Plain Diff
Replace the unix proxy with an cross-namespace proxy
parent
0dedfd6a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmd/hepto/service.go
+58
-3
58 additions, 3 deletions
cmd/hepto/service.go
with
58 additions
and
3 deletions
cmd/hepto/service.go
+
58
−
3
View file @
dcb08fd2
package
hepto
package
hepto
import
(
import
(
"encoding/json"
"fmt"
"fmt"
"io"
"io/ioutil"
"net"
"net"
"os"
"os"
"os/exec"
"os/exec"
...
@@ -13,6 +16,7 @@ import (
...
@@ -13,6 +16,7 @@ import (
"github.com/spf13/viper"
"github.com/spf13/viper"
"go.acides.org/hepto/services"
"go.acides.org/hepto/services"
"go.acides.org/selfcontain"
"go.acides.org/selfcontain"
"golang.org/x/sys/unix"
"k8s.io/component-base/version/verflag"
"k8s.io/component-base/version/verflag"
"k8s.io/component-helpers/node/util/sysctl"
"k8s.io/component-helpers/node/util/sysctl"
)
)
...
@@ -83,13 +87,64 @@ var Start = &cobra.Command{
...
@@ -83,13 +87,64 @@ var Start = &cobra.Command{
go
func
()
{
go
func
()
{
errChan
<-
selfcontain
.
RunWithArgs
(
&
config
.
Container
,
newArgs
)
errChan
<-
selfcontain
.
RunWithArgs
(
&
config
.
Container
,
newArgs
)
}()
}()
go
apiserverForward
(
errChan
)
go
func
()
{
// Wait so that the container is started and its state file exists
time
.
Sleep
(
5
*
time
.
Second
)
errChan
<-
apiserverForward
()
}()
return
<-
errChan
return
<-
errChan
},
},
}
}
func
apiserverForward
(
chan
error
)
{
// This is the apiserver forward hack, which proxifies the apiserver
// do nothing for now
// from the host to the container
// Everything is wrong here: so many magic numbers and constants, poor error
// management, this function has to go away at some point
func
apiserverForward
()
error
{
// Parsing container config and getting the namespace
statePath
:=
path
.
Join
(
config
.
Container
.
Data
,
config
.
Container
.
Name
,
"state.json"
)
stateBytes
,
err
:=
ioutil
.
ReadFile
(
statePath
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"could not read container state: %w"
,
err
)
}
var
state
map
[
string
]
interface
{}
json
.
Unmarshal
(
stateBytes
,
&
state
)
netnsPath
:=
state
[
"namespace_paths"
]
.
(
map
[
string
]
interface
{})[
"NEWNET"
]
.
(
string
)
netns
,
err
:=
unix
.
Open
(
netnsPath
,
unix
.
O_RDONLY
|
unix
.
O_CLOEXEC
,
0
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"could not get netns: %w"
,
err
)
}
// Getting the current netns
curns
,
err
:=
unix
.
Open
(
fmt
.
Sprintf
(
"/proc/%d/ns/net"
,
os
.
Getpid
()),
unix
.
O_RDONLY
|
unix
.
O_CLOEXEC
,
0
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"could not get current netns: %w"
,
err
)
}
// Actually running the proxy
l
,
err
:=
net
.
Listen
(
"tcp"
,
"[::1]:6443"
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"could not listen on tcp: %w"
,
err
)
}
config
.
Cluster
.
Logger
.
Info
(
"listening on port"
,
"port"
,
l
.
Addr
()
.
String
())
for
{
tcp
,
err
:=
l
.
Accept
()
if
err
!=
nil
{
config
.
Cluster
.
Logger
.
Error
(
err
,
"error on tcp socket"
)
}
go
func
()
{
defer
tcp
.
Close
()
// Open a socket in the apiserver namespace
unix
.
Setns
(
netns
,
unix
.
CLONE_NEWNET
)
proxy
,
err
:=
net
.
Dial
(
"tcp"
,
"[::1]:6443"
)
unix
.
Setns
(
curns
,
unix
.
CLONE_NEWNET
)
if
err
!=
nil
{
config
.
Cluster
.
Logger
.
Error
(
err
,
"cannot dial to container socket"
)
return
}
defer
proxy
.
Close
()
go
io
.
Copy
(
tcp
,
proxy
)
io
.
Copy
(
proxy
,
tcp
)
}()
}
}
}
var
Run
=
&
cobra
.
Command
{
var
Run
=
&
cobra
.
Command
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment