-
Notifications
You must be signed in to change notification settings - Fork 702
RFE-3935: Add non-scalable image warning when scaling workloads #16436
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
Open
swshende-cmd
wants to merge
4
commits into
openshift:main
Choose a base branch
from
swshende-cmd:RFE-3935
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d3be8ba
RFE-3935: Add non-scalable image warning when scaling workloads
shendeswapnil6 b744665
fixup: Address CodeRabbit review feedback
shendeswapnil6 501feaf
fixup: Run yarn i18n to fix locale key placement
shendeswapnil6 77fa443
fixup: Fix i18n key placement and trailing newline
shendeswapnil6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
146 changes: 146 additions & 0 deletions
146
frontend/packages/console-shared/src/hooks/__tests__/useNonScalableImageCheck.spec.ts
This file contains hidden or 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,146 @@ | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import { k8sGet } from '@console/internal/module/k8s'; | ||
| import { useNonScalableImageCheck } from '../useNonScalableImageCheck'; | ||
|
|
||
| jest.mock('@console/internal/module/k8s', () => ({ | ||
| ...jest.requireActual('@console/internal/module/k8s'), | ||
| k8sGet: jest.fn(), | ||
| })); | ||
|
|
||
| const mockK8sGet = k8sGet as jest.Mock; | ||
|
|
||
| const makeDeploymentConfig = (istName?: string, namespace = 'test-ns') => ({ | ||
| kind: 'DeploymentConfig', | ||
| apiVersion: 'apps.openshift.io/v1', | ||
| metadata: { name: 'test-dc', namespace }, | ||
| spec: { | ||
| replicas: 1, | ||
| triggers: istName | ||
| ? [ | ||
| { | ||
| type: 'ImageChange', | ||
| imageChangeParams: { | ||
| from: { kind: 'ImageStreamTag', name: istName, namespace }, | ||
| }, | ||
| }, | ||
| ] | ||
| : [], | ||
| }, | ||
| }); | ||
|
|
||
| const makeDeployment = (istName?: string, namespace = 'test-ns') => ({ | ||
| kind: 'Deployment', | ||
| apiVersion: 'apps/v1', | ||
| metadata: { | ||
| name: 'test-deploy', | ||
| namespace, | ||
| annotations: istName | ||
| ? { | ||
| 'image.openshift.io/triggers': JSON.stringify([ | ||
| { from: { kind: 'ImageStreamTag', name: istName, namespace } }, | ||
| ]), | ||
| } | ||
| : {}, | ||
| }, | ||
| spec: { replicas: 1 }, | ||
| }); | ||
|
|
||
| const makeIST = (nonScalable?: string | boolean) => { | ||
| const labels: Record<string, string | boolean> = {}; | ||
| if (nonScalable !== undefined) { | ||
| labels['io.openshift.non-scalable'] = nonScalable; | ||
| } | ||
| return { | ||
| image: { | ||
| dockerImageMetadata: { | ||
| Config: { | ||
| Labels: labels, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| describe('useNonScalableImageCheck', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=true when IST has io.openshift.non-scalable=true (string)', async () => { | ||
| mockK8sGet.mockResolvedValue(makeIST('true')); | ||
| const resource = makeDeploymentConfig('myapp:latest'); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isNonScalable).toBe(true); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=true when IST has io.openshift.non-scalable=true (boolean)', async () => { | ||
| mockK8sGet.mockResolvedValue(makeIST(true)); | ||
| const resource = makeDeploymentConfig('myapp:latest'); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isNonScalable).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=false when IST does not have the label', async () => { | ||
| mockK8sGet.mockResolvedValue(makeIST()); | ||
| const resource = makeDeploymentConfig('myapp:latest'); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isNonScalable).toBe(false); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=false when there are no triggers', () => { | ||
| const resource = makeDeploymentConfig(); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| expect(result.current.isNonScalable).toBe(false); | ||
| expect(result.current.loading).toBe(false); | ||
| expect(mockK8sGet).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle Deployment with image.openshift.io/triggers annotation', async () => { | ||
| mockK8sGet.mockResolvedValue(makeIST('true')); | ||
| const resource = makeDeployment('myapp:latest'); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isNonScalable).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=false when k8sGet fails', async () => { | ||
| mockK8sGet.mockRejectedValue(new Error('Forbidden')); | ||
| const resource = makeDeploymentConfig('myapp:latest'); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isNonScalable).toBe(false); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return isNonScalable=false for Deployment without trigger annotation', () => { | ||
| const resource = makeDeployment(); | ||
|
|
||
| const { result } = renderHook(() => useNonScalableImageCheck(resource)); | ||
|
|
||
| expect(result.current.isNonScalable).toBe(false); | ||
| expect(result.current.loading).toBe(false); | ||
| expect(mockK8sGet).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
114 changes: 114 additions & 0 deletions
114
frontend/packages/console-shared/src/hooks/useNonScalableImageCheck.ts
This file contains hidden or 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,114 @@ | ||
| import { useState, useEffect, useMemo } from 'react'; | ||
| import * as _ from 'lodash'; | ||
| import { ImageStreamTagModel } from '@console/internal/models'; | ||
| import type { K8sResourceKind } from '@console/internal/module/k8s'; | ||
| import { k8sGet } from '@console/internal/module/k8s'; | ||
|
|
||
| const NON_SCALABLE_LABEL = 'io.openshift.non-scalable'; | ||
| const IMAGE_TRIGGER_ANNOTATION = 'image.openshift.io/triggers'; | ||
|
|
||
| type ISTReference = { | ||
| name: string; | ||
| namespace: string; | ||
| }; | ||
|
|
||
| const getISTFromDeploymentConfig = (resource: K8sResourceKind): ISTReference | null => { | ||
| const triggers = resource?.spec?.triggers; | ||
| if (!Array.isArray(triggers)) { | ||
| return null; | ||
| } | ||
| const imageChangeTrigger = triggers.find( | ||
| (trigger) => | ||
| trigger.type === 'ImageChange' && | ||
| trigger?.imageChangeParams?.from?.kind === 'ImageStreamTag' && | ||
| !!trigger?.imageChangeParams?.from?.name, | ||
| ); | ||
| if (!imageChangeTrigger) { | ||
| return null; | ||
| } | ||
| const { name, namespace } = imageChangeTrigger.imageChangeParams.from; | ||
| return { | ||
| name, | ||
| namespace: namespace || resource.metadata?.namespace, | ||
| }; | ||
| }; | ||
|
|
||
| const getISTFromTriggerAnnotation = (resource: K8sResourceKind): ISTReference | null => { | ||
| const annotation = resource?.metadata?.annotations?.[IMAGE_TRIGGER_ANNOTATION]; | ||
| if (!annotation) { | ||
| return null; | ||
| } | ||
| try { | ||
| const triggers = JSON.parse(annotation); | ||
| const trigger = Array.isArray(triggers) | ||
| ? triggers.find((t) => t?.from?.kind === 'ImageStreamTag' && !!t?.from?.name) | ||
| : null; | ||
| if (!trigger) { | ||
| return null; | ||
| } | ||
| return { | ||
| name: trigger.from.name, | ||
| namespace: trigger.from.namespace || resource.metadata?.namespace, | ||
| }; | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const getISTReference = (resource: K8sResourceKind): ISTReference | null => { | ||
| if (resource?.kind === 'DeploymentConfig') { | ||
| return getISTFromDeploymentConfig(resource); | ||
| } | ||
| return getISTFromTriggerAnnotation(resource); | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if a workload's container image has the `io.openshift.non-scalable` label. | ||
| * Resolves the ImageStreamTag reference from the workload's triggers or annotations, | ||
| * fetches the IST, and inspects `image.dockerImageMetadata.Config.Labels`. | ||
| * | ||
| * Returns `{ isNonScalable: false }` silently on any error (missing IST, permissions, etc.). | ||
| */ | ||
| export const useNonScalableImageCheck = ( | ||
| resource: K8sResourceKind, | ||
| ): { isNonScalable: boolean; loading: boolean } => { | ||
| const [isNonScalable, setIsNonScalable] = useState(false); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| const istRef = useMemo(() => getISTReference(resource), [resource]); | ||
| const istName = istRef?.name; | ||
| const istNamespace = istRef?.namespace; | ||
|
|
||
| useEffect(() => { | ||
| if (!istName || !istNamespace) { | ||
| setIsNonScalable(false); | ||
| setLoading(false); | ||
| return undefined; | ||
| } | ||
|
|
||
| let cancelled = false; | ||
| setLoading(true); | ||
|
|
||
| k8sGet(ImageStreamTagModel, istName, istNamespace) | ||
| .then((ist: K8sResourceKind) => { | ||
| if (!cancelled) { | ||
| const labels = _.get(ist, 'image.dockerImageMetadata.Config.Labels', {}); | ||
| const nonScalableValue = labels[NON_SCALABLE_LABEL]; | ||
| setIsNonScalable(nonScalableValue === true || nonScalableValue === 'true'); | ||
| setLoading(false); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| if (!cancelled) { | ||
| setIsNonScalable(false); | ||
| setLoading(false); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [istName, istNamespace]); | ||
|
|
||
| return { isNonScalable, loading }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.