Skip to content

Commit

Permalink
Network connectivity works, DNS doesn't
Browse files Browse the repository at this point in the history
  • Loading branch information
Minion3665 committed Feb 11, 2022
1 parent 71edec8 commit 5309aaf
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 27 deletions.
12 changes: 12 additions & 0 deletions containerManager/config/mini-test/10.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:

pkgs.dockerTools.buildLayeredImage {
name = "ping-google";
config = {
Cmd = [ "${pkgsLinux.iputils}/bin/ping" "google.com" ];
};

contents = with pkgsLinux; [ iputils ];
}
35 changes: 35 additions & 0 deletions containerManager/config/mini-test/8.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:
let
repo = pkgsLinux.stdenv.mkDerivation {
name = "repo";
src = pkgs.fetchFromGitHub {
owner = "Minion3665";
repo = "container";
rev = "production";
sha256 = "sha256-wGvftnTv+79lfnPpKeOSIr44pCqHEW02XVOxGpnXqaM=";
};

buildPhase = "echo 'No build phase'";
installPhase = ''
mkdir $out/src -p
cp $src/* $out/src -r
'';
};
DISCORD_TOKEN = (import /home/minion/Private/create-machine-programmers-discord-token.nix {}).token;
in pkgs.dockerTools.buildImage {
name = "discord-bot-runner";
config = {
Env = [
"DISCORD_TOKEN=${DISCORD_TOKEN}"
"PATH=${pkgsLinux.busybox}/bin:${pkgsLinux.nodejs-17_x}/bin"
];
Entrypoint = [ "${pkgsLinux.nodejs-17_x}/bin/npm" ];
Cmd = [ "run" "container" ];
WorkingDir = "${repo}/src";

};

contents = [ pkgsLinux.python3 pkgsLinux.busybox repo ];
}
12 changes: 12 additions & 0 deletions containerManager/config/mini-test/9.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:

pkgs.dockerTools.buildLayeredImage {
name = "ping-cloudflare-dns";
config = {
Cmd = [ "${pkgsLinux.iputils}/bin/ping" "1.1.1.1" ];
};

contents = with pkgsLinux; [ iputils ];
}
90 changes: 67 additions & 23 deletions containerManager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ func BuildContainer(id string, version string) error {
func RunContainer(id string, version string) error {
log.Println("Ready to run container " + id + " with version " + version)

network, err := cni.New()
network, err := cni.New(cni.WithPluginDir([]string{"/nix/store/84qpsw3nz2zahmz9xxvzbmf8sfdmk771-cni-plugins-1.0.1/bin"}))
if err != nil {
return err
}
} // See https://github.com/containerd/go-cni/search?q=WithPluginDir

if err := network.Load(cni.WithLoNetwork); err != nil {
return err
}
//if err := network.Load(cni.WithLoNetwork); err != nil {
// return err
//}
if err := network.Load(cni.WithConfFile("./containerManager/networking/bridge.json")); err != nil {
return err
}
// See https://github.com/containernetworking/cni/blob/master/SPEC.md for the format of the config
// Important for nix paths

log.Println("Created container network & loaded configuration")

Expand Down Expand Up @@ -163,9 +165,12 @@ func RunContainer(id string, version string) error {
return err
}

//defer func(file *os.File) {
// err := file.Close(); if err != nil { panic(err) }
//}(file)
defer func(file *os.File) {
err := file.Close()
if err != nil {
panic(err)
}
}(file)

log.Println("Opened container file for reading")

Expand Down Expand Up @@ -202,18 +207,11 @@ func RunContainer(id string, version string) error {
return err
}

//defer func(container containerd.Container, ctx context.Context, opts ...containerd.DeleteOpts) {
// if err := container.Delete(ctx, opts...); err != nil { panic(err) }
//}(container, ctx)

net, err := network.Setup(ctx, id+"-"+timestamp, fmt.Sprintf("/proc/%d/ns/net", os.Getpid()))
if err != nil {
return err
}
// Print out all the interfaces along with their IP addresses
for key, _ := range net.Interfaces {
log.Println(key)
}
defer func(container containerd.Container, ctx context.Context, opts ...containerd.DeleteOpts) {
if err := container.Delete(ctx, opts...); err != nil {
panic(err)
}
}(container, ctx)

log.Printf("Successfully loaded %s container\n", container.ID())

Expand All @@ -228,13 +226,59 @@ func RunContainer(id string, version string) error {
return err
}

//defer func(task containerd.Task, ctx context.Context, opts ...containerd.ProcessDeleteOpts) {
// if _, err := task.Delete(ctx, opts...); err != nil { panic(err) }
//}(task, ctx)
defer func(task containerd.Task, ctx context.Context, opts ...containerd.ProcessDeleteOpts) {
if _, err := task.Delete(ctx, opts...); err != nil {
panic(err)
}
}(task, ctx)

log.Println("Created run-task")
log.Println(task.Metrics(ctx))

netPath := fmt.Sprintf("/proc/%d/ns/net", task.Pid())
netId := id + "-" + timestamp

/*defer func(network cni.CNI, ctx context.Context, id string, path string, opts ...cni.NamespaceOpts) {
if err := network.Remove(ctx, id, path, opts...); err != nil { panic(err) }
}(network, ctx, netId, netPath)
// This isn't needed, as the container is deleted when the task is deleted
*/

net, err := network.Setup(ctx, netId, netPath)
if err != nil {
return err
}

// Print out all the interfaces along with their IP addresses
for key, data := range net.Interfaces {

var ipText string
var macText string
var sandboxText string

if len(data.IPConfigs) > 0 {
ipText = "got IP " + data.IPConfigs[0].IP.String()
} else {
ipText = "has no in-container IP"
}

if data.Mac != "" {
macText = ", MAC address " + data.Mac
} else {
macText = ", no MAC address"
}

if data.Sandbox != "" {
sandboxText = " and is using path " + data.Sandbox
} else {
sandboxText = " and doesn't have a sandbox"
}

log.Println("Interface " + key + " " + ipText + macText + sandboxText)
}

log.Println("Connected the container to networking")

// Run the container!
if err := task.Start(ctx); err != nil {
return err
Expand Down
23 changes: 19 additions & 4 deletions containerManager/networking/bridge.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
{
"cniVersion": "0.4.4",
"name": "bridge",
"type": "bridge"
}
"cniVersion": "0.3.1",
"name": "clicks-testnet-bridge",
"type": "bridge",
"bridge": "clicks-test0",
"isDefaultGateway": true,
"forceAddress": false,
"ipMasq": true,
"hairpinMode": true,
"ipam": {
"type": "host-local",
"subnet": "10.10.0.0/16"
},
"dns": {
"nameservers": [
"1.1.1.1",
"1.0.0.1"
]
}
}

0 comments on commit 5309aaf

Please sign in to comment.