Implement some checks and reorganize code

pull/8764/head
Priya Wadhwa 2020-08-10 17:04:41 -04:00
parent c2175abcba
commit 5799b47ddb
5 changed files with 39 additions and 27 deletions

View File

@ -252,7 +252,7 @@ func deletePossibleKicLeftOver(cname string, driverName string) {
}
// TODO: move to oci.DeleteContainer?
defaultNetwork := fmt.Sprintf("%s-network", cname)
defaultNetwork := cname
err = oci.RemoveNetwork(defaultNetwork)
if err != nil {
glog.Warningf("error deleting network. :%v", errs)

View File

@ -67,15 +67,6 @@ func NewDriver(c Config) *Driver {
// Create a host using the driver's config
func (d *Driver) Create() error {
defaultNetwork := fmt.Sprintf("%s-network", d.MachineName)
defaultIPRange := "192.168.39.0/24"
err := oci.CreateNetwork(defaultNetwork, defaultIPRange)
if err != nil {
// use k8s network? fail?
return err
}
params := oci.CreateParams{
Name: d.NodeConfig.MachineName,
Image: d.NodeConfig.ImageDigest,
@ -87,8 +78,13 @@ func (d *Driver) Create() error {
ExtraArgs: []string{"--expose", fmt.Sprintf("%d", d.NodeConfig.APIServerPort)},
OCIBinary: d.NodeConfig.OCIBinary,
APIServerPort: d.NodeConfig.APIServerPort,
Network: defaultNetwork,
IP: "192.168.39.2",
}
if err := oci.CreateNetwork(d.MachineName, defaultIPRange); err != nil {
glog.Warningf("unable to create docker network; node ip may not be stable: %v", err)
} else {
params.Network = defaultNetwork
params.IP = "192.168.39.2"
}
// control plane specific options

View File

@ -165,12 +165,15 @@ func dockerContainerIP(name string) (string, string, error) {
// CreateNetwork creates a network
func CreateNetwork(name, ipRange string) error {
// TODO: validate if exist?
// check if the network already exists
if networkExists(name) {
return nil
}
// TODO: subnet conflict
// TODO: configure gateway explictly
subnet := fmt.Sprintf("--subnet=%s", ipRange)
_, err := runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", subnet, name))
_, err = runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", subnet, name))
if err != nil {
return errors.Wrapf(err, "error creating network")
}
@ -180,12 +183,23 @@ func CreateNetwork(name, ipRange string) error {
// RemoveNetwork removes a network
func RemoveNetwork(name string) error {
// TODO: check if exist?
_, err := runCmd(exec.Command(Docker, "network", "remove", name))
if err != nil {
return errors.Wrapf(err, "error removing network")
if !networkExists {
return nil
}
return nil
_, err := runCmd(exec.Command(Docker, "network", "remove", name))
return err
}
func networkExists(name string) bool {
rr, err := runCmd(exec.Command(Docker, "network", "ls", "--format", "{{.Name}}"))
if err != nil {
return errors.Wrap(err, "listing networks")
}
networks := strings.Split(rr.Output(), "\n")
for _, n := range networks {
if n == name {
return true
}
}
return false
}

View File

@ -142,18 +142,18 @@ func CreateContainerNode(p CreateParams) error {
"--label", p.NodeLabel,
}
// network
if p.OCIBinary == Docker && runtime.GOOS == "linux" { // for now only docker on linux
runArgs = append(runArgs, "--network", p.Network)
runArgs = append(runArgs, "--ip", p.IP)
}
// volume
if p.OCIBinary == Podman { // enable execing in /var
// podman mounts var/lib with no-exec by default https://github.com/containers/libpod/issues/5103
runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var:exec", p.Name))
}
if p.OCIBinary == Docker {
// on linux, we can provide a static IP for docker
if runtime.GOOS == "linux" && p.Network != "" && p.IP != "" {
runArgs = append(runArgs, "--network", p.Network)
runArgs = append(runArgs, "--ip", p.IP)
}
runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var", p.Name))
// ignore apparmore github actions docker: https://github.com/kubernetes/minikube/issues/7624
runArgs = append(runArgs, "--security-opt", "apparmor=unconfined")

View File

@ -27,6 +27,8 @@ const (
Version = "v0.0.10"
// SHA of the kic base image
baseImageSHA = "f58e0c4662bac8a9b5dda7984b185bad8502ade5d9fa364bf2755d636ab51438"
defaultIPRange = "192.168.39.0/24"
)
var (