Repository: godaddy/kubernetes-external-secrets
Pull request #22 (2019-02-20 21:28:21)

fix(rbac): adjust the poller upsert code so it doesn't need `get`


Patch:
diff --git a/lib/poller.js b/lib/poller.js
index bbedb07..23004f8 100644
--- a/lib/poller.js
+++ b/lib/poller.js
@@ -78,27 +78,13 @@ class Poller {
     const secretManifest = await this._createSecretManifest({ secretDescriptor });
     const kubeNamespace = this._kubeClient.api.v1.namespaces(this._namespace);
 
-    let kubeSecret = null;
+    this._logger.info(`upserting secret ${secretName}`);
     try {
-      this._logger.info(`fetching secret ${secretName}`);
-      kubeSecret = await kubeNamespace.secrets(secretName).get();
+      return await kubeNamespace.secrets.post({ body: secretManifest });
     } catch (err) {
-      // NOTE(jdaeli): If Not Found error, create the secret
-      // otherwise throw the error
-      if (err.statusCode !== 404) {
-        throw err;
-      }
+      if (err.statusCode !== 409) throw err;
+      return await kubeNamespace.secrets(secretName).put({ body: secretManifest });
     }
-
-    // NOTE(jdaeli): if kube secret doesn't exist, create the secret
-    // otherwise update/patch it
-    if (!kubeSecret) {
-      this._logger.info(`creating secret ${secretName}`);
-      return kubeNamespace.secrets.post({ body: secretManifest });
-    }
-
-    this._logger.info(`updating secret ${secretName}`);
-    return kubeNamespace.secrets(secretName).put({ body: secretManifest });
   }
 
   /**


Test patch:
diff --git a/lib/poller.test.js b/lib/poller.test.js
index 7cb12cd..faac15a 100644
--- a/lib/poller.test.js
+++ b/lib/poller.test.js
@@ -159,16 +159,10 @@ describe('Poller', () => {
     });
 
     it('creates new secret', async () => {
-      const notFoundError = new Error('Not Found');
-      notFoundError.statusCode = 404;
-
-      kubeNamespaceMock.get = sinon.stub().throws(notFoundError);
       kubeNamespaceMock.secrets.post = sinon.stub().resolves();
 
       await poller._upsertKubernetesSecret(upsertSecretParams);
 
-      expect(kubeNamespaceMock.secrets.calledWith('fakeSecretName')).to.be.true;
-      expect(kubeNamespaceMock.get.called).to.be.true;
       expect(kubeNamespaceMock.secrets.post.calledWith({
         body: {
           apiVersion: 'v1',
@@ -185,13 +179,14 @@ describe('Poller', () => {
     });
 
     it('updates secret', async () => {
-      kubeNamespaceMock.get = sinon.stub().resolves({ fakeSecretName: 'fakeSecretString' });
+      const conflictError = new Error('Conflict');
+      conflictError.statusCode = 409;
+      kubeNamespaceMock.secrets.post = sinon.stub().throws(conflictError);
       kubeNamespaceMock.put = sinon.stub().resolves();
 
       await poller._upsertKubernetesSecret(upsertSecretParams);
 
       expect(kubeNamespaceMock.secrets.calledWith('fakeSecretName')).to.be.true;
-      expect(kubeNamespaceMock.get.called).to.be.true;
       expect(kubeNamespaceMock.put.calledWith({
         body: {
           apiVersion: 'v1',
@@ -211,7 +206,7 @@ describe('Poller', () => {
       const internalErrorServer = new Error('Internal Error Server');
       internalErrorServer.statusCode = 500;
 
-      kubeNamespaceMock.get = sinon.stub().throws(internalErrorServer);
+      kubeNamespaceMock.secrets.post = sinon.stub().throws(internalErrorServer);
 
       let error;