implement openshiftFormatter

Signed-off-by: Morgan Martinet <morgan@mmm-experts.com>
This commit is contained in:
Morgan Martinet
2021-11-22 00:05:55 -05:00
parent 2b00e00f18
commit 3739d3f773
4 changed files with 132 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes': minor
---
Implement support for formatting Openshift dashboard url links
+1
View File
@@ -189,6 +189,7 @@ oidc
Okta
onboarding
Onboarding
Openshift
orgs
pagerduty
pageview
@@ -15,21 +15,108 @@
*/
import { openshiftFormatter } from './openshift';
describe('clusterLinks - OpenShift formatter', () => {
function formatUrl(url: URL) {
return url.toString();
}
describe('clusterLinks - Openshift formatter', () => {
it('should return an url on the workloads when there is a namespace only', () => {
expect(() =>
openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
namespace: 'bar',
},
kind: 'Deployment',
}),
).toThrowError(
'OpenShift formatter is not yet implemented. Please, contribute!',
},
kind: 'foo',
});
expect(formatUrl(url)).toBe('https://k8s.foo.com/k8s/cluster/projects/bar');
});
it('should return an url on the workloads when the kind is not recognizeed', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'UnknownKind',
});
expect(formatUrl(url)).toBe('https://k8s.foo.com/k8s/cluster/projects/bar');
});
it('should return an url on the deployment', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Deployment',
});
expect(formatUrl(url)).toBe(
'https://k8s.foo.com/k8s/ns/bar/deployments/foobar',
);
});
it('should return an url on the service', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Service',
});
expect(formatUrl(url)).toBe(
'https://k8s.foo.com/k8s/ns/bar/services/foobar',
);
});
it('should return an url on the ingress', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Ingress',
});
expect(formatUrl(url)).toBe(
'https://k8s.foo.com/k8s/ns/bar/ingresses/foobar',
);
});
it('should return an url on the deployment for a hpa', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'HorizontalPodAutoscaler',
});
expect(formatUrl(url)).toBe(
'https://k8s.foo.com/k8s/ns/bar/horizontalpodautoscalers/foobar',
);
});
it('should return an url on the PV', () => {
const url = openshiftFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
},
},
kind: 'PersistentVolume',
});
expect(formatUrl(url)).toBe(
'https://k8s.foo.com/k8s/cluster/persistentvolumes/foobar',
);
});
});
@@ -15,10 +15,30 @@
*/
import { ClusterLinksFormatterOptions } from '../../../types/types';
export function openshiftFormatter(
_options: ClusterLinksFormatterOptions,
): URL {
throw new Error(
'OpenShift formatter is not yet implemented. Please, contribute!',
);
const kindMappings: Record<string, string> = {
deployment: 'deployments',
ingress: 'ingresses',
service: 'services',
horizontalpodautoscaler: 'horizontalpodautoscalers',
persistentvolume: 'persistentvolumes',
};
export function openshiftFormatter(options: ClusterLinksFormatterOptions): URL {
const result = new URL(options.dashboardUrl.href);
const name = options.object.metadata?.name;
const namespace = options.object.metadata?.namespace;
const validKind = kindMappings[options.kind.toLocaleLowerCase('en-US')];
if (namespace) {
if (name && validKind) {
result.pathname = `k8s/ns/${namespace}/${validKind}/${name}`;
} else {
result.pathname = `k8s/cluster/projects/${namespace}`;
}
} else if (validKind) {
result.pathname = `k8s/cluster/${validKind}`;
if (name) {
result.pathname += `/${name}`;
}
}
return result;
}