Skip to content

Commit 12f58da

Browse files
committed
address pod modify problem
Signed-off-by: Petr Fedchenkov <[email protected]>
1 parent 31f4206 commit 12f58da

File tree

6 files changed

+98
-22
lines changed

6 files changed

+98
-22
lines changed

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (exp *AppExpectation) checkNetworkInstance(netInst *config.NetworkInstanceC
2929
if netInst == nil {
3030
return false
3131
}
32-
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
3333
(instanceExpect.name != "" && netInst.Displayname == instanceExpect.name) || //if name defined and the same
3434
(instanceExpect.netInstType == "switch" && netInst.InstType == config.ZNetworkInstType_ZnetInstSwitch) { //only one switch for now
3535
return true
@@ -145,14 +145,22 @@ func parseACE(inp string) *config.ACE {
145145
func (exp *AppExpectation) getAcls(ni *NetInstanceExpectation) []*config.ACE {
146146
var acls []*config.ACE
147147
var aclID int32 = 1
148-
if exp.acl != nil {
148+
if exp.acl != nil && len(exp.acl[ni.name]) > 0 {
149149
// in case of defined acl allow access only to them
150-
for _, el := range exp.acl {
151-
acl := parseACE(el)
152-
if acl != nil {
153-
acl.Id = aclID
154-
acls = append(acls, acl)
155-
aclID++
150+
for netName, acl := range exp.acl {
151+
if netName != "" && netName != ni.name {
152+
continue
153+
}
154+
for _, el := range acl {
155+
if el == "" {
156+
continue
157+
}
158+
acl := parseACE(el)
159+
if acl != nil {
160+
acl.Id = aclID
161+
acls = append(acls, acl)
162+
aclID++
163+
}
156164
}
157165
}
158166
} 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/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+
"github.com/golang/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)