Skip to content

Commit

Permalink
feat(kubernetes): add rancher dashboard formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Branch Vincent <branchevincent@gmail.com>
  • Loading branch information
branchvincent committed Sep 24, 2021
1 parent 3a3e679 commit 6c0bd7f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-chairs-argue.md
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes': patch
---

Add dashboard support for Rancher
Expand Up @@ -15,21 +15,88 @@
*/
import { rancherFormatter } from './rancher';

describe('clusterLinks - Rancher formatter', () => {
it('should return an url on the workloads when there is a namespace only', () => {
expect(() =>
rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
describe('clusterLinks - rancher formatter', () => {
it('should return a url on the workloads when there is a namespace only', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
namespace: 'bar',
},
kind: 'Deployment',
}),
).toThrowError(
'Rancher formatter is not yet implemented. Please, contribute!',
},
kind: 'foo',
});
expect(url.href).toBe('https://k8s.foo.com/explorer/workload');
});
it('should return a url on the workloads when the kind is not recognized', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'UnknownKind',
});
expect(url.href).toBe('https://k8s.foo.com/explorer/workload');
});
it('should return a url on the deployment', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Deployment',
});
expect(url.href).toBe(
'https://k8s.foo.com/explorer/apps.deployment/bar/foobar',
);
});
it('should return a url on the service', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Service',
});
expect(url.href).toBe('https://k8s.foo.com/explorer/service/bar/foobar');
});
it('should return a url on the ingress', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'Ingress',
});
expect(url.href).toBe(
'https://k8s.foo.com/explorer/networking.k8s.io.ingress/bar/foobar',
);
});
it('should return a url on the deployment for a hpa', () => {
const url = rancherFormatter({
dashboardUrl: new URL('https://k8s.foo.com/'),
object: {
metadata: {
name: 'foobar',
namespace: 'bar',
},
},
kind: 'HorizontalPodAutoscaler',
});
expect(url.href).toBe(
'https://k8s.foo.com/explorer/autoscaling.horizontalpodautoscaler/bar/foobar',
);
});
});
22 changes: 18 additions & 4 deletions plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.ts
Expand Up @@ -15,8 +15,22 @@
*/
import { ClusterLinksFormatterOptions } from '../../../types/types';

export function rancherFormatter(_options: ClusterLinksFormatterOptions): URL {
throw new Error(
'Rancher formatter is not yet implemented. Please, contribute!',
);
const kindMappings: Record<string, string> = {
deployment: 'apps.deployment',
ingress: 'networking.k8s.io.ingress',
service: 'service',
horizontalpodautoscaler: 'autoscaling.horizontalpodautoscaler',
};

export function rancherFormatter(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 (validKind && name && namespace) {
result.pathname = `explorer/${validKind}/${namespace}/${name}`;
} else if (namespace) {
result.pathname = 'explorer/workload';
}
return result;
}

0 comments on commit 6c0bd7f

Please sign in to comment.