Skip to content

Commit

Permalink
fixes remote feature (#3412) (#3414)
Browse files Browse the repository at this point in the history
* fix external check size to print warning message

Signed-off-by: adripedriza <adripedriza@gmail.com>

* set okteto cli for remote build dynamically

Signed-off-by: adripedriza <adripedriza@gmail.com>

* refactor regexp

Signed-off-by: adripedriza <adripedriza@gmail.com>

* add test

Signed-off-by: adripedriza <adripedriza@gmail.com>

---------

Signed-off-by: adripedriza <adripedriza@gmail.com>
(cherry picked from commit 40ece09)

Signed-off-by: adripedriza <adripedriza@gmail.com>
Co-authored-by: adripedriza <adripedriza@gmail.com>
  • Loading branch information
github-actions[bot] and AdrianPedriza committed Mar 1, 2023
1 parent 42456b2 commit 680f05e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/deploy/local.go
Expand Up @@ -263,7 +263,7 @@ func (ld *localDeployer) runDeploySection(ctx context.Context, opts *Options) er
}

// deploy externals if any
if opts.Manifest.External != nil {
if len(opts.Manifest.External) > 0 {
oktetoLog.SetStage("External configuration")
if !okteto.IsOkteto() {
oktetoLog.Warning("external resources cannot be deployed on a cluster not managed by okteto")
Expand Down
20 changes: 19 additions & 1 deletion cmd/deploy/remote.go
Expand Up @@ -20,12 +20,14 @@ import (
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"

remoteBuild "github.com/okteto/okteto/cmd/build/remote"
buildv2 "github.com/okteto/okteto/cmd/build/v2"
"github.com/okteto/okteto/pkg/cmd/build"
"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/constants"
oktetoErrors "github.com/okteto/okteto/pkg/errors"
oktetoLog "github.com/okteto/okteto/pkg/log"
Expand Down Expand Up @@ -106,7 +108,7 @@ func (rd *remoteDeployCommand) deploy(ctx context.Context, deployOptions *Option
}

dockerfileSyntax := dockerfileTemplateProperties{
OktetoCLIImage: constants.OktetoCLIImageForRemote,
OktetoCLIImage: getOktetoCLIVersion(config.VersionString),
UserDeployImage: deployOptions.Manifest.Deploy.Image,
OktetoBuildEnvVars: rd.builder.GetBuildEnvVars(),
ContextEnvVar: model.OktetoContextEnvVar,
Expand Down Expand Up @@ -224,3 +226,19 @@ func getOriginalCWD(cwd, manifestPath string) string {
manifestPathDir := filepath.Dir(fmt.Sprintf("/%s", manifestPath))
return strings.TrimSuffix(cwd, manifestPathDir)
}

func getOktetoCLIVersion(versionString string) string {
var version string
if match, _ := regexp.MatchString(`\d+\.\d+\.\d+`, versionString); match {
version = fmt.Sprintf(constants.OktetoCLIImageForRemoteTemplate, versionString)
} else {
remoteOktetoImage := os.Getenv(constants.OKtetoDeployRemoteImage)
if remoteOktetoImage != "" {
version = remoteOktetoImage
} else {
version = fmt.Sprintf(constants.OktetoCLIImageForRemoteTemplate, "latest")
}
}

return version
}
51 changes: 51 additions & 0 deletions cmd/deploy/remote_test.go
@@ -0,0 +1,51 @@
package deploy

import (
"os"
"testing"

"github.com/okteto/okteto/pkg/constants"
"github.com/stretchr/testify/require"
)

func Test_getOktetoCLIVersion(t *testing.T) {
var tests = []struct {
name string
versionString, expected, cliImageEnv string
}{
{
name: "no version string and no env return latest",
versionString: "",
expected: "okteto/okteto:latest",
},
{
name: "no version string return env value",
versionString: "",
cliImageEnv: "okteto/remote:test",
expected: "okteto/remote:test",
},
{
name: "found version string",
versionString: "2.2.2",
expected: "okteto/okteto:2.2.2",
},
{
name: "found incorrect version string return latest ",
versionString: "2.a.2",
expected: "okteto/okteto:latest",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.cliImageEnv != "" {
os.Setenv(constants.OKtetoDeployRemoteImage, tt.cliImageEnv)
defer os.Unsetenv(constants.OKtetoDeployRemoteImage)
}

version := getOktetoCLIVersion(tt.versionString)
require.Equal(t, version, tt.expected)
})
}
}
20 changes: 19 additions & 1 deletion cmd/destroy/remote.go
Expand Up @@ -7,10 +7,12 @@ import (
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"

remoteBuild "github.com/okteto/okteto/cmd/build/remote"
"github.com/okteto/okteto/pkg/config"
oktetoErrors "github.com/okteto/okteto/pkg/errors"

"github.com/okteto/okteto/pkg/cmd/build"
Expand Down Expand Up @@ -96,7 +98,7 @@ func (rd *remoteDestroyCommand) destroy(ctx context.Context, opts *Options) erro
}

dockerfileSyntax := dockerfileTemplateProperties{
OktetoCLIImage: constants.OktetoCLIImageForRemote,
OktetoCLIImage: getOktetoCLIVersion(config.VersionString),
UserDestroyImage: rd.manifest.Destroy.Image,
ContextEnvVar: model.OktetoContextEnvVar,
ContextValue: okteto.Context().Name,
Expand Down Expand Up @@ -204,3 +206,19 @@ func getDestroyFlags(opts *Options) []string {

return deployFlags
}

func getOktetoCLIVersion(versionString string) string {
var version string
if match, _ := regexp.MatchString(`\d+\.\d+\.\d+`, versionString); match {
version = fmt.Sprintf(constants.OktetoCLIImageForRemoteTemplate, versionString)
} else {
remoteOktetoImage := os.Getenv(constants.OKtetoDeployRemoteImage)
if remoteOktetoImage != "" {
version = remoteOktetoImage
} else {
version = fmt.Sprintf(constants.OktetoCLIImageForRemoteTemplate, "latest")
}
}

return version
}
51 changes: 51 additions & 0 deletions cmd/destroy/remote_test.go
@@ -0,0 +1,51 @@
package destroy

import (
"os"
"testing"

"github.com/okteto/okteto/pkg/constants"
"github.com/stretchr/testify/require"
)

func Test_getOktetoCLIVersion(t *testing.T) {
var tests = []struct {
name string
versionString, expected, cliImageEnv string
}{
{
name: "no version string and no env return latest",
versionString: "",
expected: "okteto/okteto:latest",
},
{
name: "no version string return env value",
versionString: "",
cliImageEnv: "okteto/remote:test",
expected: "okteto/remote:test",
},
{
name: "found version string",
versionString: "2.2.2",
expected: "okteto/okteto:2.2.2",
},
{
name: "found incorrect version string return latest ",
versionString: "2.a.2",
expected: "okteto/okteto:latest",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.cliImageEnv != "" {
os.Setenv(constants.OKtetoDeployRemoteImage, tt.cliImageEnv)
defer os.Unsetenv(constants.OKtetoDeployRemoteImage)
}

version := getOktetoCLIVersion(tt.versionString)
require.Equal(t, version, tt.expected)
})
}
}
7 changes: 5 additions & 2 deletions pkg/constants/constants.go
Expand Up @@ -53,8 +53,11 @@ const (
// OKtetoDeployRemote defines if deployment is executed remotely
OKtetoDeployRemote = "OKTETO_DEPLOY_REMOTE"

// OktetoCLIImageForRemote defines okteto CLI image to use for remote deployments
OktetoCLIImageForRemote = "okteto/okteto:remote-deploy"
// OKtetoDeployRemoteImage defines okteto cli image used for deploy an evironment remotely
OKtetoDeployRemoteImage = "OKTETO_REMOTE_CLI_IMAGE"

// OktetoCLIImageForRemoteTemplate defines okteto CLI image template to use for remote deployments
OktetoCLIImageForRemoteTemplate = "okteto/okteto:%s"

// OktetoPipelineRunnerImage defines image to use for remote deployments if empty
OktetoPipelineRunnerImage = "okteto/installer:1.7.6"
Expand Down

0 comments on commit 680f05e

Please sign in to comment.