Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #75 from ibuildthecloud/new-net
Browse files Browse the repository at this point in the history
Delete unneeded code in new networking design
  • Loading branch information
ibuildthecloud authored Oct 31, 2016
2 parents 04b660f + bccdb78 commit 9bbb7bc
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 364 deletions.
71 changes: 71 additions & 0 deletions core/compute/cni_wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// +build linux freebsd solaris openbsd darwin

package compute

import (
"context"
"fmt"
"strings"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)

// This approach was adopted from Weave's weaveproxy component. Proper credit goes to them.

var (
waitProg = "/.r/r"
waitVolume = "rancher-cni"
waitVolumeTarget = "/.r"
)

func modifyForCNI(c *client.Client, container *container.Config, hostConfig *container.HostConfig) error {
if container.Labels[cniWaitLabel] != "true" {
return nil
}
if err := setWaitEntrypoint(c, container); err != nil {
return err
}
return setWaitVolume(hostConfig, waitVolume, waitVolumeTarget, "ro")
}

func setWaitVolume(hostConfig *container.HostConfig, source, target, mode string) error {
var binds []string
for _, bind := range hostConfig.Binds {
s := strings.Split(bind, ":")
if len(s) >= 2 && s[1] == target {
continue
}
binds = append(binds, bind)
}
hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s:%s", source, target, mode))
return nil
}

func setWaitEntrypoint(c *client.Client, container *container.Config) error {
if len(container.Entrypoint) == 0 {
image, _, err := c.ImageInspectWithRaw(context.Background(), container.Image)
if err != nil {
return err
}

if len(container.Cmd) == 0 {
container.Cmd = image.Config.Cmd
}

if container.Entrypoint == nil {
container.Entrypoint = image.Config.Entrypoint
}
}

if len(container.Entrypoint) == 0 && len(container.Cmd) == 0 {
// No command, just let docker complain about it on start
return nil
}

if len(container.Entrypoint) == 0 || container.Entrypoint[0] != waitProg {
container.Entrypoint = append(container.Entrypoint, waitProg)
}

return nil
}
10 changes: 3 additions & 7 deletions core/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package compute

import (
"fmt"
"strings"
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -15,8 +18,6 @@ import (
"github.com/rancher/agent/utilities/docker"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"strings"
"time"
)

func DoInstanceActivate(instance model.Instance, host model.Host, progress *progress.Progress, dockerClient *client.Client, infoData model.InfoData) error {
Expand Down Expand Up @@ -124,11 +125,6 @@ func DoInstanceActivate(instance model.Instance, host model.Host, progress *prog
}

logrus.Infof("rancher id [%v]: Container with docker id [%v] has been started", instance.ID, containerID)

if err := RecordState(dockerClient, instance, containerID); err != nil {
return errors.Wrap(err, constants.DoInstanceActivateError+"failed to record state")
}

return nil
}

Expand Down
15 changes: 8 additions & 7 deletions core/compute/compute_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package compute
import (
"fmt"

urls "net/url"
"os"
"strconv"
"strings"
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -17,11 +23,6 @@ import (
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
urls "net/url"
"os"
"strconv"
"strings"
"time"
)

func createContainer(dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, networkConfig *network.NetworkingConfig,
Expand All @@ -43,13 +44,13 @@ func createContainer(dockerClient *client.Client, config *container.Config, host
dockerImage := utils.ParseRepoTag(imageTag)
config.Image = dockerImage.UUID

containerResponse, err := dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkConfig, name)
containerResponse, err := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, networkConfig, name)
// if image doesn't exist
if client.IsErrImageNotFound(err) {
if err := storage.PullImage(instance.Image, progress, dockerClient, imageTag); err != nil {
return "", errors.Wrap(err, constants.CreateContainerError+"failed to pull image")
}
containerResponse, err1 := dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkConfig, name)
containerResponse, err1 := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, networkConfig, name)
if err1 != nil {
return "", errors.Wrap(err1, constants.CreateContainerError+"failed to create container")
}
Expand Down
108 changes: 0 additions & 108 deletions core/compute/compute_record.go

This file was deleted.

19 changes: 4 additions & 15 deletions core/compute/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package compute

import (
"fmt"
"os"
"testing"
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/blkiodev"
Expand All @@ -16,10 +20,6 @@ import (
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"gopkg.in/check.v1"
"os"
"path"
"testing"
"time"
)

// Hook up gocheck into the "go test" runner.
Expand Down Expand Up @@ -229,17 +229,6 @@ func deleteContainer(name string) {
time.Sleep(time.Duration(500) * time.Millisecond)
}
client.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{})
RemoveStateFile(c.ID)
}
}
}

func RemoveStateFile(id string) {
if len(id) > 0 {
contDir := config.ContainerStateDir()
filePath := path.Join(contDir, id)
if _, err := os.Stat(filePath); err == nil {
os.Remove(filePath)
}
}
}
27 changes: 24 additions & 3 deletions core/compute/compute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ package compute

import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/blkiodev"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/rancher/agent/core/hostInfo"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"os"
"strconv"
"strings"
)

var (
cniWaitLabel = "io.rancher.cni.wait"
)

func setupPublishPorts(hostConfig *container.HostConfig, instance model.Instance) {
Expand Down Expand Up @@ -189,6 +197,12 @@ func setupNetworkMode(instance model.Instance, client *client.Client,
}
hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%v", id))
hostConfig.Links = nil
} else if kind == "cni" {
config.Labels[cniWaitLabel] = "true"
portsSupported = false
config.NetworkDisabled = true
hostConfig.NetworkMode = "none"
hostConfig.Links = nil
}
}
return portsSupported, hostnameSupported, nil
Expand Down Expand Up @@ -513,3 +527,10 @@ func setupDeviceOptions(hostConfig *container.HostConfig, instance model.Instanc
}

}

func dockerContainerCreate(ctx context.Context, dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (types.ContainerCreateResponse, error) {
if err := modifyForCNI(dockerClient, config, hostConfig); err != nil {
return types.ContainerCreateResponse{}, err
}
return dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, containerName)
}
9 changes: 9 additions & 0 deletions core/compute/compute_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package compute

import (
"context"

"vendor.old2/github.com/docker/docker/api/types"
"vendor.old2/github.com/docker/docker/api/types/network"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/rancher/agent/model"
Expand Down Expand Up @@ -45,3 +50,7 @@ func setupDeviceOptions(hostConfig *container.HostConfig, instance model.Instanc
func setupComputeResourceFields(hostConfig *container.HostConfig, instance model.Instance) {

}

func dockerContainerCreate(ctx context.Context, dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (types.ContainerCreateResponse, error) {
return dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, containerName)
}
9 changes: 3 additions & 6 deletions core/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package storage
import (
"fmt"

"os"
"strings"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
engineCli "github.com/docker/docker/client"
Expand All @@ -12,8 +15,6 @@ import (
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"os"
"strings"
)

func DoVolumeActivate(volume model.Volume, storagePool model.StoragePool, progress *progress.Progress, client *engineCli.Client) error {
Expand Down Expand Up @@ -85,10 +86,6 @@ func DoImageActivate(image model.Image, storagePool model.StoragePool, progress
return pullImageWrap(client, realImageUUID, pullOption, progress)
}

func DoVolumeDeactivate(volume model.Volume, storagePool model.StoragePool, progress *progress.Progress) error {
return errors.New("Not implemented")
}

func DoVolumeRemove(volume model.Volume, storagePool model.StoragePool, progress *progress.Progress, dockerClient *engineCli.Client) error {
if ok, err := IsVolumeRemoved(volume, storagePool, dockerClient); ok {
return nil
Expand Down
Loading

0 comments on commit 9bbb7bc

Please sign in to comment.