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

refactoring (support ODF in any Namespace) #1099

Merged

Conversation

SanjalKatiyar
Copy link
Collaborator

@SanjalKatiyar SanjalKatiyar commented Nov 13, 2023

Adds a new package "immer".
Adds a new extension "console.redux-reducer", for redux state.
Adds a new hook "useOprInstallNamespaceSelector", for detecting ODF install namespace.
Removes dependency on "openshift-storage" namespace.

@SanjalKatiyar SanjalKatiyar force-pushed the multiple_sc_refactor branch 7 times, most recently from 1120a41 to 712f0be Compare November 17, 2023 09:03
@SanjalKatiyar SanjalKatiyar changed the title [WIP] refactoring (multiple StorageClusters + ODF in any Namespace) refactoring (support ODF in any Namespace) Nov 17, 2023
switch (action.type) {
case nsActions.SetOprInstallNs:
return {
...state,
Copy link
Contributor

Choose a reason for hiding this comment

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

This performs a shallow copy. The internal properties will be equal by reference, this can cause bugs. Use immutable to help you work with state.
see https://immutable-js.com/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done, added immer as per the suggestion...

* Wrapper around "useK8sGet" hook.
* Ensures no API request is made unless its "safe" to do so (ODF installed namespace is fetched successfully).
*/
export const useSafeK8sGet = <R extends K8sResourceCommon = K8sResourceCommon>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this required for the usecase when the user is not an admin?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this can use used for any case (given we have required RBACs), it saves some unnecessary API calls... in case of non-admin, if we want we can pass allowFallback = true argument which would default to openshift-storage...

anyError,
}) =>
!isAllSafe ? (
<StatusBox loaded={isAllLoaded} loadError={anyError} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't StatusBox take a component as child?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it does, but current way makes it possible to display children even in case of any error (upto consumer of the FC to decide what to do)...
one of the example:
<StatusBox loaded={isAllLoaded} loadError={anyError} />{children}</StatusBox> >> then in case of non-admin this will always show error as anyError will not be empty (RBAC issue), but if I pass allowFallback argument and use ternary condition (current way) I can still display children, if I want to.

Comment on lines 132 to 134
onChange={(e) => {
onParamChange(parameterKey, e.currentTarget.value, false);
}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was testing something, then forgot to remove this... will update...

// ToDo (epic 4422): Need to pass the namespace where ceph cluster is deployed (remove from here, add dropdown)
React.useEffect(() => {
sessionStorage.setItem(
CEPH_CLUSTER_NAMESPACE_SESSION_STORAGE,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
CEPH_CLUSTER_NAMESPACE_SESSION_STORAGE,
CEPH_CLUSTER_NAMESPACE,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree it is a long name, but it is not really a CEPH_CLUSTER_NAMESPACE, it is infact the "key" that we use to store the namespace in the session... will update anyway...

useOprInstallNamespaceSelector();

const canUseFallback = allowFallback && isFallbackSafe;
const useK8sGetArgs = getValidK8sOptions(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const useK8sGetArgs = getValidK8sOptions(
const K8sGetArgs = getValidK8sOptions(

use should be prefix for hooks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack...


/**
* Wrapper around "useK8sGet" hook.
* Ensures no API request is made unless its "safe" to do so (ODF installed namespace is fetched successfully).
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC this assumption is incorrect. A fetch request will go but it will just be a useless call. Nevertheless we are adding this call to our stack. If you really want to avoid this I suggest you use the fetch functions such as k8sGetResource instead of useK8sGet hook. This applies for other safe functions as well.

Copy link
Collaborator Author

@SanjalKatiyar SanjalKatiyar Nov 22, 2023

Choose a reason for hiding this comment

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

it won't send any api call at all because of the resource object that I am passing to useK8sGet (I defined getValidK8sOptions for the very same reason)...
and I already updated useK8sGet as well (in this same PR), there is a condition if (kind) fetch(); that would ensure no api call...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same is true for other "safe" hook wrapper that I defined...

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see if (kind) fetch(); anywhere. Can you point me to it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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


type UseOprInstallNamespaceDispatch = () => (payload: nsPayload) => void;

export const useOprInstallNamespaceDispatch: UseOprInstallNamespaceDispatch =
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see much value of having a separate component. It's used in only one place. We can just simply dispatch the action with some args. Is this used in more than one place?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not really, but it is more of a directory structure to keep things logically grouped together...
in future if we need another dispatcher (diff use case) then we can create a separate file for that (here under /redux/dispatchers) and export via index...
maybe useOprInstallNamespaceDispatch is not reused, but others (in future) could be... so it's more of a way to group things under new redux directory...

import { getNamespace } from '@odf/shared/selectors';
import { SubscriptionKind } from '@odf/shared/types';
import { k8sList } from '@openshift-console/dynamic-plugin-sdk';
import { useOprInstallNamespaceDispatch } from '../dispatchers';
Copy link
Contributor

Choose a reason for hiding this comment

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

Please see that you are not adding circular dependencies with imports.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don;t think we have any circular dependency, maybe I missed somewhere ??
also we use CircularDependencyPlugin in webpack config... so I am assuming we are safe...


return {
// installed operator namespace
oprInstallNs,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we do a better name odfNamespace? Also how are we handling usecases for MCO? Do we need to detect MCO as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack !
we don't need it in MCO, there are just couple of places where we need this namespace and for that as well we need not to rely on this hook/redux (anyway MCO will be on hub and ODF on managed cluster, so this redux won't work for MCO)...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

gonna be lot of changes to rename everything though !!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done...

getOprInstallNamespace
);

return {
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 return it as an array end users will not have to use the same name for variables. It might be better as we might have to support multiple namespaces for multiple operators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

but array would mean to always read "return"ed values in same order... and if I don't want to read all values leaving gaps such as: const [oprInstallNs, _, _, isSafe] = useOprInstallNamespaceSelector();...

if we want to rename we can use const { oprInstallNs: test } = useOprInstallNamespaceSelector(); right ??

Comment on lines +37 to +38
if (kind) fetch();
else setLoaded(true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same is true for useK8sList.ts

Adds a new package "immer".
Adds a new extension "console.redux-reducer", for redux state.
Adds a new hook "useOprInstallNamespaceSelector", for detecting ODF install namespace.
Removes dependency on "openshift-storage" namespace.
@SanjalKatiyar
Copy link
Collaborator Author

/test odf-console-e2e-aws

@openshift-ci openshift-ci bot added the lgtm label Nov 23, 2023
@bipuladh
Copy link
Contributor

/lgtm

Copy link
Contributor

openshift-ci bot commented Nov 23, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bipuladh, SanjalKatiyar

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [SanjalKatiyar,bipuladh]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit 5117053 into red-hat-storage:master Nov 23, 2023
3 checks passed
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.

2 participants