Skip to content

Commit

Permalink
Adds dev.odo.push.file attribute support for pushing only mentioned f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
mik-dass committed Apr 28, 2021
1 parent 1156a13 commit 4c5663c
Show file tree
Hide file tree
Showing 12 changed files with 2,304 additions and 93 deletions.
2 changes: 1 addition & 1 deletion pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ func PushLocal(client *occlient.Client, componentName string, applicationName st
compInfo := common.ComponentInfo{
PodName: pod.Name,
}
err = sync.CopyFile(adapter, path, compInfo, targetPath, files, globExps)
err = sync.CopyFile(adapter, path, compInfo, targetPath, files, globExps, util.IndexerRet{})
if err != nil {
s.End(false)
return errors.Wrap(err, "unable push files to pod")
Expand Down
1 change: 1 addition & 0 deletions pkg/devfile/adapters/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type SyncParameters struct {
CompInfo ComponentInfo
PodChanged bool
ComponentExists bool
Files map[string]string
}

// ComponentInfo is a struct that holds information about a component i.e.; pod name, container name, and source mount (if applicable)
Expand Down
16 changes: 16 additions & 0 deletions pkg/devfile/adapters/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"os"
"path/filepath"
"strings"

"k8s.io/klog"
Expand Down Expand Up @@ -203,3 +204,18 @@ func GetCommandsMap(commands []devfilev1.Command) map[string]devfilev1.Command {
}
return commandMap
}

// GetSyncFilesFromAttributes gets the target files and folders along with their respective remote destination from the devfile
// it uses the "dev.odo.push.path" attribute in the run command
func GetSyncFilesFromAttributes(commandsMap PushCommandsMap) map[string]string {
syncMap := make(map[string]string)
if value, ok := commandsMap[devfilev1.RunCommandGroupKind]; ok {
for key, value := range value.Attributes.Strings(nil) {
if strings.HasPrefix(key, "dev.odo.push.path:") {
localValue := strings.ReplaceAll(key, "dev.odo.push.path:", "")
syncMap[filepath.Clean(localValue)] = filepath.ToSlash(filepath.Clean(value))
}
}
}
return syncMap
}
2 changes: 2 additions & 0 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
CompInfo: compInfo,
ComponentExists: componentExists,
PodChanged: podChanged,
Files: common.GetSyncFilesFromAttributes(pushDevfileCommands),
}

execRequired, err := syncAdapter.SyncFiles(syncParams)
if err != nil {
return errors.Wrapf(err, "Failed to sync to component with name %s", a.ComponentName)
Expand Down
25 changes: 15 additions & 10 deletions pkg/sync/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (a Adapter) SyncFiles(syncParameters common.SyncParameters) (isPushRequired
}

// Run the indexer and find the modified/added/deleted/renamed files
ret, err = util.RunIndexer(pushParameters.Path, absIgnoreRules)
ret, err = util.RunIndexerWithRemote(pushParameters.Path, absIgnoreRules, syncParameters.Files)
s.End(true)

if err != nil {
Expand All @@ -138,19 +138,19 @@ func (a Adapter) SyncFiles(syncParameters common.SyncParameters) (isPushRequired
// and ignore the files on which the rules apply and filter them out
filesChangedFiltered, filesDeletedFiltered := util.FilterIgnores(ret.FilesChanged, ret.FilesDeleted, absIgnoreRules)

// Remove the relative file directory from the list of deleted files
// in order to make the changes correctly within the Kubernetes pod
deletedFiles, err = util.RemoveRelativePathFromFiles(filesDeletedFiltered, pushParameters.Path)
if err != nil {
return false, errors.Wrap(err, "unable to remove relative path from list of changed/deleted files")
}
deletedFiles = append(filesDeletedFiltered, ret.RemoteDeleted...)
deletedFiles = append(deletedFiles, ret.RemoteDeleted...)
klog.V(4).Infof("List of files to be deleted: +%v", deletedFiles)
changedFiles = filesChangedFiltered
klog.V(4).Infof("List of files changed: +%v", changedFiles)

if len(filesChangedFiltered) == 0 && len(filesDeletedFiltered) == 0 && !isForcePush {
return false, nil
}

if isForcePush {
deletedFiles = append(deletedFiles, "*")
}
}

err = a.pushLocal(pushParameters.Path,
Expand All @@ -159,6 +159,7 @@ func (a Adapter) SyncFiles(syncParameters common.SyncParameters) (isPushRequired
isForcePush,
util.GetAbsGlobExps(pushParameters.Path, pushParameters.IgnoredFiles),
syncParameters.CompInfo,
ret,
)
if err != nil {
return false, errors.Wrapf(err, "failed to sync to component with name %s", a.ComponentName)
Expand All @@ -174,7 +175,7 @@ func (a Adapter) SyncFiles(syncParameters common.SyncParameters) (isPushRequired
}

// pushLocal syncs source code from the user's disk to the component
func (a Adapter) pushLocal(path string, files []string, delFiles []string, isForcePush bool, globExps []string, compInfo common.ComponentInfo) error {
func (a Adapter) pushLocal(path string, files []string, delFiles []string, isForcePush bool, globExps []string, compInfo common.ComponentInfo, ret util.IndexerRet) error {
klog.V(4).Infof("Push: componentName: %s, path: %s, files: %s, delFiles: %s, isForcePush: %+v", a.ComponentName, path, files, delFiles, isForcePush)

// Edge case: check to see that the path is NOT empty.
Expand Down Expand Up @@ -221,7 +222,7 @@ func (a Adapter) pushLocal(path string, files []string, delFiles []string, isFor

if isForcePush || len(files) > 0 {
klog.V(4).Infof("Copying files %s to pod", strings.Join(files, " "))
err = CopyFile(a.Client, path, compInfo, syncFolder, files, globExps)
err = CopyFile(a.Client, path, compInfo, syncFolder, files, globExps, ret)
if err != nil {
s.End(false)
return errors.Wrap(err, "unable push files to pod")
Expand Down Expand Up @@ -301,5 +302,9 @@ func getCmdToDeleteFiles(delFiles []string, syncFolder string) []string {
rmPaths := util.GetRemoteFilesMarkedForDeletion(delFiles, syncFolder)
klog.V(4).Infof("remote files marked for deletion are %+v", rmPaths)
cmdArr := []string{"rm", "-rf"}
return append(cmdArr, rmPaths...)

for _, remote := range rmPaths {
cmdArr = append(cmdArr, filepath.ToSlash(remote))
}
return cmdArr
}
13 changes: 11 additions & 2 deletions pkg/sync/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func TestSyncFiles(t *testing.T) {
t.Errorf("TestSyncFiles error: error creating temporary directory for the indexer: %v", err)
}

jsFile, err := os.Create(filepath.Join(directory, "red.js"))
if err != nil {
t.Errorf("TestSyncFiles error: error creating temporary file for the indexer: %v", err)
}

ctrl := gomock.NewController(t)

// Assert that Bar() is invoked.
Expand Down Expand Up @@ -218,10 +223,14 @@ func TestSyncFiles(t *testing.T) {
})
}

err = jsFile.Close()
if err != nil {
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
}
// Remove the temp dir created for the file indexer
err = os.RemoveAll(directory)
if err != nil {
t.Errorf("TestSyncFiles error: error deleting the temp dir %s", directory)
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
}
}

Expand Down Expand Up @@ -359,7 +368,7 @@ func TestPushLocal(t *testing.T) {
}

syncAdapter := New(adapterCtx, syncClient)
err := syncAdapter.pushLocal(tt.path, tt.files, tt.delFiles, tt.isForcePush, []string{}, tt.compInfo)
err := syncAdapter.pushLocal(tt.path, tt.files, tt.delFiles, tt.isForcePush, []string{}, tt.compInfo, util.IndexerRet{})
if !tt.wantErr && err != nil {
t.Errorf("TestPushLocal error: error pushing files: %v", err)
}
Expand Down
Loading

0 comments on commit 4c5663c

Please sign in to comment.