From 32f462ee3252640d15fa6f93f891d8c731abce90 Mon Sep 17 00:00:00 2001 From: Petr Fedchenkov Date: Tue, 2 Mar 2021 21:23:11 +0300 Subject: [PATCH] modify pod app link support Signed-off-by: Petr Fedchenkov --- cmd/podModify.go | 81 +++++++++++++++++++++++++++++++++++++++++--- pkg/expect/volume.go | 3 +- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/cmd/podModify.go b/cmd/podModify.go index 5dfb469f0..528520265 100644 --- a/cmd/podModify.go +++ b/cmd/podModify.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/dustin/go-humanize" "github.com/lf-edge/eden/pkg/defaults" "github.com/lf-edge/eden/pkg/expect" @@ -11,6 +12,8 @@ import ( "github.com/spf13/cobra" ) +var podLink string + //podModifyCmd is a command to modify app var podModifyCmd = &cobra.Command{ Use: "modify ", @@ -52,9 +55,23 @@ var podModifyCmd = &cobra.Command{ } opts = append(opts, expect.WithACL(acl)) opts = append(opts, expect.WithOldApp(appName)) - expectation := expect.AppExpectationFromURL(ctrl, dev, defaults.DefaultDummyExpect, appName, opts...) - appInstanceConfig := expectation.Application() + opts = append(opts, expect.WithHTTPDirectLoad(directLoad)) + diskSizeParsed, err := humanize.ParseBytes(diskSize) + if err != nil { + log.Fatal(err) + } + opts = append(opts, expect.WithDiskSize(int64(diskSizeParsed))) + link := defaults.DefaultDummyExpect + newLink := false needPurge := false + if podLink != "" { + needPurge = true + newLink = true + link = podLink + } + volumes:=dev.GetVolumes() + expectation := expect.AppExpectationFromURL(ctrl, dev, link, appName, opts...) + appInstanceConfig := expectation.Application() if len(app.Interfaces) != len(appInstanceConfig.Interfaces) { needPurge = true } else { @@ -65,14 +82,67 @@ var podModifyCmd = &cobra.Command{ } } } + app.Interfaces = appInstanceConfig.Interfaces + if newLink { + diffInDrives := true + if len(app.Drives) == len(appInstanceConfig.Drives) { + diffInDrives = false + for i, d := range app.Drives { + // check if we have difference in volumes and its images + if appInstanceConfig.Drives[i].Image.Name != d.Image.Name || appInstanceConfig.Drives[i].Image.Sha256 != d.Image.Sha256 { + diffInDrives = true + } + } + } + if diffInDrives { + // user provides different link, so we need to purge volumes + volumeIDs := dev.GetVolumes() + utils.DelEleInSliceByFunction(&volumeIDs, func(i interface{}) bool { + vol, err := ctrl.GetVolume(i.(string)) + if err != nil { + log.Fatalf("no volume in cloud %s: %s", i.(string), err) + } + for _, volRef := range app.VolumeRefList { + if vol.Uuid == volRef.Uuid { + return true + } + } + return false + }) + for _, volRef := range appInstanceConfig.VolumeRefList { + volumeIDs = append(volumeIDs, volRef.Uuid) + } + dev.SetVolumeConfigs(volumeIDs) + app.VolumeRefList = appInstanceConfig.VolumeRefList + app.Drives = appInstanceConfig.Drives + } else { + // we will increase generation number if user provide the same link + // to run image update + dev.SetVolumeConfigs(volumes) + for _, ctID := range dev.GetContentTrees() { + ct, err := ctrl.GetContentTree(ctID) + if err != nil { + log.Fatalf("no ContentTree in cloud %s: %s", ctID, err) + } + for _, v := range app.VolumeRefList { + volumeConfig, err := ctrl.GetVolume(v.Uuid) + if err != nil { + log.Fatalf("no volume in cloud %s: %s", v.Uuid, err) + } + volumeConfig.GenerationCount++ + if ct.GetUuid() == volumeConfig.Origin.DownloadContentTreeID { + ct.GenerationCount++ + } + } + } + } + } if needPurge { if app.Purge == nil { app.Purge = &config.InstanceOpsCmd{Counter: 0} } app.Purge.Counter++ } - //now we only change networks - app.Interfaces = appInstanceConfig.Interfaces if err = changer.setControllerAndDev(ctrl, dev); err != nil { log.Fatalf("setControllerAndDev: %s", err) } @@ -89,5 +159,8 @@ func podModifyInit() { podModifyCmd.Flags().StringSliceVarP(&portPublish, "publish", "p", nil, "Ports to publish in format EXTERNAL_PORT:INTERNAL_PORT") podModifyCmd.Flags().BoolVar(&aclOnlyHost, "only-host", false, "Allow access only to host and external networks") podModifyCmd.Flags().StringSliceVar(&podNetworks, "networks", nil, "Networks to connect to app (ports will be mapped to first network)") + podModifyCmd.Flags().StringVar(&podLink, "link", "", "Set new app link for pod") + podModifyCmd.Flags().StringVar(&diskSize, "disk-size", humanize.Bytes(0), "disk size (empty or 0 - same as in image)") + podModifyCmd.Flags().BoolVar(&directLoad, "direct", true, "Use direct download for image instead of eserver") podModifyCmd.Flags().StringSliceVar(&acl, "acl", nil, "Allow access only to defined hosts/ips/subnets") } diff --git a/pkg/expect/volume.go b/pkg/expect/volume.go index 6e2e39777..186338ff2 100644 --- a/pkg/expect/volume.go +++ b/pkg/expect/volume.go @@ -14,7 +14,8 @@ func (exp *AppExpectation) driveToVolume(dr *config.Drive, numberOfDrive int, co if err != nil { log.Fatalf("no volume %s found in controller: %s", volID, err) } - if el.DisplayName == fmt.Sprintf("%s_%d_m_0", contentTree.DisplayName, numberOfDrive) { + if el.Origin.DownloadContentTreeID == contentTree.Uuid && + el.DisplayName == fmt.Sprintf("%s_%d_m_0", contentTree.DisplayName, numberOfDrive) { // we already have this one in controller return el }