Skip to content

Commit

Permalink
Fix the behaviour of the new project field for Kind installation
Browse files Browse the repository at this point in the history
Reference:
kubev2v#1409 (comment)
- comment num 1.

- Enable the ProjectNameSelect component to run on Kind (non OCP)
installaiton, the same as supported for the console namespaces menu.

-Fix the namespace default value for upstream.

Signed-off-by: Sharon Gratch <[email protected]>
  • Loading branch information
sgratch committed Dec 19, 2024
1 parent 487ee0d commit e3ed2dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React from 'react';
import { useForkliftTranslation } from 'src/utils';

import { FormGroupWithHelpText, TypeaheadSelect, TypeaheadSelectOption } from '@kubev2v/common';
import {
K8sResourceKind,
useFlag,
useK8sWatchResource,
} from '@openshift-console/dynamic-plugin-sdk';
import { Popover } from '@patternfly/react-core';
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';

Expand Down Expand Up @@ -46,3 +51,20 @@ export const ProjectNameSelect: React.FC<ProjectNameSelectProps> = ({
</FormGroupWithHelpText>
);
};

export const useProjectNameSelectOptions = (defaultProject: string): TypeaheadSelectOption[] => {
const isUseProjects = useFlag('OPENSHIFT'); // OCP or Kind installations

const [projects, projectsLoaded, projectsLoadError] = useK8sWatchResource<K8sResourceKind[]>({
kind: isUseProjects ? 'Project' : 'Namespace',
isList: true,
});

return !projects || !projectsLoaded || projectsLoadError
? // In case of an error or an empty list, returns the active namespace
[{ value: defaultProject, content: defaultProject }]
: projects.map((project) => ({
value: project.metadata?.name,
content: project.metadata?.name,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ProvidersCreatePage: React.FC<{
const [activeNamespace, setActiveNamespace] = useActiveNamespace();
const [providerNames] = useK8sWatchProviderNames({ namespace });
const defaultNamespace = process?.env?.DEFAULT_NAMESPACE || 'default';
const projectName = activeNamespace === '#ALL_NS#' ? 'openshift-mtv' : activeNamespace;
const projectName = activeNamespace === '#ALL_NS#' ? defaultNamespace : activeNamespace;
const initialNamespace = namespace || projectName || defaultNamespace;

const initialState: ProvidersCreatePageState = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useReducer } from 'react';
import { Base64 } from 'js-base64';
import { ProjectNameSelect } from 'src/modules/Plans/views/create/components/ProjectNameSelect';
import {
ProjectNameSelect,
useProjectNameSelectOptions,
} from 'src/modules/Plans/views/create/components/ProjectNameSelect';
import { ModalHOC } from 'src/modules/Providers/modals';
import { validateK8sName, ValidationMsg } from 'src/modules/Providers/utils';
import { SelectableCard } from 'src/modules/Providers/utils/components/Gallery/SelectableCard';
import { SelectableGallery } from 'src/modules/Providers/utils/components/Gallery/SelectableGallery';
import { ForkliftTrans, useForkliftTranslation } from 'src/utils/i18n';

import { FormGroupWithHelpText } from '@kubev2v/common';
import {
IoK8sApiCoreV1Secret,
K8sResourceCommon,
ProviderType,
V1beta1Provider,
} from '@kubev2v/types';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { IoK8sApiCoreV1Secret, ProviderType, V1beta1Provider } from '@kubev2v/types';
import {
Flex,
FlexItem,
Expand Down Expand Up @@ -50,14 +47,7 @@ export const ProvidersCreateForm: React.FC<ProvidersCreateFormProps> = ({
}) => {
const { t } = useForkliftTranslation();

const [projects, projectsLoaded] = useK8sWatchResource<K8sResourceCommon[]>({
groupVersionKind: {
version: 'v1',
group: 'project.openshift.io',
kind: 'Project',
},
isList: true,
});
const projects = useProjectNameSelectOptions(projectName);

const initialState = {
validation: {
Expand Down Expand Up @@ -126,11 +116,7 @@ export const ProvidersCreateForm: React.FC<ProvidersCreateFormProps> = ({
<Form isWidthLimited className="forklift-section-secret-edit">
<ProjectNameSelect
value={projectName}
options={projects.map((project) => ({
value: project.metadata?.name,
content: project.metadata?.name,
}))}
isDisabled={!projectsLoaded || !projects.length}
options={projects}
onSelect={onProjectNameChange}
popoverHelpContent={
<ForkliftTrans>The project that your provider will be created in.</ForkliftTrans>
Expand Down

0 comments on commit e3ed2dc

Please sign in to comment.