-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7070 from jeff-phillips-18/pods-load-pods-overview
Fetch pods when displaying pods resources in overview pages
- Loading branch information
Showing
20 changed files
with
718 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
frontend/packages/console-shared/src/hooks/usePodsWatcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from 'react'; | ||
import { K8sResourceKind } from '@console/internal/module/k8s'; | ||
import { useK8sWatchResources } from '@console/internal/components/utils/k8s-watch-hook'; | ||
import { getPodsDataForResource, getResourcesToWatchForPods } from '../utils'; | ||
import { PodRCData } from '../types'; | ||
|
||
export const usePodsWatcher = ( | ||
resource: K8sResourceKind, | ||
kind?: string, | ||
namespace?: string, | ||
): { loaded: boolean; loadError: string; podData: PodRCData } => { | ||
const [loaded, setLoaded] = React.useState<boolean>(false); | ||
const [loadError, setLoadError] = React.useState<string>(''); | ||
const [podData, setPodData] = React.useState<PodRCData>(); | ||
const watchKind = kind || resource.kind; | ||
const watchNS = namespace || resource.metadata.namespace; | ||
const watchedResources = React.useMemo(() => getResourcesToWatchForPods(watchKind, watchNS), [ | ||
watchKind, | ||
watchNS, | ||
]); | ||
|
||
const resources = useK8sWatchResources(watchedResources); | ||
|
||
React.useEffect(() => { | ||
const errorKey = Object.keys(resources).find((key) => resources[key].loadError); | ||
if (errorKey) { | ||
setLoadError(resources[errorKey].loadError); | ||
return; | ||
} | ||
setLoadError(''); | ||
if ( | ||
Object.keys(resources).length > 0 && | ||
Object.keys(resources).every((key) => resources[key].loaded) | ||
) { | ||
const updatedPods = getPodsDataForResource(resource, watchKind, resources); | ||
setPodData(updatedPods); | ||
setLoaded(true); | ||
} | ||
}, [watchKind, resource, resources]); | ||
|
||
return { loaded, loadError, podData }; | ||
}; |
273 changes: 273 additions & 0 deletions
273
frontend/packages/console-shared/src/utils/__tests__/pod-resource-utils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
import * as _ from 'lodash'; | ||
import { | ||
MockResources, | ||
sampleCronJobs, | ||
sampleDaemonSets, | ||
sampleDeploymentConfigs, | ||
sampleDeployments, | ||
sampleStatefulSets, | ||
} from '@console/dev-console/src/components/topology/__tests__/topology-test-data'; | ||
import { | ||
getPodsForDeploymentConfig, | ||
getPodsForDeployment, | ||
getPodsForStatefulSet, | ||
getPodsForDaemonSet, | ||
getPodsForCronJob, | ||
getPodsForDeploymentConfigs, | ||
getPodsForDeployments, | ||
getPodsForStatefulSets, | ||
getPodsForDaemonSets, | ||
getPodsForCronJobs, | ||
} from '../pod-resource-utils'; | ||
|
||
let mockResources; | ||
|
||
describe('getPodsFor...', () => { | ||
beforeEach(() => { | ||
mockResources = _.cloneDeep(MockResources); | ||
}); | ||
|
||
it('should return pods and replication controllers for a given DeploymentConfig', () => { | ||
let podRCData = getPodsForDeploymentConfig(sampleDeploymentConfigs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(1); | ||
expect(podRCData.current).not.toBeNull(); | ||
expect(podRCData.previous).toBeFalsy(); | ||
expect(podRCData.isRollingOut).toBeFalsy(); | ||
|
||
podRCData = getPodsForDeploymentConfig(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
mockResources.replicationControllers = { loaded: false, loadError: 'error', data: [] }; | ||
podRCData = getPodsForDeploymentConfig(sampleDeploymentConfigs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
delete mockResources.replicationControllers; | ||
podRCData = getPodsForDeploymentConfig(sampleDeploymentConfigs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a given Deployment', () => { | ||
let podRCData = getPodsForDeployment(sampleDeployments.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(3); | ||
expect(podRCData.current).not.toBeNull(); | ||
expect(podRCData.previous).toBeFalsy(); | ||
expect(podRCData.isRollingOut).toBeFalsy(); | ||
|
||
podRCData = getPodsForDeployment(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
podRCData = getPodsForDeployment(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
mockResources.replicaSets = { loaded: false, loadError: 'error', data: [] }; | ||
podRCData = getPodsForDeployment(sampleDeployments.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
delete mockResources.replicaSets; | ||
podRCData = getPodsForDeployment(sampleDeployments.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a given StatefulSet', () => { | ||
let podRCData = getPodsForStatefulSet(sampleStatefulSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(1); | ||
expect(podRCData.current).not.toBeNull(); | ||
expect(podRCData.previous).toBeFalsy(); | ||
expect(podRCData.isRollingOut).toBeFalsy(); | ||
|
||
podRCData = getPodsForStatefulSet(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
podRCData = getPodsForStatefulSet(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
mockResources.statefulSets = { loaded: false, loadError: 'error', data: [] }; | ||
podRCData = getPodsForStatefulSet(sampleStatefulSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
delete mockResources.statefulSets; | ||
podRCData = getPodsForStatefulSet(sampleStatefulSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a given DaemonSet', () => { | ||
let podRCData = getPodsForDaemonSet(sampleDaemonSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(1); | ||
expect(podRCData.current).not.toBeNull(); | ||
expect(podRCData.previous).toBeFalsy(); | ||
expect(podRCData.isRollingOut).toBeFalsy(); | ||
|
||
podRCData = getPodsForDaemonSet(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
podRCData = getPodsForDaemonSet(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
mockResources.pods = { loaded: false, loadError: 'error', data: [] }; | ||
podRCData = getPodsForDaemonSet(sampleDaemonSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
delete mockResources.pods; | ||
podRCData = getPodsForDaemonSet(sampleDaemonSets.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a given CronJob', () => { | ||
let podRCData = getPodsForCronJob(sampleCronJobs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(2); | ||
expect(podRCData.current).not.toBeNull(); | ||
expect(podRCData.previous).toBeFalsy(); | ||
expect(podRCData.isRollingOut).toBeFalsy(); | ||
|
||
podRCData = getPodsForCronJob(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
podRCData = getPodsForCronJob(null, mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
mockResources.jobs = { loaded: false, loadError: 'error', data: [] }; | ||
podRCData = getPodsForCronJob(sampleCronJobs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
|
||
delete mockResources.jobs; | ||
podRCData = getPodsForCronJob(sampleCronJobs.data[0], mockResources); | ||
expect(podRCData.pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods and replication controllers for a set of DeploymentConfigs', () => { | ||
let podRCDataArray = getPodsForDeploymentConfigs(sampleDeploymentConfigs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(2); | ||
expect(podRCDataArray[0].pods).toHaveLength(1); | ||
expect(podRCDataArray[0].current).not.toBeNull(); | ||
expect(podRCDataArray[0].previous).toBeFalsy(); | ||
expect(podRCDataArray[0].isRollingOut).toBeFalsy(); | ||
expect(podRCDataArray[1].pods).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForDeploymentConfigs([], mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForDeploymentConfigs(null, mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
mockResources.replicationControllers = { loaded: false, loadError: 'error', data: [] }; | ||
podRCDataArray = getPodsForDeploymentConfigs(sampleDeploymentConfigs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(2); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
expect(podRCDataArray[1].pods).toHaveLength(0); | ||
|
||
delete mockResources.replicationControllers; | ||
podRCDataArray = getPodsForDeploymentConfigs(sampleDeploymentConfigs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(2); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
expect(podRCDataArray[1].pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a given Deployment', () => { | ||
let podRCDataArray = getPodsForDeployments(sampleDeployments.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(3); | ||
expect(podRCDataArray[0].pods).toHaveLength(3); | ||
expect(podRCDataArray[0].current).not.toBeNull(); | ||
expect(podRCDataArray[0].previous).toBeFalsy(); | ||
expect(podRCDataArray[0].isRollingOut).toBeFalsy(); | ||
expect(podRCDataArray[1].pods).toHaveLength(3); | ||
expect(podRCDataArray[1].current).not.toBeNull(); | ||
expect(podRCDataArray[1].previous).toBeFalsy(); | ||
expect(podRCDataArray[1].isRollingOut).toBeFalsy(); | ||
expect(podRCDataArray[2].pods).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForDeployments([], mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForDeployments(null, mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
mockResources.replicaSets = { loaded: false, loadError: 'error', data: [] }; | ||
podRCDataArray = getPodsForDeployments(sampleDeployments.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(3); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
expect(podRCDataArray[1].pods).toHaveLength(0); | ||
expect(podRCDataArray[2].pods).toHaveLength(0); | ||
|
||
delete mockResources.replicaSets; | ||
podRCDataArray = getPodsForDeployments(sampleDeployments.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(3); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
expect(podRCDataArray[1].pods).toHaveLength(0); | ||
expect(podRCDataArray[2].pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a set of StatefulSets', () => { | ||
let podRCDataArray = getPodsForStatefulSets(sampleStatefulSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(1); | ||
expect(podRCDataArray[0].current).not.toBeNull(); | ||
expect(podRCDataArray[0].previous).toBeFalsy(); | ||
expect(podRCDataArray[0].isRollingOut).toBeFalsy(); | ||
|
||
podRCDataArray = getPodsForStatefulSets([], mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForStatefulSets(null, mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
mockResources.statefulSets = { loaded: false, loadError: 'error', data: [] }; | ||
podRCDataArray = getPodsForStatefulSets(sampleStatefulSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
|
||
delete mockResources.statefulSets; | ||
podRCDataArray = getPodsForStatefulSets(sampleStatefulSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a set of DaemonSets', () => { | ||
let podRCDataArray = getPodsForDaemonSets(sampleDaemonSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(1); | ||
expect(podRCDataArray[0].current).not.toBeNull(); | ||
expect(podRCDataArray[0].previous).toBeFalsy(); | ||
expect(podRCDataArray[0].isRollingOut).toBeFalsy(); | ||
|
||
podRCDataArray = getPodsForDaemonSets([], mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForDaemonSets(null, mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
mockResources.pods = { loaded: false, loadError: 'error', data: [] }; | ||
podRCDataArray = getPodsForDaemonSets(sampleDaemonSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
|
||
delete mockResources.pods; | ||
podRCDataArray = getPodsForDaemonSets(sampleDaemonSets.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
}); | ||
|
||
it('should return pods for a set of CronJobs', () => { | ||
let podRCDataArray = getPodsForCronJobs(sampleCronJobs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(2); | ||
expect(podRCDataArray[0].current).not.toBeNull(); | ||
expect(podRCDataArray[0].previous).toBeFalsy(); | ||
expect(podRCDataArray[0].isRollingOut).toBeFalsy(); | ||
|
||
podRCDataArray = getPodsForCronJobs([], mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
podRCDataArray = getPodsForCronJobs(null, mockResources); | ||
expect(podRCDataArray).toHaveLength(0); | ||
|
||
mockResources.jobs = { loaded: false, loadError: 'error', data: [] }; | ||
podRCDataArray = getPodsForCronJobs(sampleCronJobs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
|
||
delete mockResources.jobs; | ||
podRCDataArray = getPodsForCronJobs(sampleCronJobs.data, mockResources); | ||
expect(podRCDataArray).toHaveLength(1); | ||
expect(podRCDataArray[0].pods).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.