Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4a08035

Browse files
committedApr 28, 2021
resolve network instance type switch creation problem
Signed-off-by: Petr Fedchenkov <giggsoff@gmail.com>
1 parent 1a7c1ce commit 4a08035

14 files changed

+111
-81
lines changed
 

‎cmd/edenNetwork.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,5 @@ func networkInit() {
152152
networkCmd.AddCommand(networkCreateCmd)
153153
networkCreateCmd.Flags().StringVar(&networkType, "type", "local", "Type of network: local or switch")
154154
networkCreateCmd.Flags().StringVarP(&networkName, "name", "n", "", "Name of network (empty for auto generation)")
155-
networkCreateCmd.Flags().StringVarP(&uplinkAdapter, "uplink", "u", "eth0", "Name of uplink adapter")
155+
networkCreateCmd.Flags().StringVarP(&uplinkAdapter, "uplink", "u", "eth0", "Name of uplink adapter, set to 'none' to not use uplink")
156156
}

‎cmd/edenPod.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/dustin/go-humanize"
78
"github.com/lf-edge/eden/pkg/controller/eapps"
@@ -53,6 +54,19 @@ var podCmd = &cobra.Command{
5354
Use: "pod",
5455
}
5556

57+
func processAcls(acls []string) map[string][]string {
58+
m := map[string][]string{}
59+
for _, el := range acls {
60+
parsed := strings.SplitN(el, ":", 2)
61+
if len(parsed) > 1 {
62+
m[parsed[0]] = append(m[parsed[0]], parsed[1])
63+
} else {
64+
m[""] = append(m[""], parsed[0])
65+
}
66+
}
67+
return m
68+
}
69+
5670
//podDeployCmd is command for deploy application on EVE
5771
var podDeployCmd = &cobra.Command{
5872
Use: "deploy (docker|http(s)|file)://(<TAG>[:<VERSION>] | <URL for qcow2 image> | <path to qcow2 image>)",
@@ -105,9 +119,9 @@ var podDeployCmd = &cobra.Command{
105119
opts = append(opts, expect.WithResources(appCpus, uint32(appMemoryParsed/1000)))
106120
opts = append(opts, expect.WithImageFormat(imageFormat))
107121
if aclOnlyHost {
108-
opts = append(opts, expect.WithACL([]string{""}))
122+
opts = append(opts, expect.WithACL(map[string][]string{"": {""}}))
109123
} else {
110-
opts = append(opts, expect.WithACL(acl))
124+
opts = append(opts, expect.WithACL(processAcls(acl)))
111125
}
112126
opts = append(opts, expect.WithSFTPLoad(sftpLoad))
113127
if !sftpLoad {
@@ -449,7 +463,9 @@ func podInit() {
449463
podDeployCmd.Flags().BoolVar(&directLoad, "direct", true, "Use direct download for image instead of eserver")
450464
podDeployCmd.Flags().BoolVar(&sftpLoad, "sftp", false, "Force use of sftp to load http/file image from eserver")
451465
podDeployCmd.Flags().StringSliceVar(&disks, "disks", nil, "Additional disks to use")
452-
podDeployCmd.Flags().StringSliceVar(&acl, "acl", nil, "Allow access only to defined hosts/ips/subnets")
466+
podDeployCmd.Flags().StringSliceVar(&acl, "acl", nil, `Allow access only to defined hosts/ips/subnets
467+
You can set acl for particular network in format '<network_name:acl>'
468+
To remove acls you can set empty line '<network_name>:'`)
453469
podCmd.AddCommand(podPsCmd)
454470
podCmd.AddCommand(podStopCmd)
455471
podCmd.AddCommand(podStartCmd)

‎cmd/podModify.go

+39-8
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,51 @@ var podModifyCmd = &cobra.Command{
3131
if err != nil {
3232
log.Fatalf("getControllerAndDev: %s", err)
3333
}
34-
for _, el := range dev.GetApplicationInstances() {
35-
app, err := ctrl.GetApplicationInstanceConfig(el)
34+
for _, appID := range dev.GetApplicationInstances() {
35+
app, err := ctrl.GetApplicationInstanceConfig(appID)
3636
if err != nil {
37-
log.Fatalf("no app in cloud %s: %s", el, err)
37+
log.Fatalf("no app in cloud %s: %s", appID, err)
3838
}
3939
if app.Displayname == appName {
40+
portPublishCombined := portPublish
41+
if !cmd.Flags().Changed("publish") {
42+
portPublishCombined = []string{}
43+
for _, intf := range app.Interfaces {
44+
for _, acls := range intf.Acls {
45+
lport := ""
46+
var appPort uint32
47+
for _, match := range acls.Matches {
48+
if match.Type == "lport" {
49+
lport = match.Value
50+
break
51+
}
52+
}
53+
for _, action := range acls.Actions {
54+
if action.Portmap {
55+
appPort = action.AppPort
56+
break
57+
}
58+
}
59+
if lport != "" && appPort != 0 {
60+
portPublishCombined = append(portPublishCombined, fmt.Sprintf("%s:%d", lport, appPort))
61+
}
62+
}
63+
}
64+
}
4065
var opts []expect.ExpectationOption
4166
if len(podNetworks) > 0 {
4267
for i, el := range podNetworks {
4368
if i == 0 {
4469
//allocate ports on first network
45-
opts = append(opts, expect.AddNetInstanceNameAndPortPublish(el, portPublish))
70+
opts = append(opts, expect.AddNetInstanceNameAndPortPublish(el, portPublishCombined))
4671
} else {
4772
opts = append(opts, expect.AddNetInstanceNameAndPortPublish(el, nil))
4873
}
4974
}
5075
} else {
51-
opts = append(opts, expect.WithPortsPublish(portPublish))
76+
opts = append(opts, expect.WithPortsPublish(portPublishCombined))
5277
}
53-
opts = append(opts, expect.WithACL(acl))
78+
opts = append(opts, expect.WithACL(processAcls(acl)))
5479
opts = append(opts, expect.WithOldApp(appName))
5580
expectation := expect.AppExpectationFromURL(ctrl, dev, defaults.DefaultDummyExpect, appName, opts...)
5681
appInstanceConfig := expectation.Application()
@@ -59,7 +84,11 @@ var podModifyCmd = &cobra.Command{
5984
needPurge = true
6085
} else {
6186
for ind, el := range app.Interfaces {
62-
if el.NetworkId != appInstanceConfig.Interfaces[ind].NetworkId {
87+
equals, err := utils.CompareProtoMessages(el, appInstanceConfig.Interfaces[ind])
88+
if err != nil {
89+
log.Fatalf("CompareMessages: %v", err)
90+
}
91+
if !equals {
6392
needPurge = true
6493
break
6594
}
@@ -89,5 +118,7 @@ func podModifyInit() {
89118
podModifyCmd.Flags().StringSliceVarP(&portPublish, "publish", "p", nil, "Ports to publish in format EXTERNAL_PORT:INTERNAL_PORT")
90119
podModifyCmd.Flags().BoolVar(&aclOnlyHost, "only-host", false, "Allow access only to host and external networks")
91120
podModifyCmd.Flags().StringSliceVar(&podNetworks, "networks", nil, "Networks to connect to app (ports will be mapped to first network)")
92-
podModifyCmd.Flags().StringSliceVar(&acl, "acl", nil, "Allow access only to defined hosts/ips/subnets")
121+
podModifyCmd.Flags().StringSliceVar(&acl, "acl", nil, `Allow access only to defined hosts/ips/subnets
122+
You can set acl for particular network in format '<network_name:acl>'
123+
To remove acls you can set empty line '<network_name>:'`)
93124
}

‎pkg/expect/expectation.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type AppExpectation struct {
6363
sftpLoad bool
6464

6565
disks []string
66-
acl []string
66+
acl map[string][]string // networkInstanceName -> acls
6767
}
6868

6969
//AppExpectationFromURL init AppExpectation with defined:
@@ -137,7 +137,7 @@ func AppExpectationFromURL(ctrl controller.Cloud, device *device.Ctx, appLink st
137137
if err != nil {
138138
log.Fatalf("Port map port %s could not be converted to Integer", qv)
139139
}
140-
if portNum == extPort || (portNum + defaults.DefaultPortMapOffset) == extPort {
140+
if portNum == extPort || (portNum+defaults.DefaultPortMapOffset) == extPort {
141141
ni.ports[extPort] = intPort
142142
continue exit
143143
}

‎pkg/expect/networkInstance.go

+28-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/docker/docker/pkg/namesgenerator"
10-
"github.com/lf-edge/eden/pkg/models"
1110
"github.com/lf-edge/eden/pkg/utils"
1211
"github.com/lf-edge/eve/api/go/config"
1312
"github.com/lf-edge/eve/api/go/evecommon"
@@ -30,7 +29,7 @@ func (exp *AppExpectation) checkNetworkInstance(netInst *config.NetworkInstanceC
3029
if netInst == nil {
3130
return false
3231
}
33-
if netInst.Ip.Subnet == instanceExpect.subnet || //if subnet defined and the same
32+
if (netInst.Ip.Subnet != "" && netInst.Ip.Subnet == instanceExpect.subnet) || //if subnet defined and the same
3433
(instanceExpect.name != "" && netInst.Displayname == instanceExpect.name) || //if name defined and the same
3534
(instanceExpect.netInstType == "switch" && netInst.InstType == config.ZNetworkInstType_ZnetInstSwitch) { //only one switch for now
3635
return true
@@ -45,9 +44,10 @@ func (exp *AppExpectation) createNetworkInstance(instanceExpect *NetInstanceExpe
4544
if err != nil {
4645
return nil, err
4746
}
48-
subentIPs := utils.GetSubnetIPs(instanceExpect.subnet)
4947
adapter := exp.uplinkAdapter
50-
if instanceExpect.uplinkAdapter != "" {
48+
if instanceExpect.uplinkAdapter == "none" {
49+
adapter = nil
50+
} else if instanceExpect.uplinkAdapter != "" {
5151
adapter = &config.Adapter{
5252
Name: instanceExpect.uplinkAdapter,
5353
Type: evecommon.PhyIoType_PhyIoNetEth,
@@ -63,25 +63,22 @@ func (exp *AppExpectation) createNetworkInstance(instanceExpect *NetInstanceExpe
6363
Port: adapter,
6464
Cfg: &config.NetworkInstanceOpaqueConfig{},
6565
IpType: config.AddressType_IPV4,
66-
Ip: &config.Ipspec{
66+
Ip: &config.Ipspec{},
67+
Dns: nil,
68+
}
69+
if instanceExpect.netInstType == "switch" {
70+
netInst.InstType = config.ZNetworkInstType_ZnetInstSwitch
71+
} else {
72+
subentIPs := utils.GetSubnetIPs(instanceExpect.subnet)
73+
netInst.Ip = &config.Ipspec{
6774
Subnet: instanceExpect.subnet,
6875
Gateway: subentIPs[1].String(),
6976
Dns: []string{subentIPs[1].String()},
7077
DhcpRange: &config.IpRange{
7178
Start: subentIPs[2].String(),
7279
End: subentIPs[len(subentIPs)-2].String(),
7380
},
74-
},
75-
Dns: nil,
76-
}
77-
if instanceExpect.netInstType == "switch" {
78-
netInst.InstType = config.ZNetworkInstType_ZnetInstSwitch
79-
devModel, err := models.GetDevModelByName(exp.ctrl.GetVars().DevModel)
80-
if err != nil {
81-
log.Fatal(err)
8281
}
83-
netInst.Port = &config.Adapter{Name: devModel.GetFirstAdapterForSwitches()}
84-
netInst.Ip = &config.Ipspec{}
8582
}
8683
if instanceExpect.name == "" {
8784
rand.Seed(time.Now().UnixNano())
@@ -109,7 +106,7 @@ func (exp *AppExpectation) NetworkInstances() (networkInstances map[*NetInstance
109106
}
110107
}
111108
if networkInstance == nil { //if networkInstance not exists, create it
112-
if ni.name != "" && ni.subnet == "" {
109+
if ni.name != "" && ni.netInstType == "local" && ni.subnet == "" {
113110
log.Fatalf("not found subnet with name %s", ni.name)
114111
}
115112
if networkInstance, err = exp.createNetworkInstance(ni); err != nil {
@@ -150,14 +147,22 @@ func parseACE(inp string) *config.ACE {
150147
func (exp *AppExpectation) getAcls(ni *NetInstanceExpectation) []*config.ACE {
151148
var acls []*config.ACE
152149
var aclID int32 = 1
153-
if exp.acl != nil {
150+
if exp.acl != nil && len(exp.acl[ni.name]) > 0 {
154151
// in case of defined acl allow access only to them
155-
for _, el := range exp.acl {
156-
acl := parseACE(el)
157-
if acl != nil {
158-
acl.Id = aclID
159-
acls = append(acls, acl)
160-
aclID++
152+
for netName, acl := range exp.acl {
153+
if netName != "" && netName != ni.name {
154+
continue
155+
}
156+
for _, el := range acl {
157+
if el == "" {
158+
continue
159+
}
160+
acl := parseACE(el)
161+
if acl != nil {
162+
acl.Id = aclID
163+
acls = append(acls, acl)
164+
aclID++
165+
}
161166
}
162167
}
163168
} else {

‎pkg/expect/options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func WithVolumeType(volumesType VolumeType) ExpectationOption {
159159
}
160160

161161
//WithACL sets access only for defined hosts
162-
func WithACL(acl []string) ExpectationOption {
162+
func WithACL(acl map[string][]string) ExpectationOption {
163163
return func(expectation *AppExpectation) {
164164
expectation.acl = acl
165165
}

‎pkg/models/devModel.go

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ type DevModel interface {
6868
SetPhysicalIOs([]*config.PhysicalIO)
6969
AdapterForSwitches() []string
7070
DevModelType() string
71-
GetFirstAdapterForSwitches() string
7271
SetWiFiParams(ssid string, psk string)
7372
GetPortConfig(ssid string, psk string) string
7473
DiskFormat() string

‎pkg/models/gcp.go

-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ func (ctx *DevModelGCP) DevModelType() string {
8282
return string(devModelTypeGCP)
8383
}
8484

85-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
86-
func (ctx *DevModelGCP) GetFirstAdapterForSwitches() string {
87-
if len(ctx.adapterForSwitches) > 0 {
88-
return ctx.adapterForSwitches[0]
89-
}
90-
return "uplink"
91-
}
92-
9385
func createGCP() (DevModel, error) {
9486
return &DevModelGCP{
9587
physicalIOs: generatePhysicalIOs(1, 0, 0),

‎pkg/models/general.go

-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ func (ctx *DevModelGeneral) DevModelType() string {
8282
return string(devModelTypeGeneral)
8383
}
8484

85-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
86-
func (ctx *DevModelGeneral) GetFirstAdapterForSwitches() string {
87-
if len(ctx.adapterForSwitches) > 0 {
88-
return ctx.adapterForSwitches[0]
89-
}
90-
return "uplink"
91-
}
92-
9385
func createGeneral() (DevModel, error) {
9486
return &DevModelGeneral{
9587
physicalIOs: generatePhysicalIOs(2, 0, 0),

‎pkg/models/parallels.go

-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ func (ctx *DevModelParallels) DevModelType() string {
7878
return string(devModelTypeParallels)
7979
}
8080

81-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
82-
func (ctx *DevModelParallels) GetFirstAdapterForSwitches() string {
83-
return "uplink"
84-
}
85-
8681
func createParallels() (DevModel, error) {
8782
return &DevModelParallels{
8883
physicalIOs: generatePhysicalIOs(2, 0, 4),

‎pkg/models/qemu.go

-8
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ func (ctx *DevModelQemu) DevModelType() string {
7676
return string(devModelTypeQemu)
7777
}
7878

79-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
80-
func (ctx *DevModelQemu) GetFirstAdapterForSwitches() string {
81-
if len(ctx.adapterForSwitches) > 0 {
82-
return ctx.adapterForSwitches[0]
83-
}
84-
return "uplink"
85-
}
86-
8779
func createQemu() (DevModel, error) {
8880
return &DevModelQemu{
8981
physicalIOs: generatePhysicalIOs(2, 0, 4),

‎pkg/models/rpi.go

-5
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ func (ctx *DevModelRpi) DevModelType() string {
146146
return string(devModelTypeRaspberry)
147147
}
148148

149-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
150-
func (ctx *DevModelRpi) GetFirstAdapterForSwitches() string {
151-
return "uplink"
152-
}
153-
154149
func createRpi() (DevModel, error) {
155150
return &DevModelRpi{
156151
physicalIOs: generatePhysicalIOs(1, 1, 0),

‎pkg/models/vbox.go

-8
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@ func (ctx *DevModelVBox) DevModelType() string {
7979
return string(devModelTypeVBox)
8080
}
8181

82-
//GetFirstAdapterForSwitches return first adapter available for switch networkInstance
83-
func (ctx *DevModelVBox) GetFirstAdapterForSwitches() string {
84-
if len(ctx.adapterForSwitches) > 0 {
85-
return ctx.adapterForSwitches[0]
86-
}
87-
return "uplink"
88-
}
89-
9082
func createVBox() (DevModel, error) {
9183
return &DevModelVBox{
9284
physicalIOs: generatePhysicalIOs(2, 0, 4),

‎pkg/utils/proto.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
7+
"google.golang.org/protobuf/proto"
8+
)
9+
10+
//CompareProtoMessages returns true if messages are equal
11+
func CompareProtoMessages(m1, m2 proto.Message) (bool, error) {
12+
m1Data, err := proto.Marshal(m1)
13+
if err != nil {
14+
return false, fmt.Errorf("cannot marshal interface: %v", err)
15+
}
16+
m2Data, err := proto.Marshal(m2)
17+
if err != nil {
18+
return false, fmt.Errorf("cannot marshal interface: %v", err)
19+
}
20+
return bytes.Equal(m1Data, m2Data), nil
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.