forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_run.py
More file actions
172 lines (163 loc) · 6.98 KB
/
Copy pathcloud_run.py
File metadata and controls
172 lines (163 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import annotations
import json
import subprocess
from scripts.runtime_env_durable_dispatch_contracts import ValidationError
from scripts.runtime_env_validation.common import (
ConfigDict,
StringMap,
_as_config_dict,
_as_config_list,
_env_entries_by_name,
_network_flags,
_validate_cloud_run_secret_entries,
_validate_env_entries,
_validate_forbidden_env_entries,
compute_project,
)
from scripts.runtime_env_validation.workflows import _validate_workflow_flags
def _cloud_run_network_flags_from_annotations(annotations: object) -> StringMap:
annotations_dict = _as_config_dict(annotations)
if annotations_dict is None:
return {}
flags: StringMap = {}
network_interfaces = annotations_dict.get('run.googleapis.com/network-interfaces')
if isinstance(network_interfaces, str) and network_interfaces:
try:
parsed_interfaces = _as_config_list(json.loads(network_interfaces)) or []
except json.JSONDecodeError:
parsed_interfaces = []
if parsed_interfaces:
first_interface = _as_config_dict(parsed_interfaces[0])
if first_interface is not None:
network = first_interface.get('network')
subnet = first_interface.get('subnetwork')
if isinstance(network, str):
flags['--network'] = network
if isinstance(subnet, str):
flags['--subnet'] = subnet
egress = annotations_dict.get('run.googleapis.com/vpc-access-egress')
if isinstance(egress, str):
flags['--vpc-egress'] = egress
return flags
def _fetch_live_cloud_run_state(env_config: ConfigDict) -> ConfigDict:
# This deploy pipeline (gcp_backend.yml) deploys Cloud Run *services* only — the declared
# Cloud Run jobs (memory-maintenance-job, notifications-job) ship via their own workflows,
# so their live state is owned elsewhere. Fetch and live-validate services only; validating
# a job this pipeline does not deploy produced false failures (a not-found job crashed the
# whole deploy, and notifications-job's separately-managed env legitimately differs). The
# job contract is still validated statically against the rendered state.
services: ConfigDict = {}
project = compute_project(env_config)
region = env_config['region']
cloud_run = _as_config_dict(env_config.get('cloud_run')) or {}
service_configs = _as_config_dict(cloud_run.get('services')) or {}
for service in service_configs:
command = [
'gcloud',
'run',
'services',
'describe',
service,
f'--project={project}',
f'--region={region}',
'--format=json',
]
result = subprocess.run(command, check=True, capture_output=True, text=True)
raw_service_state = json.loads(result.stdout)
service_state = _as_config_dict(raw_service_state) or {}
spec = _as_config_dict(service_state.get('spec')) or {}
template = _as_config_dict(spec.get('template')) or {}
metadata = _as_config_dict(template.get('metadata')) or {}
annotations = _as_config_dict(metadata.get('annotations')) or {}
template_spec = _as_config_dict(template.get('spec')) or {}
containers = _as_config_list(template_spec.get('containers')) or [{}]
first_container = _as_config_dict(containers[0]) or {}
services[service] = {
'env': first_container.get('env', []),
'flags': _cloud_run_network_flags_from_annotations(annotations),
}
return {'services': services}
def _validate_cloud_run(
env_config: ConfigDict,
cloud_run_state: ConfigDict,
*,
strict_provisional: bool,
) -> list[ValidationError]:
errors: list[ValidationError] = []
state_services = _as_config_dict(cloud_run_state.get('services'))
if state_services is None:
return [ValidationError('cloud_run', 'state must contain services mapping')]
cloud_run = _as_config_dict(env_config['cloud_run']) or {}
service_configs = _as_config_dict(cloud_run.get('services')) or {}
for service, raw_service_config in service_configs.items():
service_config = _as_config_dict(raw_service_config) or {}
service_state = _as_config_dict(state_services.get(service))
if service_state is None:
if service_config.get('provisional') and not strict_provisional:
continue
errors.append(ValidationError(f'cloud_run/{service}', 'missing service state'))
continue
actual_env = _env_entries_by_name(service_state.get('env', []))
errors.extend(
_validate_env_entries(
scope=f'cloud_run/{service}',
expected=service_config.get('env', {}),
actual=actual_env,
strict_provisional=strict_provisional,
)
)
errors.extend(
_validate_forbidden_env_entries(
scope=f'cloud_run/{service}',
forbidden=service_config.get('forbidden_env'),
actual=actual_env,
)
)
errors.extend(
_validate_cloud_run_secret_entries(
scope=f'cloud_run/{service}',
expected=service_config.get('secrets', {}),
actual=actual_env,
)
)
errors.extend(
_validate_workflow_flags(
scope=f'cloud_run/{service}',
expected=_network_flags(env_config),
actual=service_state.get('flags', {}),
strict_provisional=strict_provisional,
)
)
state_jobs = _as_config_dict(cloud_run_state.get('jobs'))
if state_jobs is not None:
job_configs = _as_config_dict(cloud_run.get('jobs')) or {}
for job, raw_job_config in job_configs.items():
job_config = _as_config_dict(raw_job_config) or {}
job_state = _as_config_dict(state_jobs.get(job))
if job_state is None:
errors.append(ValidationError(f'cloud_run/{job}', 'missing job state'))
continue
actual_env = _env_entries_by_name(job_state.get('env', []))
errors.extend(
_validate_env_entries(
scope=f'cloud_run/{job}',
expected=job_config.get('env', {}),
actual=actual_env,
strict_provisional=strict_provisional,
)
)
errors.extend(
_validate_forbidden_env_entries(
scope=f'cloud_run/{job}',
forbidden=job_config.get('forbidden_env'),
actual=actual_env,
)
)
errors.extend(
_validate_cloud_run_secret_entries(
scope=f'cloud_run/{job}',
expected=job_config.get('secrets', {}),
actual=actual_env,
)
)
return errors