Skip to content

Commit 39f7351

Browse files
fix(openshift-image-registry): fix #596: Add mock data for test page and cleanup some small code smells (#1024)
fix #596: Add mock data for test page and cleanup some small code smells
1 parent d3fdcdf commit 39f7351

File tree

9 files changed

+1926
-34
lines changed

9 files changed

+1926
-34
lines changed

plugins/openshift-image-registry/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ The OpenShift Image Registry plugin displays all ImageStreams in an Openshift cl
44

55
## For administrators
66

7+
### Prerequisites
8+
9+
The OpenShift Image Registry plugin requires read access to **_all_** `ImageStreams` and `ImageStreamTags` on a cluster. (Currently only a single cluster is supported.)
10+
11+
You can create a `ServiceAccount`, `ClusterRole` and `ClusterRoleBinding` with this commands.
12+
13+
Please notice that the ServiceAccount will be created in your current namespace while the `ClusterRole` and `ClusterRoleBinding` giving access to all namespaces are cluster-wide resources.
14+
15+
Additional information on these commands could be found in the [OpenShift Container Platform authentication and authorization documentation](https://docs.openshift.com/container-platform/latest/authentication/index.html).
16+
17+
```console
18+
oc create serviceaccount janus-idp-openshift-image-registry-reader
19+
20+
oc create clusterrole janus-idp-openshift-image-registry-reader --verb=get,watch,list --resource=imagestreams --resource=imagestreamtags
21+
22+
oc adm policy add-cluster-role-to-user janus-idp-openshift-image-registry-reader -z janus-idp-openshift-image-registry-reader
23+
```
24+
25+
And finally you can use this command to create a token that is valid for one week:
26+
27+
```console
28+
oc create token --duration=168h janus-idp-openshift-image-registry-reader
29+
```
30+
731
### Installation
832

933
Run the following command to install the OpenShift Image Registry plugin:
@@ -46,7 +70,7 @@ yarn workspace app add @janus-idp/backstage-plugin-openshift-image-registry
4670
to="openshift-image-registry"
4771
text="Image Registry"
4872
/>
49-
;{/* highlight-add-end */}
73+
{/* highlight-add-end */}
5074
</SidebarGroup>
5175
{/* ... */}
5276
</Sidebar>
Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,97 @@
11
import React from 'react';
22

33
import { createDevApp } from '@backstage/dev-utils';
4+
import { TestApiProvider } from '@backstage/test-utils';
45

6+
import {
7+
openshiftImageRegistryApiRef,
8+
OpenshiftImageRegistryApiV1,
9+
} from '../src/api';
510
import {
611
OpenshiftImageRegistryPage,
712
openshiftImageRegistryPlugin,
813
} from '../src/plugin';
14+
// Contains just the openshift namespace for now
15+
import namespaces from './namespaces.json';
16+
// Extracted from an OpenShift 4.13 cluster, reduced to Go, Java, Node.js and Python entries
17+
// oc get -n openshift \
18+
// imagestream/golang \
19+
// imagestream/java \
20+
// imagestream/nodejs \
21+
// imagestream/python \
22+
// -o json | jq . \
23+
// > plugins/openshift-image-registry/dev/openshift-imagestreams.json
24+
import openshiftImageStreams from './openshift-imagestreams.json';
25+
// The origin list view doesn't contain all neccessary infomation, so this list view
26+
// is build from three API calls to match the data in openshift-imagestreams.json
27+
//
28+
// oc get -n openshift \
29+
// imagestreamtag/golang:1.18-ubi7 \
30+
// imagestreamtag/java:11 \
31+
// imagestreamtag/nodejs:14-ubi7 \
32+
// imagestreamtag/python:2.7-ubi8 \
33+
// -o json | jq . \
34+
// > plugins/openshift-image-registry/dev/openshift-imagestreamtags.json
35+
import openshiftImageStreamTags from './openshift-imagestreamtags.json';
36+
37+
class MockOpenshiftImageRegistryApi implements OpenshiftImageRegistryApiV1 {
38+
async getAllImageStreams(): Promise<any> {
39+
return openshiftImageStreams.items;
40+
}
41+
async getImageStreams(ns: string): Promise<any> {
42+
return openshiftImageStreams.items.filter(
43+
item => item.metadata.namespace === ns,
44+
);
45+
}
46+
async getImageStream(ns: string, imageName: string): Promise<any> {
47+
return openshiftImageStreams.items.find(
48+
item =>
49+
item.metadata.namespace === ns && item.metadata.name === imageName,
50+
);
51+
}
52+
async getImageStreamTags(ns: string, imageName: string): Promise<any> {
53+
if (imageName.includes(':')) {
54+
return openshiftImageStreamTags.items.find(
55+
item =>
56+
item.metadata.namespace === ns && item.metadata.name === imageName,
57+
);
58+
}
59+
return openshiftImageStreamTags.items.find(
60+
item =>
61+
item.metadata.namespace === ns &&
62+
(item.metadata.name === imageName ||
63+
item.metadata.name.startsWith(`${imageName}:`)),
64+
);
65+
}
66+
async getImageStreamTag(
67+
ns: string,
68+
imageName: string,
69+
tag: string,
70+
): Promise<any> {
71+
return openshiftImageStreamTags.items.find(
72+
item =>
73+
item.metadata.namespace === ns &&
74+
item.metadata.name === `${imageName}:${tag}`,
75+
);
76+
}
77+
async getNamespaces(): Promise<any> {
78+
return namespaces.items;
79+
}
80+
}
981

1082
createDevApp()
1183
.registerPlugin(openshiftImageRegistryPlugin)
1284
.addPage({
13-
element: <OpenshiftImageRegistryPage />,
14-
title: 'Root Page',
85+
element: (
86+
<TestApiProvider
87+
apis={[
88+
[openshiftImageRegistryApiRef, new MockOpenshiftImageRegistryApi()],
89+
]}
90+
>
91+
<OpenshiftImageRegistryPage />
92+
</TestApiProvider>
93+
),
94+
title: 'Image Registry',
1595
path: '/openshift-image-registry',
1696
})
1797
.render();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"kind": "NamespaceList",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"resourceVersion": "81626349"
6+
},
7+
"items": [
8+
{
9+
"metadata": {
10+
"name": "openshift",
11+
"uid": "e9c1249f-b300-4b43-8c10-cd8473280844",
12+
"resourceVersion": "75916512",
13+
"creationTimestamp": "2023-07-26T18:44:09Z",
14+
"labels": {
15+
"kubernetes.io/metadata.name": "openshift"
16+
}
17+
},
18+
"spec": {
19+
"finalizers": ["kubernetes"]
20+
},
21+
"status": {
22+
"phase": "Active"
23+
}
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)