Skip to content

Commit

Permalink
fix: clean manifest path in remote deploy to fix regression (#4195)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Andrea Falzetti <andrea@okteto.com>
  • Loading branch information
andreafalzetti committed Mar 8, 2024
1 parent fb49fc0 commit e568f2a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/build/v1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spf13/afero"
)

// OktetoBuilder It is a wrapper of basic.Builder to build an image specificied by a Dockerfile. a.k.a. Builder v1
// OktetoBuilder It is a wrapper of basic.Builder to build an image specified by a Dockerfile. a.k.a. Builder v1
// It mainly extends the basic.Builder with the ability to expand the image tag with the environment variables and
// printing the corresponding output when the build finishes.
type OktetoBuilder struct {
Expand Down
12 changes: 2 additions & 10 deletions cmd/deploy/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (rd *remoteDeployCommand) createDockerfile(tmpDir string, opts *Options) (s
// build the services) so we would create a remote executor without certain files
// necessary for the later deployment which would cause an error when deploying
// remotely due to the lack of these files.
if err := remote.CreateDockerignoreFileWithFilesystem(cwd, tmpDir, opts.ManifestPathFlag, rd.fs); err != nil {
if err := remote.CreateDockerignoreFileWithFilesystem(cwd, tmpDir, filesystem.CleanManifestPath(opts.ManifestPathFlag), rd.fs); err != nil {
return "", err
}

Expand All @@ -339,15 +339,7 @@ func getDeployFlags(opts *Options) ([]string, error) {
}

if opts.ManifestPathFlag != "" {
lastFolder := filepath.Base(filepath.Dir(opts.ManifestPathFlag))
if lastFolder == ".okteto" {
path := filepath.Clean(opts.ManifestPathFlag)
parts := strings.Split(path, string(filepath.Separator))

deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filepath.Join(parts[len(parts)-2:]...)))
} else {
deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filepath.Base(opts.ManifestPathFlag)))
}
deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filesystem.CleanManifestPath(opts.ManifestPathFlag)))
}

if len(opts.Variables) > 0 {
Expand Down
1 change: 0 additions & 1 deletion cmd/deploy/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,5 +724,4 @@ func TestGetOriginalCWD(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, result)
})

}
12 changes: 2 additions & 10 deletions cmd/destroy/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (rd *remoteDestroyCommand) createDockerfile(tempDir string, opts *Options)
return "", err
}

if err = remote.CreateDockerignoreFileWithFilesystem(cwd, tempDir, opts.ManifestPathFlag, rd.fs); err != nil {
if err = remote.CreateDockerignoreFileWithFilesystem(cwd, tempDir, filesystem.CleanManifestPath(opts.ManifestPathFlag), rd.fs); err != nil {
return "", err
}

Expand Down Expand Up @@ -345,15 +345,7 @@ func getDestroyFlags(opts *Options) []string {
}

if opts.ManifestPathFlag != "" {
lastFolder := filepath.Base(filepath.Dir(opts.ManifestPathFlag))
if lastFolder == ".okteto" {
path := filepath.Clean(opts.ManifestPathFlag)
parts := strings.Split(path, string(filepath.Separator))

deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filepath.Join(parts[len(parts)-2:]...)))
} else {
deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filepath.Base(opts.ManifestPathFlag)))
}
deployFlags = append(deployFlags, fmt.Sprintf("--file %s", filesystem.CleanManifestPath(opts.ManifestPathFlag)))
}

if opts.DestroyVolumes {
Expand Down
32 changes: 32 additions & 0 deletions pkg/filesystem/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filesystem

import (
"path/filepath"
"strings"
)

// CleanManifestPath removes the path to the manifest file, in case the command was executed from a parent or child folder
func CleanManifestPath(manifestPath string) string {
lastFolder := filepath.Base(filepath.Dir(manifestPath))
if lastFolder == ".okteto" {
path := filepath.Clean(manifestPath)
parts := strings.Split(path, string(filepath.Separator))

return filepath.Join(parts[len(parts)-2:]...)
} else {
return filepath.Base(manifestPath)
}
}
57 changes: 57 additions & 0 deletions pkg/filesystem/manifest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filesystem

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_cleanManifestPath(t *testing.T) {
var tests = []struct {
name string
manifest string
expected string
}{
{
name: "empty manifest",
manifest: "",
expected: ".",
},
{
name: "absolute path to manifest file",
manifest: "/path/to/service/okteto.yml",
expected: "okteto.yml",
},
{
name: "relative path to manifest file",
manifest: "./service/okteto.yml",
expected: "okteto.yml",
},
{
name: "manifest within .okteto",
manifest: "/path/to/service/.okteto/okteto.yml",
expected: filepath.Clean(".okteto/okteto.yml"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CleanManifestPath(tt.manifest)
assert.Equal(t, tt.expected, result)
})
}
}

0 comments on commit e568f2a

Please sign in to comment.