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 okteto endpoints namespace detection from okteto.yml #4186

Merged
merged 8 commits into from Feb 23, 2024

Conversation

andreafalzetti
Copy link
Contributor

@andreafalzetti andreafalzetti commented Feb 20, 2024

Proposed changes

okteto endpoints ignores the property namespace of the okteto.yml.

How to validate

  1. Create a new namespace where you have nothing deployed to (from now referred as NS1)
  2. Select NS1 as your current default
  3. Using any project (ie. https://github.com/okteto/movies-frontend)
  4. Modify the okteto.yml and add a top-level namespace: <NS2> (replace <NS2> with a namespace different from NS1)
  5. Run okteto endpoints and observe how the CLI outputs Using <NS1> @ <Context> as context
  6. It's worth noticing that the endpoints are printed correctly, it's just the namespace name that is wrong (The CLI uses the correct one, but it displayes the wrong one)

Build the CLI from this PR and repeat the steps:

  1. Observe how okteto endpoints print the correct namespace
  2. Deploy the app and observe the right endpoints printer for each NS

Extra:
Currently if you run okteto endpoints -f not-exists.yml the endoints cmd falls back on okteto.yml. This behavior is inconsistent with other cmds so I've changed it to return an error (like okteto deploy).

Test Scenarios

  • okteto endpoints
  • okteto endpoints -n <NS>
  • okteto endpoints with namespace: <NS>

CLI Quality Reminders 🔧

For both authors and reviewers:

  • Scrutinize for potential regressions
  • Ensure key automated tests are in place
  • Build the CLI and test using the validation steps
  • Assess Developer Experience impact (log messages, performances, etc)
  • If too broad, consider breaking into smaller PRs
  • Adhere to our code style and code review guidelines

This comment was marked as outdated.

@andreafalzetti andreafalzetti added run-e2e-unix When used on a PR run unix e2e and removed no-dco labels Feb 20, 2024

This comment was marked as outdated.

… file

Signed-off-by: Andrea Falzetti <andrea@okteto.com>
@andreafalzetti andreafalzetti changed the title Fix/okteto endpoints Fix okteto endpoints namespace detection from okteto.yml Feb 20, 2024
}
if err := contextCMD.NewContextCommand().Run(ctx, ctxOptions); err != nil {
return err
// Loads, updates and uses the context from path. If not found, it creates and uses a new context
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same logic used in okteto deploy

Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Copy link

codecov bot commented Feb 20, 2024

Codecov Report

Merging #4186 (231827a) into master (31cbb7c) will increase coverage by 0.03%.
Report is 3 commits behind head on master.
The diff coverage is 0.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4186      +/-   ##
==========================================
+ Coverage   45.55%   45.59%   +0.03%     
==========================================
  Files         289      289              
  Lines       27108    27101       -7     
==========================================
+ Hits        12350    12356       +6     
+ Misses      13672    13661      -11     
+ Partials     1086     1084       -2     

Copy link
Member

@ifbyol ifbyol left a comment

Choose a reason for hiding this comment

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

Testing it I see that with the code in this PR, the context used is not printed in the CLI output.

You can see that when I use ok (the binary built with the changes in this branch), the context is not displayed and it directly shows the endpoints

If I use okteto (latest stable CLI version), it shows the context used (the wrong one)

Screenshot 2024-02-21 at 09 20 40

@@ -224,7 +224,7 @@ func LoadStackWithContext(ctx context.Context, name, namespace string, stackPath
}

// LoadContextFromPath initializes the okteto context taking into account command flags and manifest namespace/context fields
func LoadContextFromPath(ctx context.Context, namespace, k8sContext, path string) error {
func LoadContextFromPath(ctx context.Context, namespace, k8sContext, path string, defaultCtxOpts *Options) error {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a pointer for defaultCtxOpts? I see we always pass a value. We could just pass the value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no need to be pointer actually, the code is much better with a value type, changed in e0b7d6b

thanks!

Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Signed-off-by: Andrea Falzetti <andrea@okteto.com>
@andreafalzetti
Copy link
Contributor Author

andreafalzetti commented Feb 21, 2024

Testing it I see that with the code in this PR, the context used is not printed in the CLI output.

You can see that when I use ok (the binary built with the changes in this branch), the context is not displayed and it directly shows the endpoints

If I use okteto (latest stable CLI version), it shows the context used (the wrong one)
Screenshot 2024-02-21 at 09 20 40

Good catch @ifbyol! I made the Show dynamic and forgot to revert a change I had to make it show depending on the output flag. 🙇

@andreafalzetti andreafalzetti marked this pull request as ready for review February 21, 2024 10:38
@andreafalzetti andreafalzetti requested a review from a team as a code owner February 21, 2024 10:38
if err := contextCMD.NewContextCommand().Run(ctx, ctxOptions); err != nil {
return err
// Loads, updates and uses the context from path. If not found, it creates and uses a new context
if err := contextCMD.LoadContextFromPath(ctx, options.Namespace, options.K8sContext, options.ManifestPath, contextCMD.Options{Show: options.Output == ""}); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Why for the endpoints command the Show value would depend on the options.Output value but for the deploy and destroy command it is always true. What is special about the endpoints command?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the okteto endpoints command can be used as follows:

okteto endpoints --output=json | jq XYZ

or

okteto endpoints --output=md > README.md

In those scenarios we cannot output the plaintext header or it would break those scenarios. For build and deploy we don't have such use-cases, or at least I am not aware or them, so the Show value - only for endpoints depend on its --output flag.

Worth mentioning that for "plaintext" in most commands of the CLI we use empty string. So by default an empty --output flag means "plaintext". So the check is a bit odd. I hope that with 3.0 we can add a third allowed value for --output plaintext or --output text to make this more explicit.

Copy link
Member

Choose a reason for hiding this comment

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

For deploy and destroy we have the global flag --log-output which could be also json (I was confusing the flag output with that global flag). I was testing deploy and destroy, and when the --log-output flag is set, we are not displaying the context information even if we set Show: true in the context.

I see it working fine but I think this is confusing for the user. If I execute okteto endpoints --log-output=json it doesn't print anything in the output

@ifbyol
Copy link
Member

ifbyol commented Feb 22, 2024

I was testing it and If I specify the namespace through the flag -n, we don't show the context information, but prior this change, we were displaying it

Screenshot 2024-02-22 at 09 27 23

This comment was marked as off-topic.

@derek derek bot added the no-dco label Feb 22, 2024
@andreafalzetti
Copy link
Contributor Author

I was testing it and If I specify the namespace through the flag -n, we don't show the context information, but prior this change, we were displaying it
Screenshot 2024-02-22 at 09 27 23

Good catch, I think the scenario you reproduced is because when you specify the namespace in both places (manifest + flag), contextCMD.LoadContextFromPath fails (due to error oktetoErrors.ErrNamespaceNotMatching from the func UpdateNamespace), calling the fallback logic contextCMD.NewContextCommand().Run, which had Show: false hardcoded.

After looking at why it had Show: false hardcoded, it seems to me a safeguard to avoid showing the header twice. In fact, in case of error occurring after the display of the header in the first func, the second func might display it again. Considering that the display is done just before a return nil, I don't think it's risky to pass a dynamic Show in both functions.

However, keeping false will never make the header appear, so it's a conflicting logic, so I've pushed a new commit with this change, fixing this

if err := contextCMD.NewContextCommand().Run(ctx, ctxOptions); err != nil {
return err
// false for 'json' and 'md' to avoid breaking their syntax
showCtxHeader := options.Output == ""
Copy link
Member

Choose a reason for hiding this comment

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

👏 Thanks for the comment clarifying the case

@andreafalzetti andreafalzetti merged commit a1e8c96 into master Feb 23, 2024
16 of 18 checks passed
@andreafalzetti andreafalzetti deleted the fix/okteto-endpoints branch February 23, 2024 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-dco release/bug-fix run-e2e-unix When used on a PR run unix e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants