Skip to content

Commit

Permalink
Show all default pull secrets for a project
Browse files Browse the repository at this point in the history
  • Loading branch information
kdoberst committed Jul 23, 2021
1 parent 2be6169 commit e9c6cee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
11 changes: 5 additions & 6 deletions frontend/__tests__/components/namespace.spec.tsx
Expand Up @@ -6,7 +6,7 @@ import { PullSecret } from '../../public/components/namespace';
import * as k8s from '../../public/module/k8s';
import { LoadingInline } from '../../public/components/utils';
import { testNamespace } from '../../__mocks__/k8sResourcesMocks';
import { SecretModel } from '../../public/models';
import { ServiceAccountModel } from '../../public/models';

jest.mock('react-i18next', () => {
const reactI18next = require.requireActual('react-i18next');
Expand All @@ -30,12 +30,11 @@ describe(PullSecret.displayName, () => {
it('renders link to open modal once pull secrets are loaded', (done) => {
spyAndExpect(spyOn(k8s, 'k8sGet'))(Promise.resolve({ items: [] }))
.then(([model, name, namespace, options]) => {
expect(model).toEqual(SecretModel);
expect(name).toBe(null);
expect(model).toEqual(ServiceAccountModel);
expect(name).toBe('default');
expect(namespace).toEqual(testNamespace.metadata.name);
expect(options).toEqual({
queryParams: { fieldSelector: 'type=kubernetes.io/dockerconfigjson' },
});

expect(options).toEqual({});
})
.then(() => {
wrapper.update();
Expand Down
72 changes: 38 additions & 34 deletions frontend/public/components/namespace.jsx
Expand Up @@ -35,7 +35,13 @@ import {
} from '@console/shared';
import { ByteDataTypes } from '@console/shared/src/graph-helper/data-utils';

import { ConsoleLinkModel, NamespaceModel, ProjectModel, SecretModel } from '../models';
import {
ConsoleLinkModel,
NamespaceModel,
ProjectModel,
SecretModel,
ServiceAccountModel,
} from '../models';
import { coFetchJSON } from '../co-fetch';
import { k8sGet, referenceForModel } from '../module/k8s';
import * as k8sActions from '../actions/k8s';
Expand Down Expand Up @@ -831,43 +837,48 @@ export const ProjectsPage = connectToFlags(
/** @type {React.SFC<{namespace: K8sResourceKind}>} */
export const PullSecret = (props) => {
const [isLoading, setIsLoading] = React.useState(true);
const [data, setData] = React.useState(undefined);
const [data, setData] = React.useState([]);
const { t } = useTranslation();
const { namespace, canViewSecrets } = props;

React.useEffect(() => {
k8sGet(SecretModel, null, props.namespace.metadata.name, {
queryParams: { fieldSelector: 'type=kubernetes.io/dockerconfigjson' },
})
.then((pullSecrets) => {
k8sGet(ServiceAccountModel, 'default', namespace.metadata.name, {})
.then((serviceAccount) => {
setIsLoading(false);
setData(_.get(pullSecrets, 'items[0]'));
setData(serviceAccount.imagePullSecrets ?? []);
})
.catch((error) => {
setIsLoading(false);
setData(undefined);
// A 404 just means that no pull secrets exist
if (error.status !== 404) {
throw error;
}
setData([]);
throw error;
});
}, [props.namespace.metadata.name]);
}, [namespace.metadata.name]);

if (isLoading) {
return <LoadingInline />;
}
const modal = () =>
configureNamespacePullSecretModal({ namespace: props.namespace, pullSecret: data });
const modal = () => configureNamespacePullSecretModal({ namespace, pullSecret: undefined });

const secrets =
data.length > 0 ? (
(data.map((secret) => (
<div key={secret.name}>
<ResourceLink
kind="Secret"
name={secret.name}
namespace={namespace.metadata.name}
linkTo={canViewSecrets}
/>
</div>
)))
) : (
(<Button variant="link" type="button" isInline onClick={modal}>
{t('public~Not configured')}
<PencilAltIcon className="co-icon-space-l pf-c-button-icon--plain" />
</Button>)
);

return (
<>
{data ? (
<ResourceLink kind="Secret" name={data.metadata.name} namespace={data.metadata.namespace} />
) : (
<Button variant="link" type="button" isInline onClick={modal}>
{t('public~Not configured')}
<PencilAltIcon className="co-icon-space-l pf-c-button-icon--plain" />
</Button>
)}
<dt>{t('public~Default pull Secret', { count: data.length })}</dt>
<dd>{isLoading ? <LoadingInline /> : secrets}</dd>
</>
);
};
Expand Down Expand Up @@ -968,14 +979,7 @@ export const NamespaceSummary = ({ ns }) => {
<DetailsItem label={t('public~Status')} obj={ns} path="status.phase">
<Status status={ns.status.phase} />
</DetailsItem>
{canListSecrets && (
<>
<dt>{t('public~Default pull secret')}</dt>
<dd>
<PullSecret namespace={ns} />
</dd>
</>
)}
<PullSecret namespace={ns} canViewSecrets={canListSecrets} />
<dt>{t('public~NetworkPolicies')}</dt>
<dd>
<Link to={`/k8s/ns/${ns.metadata.name}/networkpolicies`}>
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/locales/en/public.json
Expand Up @@ -1024,9 +1024,9 @@
"Welcome to OpenShift": "Welcome to OpenShift",
"No projects found": "No projects found",
"by name or display name": "by name or display name",
"Default pull Secret_plural": "Default pull Secrets",
"Memory usage by pod (top 10)": "Memory usage by pod (top 10)",
"Resource usage": "Resource usage",
"Default pull secret": "Default pull secret",
"NetworkPolicies": "NetworkPolicies",
"All Projects": "All Projects",
"All Namespaces": "All Namespaces",
Expand Down

0 comments on commit e9c6cee

Please sign in to comment.