forked from GoogleCloudPlatform/pubsub2inbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommendations.yaml
311 lines (301 loc) · 13 KB
/
recommendations.yaml
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
retryPeriod: 4 days ago
# This example works in conjunction with Pubsub2Inbox function defined in
# the project-finder.yaml configuration. This function will accept a project
# input, fetch recommendations, create a report on a storage bucket for that
# report and then send the report via email to the owner. The owner email
# is extracted from the project's owner label and some character substitution
# is performed (eg. dashes to dots).
processors:
- genericjson
- recommendations
# If set to true, all outputs must succeed in order the function to be considered successful.
allOutputsMustSucceed: true
recommendations:
quota_project_id: "YOUR-PROJECT-ID"
fetch_recommendations: "{{ includeRecommendations|default('true') }}"
fetch_insights: "{{ includeRecommendations|default('true') }}"
recommender_types:
# Full list: https://cloud.google.com/recommender/docs/recommenders#recommenders
- google.compute.instance.MachineTypeRecommender # Location: zone
- google.compute.instanceGroupManager.MachineTypeRecommender # Location: zone
- google.compute.instance.IdleResourceRecommender # Location: zone
- google.compute.disk.IdleResourceRecommender # Location: region & zone
- google.compute.address.IdleResourceRecommender # Location: region & global
- google.compute.image.IdleResourceRecommender # Location: region & global
- google.compute.commitment.UsageCommitmentRecommender # Location: region
- google.cloudsql.instance.OutOfDiskRecommender
- google.cloudsql.instance.IdleRecommender
- google.cloudsql.instance.OverprovisionedRecommender
- google.iam.policy.Recommender
- google.logging.productSuggestion.ContainerRecommender
- google.monitoring.productSuggestion.ComputeRecommender
- google.resourcemanager.projectUtilization.Recommender
recommendation_filter: 'stateInfo.state=ACTIVE'
insight_types:
# Full list: https://cloud.google.com/recommender/docs/insights/insight-types
- google.compute.firewall.Insight
- google.iam.policy.Insight
- google.iam.serviceAccount.Insight
# New insights
- google.compute.disk.IdleResourceInsight
- google.compute.image.IdleResourceInsight
- google.compute.address.IdleResourceInsight
- google.cloudasset.asset.Insight
insight_filter: 'stateInfo.state=ACTIVE'
#organizations:
# - 1234567
projects: |
{% set project = [data.projectId] %}{{ project|json_encode }}
#billingAccounts:
# - 123456-789012-ABCDEF
locations:
- global
- europe
- us
- europe-west4*
vars:
types:
organizations: "Organization"
projects: "Project"
folders: "Folder"
billingAccount: "Billing account"
commitment_types:
THIRTY_SIX_MONTH: "36 months"
TWELVE_MONTH: "12 months"
links:
google.compute.instance.IdleResourceRecommender: https://console.cloud.google.com/home/recommendations/list/COMPUTE_IDLE_RESOURCES?
google.compute.instance.MachineTypeRecommender: https://console.cloud.google.com/home/recommendations/list/VM_COST?
google.compute.instanceGroupManager.MachineTypeRecommender: https://console.cloud.google.com/home/recommendations/list/VM_COST?
google.compute.disk.IdleResourceRecommender: https://console.cloud.google.com/home/recommendations/list/COMPUTE_IDLE_RESOURCES?
google.compute.firewall.Insight: https://console.cloud.google.com/home/recommendations/list/SHADOWED_FIREWALL_RULES?
google.compute.commitment.UsageCommitmentRecommender: https://console.cloud.google.com/home/recommendations/list/PROJECT_COMMITMENTS?
google.iam.policy.Insight: https://console.cloud.google.com/home/recommendations/list/IAM_ROLES?
subtypes:
CHANGE_MACHINE_TYPE: "Change instance types"
CHANGE_MACHINE_FAMILY: "Change instance families"
STOP_VM: "Stop idle instances"
SNAPSHOT_AND_DELETE_DISK: "Snapshot and remove disks"
DELETE_DISK: "Remove disks"
PERMISSIONS_USAGE: "Improve IAM permissions"
SERVICE_ACCOUNT_USAGE: "Remove unused service accounts"
REPLACE_ROLE: "Replace IAM roles"
REMOVE_ROLE: "Remove unused IAM roles"
SHADOWED_RULE: "Resolve shadowed firewall rules"
UNUSED_RULE: "Remove unused firewall rules"
UNUSED_ATTRIBUTE: "Unused firewall attributes"
DELETE_ADDRESS: "Delete unused public IP addresses"
PROJECT_SCOPED_COMMITMENTS: "Purchase project commitments"
DELETE_IMAGE: "Delete unused images"
BILLING_ACCOUNT_SCOPED_COMMITMENTS: "Purchase commitments"
EXTERNAL_MEMBER: "External members in privileged group"
EXTERNAL_SERVICE_ACCOUNT_IMPERSONATOR: "Cross-project SA impersonation"
EXTERNAL_POLICY_EDITOR: "External IAM policy modifier"
EXTERNAL_CLOUD_STORAGE_OBJECT_VIEWER: "External Cloud Storage viewer"
TERMINATED_MEMBER: "Inactive users in IAM policy"
PUBLIC_IAM_POLICY: "Publicly accessible resources"
OWNER_TERMINATED_PROJECT: "Inactive user as Owner"
outputs:
# This creates the project specif report on in a bucket as XLSX file
- type: gcs
bucket: YOUR-BUCKET
object: Recommendations_{{ ""|strftime('%b-%Y') }}.xlsx
base64decode: true
contents: |
{%- set workbook -%}
{%- for parent, parent_recommendations in recommendations.items() -%}
{% set total_cost = namespace(cost=0) %}
{% set row_class = cycler('odd', 'even') %}
{%- for r in parent_recommendations -%}
{% if loop.first %}
<table>
<thead>
<tr>
<td><b>Parent</b></td>
<td><b>Resource</td>
<td><b>Recommended action</b></td>
<td><b>Description</b></td>
<td><b>Monthly impact</b></td>
</tr>
</thead>
<tbody>
{% endif %}
{% for og in r.recommendation.content.operation_groups %}
{% for op in og.operations %}
{% if op.action != 'test' and not op.resource.endswith('$snapshot-name') %}
<tr class="{{ row_class.next() }}">
<td>{{ types[r.type] }}: {% if r.type == "projects" %}{{ r.parent[2] }} ({{ r.parent[0] }}){% else %}{{ r.parent }}{% endif %}</td>
<td>{% if r.recommender_type in links %}<a href="{{ links[r.recommender_type] }}{{ r.link }}">{% endif %}{% if op.resource_type != 'compute.googleapis.com/Commitment' -%}
{{ op.resource|split("/")|index(-1) }}
{%- else -%}
{% for res in op.value.resources %}{{ res.type|title }}: {% if res.type == 'VCPU' %}{{ res.amount }} vCPU(s){% else %}{{ (res.amount/1024)|round }} GB{% endif %}{% endfor %}
{%- endif %}{% if r.recommender_type in links %}</a>{% endif %}
</td>
<td>{{ op.action|title }}</td>
<td>{{ r.recommendation.description }}</td>
<td>{% if 'cost_projection' in r.recommendation.primary_impact %}{{ r.recommendation.primary_impact.cost_projection.cost|format_cost() }}{% endif %}</td>
</tr>
{% endif %}
{% endfor %}
{% endfor %}
{%- if 'cost_projection' in r.recommendation.primary_impact -%}
{%- set total_cost.cost = total_cost.cost + r.recommendation.primary_impact.cost_projection.cost|get_cost() -%}
{%- endif -%}
{% if loop.last %}
<tfoot>
<tr>
<td colspan="3">Total monthly cost impact:</td>
<td>{{ "{:.2f}".format(total_cost.cost) }} USD</td>
</tr>
</tfoot>
</tbody>
</table>
{% endif %}
{%- endfor -%}
{%- endfor -%}
{%- endset -%}{{ workbook|html_table_to_xlsx }}
# This sends out the email to the project owner
- type: mail
transports:
- type: sendgrid
apiKey: ""
from: [email protected]
to: |
{{ data.labels.owner|replace('-', '.') }}@your.domain
subject: |
[Google Cloud Platform] Recommendations and Insights report, {{ ""|strftime('%b %Y') }}
body:
attachments:
- gs://YOUR-BUCKET/Recommendations_{{ ""|strftime('%b-%Y') }}.xlsx
html: |
<html><head><title>Recommendations and Insights, {{ ""|strftime('%b %Y') }}</title>
<style type="text/css">
body {
font-family: 'Google Sans', 'Segoe UI', sans-serif;
}
table {
border-collapse: collapse;
}
thead {
background-color: #0F9D58;
color: white;
}
th {
padding: 0.5em;
text-align: left;
}
td {
padding: 0.2em;
}
.even {
background-color: #ddd;
}
.count {
text-align: right;
font-weight: bolder;
font-size: 1.5em;
color: #0F9D58;
}
.subtype {
font-weight: bold;
font-size: 1.5em;
}
.recommendation {
padding: 1em;
margin: 1em;
}
.one-recommendation {
border: 2px solid #4285F4;
}
.one-recommendation td {
padding: 0.5em;
}
.insight {
padding: 1em;
margin: 1em;
}
.one-insight {
border: 2px solid #F4B400;
}
.one-insight td {
padding: 0.5em;
}
</style>
</head>
<body>
<h1>Google Cloud Platform - Recommendations and Insights report, {{ ""|strftime('%b %Y') }}</h1>
<h2>Recommendations</h2>
{% for parent, rollup in recommendations_rollup.items() %}
{% set total_cost = namespace(cost=0) %}
{% set row_class = cycler('1', '2', '3', 'break') %}
{% for sub_type, stats in rollup.items() %}
{% if loop.first %}
<h3>{{ types[stats.type] }}: {% if stats.type == "projects" %}{{ stats.parent[2] }} ({{ stats.parent[0] }}){% else %}{{ stats.parent }}{% endif %}</h3>
<table>
<tr>
{% endif %}
{% if row_class.next() == 'break' %}
</tr>
<tr>
{% endif %}
<td class="recommendation">
<table class="one-recommendation" width="100%">
<tr>
<td class="subtype">{{ subtypes[sub_type] }}</td>
<td class="count">{{ stats.count }}</td>
</tr>
<tr>
<td colspan="2" class="impact">Total monthly cost impact: <b>{{ stats.cost|format_cost }}</b></td>
</tr>
</table>
</td>
{% if loop.last %}
</tr>
</table>
<a href="https://console.cloud.google.com/home/recommendations?project={% if stats.type == "projects" %}{{ stats.parent[0] }}{% else %}{{ stats.parent }}{% endif %}">View recommendations in Cloud Console.</a>
{% endif %}
{% endfor %}
{% endfor %}
<h2>Insights</h2>
{% for parent, rollup in insights_rollup.items() %}
{% set row_class = cycler('1', '2', '3', 'break') %}
{% for sub_type, stats in rollup.items() %}
{% if loop.first %}
<h3>{{ types[stats.type] }}: {% if stats.type == "projects" %}{{ stats.parent[2] }} ({{ stats.parent[0] }}){% else %}{{ stats.parent }}{% endif %}</h3>
<table>
<tr>
{% endif %}
{% if row_class.next() == 'break' %}
</tr>
<tr>
{% endif %}
<td class="insight">
<table class="one-insight" width="100%">
<tr>
<td class="subtype">{{ subtypes[sub_type] }} ({{ sub_type }})</td>
<td class="count">{{ stats.count }}</td>
</tr>
</table>
</td>
{% if loop.last %}
</tr>
</table>
<a href="https://console.cloud.google.com/home/recommendations?project={% if stats.type == "projects" %}{{ stats.parent[0] }}{% else %}{{ stats.parent }}{% endif %}">View insights in Cloud Console.</a>
{% endif %}
{% endfor %}
{% endfor %}
</body>
</html>