Skip to content

Commit

Permalink
Update backend url for namespaces to require cluster. (#1843)
Browse files Browse the repository at this point in the history
* Update backend url for namespaces to require cluster.

* Update ContextSelector after rebasing.
  • Loading branch information
absoludity committed Jul 21, 2020
1 parent dcda5cc commit 5a8d743
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 116 deletions.
16 changes: 8 additions & 8 deletions dashboard/src/actions/namespace.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe("fetchNamespaces", () => {
},
];

await store.dispatch(fetchNamespaces());
await store.dispatch(fetchNamespaces("default-c"));
expect(store.getActions()).toEqual(expectedActions);
});

Expand All @@ -81,7 +81,7 @@ describe("fetchNamespaces", () => {
},
];

await store.dispatch(fetchNamespaces());
await store.dispatch(fetchNamespaces("default-c"));

expect(store.getActions()).toEqual(expectedActions);
});
Expand All @@ -106,7 +106,7 @@ describe("createNamespace", () => {
},
];

const res = await store.dispatch(createNamespace("overlook-hotel"));
const res = await store.dispatch(createNamespace("default-c", "overlook-hotel"));
expect(res).toBe(true);
expect(store.getActions()).toEqual(expectedActions);
});
Expand All @@ -121,7 +121,7 @@ describe("createNamespace", () => {
},
];

const res = await store.dispatch(createNamespace("foo"));
const res = await store.dispatch(createNamespace("default-c", "foo"));
expect(res).toBe(false);
expect(store.getActions()).toEqual(expectedActions);
});
Expand All @@ -134,14 +134,14 @@ describe("getNamespace", () => {
const expectedActions = [
{
type: getType(requestNamespace),
payload: "default",
payload: "default-ns",
},
{
type: getType(receiveNamespace),
payload: ns,
},
];
const r = await store.dispatch(getNamespace("default"));
const r = await store.dispatch(getNamespace("default-c", "default-ns"));
expect(r).toBe(true);
expect(store.getActions()).toEqual(expectedActions);
});
Expand All @@ -152,14 +152,14 @@ describe("getNamespace", () => {
const expectedActions = [
{
type: getType(requestNamespace),
payload: "default",
payload: "default-ns",
},
{
type: getType(errorNamespaces),
payload: { err, op: "get" },
},
];
const r = await store.dispatch(getNamespace("default"));
const r = await store.dispatch(getNamespace("default-c", "default-ns"));
expect(r).toBe(false);
expect(store.getActions()).toEqual(expectedActions);
});
Expand Down
14 changes: 9 additions & 5 deletions dashboard/src/actions/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ const allActions = [
];
export type NamespaceAction = ActionType<typeof allActions[number]>;

export function fetchNamespaces(): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
export function fetchNamespaces(
cluster: string,
): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
const namespaceList = await Namespace.list();
const namespaceList = await Namespace.list(cluster);
const namespaceStrings = namespaceList.namespaces.map((n: IResource) => n.metadata.name);
dispatch(receiveNamespaces(namespaceStrings));
} catch (e) {
Expand All @@ -55,13 +57,14 @@ export function fetchNamespaces(): ThunkAction<Promise<void>, IStoreState, null,
}

export function createNamespace(
cluster: string,
ns: string,
): ThunkAction<Promise<boolean>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
await Namespace.create(ns);
await Namespace.create(cluster, ns);
dispatch(postNamespace(ns));
dispatch(fetchNamespaces());
dispatch(fetchNamespaces(cluster));
return true;
} catch (e) {
dispatch(errorNamespaces(e, "create"));
Expand All @@ -71,12 +74,13 @@ export function createNamespace(
}

export function getNamespace(
cluster: string,
ns: string,
): ThunkAction<Promise<boolean>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
dispatch(requestNamespace(ns));
const namespace = await Namespace.get(ns);
const namespace = await Namespace.get(cluster, ns);
dispatch(receiveNamespace(namespace));
return true;
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/components/Header/ContextSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ it("fetches namespaces", () => {

expect(fetchNamespaces).toHaveBeenCalled();
expect(getNamespace).toHaveBeenCalledWith(
defaultProps.clusters.currentCluster,
defaultProps.clusters.clusters.default.currentNamespace,
);
});
Expand Down
10 changes: 5 additions & 5 deletions dashboard/src/components/Header/ContextSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ ClarityIcons.addIcons(clusterIcon, fileGroupIcon, angleIcon);
export interface IContextSelectorProps {
clusters: IClustersState;
defaultNamespace: string;
fetchNamespaces: () => void;
createNamespace: (ns: string) => Promise<boolean>;
getNamespace: (ns: string) => void;
fetchNamespaces: (cluster: string) => void;
createNamespace: (cluster: string, ns: string) => Promise<boolean>;
getNamespace: (cluster: string, ns: string) => void;
setNamespace: (ns: string) => void;
}

Expand All @@ -43,9 +43,9 @@ function ContextSelector({
useOutsideClick(setOpen, [ref], open);

useEffect(() => {
fetchNamespaces();
fetchNamespaces(clusters.currentCluster);
if (namespaceSelected !== definedNamespaces.all) {
getNamespace(namespaceSelected);
getNamespace(clusters.currentCluster, namespaceSelected);
}
}, [fetchNamespaces, namespaceSelected, getNamespace]);

Expand Down
4 changes: 2 additions & 2 deletions dashboard/src/components/Header/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ it("renders the namespace switcher", () => {
expect(namespaceSelector.props()).toEqual(
expect.objectContaining({
defaultNamespace: defaultProps.defaultNamespace,
cluster: defaultProps.clusters.clusters.default,
clusters: defaultProps.clusters,
}),
);
});
Expand Down Expand Up @@ -130,7 +130,7 @@ it("call setNamespace and getNamespace when selecting a namespace", () => {
onChange("bar");

expect(setNamespace).toHaveBeenCalledWith("bar");
expect(getNamespace).toHaveBeenCalledWith("bar");
expect(getNamespace).toHaveBeenCalledWith("default", "bar");
expect(createNamespace).not.toHaveBeenCalled();
});

Expand Down
18 changes: 12 additions & 6 deletions dashboard/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import NamespaceSelector from "./NamespaceSelector";
export interface IHeaderProps {
appVersion: string;
authenticated: boolean;
fetchNamespaces: () => void;
fetchNamespaces: (cluster: string) => void;
logout: () => void;
clusters: IClustersState;
defaultNamespace: string;
pathname: string;
push: (path: string) => void;
setNamespace: (ns: string) => void;
createNamespace: (ns: string) => Promise<boolean>;
getNamespace: (ns: string) => void;
createNamespace: (cluster: string, ns: string) => Promise<boolean>;
getNamespace: (cluster: string, ns: string) => void;
featureFlags: IFeatureFlags;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ class Header extends React.Component<IHeaderProps, IHeaderState> {
{showNav && (
<div className="header__nav header__nav-config">
<NamespaceSelector
cluster={cluster}
clusters={clusters}
defaultNamespace={defaultNamespace}
onChange={this.handleNamespaceChange}
fetchNamespaces={fetchNamespaces}
Expand Down Expand Up @@ -189,11 +189,17 @@ class Header extends React.Component<IHeaderProps, IHeaderState> {
};

private handleNamespaceChange = (ns: string) => {
const { pathname, push, setNamespace, getNamespace } = this.props;
const {
clusters: { currentCluster },
pathname,
push,
setNamespace,
getNamespace,
} = this.props;
const to = pathname.replace(/\/ns\/[^/]*/, `/ns/${ns}`);
setNamespace(ns);
if (ns !== definedNamespaces.all) {
getNamespace(ns);
getNamespace(currentCluster, ns);
}
if (to !== pathname) {
push(to);
Expand Down
6 changes: 3 additions & 3 deletions dashboard/src/components/Header/Header.v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import Menu from "./Menu";

interface IHeaderProps {
authenticated: boolean;
fetchNamespaces: () => void;
fetchNamespaces: (cluster: string) => void;
logout: () => void;
clusters: IClustersState;
defaultNamespace: string;
appVersion: string;
push: (path: string) => void;
setNamespace: (ns: string) => void;
createNamespace: (ns: string) => Promise<boolean>;
getNamespace: (ns: string) => void;
createNamespace: (cluster: string, ns: string) => Promise<boolean>;
getNamespace: (cluster: string, ns: string) => void;
}

function Header(props: IHeaderProps) {
Expand Down
81 changes: 54 additions & 27 deletions dashboard/src/components/Header/NamespaceSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import NewNamespace from "./NewNamespace";

const defaultProps = {
fetchNamespaces: jest.fn(),
cluster: {
currentNamespace: "namespace-two",
namespaces: ["namespace-one", "namespace-two"],
} as IClusterState,
clusters: {
currentCluster: "default",
clusters: {
default: {
currentNamespace: "namespace-two",
namespaces: ["namespace-one", "namespace-two"],
} as IClusterState,
},
},
defaultNamespace: "kubeapps-user",
onChange: jest.fn(),
createNamespace: jest.fn(),
Expand All @@ -24,8 +29,8 @@ it("renders the given namespaces with current selection", () => {
const select = wrapper.find(".NamespaceSelector__select").first();

const expectedValue = {
label: defaultProps.cluster.currentNamespace,
value: defaultProps.cluster.currentNamespace,
label: defaultProps.clusters.clusters.default.currentNamespace,
value: defaultProps.clusters.clusters.default.currentNamespace,
};
expect(select.props()).toMatchObject({
value: expectedValue,
Expand All @@ -41,9 +46,15 @@ it("renders the given namespaces with current selection", () => {
it("render with the default namespace selected if no current selection", () => {
const props = {
...defaultProps,
cluster: {
...defaultProps.cluster,
currentNamespace: "",
clusters: {
...defaultProps.clusters,
clusters: {
...defaultProps.clusters.clusters,
default: {
currentNamespace: "",
namespaces: [],
},
},
},
} as INamespaceSelectorProps;
const wrapper = shallow(<NamespaceSelector {...props} />);
Expand All @@ -69,7 +80,7 @@ it("opens the modal to add a new namespace and creates it", async () => {
wrapper.update();

wrapper.find(".button-primary").simulate("click");
expect(createNamespace).toHaveBeenCalledWith("test");
expect(createNamespace).toHaveBeenCalledWith("default", "test");
// hack to wait for the state to be updated
await new Promise(res =>
setTimeout(() => {
Expand All @@ -83,29 +94,45 @@ it("opens the modal to add a new namespace and creates it", async () => {
it("fetches namespaces and retrive the current namespace", () => {
const fetchNamespaces = jest.fn();
const getNamespace = jest.fn();
shallow(
<NamespaceSelector
{...defaultProps}
fetchNamespaces={fetchNamespaces}
getNamespace={getNamespace}
cluster={{ currentNamespace: "foo", namespaces: [] }}
/>,
);
const props = {
...defaultProps,
fetchNamespaces,
getNamespace,
clusters: {
...defaultProps.clusters,
clusters: {
...defaultProps.clusters.clusters,
default: {
currentNamespace: "foo",
namespaces: [],
},
},
},
};
shallow(<NamespaceSelector {...props} />);
expect(fetchNamespaces).toHaveBeenCalled();
expect(getNamespace).toHaveBeenCalledWith("foo");
expect(getNamespace).toHaveBeenCalledWith("default", "foo");
});

it("doesnt' get the current namespace if all namespaces is selected", () => {
const fetchNamespaces = jest.fn();
const getNamespace = jest.fn();
shallow(
<NamespaceSelector
{...defaultProps}
fetchNamespaces={fetchNamespaces}
getNamespace={getNamespace}
cluster={{ currentNamespace: "_all", namespaces: [] }}
/>,
);
const props = {
...defaultProps,
fetchNamespaces,
getNamespace,
clusters: {
...defaultProps.clusters,
clusters: {
...defaultProps.clusters.clusters,
default: {
currentNamespace: "_all",
namespaces: [],
},
},
},
};
shallow(<NamespaceSelector {...props} />);
expect(fetchNamespaces).toHaveBeenCalled();
expect(getNamespace).not.toHaveBeenCalled();
});

0 comments on commit 5a8d743

Please sign in to comment.