-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zahar Pecherichny <[email protected]>
- Loading branch information
Showing
6 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,52 @@ | ||
import querystring = require('querystring'); | ||
import pluralize = require('pluralize'); | ||
|
||
export interface KubernetesObject { | ||
apiVersion: string; | ||
kind: string; | ||
metadata?: { | ||
name?: string; | ||
namespace?: string; | ||
labels?: object; | ||
}; | ||
} | ||
|
||
export function CreatePath(object: KubernetesObject, include_name: boolean = true) { | ||
if (!object.apiVersion) { | ||
throw new Error('The object passed must contain apiVersion field') | ||
} | ||
if (!object.kind) { | ||
throw new Error('The object passed must contain kind field') | ||
} | ||
|
||
let path = "" | ||
|
||
// add apiVersion | ||
if (object.apiVersion == "v1") { | ||
path += "/api/v1" | ||
} else { | ||
path += "/apis/" + object.apiVersion | ||
} | ||
|
||
// add namespace if object namespaced | ||
if (object.metadata?.namespace) { | ||
path += "/namespaces/" + object.metadata.namespace | ||
} | ||
|
||
// add object kind | ||
// TODO: Use discovery api to get the correct plural | ||
path += "/" + pluralize(object.kind.toLowerCase()) | ||
|
||
// add object name | ||
if (include_name && object.metadata?.name) { | ||
path += "/" + object.metadata.name | ||
} | ||
|
||
// add label selector | ||
const labels = object.metadata?.labels | ||
if (labels) { | ||
path += "?" + querystring.stringify({labelSelector: Object.keys(labels).map(label => label + "=" + labels[label]).join(",")}) | ||
} | ||
|
||
return path | ||
} |
This file contains 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,84 @@ | ||
import { expect } from 'chai'; | ||
import { CreatePath } from './generic_api'; | ||
|
||
|
||
|
||
describe('CreatePath', () => { | ||
it('should convert namespace to path', async () => { | ||
const obj1 = { | ||
apiVersion: "v1", | ||
kind: "Namespace" | ||
}; | ||
|
||
const path1 = CreatePath(obj1); | ||
expect(path1).to.equal("/api/v1/namespaces"); | ||
|
||
const obj2 = { | ||
apiVersion: "v1", | ||
kind: "Namespace", | ||
metadata: { | ||
name: "test" | ||
} | ||
}; | ||
|
||
const path2 = CreatePath(obj2); | ||
expect(path2).to.equal("/api/v1/namespaces/test"); | ||
}); | ||
|
||
it('should convert custom resource to path', async () => { | ||
const obj1 = { | ||
apiVersion: "fake.crd.io/v1", | ||
kind: "fakekind", | ||
metadata: { | ||
name: "fake-name", | ||
namespace: "fake-namespace" | ||
} | ||
}; | ||
|
||
const path1 = CreatePath(obj1); | ||
expect(path1).to.equal("/apis/fake.crd.io/v1/namespaces/fake-namespace/fakekinds/fake-name"); | ||
}); | ||
|
||
it('should convert objects with labels to path with labels selector', async () => { | ||
const obj1 = { | ||
apiVersion: "v1", | ||
kind: "pod", | ||
metadata: { | ||
labels: { | ||
label1: "value1", | ||
label2: "value2" | ||
} | ||
} | ||
}; | ||
|
||
const path1 = CreatePath(obj1); | ||
expect(path1).to.equal("/api/v1/pods?labelSelector=label1%3Dvalue1%2Clabel2%3Dvalue2"); | ||
}); | ||
|
||
it('should convert cluster wide object to path', async () => { | ||
const obj1 = { | ||
apiVersion: "rbac.authorization.k8s.io/v1", | ||
kind: "ClusterRoleBinding" | ||
}; | ||
|
||
const path1 = CreatePath(obj1); | ||
expect(path1).to.equal("/apis/rbac.authorization.k8s.io/v1/clusterrolebindings"); | ||
}); | ||
|
||
it('should convert object to path while skipping the name', async () => { | ||
const obj1 = { | ||
apiVersion: "rbac.authorization.k8s.io/v1", | ||
kind: "RoleBinding", | ||
metadata: { | ||
name: "fake-name", | ||
namespace: "fake-namespace" | ||
} | ||
}; | ||
|
||
const path1 = CreatePath(obj1); | ||
expect(path1).to.equal("/apis/rbac.authorization.k8s.io/v1/namespaces/fake-namespace/rolebindings/fake-name"); | ||
|
||
const path2 = CreatePath(obj1, false); | ||
expect(path2).to.equal("/apis/rbac.authorization.k8s.io/v1/namespaces/fake-namespace/rolebindings"); | ||
}); | ||
}); |
This file contains 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 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