forked from okteto/okteto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: autodeploy flag on okteto up (okteto#4671)
* fix: autodeploy flag on okteto up Signed-off-by: Javier Lopez <[email protected]> * refactor: address feedback Signed-off-by: Javier Lopez <[email protected]> * fix: golangci Signed-off-by: Javier Lopez <[email protected]> * refactor: remove unused parameter from NewDevEnvDeployerManager Signed-off-by: Javier Lopez <[email protected]> * fix: e2e test Signed-off-by: Javier Lopez <[email protected]> * test: uncomment and enable test cases for okteto context and k8s client error Signed-off-by: Javier Lopez <[email protected]> --------- Signed-off-by: Javier Lopez <[email protected]>
- Loading branch information
1 parent
21efc74
commit 9f5a1b2
Showing
4 changed files
with
284 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2025 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 up | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/okteto/okteto/cmd/deploy" | ||
pipelineCMD "github.com/okteto/okteto/cmd/pipeline" | ||
"github.com/okteto/okteto/pkg/analytics" | ||
"github.com/okteto/okteto/pkg/cmd/pipeline" | ||
"github.com/okteto/okteto/pkg/env" | ||
oktetoErrors "github.com/okteto/okteto/pkg/errors" | ||
oktetoLog "github.com/okteto/okteto/pkg/log" | ||
"github.com/okteto/okteto/pkg/log/io" | ||
"github.com/okteto/okteto/pkg/model" | ||
"github.com/okteto/okteto/pkg/okteto" | ||
"github.com/spf13/afero" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const ( | ||
// oktetoAutoDeployEnvVar if set the application will be deployed while running okteto up | ||
oktetoAutoDeployEnvVar = "OKTETO_AUTODEPLOY" | ||
) | ||
|
||
// devEnvDeployerManager deploys the dev environment | ||
type devEnvDeployerManager struct { | ||
isDevEnvDeployed func(ctx context.Context, name, namespace string, c kubernetes.Interface) bool | ||
|
||
k8sClientProvider okteto.K8sClientProvider | ||
ioCtrl *io.Controller | ||
getDeployer func(deployParams) (deployer, error) | ||
} | ||
|
||
type deployer interface { | ||
Run(ctx context.Context, opts *deploy.Options) error | ||
TrackDeploy(manifest *model.Manifest, runInRemoteFlag bool, startTime time.Time, err error) | ||
} | ||
|
||
type deployParams struct { | ||
deployFlag bool | ||
okCtx *okteto.Context | ||
devenvName, ns string | ||
manifestPathFlag, manifestPath string | ||
manifest *model.Manifest | ||
} | ||
|
||
// NewDevEnvDeployerManager creates a new DevEnvDeployer | ||
func NewDevEnvDeployerManager(up *upContext, ioCtrl *io.Controller, k8sLogger *io.K8sLogger) *devEnvDeployerManager { | ||
return &devEnvDeployerManager{ | ||
ioCtrl: ioCtrl, | ||
k8sClientProvider: up.K8sClientProvider, | ||
isDevEnvDeployed: pipeline.IsDeployed, | ||
getDeployer: func(params deployParams) (deployer, error) { | ||
k8sProvider := okteto.NewK8sClientProviderWithLogger(k8sLogger) | ||
pc, err := pipelineCMD.NewCommand() | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := &deploy.Command{ | ||
// reuse the manifest we already have in the upContext so we don't open a file again | ||
GetManifest: func(string, afero.Fs) (*model.Manifest, error) { | ||
return params.manifest, nil | ||
}, | ||
GetDeployer: deploy.GetDeployer, | ||
K8sClientProvider: k8sProvider, | ||
Builder: up.builder, | ||
Fs: up.Fs, | ||
CfgMapHandler: deploy.NewConfigmapHandler(k8sProvider, k8sLogger), | ||
PipelineCMD: pc, | ||
DeployWaiter: deploy.NewDeployWaiter(k8sProvider, k8sLogger), | ||
EndpointGetter: deploy.NewEndpointGetter, | ||
AnalyticsTracker: up.analyticsTracker, | ||
IoCtrl: ioCtrl, | ||
} | ||
return c, nil | ||
}, | ||
} | ||
} | ||
|
||
// DeployIfNeeded deploys the app if it's not already deployed or if the user has set the auto deploy env var or the --deploy flag | ||
func (dd *devEnvDeployerManager) DeployIfNeeded(ctx context.Context, params deployParams, analyticsMeta *analytics.UpMetricsMetadata) error { | ||
if !params.okCtx.IsOkteto { | ||
dd.ioCtrl.Logger().Infof("Deploy is skipped because is not okteto context") | ||
return nil | ||
} | ||
k8sClient, _, err := dd.k8sClientProvider.Provide(params.okCtx.Cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mustDeploy := params.deployFlag | ||
if env.LoadBoolean(oktetoAutoDeployEnvVar) { | ||
mustDeploy = true | ||
} | ||
|
||
if mustDeploy || !dd.isDevEnvDeployed(ctx, params.devenvName, params.ns, k8sClient) { | ||
deployer, err := dd.getDeployer(params) | ||
if err != nil { | ||
dd.ioCtrl.Logger().Infof("failed to create deployer: %s", err) | ||
return fmt.Errorf("failed to create deployer: %w", err) | ||
} | ||
|
||
deployOpts := &deploy.Options{ | ||
Name: params.devenvName, | ||
Namespace: params.ns, | ||
ManifestPathFlag: params.manifestPathFlag, | ||
ManifestPath: params.manifestPath, | ||
Timeout: 5 * time.Minute, | ||
NoBuild: false, | ||
} | ||
startTime := time.Now() | ||
err = deployer.Run(ctx, deployOpts) | ||
go analyticsMeta.HasRunDeploy() | ||
deployer.TrackDeploy(params.manifest, deploy.ShouldRunInRemote(deployOpts), startTime, err) | ||
// only allow error.ErrManifestFoundButNoDeployAndDependenciesCommands to go forward - autocreate property will deploy the app | ||
if err != nil && !errors.Is(err, oktetoErrors.ErrManifestFoundButNoDeployAndDependenciesCommands) { | ||
return err | ||
} | ||
|
||
} else { | ||
oktetoLog.Information("'%s' was already deployed. To redeploy run 'okteto deploy' or 'okteto up --deploy'", params.devenvName) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2025 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 up | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/okteto/okteto/cmd/deploy" | ||
"github.com/okteto/okteto/internal/test" | ||
"github.com/okteto/okteto/pkg/analytics" | ||
oktetoErrors "github.com/okteto/okteto/pkg/errors" | ||
"github.com/okteto/okteto/pkg/log/io" | ||
"github.com/okteto/okteto/pkg/model" | ||
"github.com/okteto/okteto/pkg/okteto" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type fakeDeployer struct { | ||
deployed bool | ||
err error | ||
tracked bool | ||
} | ||
|
||
// Deploy implements the devEnvEnvDeployStrategy interface | ||
func (f *fakeDeployer) Run(context.Context, *deploy.Options) error { | ||
f.deployed = true | ||
return f.err | ||
} | ||
|
||
func (f *fakeDeployer) TrackDeploy(*model.Manifest, bool, time.Time, error) { | ||
f.tracked = true | ||
} | ||
|
||
func TestDeployIfNeeded(t *testing.T) { | ||
type input struct { | ||
isOkteto bool | ||
mustDeploy bool | ||
isDeployedApp bool | ||
k8sClientProviderErr error | ||
deployErr error | ||
} | ||
type expected struct { | ||
isDeploymentExpected bool | ||
expectedErr error | ||
} | ||
tests := []struct { | ||
name string | ||
input input | ||
expected expected | ||
}{ | ||
{ | ||
name: "not okteto context", | ||
input: input{isOkteto: false}, | ||
expected: expected{isDeploymentExpected: false, expectedErr: nil}, | ||
}, | ||
{ | ||
name: "error creating k8s client", | ||
input: input{isOkteto: true, k8sClientProviderErr: assert.AnError}, | ||
expected: expected{isDeploymentExpected: false, expectedErr: assert.AnError}, | ||
}, | ||
{ | ||
name: "must deploy: true // isDeployedApp: false", | ||
input: input{isOkteto: true, k8sClientProviderErr: nil, mustDeploy: true}, | ||
expected: expected{isDeploymentExpected: true, expectedErr: nil}, | ||
}, | ||
{ | ||
name: "must deploy: true // isDeployedApp: true", | ||
input: input{isOkteto: true, k8sClientProviderErr: nil, mustDeploy: true, isDeployedApp: true}, | ||
expected: expected{isDeploymentExpected: true, expectedErr: nil}, | ||
}, | ||
{ | ||
name: "must deploy: false // isDeployedApp: true", | ||
input: input{isOkteto: true, k8sClientProviderErr: nil, mustDeploy: false, isDeployedApp: true}, | ||
expected: expected{isDeploymentExpected: false, expectedErr: nil}, | ||
}, | ||
{ | ||
name: "must deploy: false // isDeployedApp: false", | ||
input: input{isOkteto: true, k8sClientProviderErr: nil, mustDeploy: false}, | ||
expected: expected{isDeploymentExpected: true, expectedErr: nil}, | ||
}, | ||
{ | ||
name: "deploy but error deploying", | ||
input: input{isOkteto: true, mustDeploy: false, deployErr: assert.AnError}, | ||
expected: expected{isDeploymentExpected: true, expectedErr: assert.AnError}, | ||
}, | ||
{ | ||
name: "must deploy: false // isDeployedApp: false", | ||
input: input{isOkteto: true, mustDeploy: true, deployErr: oktetoErrors.ErrManifestFoundButNoDeployAndDependenciesCommands}, | ||
expected: expected{isDeploymentExpected: true, expectedErr: nil}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fakeDeployer := &fakeDeployer{err: tt.input.deployErr} | ||
deployer := &devEnvDeployerManager{ | ||
ioCtrl: io.NewIOController(), | ||
k8sClientProvider: &test.FakeK8sProvider{ | ||
ErrProvide: tt.input.k8sClientProviderErr, | ||
}, | ||
isDevEnvDeployed: func(ctx context.Context, name, namespace string, c kubernetes.Interface) bool { | ||
return tt.input.isDeployedApp | ||
}, | ||
getDeployer: func(params deployParams) (deployer, error) { | ||
return fakeDeployer, nil | ||
}, | ||
} | ||
deployParams := deployParams{ | ||
deployFlag: tt.input.mustDeploy, | ||
okCtx: &okteto.Context{IsOkteto: tt.input.isOkteto}, | ||
} | ||
err := deployer.DeployIfNeeded(context.Background(), deployParams, &analytics.UpMetricsMetadata{}) | ||
assert.ErrorIs(t, tt.expected.expectedErr, err) | ||
assert.Equal(t, tt.expected.isDeploymentExpected, fakeDeployer.deployed) | ||
assert.Equal(t, tt.expected.isDeploymentExpected, fakeDeployer.tracked) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters