Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stack name when manifest at .okteto folder #2742

Merged
merged 7 commits into from
Jun 10, 2022

Conversation

teresaromero
Copy link
Member

@teresaromero teresaromero commented Jun 1, 2022

Signed-off-by: Teresa Romero teresa@okteto.com

Fixes #2661

Proposed changes

  • when docker-compose.yaml is under an .okteto folder at the repository, the dev environment name was being used was okteto. - fixed the parsing for directories
  • if dev environment was okteto destroy and redeploy the components
  • old okteto dev environment will be left empty, used can activly destroy it by ui or cli destroy --name

How to reproduce

@teresaromero teresaromero requested a review from a team June 1, 2022 10:30
@codecov
Copy link

codecov bot commented Jun 1, 2022

Codecov Report

Merging #2742 (0e65641) into master (4e10434) will increase coverage by 0.07%.
The diff coverage is 50.00%.

@@            Coverage Diff             @@
##           master    #2742      +/-   ##
==========================================
+ Coverage   32.10%   32.18%   +0.07%     
==========================================
  Files         158      158              
  Lines       18580    18592      +12     
==========================================
+ Hits         5966     5984      +18     
+ Misses      11909    11896      -13     
- Partials      705      712       +7     
Impacted Files Coverage Δ
pkg/cmd/stack/deploy.go 23.26% <42.85%> (+2.84%) ⬆️
pkg/model/utils.go 68.93% <100.00%> (+0.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4e10434...0e65641. Read the comment docs.

@@ -152,13 +152,18 @@ func Deploy(ctx context.Context) *cobra.Command {
}
}

// when name is an option - this overrides the manifest or inferred name
if options.Name != "" {
os.Setenv(model.OktetoNameEnvVar, options.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very indirect way of setting this and is quite brittle. We've effectively set a global variable, in env, in order to make a decision later on. What are all of the ways this env var get set and how can we track them? Is there a more direct approach we can take that eliminates this?

It seems like a better, and less brittle way, would be to set the name as a config option and somehow passing it into the code that will eventually do the deploy directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, this was actually not needed to be fixed :/ i've updated the scope for the PR due to the comments at the issue #2661

@teresaromero teresaromero changed the title Override stack name if option is not empty Fix stack name when manifest at .okteto folder Jun 2, 2022
@ifbyol
Copy link
Member

ifbyol commented Jun 2, 2022

Note to bear in mind. This could be considered as a non-backward compatible change. If I already executed deployed an application with current version of the CLI, I would have a dev environment called okteto. If I update the CLI after releasing this change and do a redeploy using the same command, now, the name of the dev environment would be different (okteto folder's parent folder) and I would deploy the entire dev environment instead of redeploying the existing one and it might end up with some k8s object collision

I don't know how frequent could be this case, but commented just to take into account

cc @jmacelroy @pchico83

@teresaromero
Copy link
Member Author

Note to bear in mind. This could be considered as a non-backward compatible change. If I already executed deployed an application with current version of the CLI, I would have a dev environment called okteto. If I update the CLI after releasing this change and do a redeploy using the same command, now, the name of the dev environment would be different (okteto folder's parent folder) and I would deploy the entire dev environment instead of redeploying the existing one and it might end up with some k8s object collision

I don't know how frequent could be this case, but commented just to take into account

cc @jmacelroy @pchico83

  • Running okteto deploy with current -> deploys the okteto dev environment
  • Running again okteto deploy with changes -> creates an empty dev environment with the correct name (inferred by the folder) but there are some warnings and the k8s components are not "updated" to be at the new dev environment

CLI logs:


❯ okteto deploy
 i  Using teresa @ okteto.teresa.dev.okteto.net as context
 i  Building image for service 'vote'...
 i  To rebuild your image manually run 'okteto build vote' or 'okteto deploy --build'
 i  Building image for service 'vote'
[+] Building 22.9s (11/11) FINISHED
 ✓  Image for service 'vote' pushed to registry: registry.teresa.dev.okteto.net/teresa/okteto-vote:okteto-with-volume-mounts
 i  Deploying compose
 ✓  Volume 'redis' created
 ✓  Service 'vote' created
 ✓  Service 'redis' created
 ✓  Compose 'okteto' successfully deployed
 ✓  Development environment 'okteto' successfully deployed
 i  Run 'okteto up' to activate your development container
❯ ok deploy
 i  Using teresa @ okteto.teresa.dev.okteto.net as context
 i  Images were already built. To rebuild your images run 'okteto build' or 'okteto deploy --build'
 ✓  Endpoint 'vote' created
 !  skipping creation of volume 'redis' due to name collision with volume in stack 'okteto'
 !  skipping deploy of deployment 'vote' due to name collision with deployment in stack 'okteto'
 !  skipping deploy of statefulset 'redis' due to name collision with statefulset in stack 'okteto'
 ✓  Compose 'compose-getting-started' successfully deployed

Screenshot 2022-06-03 at 09 09 36

@ifbyol
Copy link
Member

ifbyol commented Jun 3, 2022

@teresaromero what would happen if after that I execute okteto destroy?

@teresaromero
Copy link
Member Author

@teresaromero what would happen if after that I execute okteto destroy?

when using the current one, the okteto dev environment is deleted, but the changes, deletes the new dev environment, okteto still is there

Should we add some logic so the components change their labels and dev environment name to be swaped?

@pchico83
Copy link
Contributor

pchico83 commented Jun 3, 2022

@teresaromero what would happen if after that I execute okteto destroy?

when using the current one, the okteto dev environment is deleted, but the changes, deletes the new dev environment, okteto still is there

Should we add some logic so the components change their labels and dev environment name to be swaped?

I would add logic to, in case we are in .okteto, replace this warning ! skipping by actually modifying the component if its dev environment name was okteto.

@ifbyol great catch!

@teresaromero
Copy link
Member Author

@teresaromero what would happen if after that I execute okteto destroy?

when using the current one, the okteto dev environment is deleted, but the changes, deletes the new dev environment, okteto still is there
Should we add some logic so the components change their labels and dev environment name to be swaped?

I would add logic to, in case we are in .okteto, replace this warning ! skipping by actually modifying the component if its dev environment name was okteto.

@ifbyol great catch!

I've been going tthough the code to make this modification, we have to change all components ( deployments, sfs, jobs, volumes, etc...) ?

@teresaromero
Copy link
Member Author

teresaromero commented Jun 6, 2022

I've tried to add some logic to swap the stack names in case the old one was okteto but i get an error from k8s

Error updating deployment of service 'vote': Deployment.apps "vote" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"stack.okteto.com/name":"compose-getting-started", "stack.okteto.com/service":"vote"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

as the labels are inmutable, should then display a warning message so the user can destroy the okteto dev environment and start again?

cc: @pchico83 @ifbyol

@pchico83
Copy link
Contributor

pchico83 commented Jun 6, 2022

I've tried to add some logic to swap the stack names in case the old one was okteto but i get an error from k8s

Error updating deployment of service 'vote': Deployment.apps "vote" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"stack.okteto.com/name":"compose-getting-started", "stack.okteto.com/service":"vote"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

as the labels are inmutable, should then display a warning message so the user can destroy the okteto dev environment and start again?

cc: @pchico83 @ifbyol

@teresaromero I would automatically destroy + deploy the resources, and keep only the volumes. That way, the operation is transparent to the user. We already do something similar when a statefulset cannot be edited

@teresaromero
Copy link
Member Author

@ifbyol @jLopezbarb can you check the latest changes that involve destroy de compontent and redeploy with new labels? thanks

@teresaromero
Copy link
Member Author

edit description with instructions on how to reproduce the change

Comment on lines 506 to 508
if err := deployments.Destroy(ctx, old.Name, old.Namespace, c); err != nil {
return false, fmt.Errorf("error updating deployment of service '%s': %s", svcName, err.Error())
}
if _, err := deployments.Deploy(ctx, d, c); err != nil {
return false, fmt.Errorf("error updating deployment of service '%s': %s", svcName, err.Error())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd, why if it failed because a temporal error redeploying? Any error in a redeploy would imply to destroy a deploy again even if it is not necessary, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, in case of statefulsets we make this only when the error is not "Forbidden", do you think we should control other types of errorS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifbyol i've reverted the change here and added an exception prior to deploying for first time to check if is the exception, if yes, then we destroy and redeploy

Copy link
Member

@ifbyol ifbyol Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I think it looks good now.

Maybe we can include a comment before the if to explain why this piece of code is needed? If not, in a few months we won't remember why we do that check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've added a comment with a link to this PR and some context

Signed-off-by: Teresa Romero <teresa@okteto.com>
Signed-off-by: Teresa Romero <teresa@okteto.com>
Signed-off-by: Teresa Romero <teresa@okteto.com>
return false, fmt.Errorf("skipping deploy of deployment '%s' due to name collision with deployment in stack '%s'", svcName, old.Labels[model.StackNameLabel])
}
if v, ok := old.Labels[model.DeployedByLabel]; ok {
if old.Labels[model.StackNameLabel] == "okteto" {
d.Labels[model.DeployedByLabel] = s.Name
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need an else here? If you set the deployed-by label to the new name, the line 498 will overwrite it with the value coming from the old deployment, right?

Or moving the line 498 before doing the okteto check would be enough

Signed-off-by: Teresa Romero <teresa@okteto.com>
Signed-off-by: Teresa Romero <teresa@okteto.com>
@jLopezbarb
Copy link
Contributor

Can we add some unit test with the scenario of having an .okteto folder

Signed-off-by: Teresa Romero <teresa@okteto.com>
Signed-off-by: Teresa Romero <teresa@okteto.com>
@teresaromero
Copy link
Member Author

teresaromero commented Jun 10, 2022

@maroshii i've added both 2 labels: bugfix and breaking change... its definatly a bug fix as the users which use an .okteto folder for their compose and stacks will now have the dev environment name fixed, but at the same time, if someone had a dev environment deployed with prev version of cli, now the old one will be empty and they will se a new one with the correct name, not sure if this is a breking change, or "not that breaking" change.. but is a change :/

EDIT: on i get an error that only 1 so... i will go with bug fix as we have logic in place to dont break

@teresaromero teresaromero merged commit fe90604 into master Jun 10, 2022
@teresaromero teresaromero deleted the tere/2661-fix-name-stack branch June 10, 2022 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

okteto deploy --name isn't propagated to my stack name label
5 participants