Procházet zdrojové kódy

Add PVC, create init job, separate rpc pod

Anuj Bansal před 3 roky
rodič
revize
613eec1706

+ 103 - 52
devops/infrastructure/node-network/index.ts

@@ -58,31 +58,64 @@ const jsonModifyConfig = new configMapFromFile(
   resourceOptions
 ).configName
 
-const dataPath = '/subkey-data'
-const builderPath = '/builder-data'
+const chainDataPath = '/chain-data'
 const networkSuffix = config.get('networkSuffix') || '8129'
-const chainSpecPath = `${builderPath}/chainspec-raw.json`
+const chainSpecPath = `${chainDataPath}/chainspec-raw.json`
 const numberOfValidators = config.getNumber('numberOfValidators') || 2
 
-const subkeyContainers = getSubkeyContainers(numberOfValidators, dataPath)
-const validatorContainers = getValidatorContainers(numberOfValidators, dataPath, builderPath, chainSpecPath)
+const subkeyContainers = getSubkeyContainers(numberOfValidators, chainDataPath)
+// const validatorContainers = getValidatorContainers(numberOfValidators, dataPath, builderPath, chainSpecPath)
 
-const deployment = new k8s.apps.v1.Deployment(
-  name,
+const pvc = new k8s.core.v1.PersistentVolumeClaim(
+  `${name}-pvc`,
   {
     metadata: {
-      namespace: namespaceName,
       labels: appLabels,
+      namespace: namespaceName,
+      name: `${name}-pvc`,
     },
     spec: {
-      replicas: 1,
-      selector: { matchLabels: appLabels },
-      template: {
-        metadata: {
-          labels: appLabels,
+      accessModes: ['ReadWriteMany'],
+      resources: {
+        requests: {
+          storage: `1Gi`,
         },
+      },
+    },
+  },
+  resourceOptions
+)
+
+if (isMinikube) {
+  const pv = new k8s.core.v1.PersistentVolume(`${name}-pv`, {
+    metadata: {
+      labels: { ...appLabels, type: 'local' },
+      namespace: namespaceName,
+      name: `${name}-pv`,
+    },
+    spec: {
+      accessModes: ['ReadWriteMany'],
+      capacity: {
+        storage: `1Gi`,
+      },
+      hostPath: {
+        path: '/mnt/data/ckan',
+      },
+    },
+  })
+}
+
+const chainDataPrepareJob = new k8s.batch.v1.Job(
+  'chain-data',
+  {
+    metadata: {
+      namespace: namespaceName,
+    },
+    spec: {
+      backoffLimit: 0,
+      template: {
         spec: {
-          initContainers: [
+          containers: [
             ...subkeyContainers,
             {
               name: 'builder-node',
@@ -90,13 +123,13 @@ const deployment = new k8s.apps.v1.Deployment(
               command: ['/bin/sh', '-c'],
               args: [
                 `/joystream/chain-spec-builder generate -a ${numberOfValidators} \
-                --chain-spec-path ${builderPath}/chainspec.json --deployment live \
-                --endowed 1 --keystore-path ${builderPath}/data >> ${builderPath}/seeds.txt`,
+                --chain-spec-path ${chainDataPath}/chainspec.json --deployment live \
+                --endowed 1 --keystore-path ${chainDataPath}/data >> ${chainDataPath}/seeds.txt`,
               ],
               volumeMounts: [
                 {
-                  name: 'builder-data',
-                  mountPath: builderPath,
+                  name: 'config-data',
+                  mountPath: chainDataPath,
                 },
               ],
             },
@@ -104,7 +137,7 @@ const deployment = new k8s.apps.v1.Deployment(
               name: 'json-modify',
               image: 'python',
               command: ['python'],
-              args: ['/scripts/json_modify.py', '--path', `${builderPath}/chainspec.json`, '--prefix', networkSuffix],
+              args: ['/scripts/json_modify.py', '--path', `${chainDataPath}/chainspec.json`, '--prefix', networkSuffix],
               volumeMounts: [
                 {
                   mountPath: '/scripts/json_modify.py',
@@ -112,12 +145,8 @@ const deployment = new k8s.apps.v1.Deployment(
                   subPath: 'fileData',
                 },
                 {
-                  name: 'builder-data',
-                  mountPath: builderPath,
-                },
-                {
-                  name: 'subkey-data',
-                  mountPath: dataPath,
+                  name: 'config-data',
+                  mountPath: chainDataPath,
                 },
               ],
             },
@@ -125,17 +154,54 @@ const deployment = new k8s.apps.v1.Deployment(
               name: 'raw-chain-spec',
               image: 'joystream/node:latest',
               command: ['/bin/sh', '-c'],
-              args: [`/joystream/node build-spec --chain ${builderPath}/chainspec.json --raw > ${chainSpecPath}`],
+              args: [`/joystream/node build-spec --chain ${chainDataPath}/chainspec.json --raw > ${chainSpecPath}`],
               volumeMounts: [
                 {
-                  name: 'builder-data',
-                  mountPath: builderPath,
+                  name: 'config-data',
+                  mountPath: chainDataPath,
                 },
               ],
             },
           ],
+          volumes: [
+            {
+              name: 'json-modify-script',
+              configMap: {
+                name: jsonModifyConfig,
+              },
+            },
+            {
+              name: 'config-data',
+              persistentVolumeClaim: {
+                claimName: `${name}-pvc`,
+              },
+            },
+          ],
+          restartPolicy: 'Never',
+        },
+      },
+    },
+  },
+  { ...resourceOptions }
+)
+
+const deployment = new k8s.apps.v1.Deployment(
+  `rpc-node`,
+  {
+    metadata: {
+      namespace: namespaceName,
+      labels: appLabels,
+    },
+    spec: {
+      replicas: 1,
+      selector: { matchLabels: appLabels },
+      template: {
+        metadata: {
+          labels: appLabels,
+        },
+        spec: {
+          initContainers: [],
           containers: [
-            ...validatorContainers,
             {
               name: 'rpc-node',
               image: 'joystream/node:latest',
@@ -147,8 +213,6 @@ const deployment = new k8s.apps.v1.Deployment(
               args: [
                 '--chain',
                 chainSpecPath,
-                '--unsafe-rpc-external',
-                '--unsafe-ws-external',
                 '--ws-external',
                 '--rpc-cors',
                 'all',
@@ -163,29 +227,17 @@ const deployment = new k8s.apps.v1.Deployment(
               ],
               volumeMounts: [
                 {
-                  name: 'subkey-data',
-                  mountPath: dataPath,
-                },
-                {
-                  name: 'builder-data',
-                  mountPath: builderPath,
+                  name: 'config-data',
+                  mountPath: chainDataPath,
                 },
               ],
             },
           ],
           volumes: [
             {
-              name: 'subkey-data',
-              emptyDir: {},
-            },
-            {
-              name: 'builder-data',
-              emptyDir: {},
-            },
-            {
-              name: 'json-modify-script',
-              configMap: {
-                name: jsonModifyConfig,
+              name: 'config-data',
+              persistentVolumeClaim: {
+                claimName: `${name}-pvc`,
               },
             },
           ],
@@ -193,7 +245,7 @@ const deployment = new k8s.apps.v1.Deployment(
       },
     },
   },
-  { ...resourceOptions }
+  { ...resourceOptions, dependsOn: chainDataPrepareJob }
 )
 
 // Export the Deployment name
@@ -210,9 +262,8 @@ const service = new k8s.core.v1.Service(
     },
     spec: {
       ports: [
-        { name: 'port-1', port: 9944, targetPort: 'rpc-9944' },
-        { name: 'port-2', port: 9933, targetPort: 'rpc-9933' },
-        { name: 'port-3', port: 30333, targetPort: 'rpc-30333' },
+        { name: 'port-1', port: 9944 },
+        { name: 'port-2', port: 9933 },
       ],
       selector: appLabels,
     },

+ 4 - 13
devops/infrastructure/node-network/utils.ts

@@ -8,7 +8,7 @@ export const getSubkeyContainers = (validators: number, dataPath: string) => {
       args: [`subkey generate-node-key >> ${dataPath}/privatekey${i} 2>> ${dataPath}/publickey${i}`],
       volumeMounts: [
         {
-          name: 'subkey-data',
+          name: 'config-data',
           mountPath: dataPath,
         },
       ],
@@ -17,12 +17,7 @@ export const getSubkeyContainers = (validators: number, dataPath: string) => {
   return result
 }
 
-export const getValidatorContainers = (
-  validators: number,
-  dataPath: string,
-  builderPath: string,
-  chainSpecPath: string
-) => {
+export const getValidatorContainers = (validators: number, dataPath: string, chainSpecPath: string) => {
   const result = []
   for (let i = 1; i <= validators; i++) {
     result.push({
@@ -36,20 +31,16 @@ export const getValidatorContainers = (
         '--node-key-file',
         `${dataPath}/privatekey${i}`,
         '--keystore-path',
-        `${builderPath}/data/auth-${i - 1}`,
+        `${dataPath}/data/auth-${i - 1}`,
         '--validator',
         '--log',
         'runtime,txpool,transaction-pool,trace=sync',
       ],
       volumeMounts: [
         {
-          name: 'subkey-data',
+          name: 'config-data',
           mountPath: dataPath,
         },
-        {
-          name: 'builder-data',
-          mountPath: builderPath,
-        },
       ],
     })
   }