Skip to content

Teleterm: fix incorrect cluster URI when fetching kube namespaces on a leaf cluster (access request)#48786

Merged
kimlisa merged 6 commits intomasterfrom
lisa/fix-kube-bug
Nov 14, 2024
Merged

Teleterm: fix incorrect cluster URI when fetching kube namespaces on a leaf cluster (access request)#48786
kimlisa merged 6 commits intomasterfrom
lisa/fix-kube-bug

Conversation

@kimlisa
Copy link
Copy Markdown
Contributor

@kimlisa kimlisa commented Nov 12, 2024

resolves #48510

  • teleterm: fixes fetching namespaces with incorrect cluster URI when on leaf cluster. we already had the correct URI info from the kube_cluster's originalItem, so i just used that
  • teleterm: fixes re-ordering of selected namespaces when done selecting. this is only half fixed. it's fixed for teleterm but not for web UI. teleterm uses a set, while web UI still uses a map (which doesn't preserve order). web UI really needs an entire data refactor, but i didn't deem this little quirk to be bad enough to spend time on it

changelog: Fix incorrect cluster name when querying for Kubernetes namespaces on a leaf cluster for Connect UI.

@kimlisa
Copy link
Copy Markdown
Contributor Author

kimlisa commented Nov 12, 2024

@ravicious also gave feedback on removing the nextKey, but i didn't think it hurt to be there, b/c what if we did want to provide pagination in the future? i can remove it if it still bothers you though

eg: b42e163

@kimlisa kimlisa requested review from gzdunek and ravicious and removed request for avatus and kiosion November 12, 2024 08:31
@kimlisa kimlisa changed the title Teleterm: fix incorrect cluster URI when fetching kube namespaces on a leaf cluster Teleterm: fix incorrect cluster URI when fetching kube namespaces on a leaf cluster (access request) Nov 12, 2024
Comment thread lib/teleterm/daemon/daemon.go
Comment on lines +103 to +104
namespaceUris: KubeResourceNamespaceUri[],
kubeClusterUri: string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So we need kubeClusterUri to validate if all namespaceUris come from the same kube cluster?
It feels to me too complicated, perhaps we shouldn't store namespace uris but just their names.
Since a namespace is always kept in a specific kube cluster, we don't need its whole uri.
So it would be:

async updateNamespacesForKubeCluster(
    kubeUri: KubeUri
    namespaces: string[],
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's fine. While it's true that in this specific context it might seem overboard, in the broader view it lets us treat each namespace as a unique item. In that sense, it simplifies things. If we stored sole namespace names, we'd have to always remember about converting them to URIs when taking them out of AccessRequestsService.

I'm not 100% sure about this, but it feels like long-term it's a better choice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we stored sole namespace names, we'd have to always remember about converting them to URIs when taking them out of AccessRequestsService.

Right, converting to URI could be inconvenient. Well, I don't have a strong opinion on this, so let's keep it as is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Web UI still has this weird re-ordering because web UI still stores resource ids in a map which doesn't preserve order

Do you mean the JavaScript Map? It should preserve the insertion order. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no it's just a js object, now that you mention it... maybe it will be easy to just replace it with a js map (but not in this PR)

Comment thread web/packages/teleterm/src/ui/services/workspacesService/accessRequestsService.ts Outdated
Comment on lines +103 to +104
namespaceUris: KubeResourceNamespaceUri[],
kubeClusterUri: string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's fine. While it's true that in this specific context it might seem overboard, in the broader view it lets us treat each namespace as a unique item. In that sense, it simplifies things. If we stored sole namespace names, we'd have to always remember about converting them to URIs when taking them out of AccessRequestsService.

I'm not 100% sure about this, but it feels like long-term it's a better choice.

Comment thread web/packages/teleterm/src/ui/services/workspacesService/accessRequestsService.ts Outdated
Comment thread web/packages/teleterm/src/ui/services/workspacesService/accessRequestsService.ts Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ravicious also gave feedback on removing the nextKey, but i didn't think it hurt to be there, b/c what if we did want to provide pagination in the future? i can remove it if it still bothers you though

I think we should just remove it to signify that the RPC does not offer pagination. If we ever need to add it back, we can create ListKubernetesResourcesV2. I wouldn't want someone to look at the response message and think that the RPC supports pagination.

{ value: 'bob', label: 'bob' },
]);
});
await act(() =>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

await on act is not necessary, unless the callback passed to act returns a promise. This is the case for a lot of await act in this file. TypeScript's lang server should highlight all of them if you have it enabled in your editor. There's also the @typescript-eslint/await-thenable rule, but enabling it requires adjusting the ESLint config a little bit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hrm, i actually do need them in almost all cases, b/c most are react state setters which are async? (the test fails without them)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

React state setters are technically sync and they don't need to be awaited. TS lang server highlights those callsites because act is given a sync function, so there's nothing to await.

The errors after removing await stem from the fact that useAccessRequestCheckout has an effect which fetches resource roles every time you change pendingAccessRequestRequest. This means each call to updateNamespacesForKubeCluster causes this effect to be run again.

At first I removed those await calls and added waitFor for fetchResourceRolesAttempt. But then I looked up the docs for act and the React team actually recommends passing an async function to act. So I left the awaits, but I made sure that we always pass an async function there. This way the TS language server doesn't complain about those callsites.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I made a PR with those changes: #48969

Comment thread web/packages/teleterm/src/ui/services/workspacesService/accessRequestsService.ts Outdated
Copy link
Copy Markdown
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

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

I tested this against #48510 and the repro for selection order from #47347 (comment) and it works for both.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@gzdunek In the test for useAccessRequestCheckout I noticed that we await a few methods from AccessRequestsService. I looked at them and it's because they do this:

    if (!(await this.canUpdateRequest('resource'))) {
      return;
    }

I think making those methods async might have been a mistake. This layer should be kept as simple as possible and canUpdateRequest could be taken care of one layer above, on a layer that has access to both AccessRequestsService and ModalsService. Like in some kind of a hook or a context.

This would reduce coupling between services and avoid async methods in a class that's largely responsible for just managing some state.

I know we use async methods in WorkspacesService already, but that class is in a bit of a mess itself. ClustersService's usage of async methods feels better because it's all about getting data and storing data. The way WorkspacesService and AccessRequestsService use async methods mixes together the data layer and the UI layer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think my reasoning was to have a centralized place where changing request type is handled. But yeah, it would be better to do it on a different level, and AccessRequestsService should just overwrite the existing state with the new request (e.g. when trying to add role to a resource request etc).
I will keep that mind.

If you add a root and leaf kube cluster,and on leaf Teleport cluster,
listing namespaces for root kube cluster was referring to incorrect
Teleport cluster URI
Web UI still has this weird re-ordering because web UI still stores
resource ids in a map which doesn't preserve order
@kimlisa kimlisa enabled auto-merge November 14, 2024 12:02
@kimlisa kimlisa added this pull request to the merge queue Nov 14, 2024
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 14, 2024
@kimlisa kimlisa added this pull request to the merge queue Nov 14, 2024
Merged via the queue into master with commit 8299ab5 Nov 14, 2024
@kimlisa kimlisa deleted the lisa/fix-kube-bug branch November 14, 2024 12:51
@public-teleport-github-review-bot
Copy link
Copy Markdown

@kimlisa See the table below for backport results.

Branch Result
branch/v16 Failed
branch/v17 Create PR

kimlisa added a commit that referenced this pull request Nov 14, 2024
…a leaf cluster (access request) (#48786)

* Use tc.SiteName over cluster.Name

* Teleterm: fix fetching namespaces with incorrect cluster URI

If you add a root and leaf kube cluster,and on leaf Teleport cluster,
listing namespaces for root kube cluster was referring to incorrect
Teleport cluster URI

* Teleterm: fix re-ordering of selected namespaces when done selecting

Web UI still has this weird re-ordering because web UI still stores
resource ids in a map which doesn't preserve order

* Add requested length test

* Address CR

* Address CRs
github-merge-queue Bot pushed a commit that referenced this pull request Nov 14, 2024
…es on a leaf cluster (#48990)

* Teleterm: fix incorrect cluster URI when fetching kube namespaces on a leaf cluster (access request) (#48786)

* Use tc.SiteName over cluster.Name

* Teleterm: fix fetching namespaces with incorrect cluster URI

If you add a root and leaf kube cluster,and on leaf Teleport cluster,
listing namespaces for root kube cluster was referring to incorrect
Teleport cluster URI

* Teleterm: fix re-ordering of selected namespaces when done selecting

Web UI still has this weird re-ordering because web UI still stores
resource ids in a map which doesn't preserve order

* Add requested length test

* Address CR

* Address CRs

* Update snaps
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.

Selecting kube namespace for access request in Connect sources cluster URI from wrong place

3 participants