Skip to content

Commit

Permalink
add support for any namespaced api
Browse files Browse the repository at this point in the history
emaildanwilson committed Oct 5, 2017
1 parent 62e01b5 commit 75b5286
Showing 4 changed files with 91 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -78,6 +78,13 @@ client.routes = client.createCollection('routes', schema, innerCollections, opti
// then use the routes collection like any other
```

## createCollection options

```
apiPrefix: "apis" // Sets the prefix to the api path for the new collection.
namespaced: true // Controls if paths include "/namespaces/${namespace}".
```

## Custom Collection for k8s deployments

```js
17 changes: 13 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -16,13 +16,24 @@ module.exports = function (info) {
, namespace = info.namespace;

var getUrl = function (object) {
var prefix = 'api';
var prefix = 'api', namespaced = false;

// Allow options to override default API prefix.
if (object.options && object.options.apiPrefix) {
prefix = object.options.apiPrefix;
}

//Add option to override namespaces in the URI
if (object.options && object.options.namespaced) {
namespaced = true;
}

// v1beta3 and greater uses lowercase endpoints instead of
// camelCase and defines namespaces in the query URL.
if ((version === 'v1beta3' || version === 'v1' || version === 'extensions/v1beta1')) {
namespaced = true;
}

// Define base URL for the query.
var query = protocol + '://' + path.join(host, prefix, version);

@@ -37,9 +48,7 @@ module.exports = function (info) {
var endpoint = object.endpoint;
}

// v1beta3 and greater uses lowercase endpoints instead of
// camelCase and defines namespaces in the query URL.
if ((version === 'v1beta3' || version === 'v1' || version === 'extensions/v1beta1')) {
if ( namespaced ) {
endpoint = endpoint.toLowerCase();
// Never use URL namespacing for namespace or node endpoints.
if (namespace && !endpoint.match(/^namespaces/) && !endpoint.match(/^nodes/)){
17 changes: 17 additions & 0 deletions test/json/horizontalpodautoscaler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"apiVersion": "autoscaling/v1",
"kind": "HorizontalPodAutoscaler",
"metadata": {
"name": "hpa"
},
"spec": {
"maxReplicas": 10,
"minReplicas": 1,
"scaleTargetRef": {
"apiVersion": "extensions/v1beta1",
"kind": "Deployment",
"name": "mydeployment"
},
"targetCPUUtilizationPercentage": 50
}
}
54 changes: 54 additions & 0 deletions test/test-horizontalpodautoscaler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var should = require('should');
var assert = require('assert');
var Client = require('../');
var fs = require('fs');
var testData = require('./json/horizontalpodautoscaler.json');

describe('Test k8s horizontalpodautoscaler API', function() {
this.timeout(5000);
var client, options;
options = JSON.parse(JSON.stringify(require('./config.json').k8s))
options.version = "autoscaling/v1";
client = new Client(options);
client.object = client.createCollection("horizontalpodautoscalers", null, null, { apiPrefix : "apis", namespaced: true });

it('should create an hpa', function(done) {
client.object.create(testData, function (err, data) {
assert(err == null, JSON.stringify(err));
data.should.be.an.instanceOf(Object).and.have.properties(['kind', 'apiVersion', 'metadata']);
data.metadata.name.should.be.equal(testData.metadata.name);
done();
});
});

it('should return the list of hpas', function(done) {
client.object.get(function (err, data) {
assert(err == null);
data.should.be.an.instanceOf(Array)
data[0].should.be.an.instanceOf(Object).and.have.properties(['kind', 'apiVersion', 'metadata', 'items']);
data[0].items.should.be.an.instanceOf(Array)
data[0].kind.should.be.equal('HorizontalPodAutoscalerList');
done();
});
});

it('should return the hpa with specified id', function(done) {
var nsId = testData.metadata.name;
client.object.get(nsId, function (err, data) {
assert(err == null);
data.should.be.an.instanceOf(Object).and.have.properties(['kind', 'apiVersion', 'metadata']);
data.metadata.name.should.be.equal(testData.metadata.name);
done();
});
});

it('should delete the hpa with specified id', function(done) {
client.object.delete(testData.metadata.name, function (err, data) {
assert(err == null);
data.should.be.an.instanceOf(Object).and.have.properties(['kind', 'apiVersion', 'metadata', 'status']);
data.status.should.be.equal('Success');
done();
});
});

});

0 comments on commit 75b5286

Please sign in to comment.