15
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
18
- import { readFile } from "node:fs/promises" ;
19
- import { isAbsolute , resolve } from "node:path" ;
20
-
21
- import type k8s from "@kubernetes/client-node" ;
22
-
23
18
import { BaseCommand } from "../../baseCommand.js" ;
24
- import { commandObj } from "../../lib/commandObj.js" ;
25
19
import { CHAIN_FLAGS , PEER_AND_OFFER_NAMES_FLAGS } from "../../lib/const.js" ;
26
20
import { initCli } from "../../lib/lifeCycle.js" ;
27
- import { projectRootDir } from "../../lib/paths.js" ;
28
- import { resolveComputePeersByNames } from "../../lib/resolveComputePeersByNames.js" ;
21
+ import { deployManifests } from "../../lib/manifestsDeploy.js" ;
29
22
30
23
export default class Deploy extends BaseCommand < typeof Deploy > {
31
24
static override description = "Deploy manifests" ;
@@ -36,89 +29,6 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
36
29
} ;
37
30
async run ( ) : Promise < void > {
38
31
const { flags } = await initCli ( this , await this . parse ( Deploy ) ) ;
39
- const computePeers = await resolveComputePeersByNames ( flags ) ;
40
-
41
- for ( const { kubeconfigPath, name, manifestPath } of computePeers ) {
42
- await kubectlApply ( kubeconfigPath , manifestPath , name ) ;
43
- }
44
- }
45
- }
46
-
47
- /**
48
- * This function comes from https://github.com/kubernetes-client/javascript
49
- * Replicate the functionality of `kubectl apply`. That is, create the resources defined in the `specFile` if they do
50
- * not exist, patch them if they do exist.
51
- *
52
- * @param specPath File system path to a YAML Kubernetes spec.
53
- * @return Array of resources created
54
- */
55
- async function kubectlApply (
56
- kubeconfigPath : string ,
57
- specPath : string ,
58
- peerName : string ,
59
- ) : Promise < k8s . KubernetesObject [ ] > {
60
- const kubeconfigAbsolutePath = isAbsolute ( kubeconfigPath )
61
- ? kubeconfigPath
62
- : resolve ( projectRootDir , kubeconfigPath ) ;
63
-
64
- commandObj . log (
65
- `Applying manifest for computePeer ${ peerName } \nKubeconfig: ${ kubeconfigAbsolutePath } \nManifest: ${ specPath } ` ,
66
- ) ;
67
-
68
- const k8s = await import ( "@kubernetes/client-node" ) ;
69
- const kc = new k8s . KubeConfig ( kubeconfigAbsolutePath ) ;
70
- kc . loadFromFile ( kubeconfigAbsolutePath ) ;
71
-
72
- /* eslint-disable */
73
- const client = k8s . KubernetesObjectApi . makeApiClient ( kc ) ;
74
-
75
- const specString = await readFile ( specPath , "utf8" ) ;
76
- const specs : k8s . KubernetesObject [ ] = k8s . loadAllYaml ( specString ) ;
77
-
78
- const validSpecs = specs . filter ( ( s ) => {
79
- return s && s . kind && s . metadata ;
80
- } ) ;
81
-
82
- const created : k8s . KubernetesObject [ ] = [ ] ;
83
-
84
- for ( const spec of validSpecs ) {
85
- // this is to convince the old version of TypeScript that metadata exists even though we already filtered specs
86
- // without metadata out
87
- spec . metadata = spec . metadata || { } ;
88
- spec . metadata . annotations = spec . metadata . annotations || { } ;
89
-
90
- delete spec . metadata . annotations [
91
- "kubectl.kubernetes.io/last-applied-configuration"
92
- ] ;
93
-
94
- spec . metadata . annotations [
95
- "kubectl.kubernetes.io/last-applied-configuration"
96
- ] = JSON . stringify ( spec ) ;
97
-
98
- try {
99
- // try to get the resource, if it does not exist an error will be thrown and we will end up in the catch
100
- // block.
101
- // @ts -expect-error
102
- await client . read ( spec ) ;
103
- // we got the resource, so it exists, so patch it
104
- //
105
- // Note that this could fail if the spec refers to a custom resource. For custom resources you may need
106
- // to specify a different patch merge strategy in the content-type header.
107
- //
108
- // See: https://github.com/kubernetes/kubernetes/issues/97423
109
- const response = await client . patch ( spec ) ;
110
- created . push ( response . body ) ;
111
- } catch ( err ) {
112
- // if the resource doesnt exist then create it
113
- if ( err instanceof k8s . HttpError && err . statusCode === 404 ) {
114
- const response = await client . create ( spec ) ;
115
- created . push ( response . body ) ;
116
- } else {
117
- throw err ;
118
- }
119
- }
32
+ await deployManifests ( { flags } ) ;
120
33
}
121
-
122
- return created ;
123
- /* eslint-enable */
124
34
}
0 commit comments