Skip to content

Commit 4d0cef8

Browse files
committed
move GKE specific logic into getGKEDescriptor
1 parent 4631dfa commit 4d0cef8

2 files changed

Lines changed: 22 additions & 25 deletions

File tree

packages/logging/src/metadata.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,23 @@ Metadata.getGCEDescriptor = function(projectId) {
9797
* @private
9898
*
9999
* @param {string} projectId - The project ID.
100-
* @param {string} clusterName - GKE cluster name.
100+
* @param {function} callback - The callback function.
101101
* @return {object}
102102
*/
103-
Metadata.getGKEDescriptor = function(projectId, clusterName) {
104-
return {
105-
type: 'container',
106-
labels: {
107-
// TODO(ofrobots): it would be good to include the namespace_id as well.
108-
cluster_name: clusterName,
109-
project_id: projectId
103+
Metadata.getGKEDescriptor = function(projectId, callback) {
104+
gcpMetadata.instance('attributes/clusterName', function(err, _, clusterName) {
105+
if (err) {
106+
return callback(err);
110107
}
111-
};
108+
callback(null, {
109+
type: 'container',
110+
labels: {
111+
// TODO(ofrobots): it would be good to include the namespace_id as well.
112+
cluster_name: clusterName,
113+
project_id: projectId
114+
}
115+
})
116+
});
112117
};
113118

114119
/**
@@ -144,13 +149,7 @@ Metadata.prototype.getDefaultResource = function(callback) {
144149

145150
self.logging.auth.getEnvironment(function(err, env) {
146151
if (env.IS_CONTAINER_ENGINE) {
147-
gcpMetadata.instance('attributes/clusterName',
148-
function (err, _, clusterName) {
149-
if (err) {
150-
return callback(err);
151-
}
152-
callback(null, Metadata.getGKEDescriptor(projectId, clusterName));
153-
});
152+
Metadata.getGKEDescriptor(projectId, callback);
154153
} else {
155154
var defaultResource;
156155

packages/logging/test/metadata.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,8 @@ describe('metadata', function() {
265265
describe('container engine', function() {
266266
it('should return correct descriptor', function(done) {
267267
var RETURNED_CLUSTER_NAME = 'fake-cluster-name';
268-
var DESCRIPTOR = {};
269-
270268
instanceValueOverride = RETURNED_CLUSTER_NAME;
271269

272-
Metadata.getGKEDescriptor = function(projectId, clusterName) {
273-
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
274-
assert.strictEqual(clusterName, RETURNED_CLUSTER_NAME);
275-
return DESCRIPTOR;
276-
};
277-
278270
metadata.logging.auth.getEnvironment = function(callback) {
279271
callback(null, {
280272
IS_COMPUTE_ENGINE: true,
@@ -284,7 +276,13 @@ describe('metadata', function() {
284276

285277
metadata.getDefaultResource(function(err, defaultResource) {
286278
assert.ifError(err);
287-
assert.strictEqual(defaultResource, DESCRIPTOR);
279+
assert.deepStrictEqual(defaultResource, {
280+
type: 'container',
281+
labels: {
282+
cluster_name: RETURNED_CLUSTER_NAME,
283+
project_id: RETURNED_PROJECT_ID
284+
}
285+
});
288286
done();
289287
});
290288
});

0 commit comments

Comments
 (0)