@catladder/pipeline 2.6.3 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,16 +38,14 @@ const getJobRunScriptForJob = (
38
38
 
39
39
  export const getDeleteSchedulesScripts = (context: ComponentContext) => {
40
40
  const deployConfig = getCloudRunDeployConfig(context);
41
- const jobsWithSchedule = getCloudRunJobsWithSchedule(context);
41
+ const schedules = getSchedules(context);
42
42
  const argsString = createArgsString({
43
43
  project: deployConfig.projectId,
44
44
  location: deployConfig.region,
45
45
  });
46
- return jobsWithSchedule
47
- .map(({ schedulerName }) => {
48
- return [
49
- `${gcloudSchedulerCmd()} jobs delete ${schedulerName} ${argsString}`,
50
- ];
46
+ return schedules
47
+ .map(({ name }) => {
48
+ return [`${gcloudSchedulerCmd()} jobs delete ${name} ${argsString}`];
51
49
  })
52
50
  .flat();
53
51
  };
@@ -86,24 +84,10 @@ export const getJobRunScripts = (
86
84
 
87
85
  export const getJobCreateScripts = (context: ComponentContext): string[] =>
88
86
  getCloudRunJobsWithNames(context).map(
89
- (
90
- {
91
- job: {
92
- command,
93
- image,
94
- cpu,
95
- memory = "512Mi",
96
- timeout = "10m",
97
- parallelism = 1,
98
- volumes,
99
- },
100
- jobName,
101
- },
102
- jobIndex,
103
- ): string => {
104
- const commandArray = Array.isArray(command)
105
- ? command
106
- : command.split(" ");
87
+ ({ job, jobName }, jobIndex): string => {
88
+ const commandArray = Array.isArray(job.command)
89
+ ? job.command
90
+ : job.command.split(" ");
107
91
 
108
92
  const {
109
93
  image: commonImage,
@@ -115,18 +99,26 @@ export const getJobCreateScripts = (context: ComponentContext): string[] =>
115
99
  {
116
100
  command: `"${commandArray.join(",")}"`,
117
101
  labels: `"${makeLabelString(getLabels(context))},cloud-run-job-name=$current_job_name"`,
118
- image: `"${image ?? commonImage}"`,
102
+ image: `"${job.image ?? commonImage}"`,
119
103
  project,
120
104
  region,
121
- cpu,
122
- memory,
123
- parallelism,
124
- "task-timeout": timeout,
105
+ cpu: job.cpu,
106
+ memory: job.memory ?? "512Mi",
107
+ parallelism: job.parallelism ?? 1,
108
+
109
+ "task-timeout": job.timeout ?? "10m",
125
110
  "env-vars-file": ENV_VARS_FILENAME,
126
111
  "max-retries": 0,
112
+
127
113
  ...deployArgs,
114
+
115
+ // network
116
+ "vpc-connector": job?.vpcConnector,
117
+ "vpc-egress": job?.vpcEgress,
118
+ network: job?.network,
119
+ subnet: job?.subnet,
128
120
  },
129
- ...createVolumeConfig(volumes, "job"),
121
+ ...createVolumeConfig(job.volumes, "job"),
130
122
  );
131
123
 
132
124
  return [
@@ -148,48 +140,62 @@ export const getJobCreateScripts = (context: ComponentContext): string[] =>
148
140
  export const getCreateScheduleScripts = (
149
141
  context: ComponentContext,
150
142
  ): string[] => {
151
- const jobsWithSchedule = getCloudRunJobsWithSchedule(context);
143
+ const schedules = getSchedules(context);
152
144
  const { region: location, projectId: project } =
153
145
  getCloudRunDeployConfig(context);
154
146
 
155
- const uriBase = `https://${location}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${project}/jobs`;
156
- const gcloudArgs = {
157
- project,
158
- location,
159
- uri: `"$current_job_uri"`,
160
- "http-method": "POST",
161
- "oauth-service-account-email": `"$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com"`,
162
- };
163
-
164
- return jobsWithSchedule.map(
165
- (
166
- { job: { maxRetryAttempts, schedule }, jobName, schedulerName },
167
- jobIndex,
168
- ): string => {
169
- const argsString = createArgsString({
170
- ...gcloudArgs,
171
- schedule: `"${schedule}"`,
172
- "max-retry-attempts": maxRetryAttempts ?? 0,
173
- });
174
- return [
175
- jobIndex === 0
176
- ? `exist_scheduler_names="$(\n ${gcloudSchedulerCmd()} jobs list --filter='httpTarget.uri ~ ${context.env}.*${context.name}' --format='value(name)' --limit=999 --location='${location}' --project='${project}'\n)"`
177
- : null,
178
- `current_job_uri="${uriBase}/${jobName}:run"`,
179
- `current_scheduler_name="${schedulerName}"`,
180
- `if grep "$current_scheduler_name" <<<"$exist_scheduler_names" >/dev/null; then`,
181
- ` ${gcloudSchedulerCmd()} jobs update http "$current_scheduler_name" ${argsString}`,
182
- `else`,
183
- ` ${gcloudSchedulerCmd()} jobs create http "$current_scheduler_name" ${argsString}`,
184
- `fi`,
185
- ]
186
- .filter(notNil)
187
- .join("\n");
188
- },
189
- );
147
+ return schedules.map((scheduler, jobIndex): string => {
148
+ const uri = getSchedulerUrl(scheduler, context);
149
+
150
+ const argsString = createArgsString({
151
+ project,
152
+ location,
153
+ uri: `"$current_job_uri"`,
154
+ "http-method": "POST",
155
+ "oauth-service-account-email": `"$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com"`,
156
+ schedule: `"${scheduler.schedule}"`,
157
+ "max-retry-attempts": scheduler.maxRetryAttempts ?? 0,
158
+ });
159
+ return [
160
+ jobIndex === 0
161
+ ? `exist_scheduler_names="$(\n ${gcloudSchedulerCmd()} jobs list --filter='httpTarget.uri ~ ${context.env}.*${context.name}' --format='value(name)' --limit=999 --location='${location}' --project='${project}'\n)"`
162
+ : null,
163
+ `current_job_uri="${uri}"`,
164
+ `current_scheduler_name="${scheduler.name}"`,
165
+ `if grep "$current_scheduler_name" <<<"$exist_scheduler_names" >/dev/null; then`,
166
+ ` ${gcloudSchedulerCmd()} jobs update http "$current_scheduler_name" ${argsString}`,
167
+ `else`,
168
+ ` ${gcloudSchedulerCmd()} jobs create http "$current_scheduler_name" ${argsString}`,
169
+ `fi`,
170
+ ]
171
+ .filter(notNil)
172
+ .join("\n");
173
+ });
174
+ };
175
+
176
+ type Scheduler = {
177
+ name: StringOrBashExpression;
178
+ maxRetryAttempts?: number;
179
+ schedule: string;
180
+ } & {
181
+ type: "cloudRunJob";
182
+ jobName: StringOrBashExpression;
183
+ };
184
+
185
+ const getSchedulerUrl = (scheduler: Scheduler, context: ComponentContext) => {
186
+ if (scheduler.type === "cloudRunJob") {
187
+ const { region: location, projectId: project } =
188
+ getCloudRunDeployConfig(context);
189
+
190
+ const uriBase = `https://${location}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${project}/jobs`;
191
+
192
+ return `${uriBase}/${scheduler.jobName}:run`;
193
+ }
194
+
195
+ throw new Error(`Unknown scheduler type: ${scheduler.type}`);
190
196
  };
191
197
 
192
- const getCloudRunJobsWithSchedule = (context: ComponentContext) => {
198
+ const getSchedules = (context: ComponentContext): Scheduler[] => {
193
199
  const jobsWithNames = getCloudRunJobsWithNames(context);
194
200
 
195
201
  return jobsWithNames
@@ -202,12 +208,16 @@ const getCloudRunJobsWithSchedule = (context: ComponentContext) => {
202
208
  jobKey: string;
203
209
  } => entry.job.when === "schedule",
204
210
  )
205
- .map(({ job, jobName, jobKey }) => ({
206
- job,
207
- jobName,
208
- jobKey,
209
- schedulerName: jobName.concat("-scheduler"),
210
- }));
211
+ .map(({ job: { maxRetryAttempts, schedule }, jobName }) => {
212
+ const schedulerName = jobName.concat("-scheduler");
213
+ return {
214
+ name: schedulerName,
215
+ maxRetryAttempts,
216
+ schedule,
217
+ type: "cloudRunJob",
218
+ jobName,
219
+ };
220
+ });
211
221
  };
212
222
 
213
223
  const getCloudRunJobsWithNames = (context: ComponentContext) => {
@@ -158,6 +158,20 @@ export type DeployConfigCloudRunService = {
158
158
  */
159
159
  executionEnvironment?: "gen2" | "gen1";
160
160
 
161
+ /**
162
+ * Use http2 end-to-end. See https://cloud.google.com/run/docs/configuring/http2
163
+ *
164
+ * Defaults to false.
165
+ *
166
+ * Your service needs to be able to handle http2 requests.
167
+ * Its recommended to use http2 without tls, since cloud run handles the encryption for you. (so called "h2c" (http2 cleartext))
168
+ *
169
+ */
170
+ http2?: boolean;
171
+ } & DeployConfigCloudRunWithVolumes &
172
+ DeployConfigCloudRunNetworkConfig;
173
+
174
+ export type DeployConfigCloudRunNetworkConfig = {
161
175
  /* the vpc network, see https://cloud.google.com/sdk/gcloud/reference/run/deploy#--network */
162
176
  network?: string;
163
177
 
@@ -174,18 +188,7 @@ export type DeployConfigCloudRunService = {
174
188
  * vpc connector
175
189
  */
176
190
  vpcConnector?: string;
177
-
178
- /**
179
- * Use http2 end-to-end. See https://cloud.google.com/run/docs/configuring/http2
180
- *
181
- * Defaults to false.
182
- *
183
- * Your service needs to be able to handle http2 requests.
184
- * Its recommended to use http2 without tls, since cloud run handles the encryption for you. (so called "h2c" (http2 cleartext))
185
- *
186
- */
187
- http2?: boolean;
188
- } & DeployConfigCloudRunWithVolumes;
191
+ };
189
192
 
190
193
  export type DeployConfigCloudRunJobBase = {
191
194
  /**
@@ -219,7 +222,8 @@ export type DeployConfigCloudRunJobBase = {
219
222
  * number of tasks that may run concurrently, defaults to 1
220
223
  */
221
224
  parallelism?: number;
222
- } & DeployConfigCloudRunWithVolumes;
225
+ } & DeployConfigCloudRunWithVolumes &
226
+ DeployConfigCloudRunNetworkConfig;
223
227
 
224
228
  type Minute = string;
225
229
  type Hour = string;