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

ui - enhance flow typing #3002

Merged
merged 20 commits into from
Jan 18, 2021
Merged

ui - enhance flow typing #3002

merged 20 commits into from
Jan 18, 2021

Conversation

JBWatenbergScality
Copy link
Contributor

Component:

ui, typing

Context:

This pull request is a first milestone to introduce better typings in the project.
This brings type-safety and enhanced developer experience to the project.

As introducing strong typing everywhere in the project would require a few more pull requests the idea of this one is to focus on kubernetes client and reducers that make use of kubernetes client. However it also brings typings for few others reducers and introduce starting points to expand typing to other areas in the project.

Closes: #2999

See: #3000 #3001

@JBWatenbergScality JBWatenbergScality requested a review from a team as a code owner December 24, 2020 15:49
@bert-e
Copy link
Contributor

bert-e commented Dec 24, 2020

Hello jbwatenbergscality,

My role is to assist you with the merge of this
pull request. Please type @bert-e help to get information
on this process, or consult the user documentation.

Status report is not available.

@bert-e
Copy link
Contributor

bert-e commented Dec 24, 2020

Branches have diverged

This pull request's source branch improvement/flow-typing has diverged from
development/2.7 by more than 50 commits.

To avoid any integration risks, please re-synchronize them using one of the
following solutions:

  • Merge origin/development/2.7 into improvement/flow-typing
  • Rebase improvement/flow-typing onto origin/development/2.7

Note: If you choose to rebase, you may have to ask me to rebuild
integration branches using the reset command.

@bert-e
Copy link
Contributor

bert-e commented Dec 24, 2020

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • one peer

Peer approvals must include at least 1 approval from the following list:

Copy link
Contributor

@gdemonet gdemonet left a comment

Choose a reason for hiding this comment

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

Super cool stuff, thanks for starting this heavy lifting, I'm confident this will increase productivity a lot!
Some comments and suggestions here and there 😉 , just adding a more general one here: are you running Prettier? We have a committed .prettierrc for you to use, the goal being to keep the code consistent without spending time on aesthetics in PR reviews 😜

kind: 'Volume',
metadata?: V1ObjectMeta,
spec: {
mode: 'Filesystem',
Copy link
Contributor

Choose a reason for hiding this comment

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

The mode can be either Filesystem or Block, actually. See our docs or the CRD

storageClassName: string
},
status: {
deviceName: string,
Copy link
Contributor

Choose a reason for hiding this comment

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

If this was not yet reconciled (e.g. because the Volume is Pending), this will be empty from the Operator's PoV, and absent in the API object.

},
storageClassName: string
},
status: {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to be exhaustive, there is also a status.job key (which is also optional, and its usage is internal to the Operator).

return { type: UPDATE_VOLUMES, payload };
};

export const fetchCurrentVolumeObjectAction = (volumeName) => {
export const fetchCurrentVolumeObjectAction = (volumeName: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we can safely assume this should be a string, no?

Comment on lines 186 to 191
export function* fetchVolumes(): Generator<{
body: {items: Metalk8sV1Alpha1Volume[]};
} | {error: any, body: null}, {
body: {items: Metalk8sV1Alpha1Volume[]};
} | {error: any, body: null}, {
body: {items: Metalk8sV1Alpha1Volume[]};
} | {error: any, body: null}> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that's a valid type for sagas. IIUC, the type signature for a coroutine is Generator<Yield, Return, Next>. Sagas only yield "effects" (in the sense of redux-saga), and they may be fed with anything.
For this reason, there is a handy type definition in flow-typed for the redux-saga library:

import type { Saga } from 'redux-saga';

// same as:
declare type Saga<T> = Generator<Effect, T, any>

See the definitions here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks I was looking for those typings desperately 👍 👍

export type ConfigState = {
language: string,
theme: Theme,
api: Config | null,
Copy link
Contributor

Choose a reason for hiding this comment

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

?Config would work here as well, IIUC

return { type: SET_THEMES, payload: themes };
}

// Selectors
export const languageSelector = (state) => state.config.language;
export const apiConfigSelector = (state) => state.config.api;
export const languageSelector = (state: RootState) => state.config.language;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a question: since RootState is properly typed, is flow able to infer the return type of these selectors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, flow is inferring the type of languageSelector from state.config.language

const result = yield call(ApiSalt.authenticate, user);
if (!result.error) {
export function* authenticateSaltApi(): Generator<any, void, any> {
const api: Config | null = yield select(apiConfigSelector);
Copy link
Contributor

Choose a reason for hiding this comment

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

(ugh, even if flow was able to infer the return type of a selector, it can't guess the value injected by the coroutine's "owner"... some big limitation of sagas here)

Copy link
Contributor Author

@JBWatenbergScality JBWatenbergScality Dec 28, 2020

Choose a reason for hiding this comment

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

Yes the simplest way I found to type complex saga with different return type over iterations is to type Next as any and force the type of each of the variables 😞 . Even through this is not directly linked to redux but more to typing, IMO redux and redux saga are adding a lot of complexity for such simple tasks as data fetching, I prefer using libraries such as react-query or swr that greatly ease data fetching and caching use cases and that are way more easier to type as in most cases the typing would be inferred from API clients typing.

export function* authenticateSaltApi(): Generator<any, void, any> {
const api: Config | null = yield select(apiConfigSelector);
const user: User = yield select((state: RootState) => state.oidc?.user);
const result: { error: any } | ApiSalt.SaltToken = yield call(ApiSalt.authenticate, user);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be handy to have type aliases for such "result" values:

declare type Result<T> = { error: any } | T

// Or for API results under a `body` key
declare type APIResult<T> = Result<{ body: T }>

Here we'd have:

const result: Result<Api.SaltToken> = yield call(...)

Comment on lines 58 to 61
export const useTypedSelector: <TSelected>(
selector: (state: RootState) => TSelected,
equalityFn?: (left: TSelected, right: TSelected) => boolean
) => TSelected = useSelector;
Copy link
Contributor

Choose a reason for hiding this comment

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

Two comments:

  • I think we have defined custom hooks in src/services/utils.js(though granted, it doesn't make much sense either) - IMHO, adding a plain src/hooks.js could make it easier to find in the future
  • the purpose of this custom hook is not obvious at first glance, so I'd suggest we add a comment to explain it (and remind the reader that they should use it instead of the standard useSelector hook)

@bert-e
Copy link
Contributor

bert-e commented Dec 28, 2020

Conflict

There is a conflict between your branch improvement/flow-typing and the
destination branch development/2.7.

Please resolve the conflict on the feature branch (improvement/flow-typing).

 $ git fetch
 $ git checkout origin/improvement/flow-typing
 $ git merge origin/development/2.7
 $ # <intense conflict resolution>
 $ git commit
 $ git push origin HEAD:improvement/flow-typing

@bert-e
Copy link
Contributor

bert-e commented Dec 29, 2020

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • one peer

Peer approvals must include at least 1 approval from the following list:

The following reviewers are expecting changes from the author, or must review again:

@@ -0,0 +1,23 @@
## Metalk8s client generator

It generates a flow client based on `@kubernetes/client` custom objects client from kubernetes `CustomResourceDefinition` of metalk8s volumes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome 👍

ChengYanJin
ChengYanJin previously approved these changes Jan 8, 2021
Copy link
Contributor

@ChengYanJin ChengYanJin left a comment

Choose a reason for hiding this comment

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

LGTM!
Can't wait to get it to merge and benefit from the typing.

@bert-e
Copy link
Contributor

bert-e commented Jan 8, 2021

Conflict

There is a conflict between your branch improvement/flow-typing and the
destination branch development/2.7.

Please resolve the conflict on the feature branch (improvement/flow-typing).

 $ git fetch
 $ git checkout origin/improvement/flow-typing
 $ git merge origin/development/2.7
 $ # <intense conflict resolution>
 $ git commit
 $ git push origin HEAD:improvement/flow-typing

@bert-e
Copy link
Contributor

bert-e commented Jan 8, 2021

Integration data created

I have created the integration data for the additional destination branches.

The following branches will NOT be impacted:

  • development/1.0
  • development/1.1
  • development/1.2
  • development/1.3
  • development/2.0
  • development/2.1
  • development/2.2
  • development/2.3
  • development/2.4
  • development/2.5
  • development/2.6

You can set option create_pull_requests if you need me to create
integration pull requests in addition to integration branches, with:

@bert-e create_pull_requests

@bert-e
Copy link
Contributor

bert-e commented Jan 8, 2021

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • one peer

Peer approvals must include at least 1 approval from the following list:

The following reviewers are expecting changes from the author, or must review again:

Copy link
Contributor

@gdemonet gdemonet left a comment

Choose a reason for hiding this comment

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

Could we squash the commits into logical units, now that reviewers approved? I'm not too fond of fix stuff commits, especially for fixing things introduced in the same PR.

@bert-e
Copy link
Contributor

bert-e commented Jan 8, 2021

History mismatch

Merge commit #6e03a09581f9c41616f1bf5816c34e72ea53663c on the integration branch
w/2.8/improvement/flow-typing is merging a branch which is neither the current
branch improvement/flow-typing nor the development branch
development/2.8.

It is likely due to a rebase of the branch improvement/flow-typing and the
merge is not possible until all related w/* branches are deleted or updated.

Please use the reset command to have me reinitialize these branches.

Comment on lines 59 to 79
export async function get${singleName}(${singleName}Name: string): Promise<Result<${singleName}>> {
if (!customObjects) {
return { error: 'customObject has not yet been initialized' };
}
try {
return await customObjects.getClusterCustomObject(
'${crdSpec.spec.group}',
'${crdSpec.spec.version}',
'${crdSpec.spec.names.plural}',
${singleName}Name,
);
} catch (error) {
return { error };
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this part should be squashed with ui: add a flow typed client generator (it's more than just ui: use generated metalk8s client and types)

gdemonet
gdemonet previously approved these changes Jan 9, 2021
@gdemonet gdemonet dismissed their stale review January 9, 2021 18:58

Checked the CI, it looks like unit tests are failing (apparently src/ducks/app/volumes.test.js was not updated to use the generated client, and something changed in the pods state, which makes src/ducks/app/pods.test.js fail).

ChengYanJin
ChengYanJin previously approved these changes Jan 13, 2021
@JBWatenbergScality
Copy link
Contributor Author

/approve

@bert-e
Copy link
Contributor

bert-e commented Jan 13, 2021

History mismatch

Merge commit #6e03a09581f9c41616f1bf5816c34e72ea53663c on the integration branch
w/2.8/improvement/flow-typing is merging a branch which is neither the current
branch improvement/flow-typing nor the development branch
development/2.8.

It is likely due to a rebase of the branch improvement/flow-typing and the
merge is not possible until all related w/* branches are deleted or updated.

Please use the reset command to have me reinitialize these branches.

The following options are set: approve

@JBWatenbergScality
Copy link
Contributor Author

/reset

@bert-e
Copy link
Contributor

bert-e commented Jan 13, 2021

Reset complete

I have successfully deleted this pull request's integration branches.

The following options are set: approve

@bert-e
Copy link
Contributor

bert-e commented Jan 13, 2021

Integration data created

I have created the integration data for the additional destination branches.

The following branches will NOT be impacted:

  • development/1.0
  • development/1.1
  • development/1.2
  • development/1.3
  • development/2.0
  • development/2.1
  • development/2.2
  • development/2.3
  • development/2.4
  • development/2.5
  • development/2.6

You can set option create_pull_requests if you need me to create
integration pull requests in addition to integration branches, with:

@bert-e create_pull_requests

The following options are set: approve

@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

History mismatch

Merge commit #9a1bb97024ac924946876225330345dcd94bf116 on the integration branch
w/2.8/improvement/flow-typing is merging a branch which is neither the current
branch improvement/flow-typing nor the development branch
development/2.8.

It is likely due to a rebase of the branch improvement/flow-typing and the
merge is not possible until all related w/* branches are deleted or updated.

Please use the reset command to have me reinitialize these branches.

The following options are set: approve

@JBWatenbergScality
Copy link
Contributor Author

/reset

@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

Reset complete

I have successfully deleted this pull request's integration branches.

The following options are set: approve

@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

Integration data created

I have created the integration data for the additional destination branches.

The following branches will NOT be impacted:

  • development/1.0
  • development/1.1
  • development/1.2
  • development/1.3
  • development/2.0
  • development/2.1
  • development/2.2
  • development/2.3
  • development/2.4
  • development/2.5
  • development/2.6

You can set option create_pull_requests if you need me to create
integration pull requests in addition to integration branches, with:

@bert-e create_pull_requests

The following options are set: approve

@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • one peer

Peer approvals must include at least 1 approval from the following list:

The following options are set: approve

@ChengYanJin ChengYanJin changed the base branch from development/2.7 to development/2.8 January 18, 2021 17:16
@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

In the queue

The changeset has received all authorizations and has been added to the
relevant queue(s). The queue(s) will be merged in the target development
branch(es) as soon as builds have passed.

The changeset will be merged in:

  • ✔️ development/2.8

The following branches will NOT be impacted:

  • development/1.0
  • development/1.1
  • development/1.2
  • development/1.3
  • development/2.0
  • development/2.1
  • development/2.2
  • development/2.3
  • development/2.4
  • development/2.5
  • development/2.6
  • development/2.7

There is no action required on your side. You will be notified here once
the changeset has been merged. In the unlikely event that the changeset
fails permanently on the queue, a member of the admin team will
contact you to help resolve the matter.

IMPORTANT

Please do not attempt to modify this pull request.

  • Any commit you add on the source branch will trigger a new cycle after the
    current queue is merged.
  • Any commit you add on one of the integration branches will be lost.

If you need this pull request to be removed from the queue, please contact a
member of the admin team now.

The following options are set: approve

@bert-e
Copy link
Contributor

bert-e commented Jan 18, 2021

I have successfully merged the changeset of this pull request
into targetted development branches:

  • ✔️ development/2.8

The following branches have NOT changed:

  • development/1.0
  • development/1.1
  • development/1.2
  • development/1.3
  • development/2.0
  • development/2.1
  • development/2.2
  • development/2.3
  • development/2.4
  • development/2.5
  • development/2.6
  • development/2.7

Please check the status of the associated issue None.

Goodbye jbwatenbergscality.

@bert-e bert-e merged commit 68390f4 into development/2.8 Jan 18, 2021
@bert-e bert-e deleted the improvement/flow-typing branch January 18, 2021 18:52
ChengYanJin added a commit that referenced this pull request Feb 5, 2021
ChengYanJin added a commit that referenced this pull request Feb 5, 2021
ChengYanJin added a commit that referenced this pull request Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UI - Enhance application typing - kubernetes client and reducers
4 participants