qontract-reconcile 0.10.2.dev90__py3-none-any.whl → 0.10.2.dev91__py3-none-any.whl

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: qontract-reconcile
3
- Version: 0.10.2.dev90
3
+ Version: 0.10.2.dev91
4
4
  Summary: Collection of tools to reconcile services with their desired state as defined in the app-interface DB.
5
5
  Project-URL: homepage, https://github.com/app-sre/qontract-reconcile
6
6
  Project-URL: repository, https://github.com/app-sre/qontract-reconcile
@@ -208,11 +208,11 @@ reconcile/external_resources/secrets_sync.py,sha256=ZDxzGZ6wC4zxLhA7-L39xDRH6rzU
208
208
  reconcile/external_resources/state.py,sha256=gF3ACdl7YiUlbQ4uEGrD6i_Txxqr6mT9f8IFlTQ-8dY,13176
209
209
  reconcile/fleet_labeler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
210
210
  reconcile/fleet_labeler/dependencies.py,sha256=ZOpmtwgxEPBWU2yHRc6rhPwlvqhwYSsNMJQ_jVq1dLI,2993
211
- reconcile/fleet_labeler/integration.py,sha256=GR-LUDfDp29VNy1aSLun1UwEOQ1AAXXC18ZdngdCsRg,8976
211
+ reconcile/fleet_labeler/integration.py,sha256=x-vfUcWRCI8-_2O1mdEdSbqGM51IwEBgUUKQPlu5_TM,12849
212
212
  reconcile/fleet_labeler/merge_request.py,sha256=SfGxXInxeJzVnsTtO0ZC9-PesUJMdpKxKY9eCB6ms-g,1538
213
- reconcile/fleet_labeler/meta.py,sha256=DF7O4T9wvQ7-xTWXvuNw1OG_F0SBmRrjFBtVy9wWh9U,146
213
+ reconcile/fleet_labeler/meta.py,sha256=lWnpH2U0PHCPXu9Ok_CPmO494qQJQ5pOuqo28s0jzIQ,146
214
214
  reconcile/fleet_labeler/metrics.py,sha256=wx9BmXLsN67m-aSsf81iB7Ehj5SzUsS2WB75isUReZg,662
215
- reconcile/fleet_labeler/ocm.py,sha256=25UVRQDcbcJFQgg4WDXvUI5M8w9yqr0noiy90-sbQjY,2699
215
+ reconcile/fleet_labeler/ocm.py,sha256=1VkUZtgcoqg-F9r--VtTg2LReumkOcRBIPhj-yTCKE0,3470
216
216
  reconcile/fleet_labeler/validate.py,sha256=gzc2tt7h9F60h7dcyJfEmsnjnfuux5Jtc_WzrIqr-5k,2541
217
217
  reconcile/fleet_labeler/vcs.py,sha256=6UHUQ08AGAHXF7629I6X-T_E1pvx96LxjS66EeOzve4,1108
218
218
  reconcile/glitchtip/README.md,sha256=rfXT6jNP9khJW65jL7I2PgoxvxgcGGuJF8NpbzufEQ4,4335
@@ -786,7 +786,7 @@ tools/saas_promotion_state/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJ
786
786
  tools/saas_promotion_state/saas_promotion_state.py,sha256=UfwwRLS5Ya4_Nh1w5n1dvoYtchQvYE9yj1VANt2IKqI,3925
787
787
  tools/sre_checkpoints/__init__.py,sha256=CDaDaywJnmRCLyl_NCcvxi-Zc0hTi_3OdwKiFOyS39I,145
788
788
  tools/sre_checkpoints/util.py,sha256=zEDbGr18ZeHNQwW8pUsr2JRjuXIPz--WAGJxZo9sv_Y,894
789
- qontract_reconcile-0.10.2.dev90.dist-info/METADATA,sha256=ITNNwFXdctUPEAooDO8HTV-cc3Z7izzfNugSWuoAFwU,24565
790
- qontract_reconcile-0.10.2.dev90.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
791
- qontract_reconcile-0.10.2.dev90.dist-info/entry_points.txt,sha256=5i9l54La3vQrDLAdwDKQWC0iG4sV9RRfOb1BpvzOWLc,698
792
- qontract_reconcile-0.10.2.dev90.dist-info/RECORD,,
789
+ qontract_reconcile-0.10.2.dev91.dist-info/METADATA,sha256=Zb9JTF0crcCR37ioM_rENLPJWgXh1_1IyFQGxR8kqx8,24565
790
+ qontract_reconcile-0.10.2.dev91.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
791
+ qontract_reconcile-0.10.2.dev91.dist-info/entry_points.txt,sha256=5i9l54La3vQrDLAdwDKQWC0iG4sV9RRfOb1BpvzOWLc,698
792
+ qontract_reconcile-0.10.2.dev91.dist-info/RECORD,,
@@ -26,6 +26,7 @@ from reconcile.typed_queries.fleet_labels import get_fleet_label_specs
26
26
  from reconcile.utils import (
27
27
  metrics,
28
28
  )
29
+ from reconcile.utils.differ import diff_mappings
29
30
  from reconcile.utils.jinja2.utils import process_jinja2_template
30
31
  from reconcile.utils.ruamel import create_ruamel_instance
31
32
  from reconcile.utils.runtime.integration import (
@@ -34,6 +35,18 @@ from reconcile.utils.runtime.integration import (
34
35
  )
35
36
 
36
37
 
38
+ class ClusterData(BaseModel):
39
+ """
40
+ Helper structure for synching process
41
+ """
42
+
43
+ name: str
44
+ server_url: str
45
+ subscription_id: str
46
+ desired_label_default: FleetLabelDefaultV1
47
+ current_subscription_labels: dict[str, str]
48
+
49
+
37
50
  class FleetLabelerIntegration(QontractReconcileIntegration[NoParams]):
38
51
  def __init__(self) -> None:
39
52
  super().__init__(NoParams())
@@ -59,13 +72,57 @@ class FleetLabelerIntegration(QontractReconcileIntegration[NoParams]):
59
72
  def reconcile(self, dependencies: Dependencies) -> None:
60
73
  validate_label_specs(specs=dependencies.label_specs_by_name)
61
74
  for spec_name, ocm in dependencies.ocm_clients_by_label_spec_name.items():
75
+ spec = dependencies.label_specs_by_name[spec_name]
76
+ discovered_clusters = self._discover_desired_clusters(
77
+ spec=spec,
78
+ ocm=ocm,
79
+ )
80
+ all_desired_clusters = {
81
+ k: v[0] for k, v in discovered_clusters.items() if len(v) == 1
82
+ }
83
+ clusters_with_duplicate_matches = {
84
+ k: v for k, v in discovered_clusters.items() if len(v) > 1
85
+ }
62
86
  self._sync_cluster_inventory(
63
87
  ocm=ocm,
64
88
  spec=dependencies.label_specs_by_name[spec_name],
65
89
  vcs=dependencies.vcs,
90
+ all_desired_clusters=all_desired_clusters,
91
+ clusters_with_duplicate_matches=clusters_with_duplicate_matches,
92
+ dry_run=dependencies.dry_run,
93
+ )
94
+ self._sync_subscription_labels(
95
+ spec=spec,
96
+ desired_clusters=all_desired_clusters,
97
+ ocm=ocm,
66
98
  dry_run=dependencies.dry_run,
67
99
  )
68
100
 
101
+ def _discover_desired_clusters(
102
+ self, spec: FleetLabelsSpecV1, ocm: OCMClient
103
+ ) -> dict[str, list[ClusterData]]:
104
+ clusters: dict[str, list[ClusterData]] = defaultdict(list)
105
+ for label_default in spec.label_defaults:
106
+ match_subscription_labels = dict(label_default.match_subscription_labels)
107
+ for cluster in ocm.discover_clusters_by_labels(
108
+ labels=match_subscription_labels
109
+ ):
110
+ # TODO: ideally we filter on server side - see TODO in ocm.py
111
+ if (
112
+ match_subscription_labels.items()
113
+ <= cluster.subscription_labels.items()
114
+ ):
115
+ clusters[cluster.cluster_id].append(
116
+ ClusterData(
117
+ subscription_id=cluster.subscription_id,
118
+ desired_label_default=label_default,
119
+ name=cluster.name,
120
+ server_url=cluster.server_url,
121
+ current_subscription_labels=cluster.subscription_labels,
122
+ )
123
+ )
124
+ return clusters
125
+
69
126
  def _render_default_labels(
70
127
  self,
71
128
  template: FleetSubscriptionLabelTemplateV1,
@@ -115,63 +172,91 @@ class FleetLabelerIntegration(QontractReconcileIntegration[NoParams]):
115
172
  yml.dump(content, stream)
116
173
  return stream.getvalue()
117
174
 
175
+ def _sync_subscription_labels(
176
+ self,
177
+ spec: FleetLabelsSpecV1,
178
+ desired_clusters: dict[str, ClusterData],
179
+ ocm: OCMClient,
180
+ dry_run: bool,
181
+ ) -> None:
182
+ """
183
+ Synchronize subscription labels for clusters in the spec's inventory.
184
+ Note, that we only update labels for clusters which are also part of the
185
+ discovered desired state.
186
+ I.e., we only operate on clusters that are in both, current state and desired state.
187
+ That way we ensure we do not work on deleted clusters and still synch only on
188
+ whats written in the rendered spec yet.
189
+ """
190
+ for cluster in spec.clusters:
191
+ if not desired_clusters.get(cluster.cluster_id):
192
+ # The cluster is not part of the desired inventory (will be updated with MR soon)
193
+ continue
194
+ # Ensure we only handle labels for our managed prefix
195
+ current_subscription_labels = {
196
+ k: v
197
+ for k, v in desired_clusters[
198
+ cluster.cluster_id
199
+ ].current_subscription_labels.items()
200
+ if k.startswith(spec.managed_subscription_label_prefix)
201
+ }
202
+ desired_subscription_labels = {
203
+ f"{spec.managed_subscription_label_prefix}.{k}": v
204
+ for k, v in dict(cluster.subscription_labels).items()
205
+ }
206
+ diff = diff_mappings(
207
+ current=current_subscription_labels,
208
+ desired=desired_subscription_labels,
209
+ )
210
+ for key in diff.add:
211
+ value = desired_subscription_labels[key]
212
+ logging.info(
213
+ f"[{spec.name}] Adding label '{key}={value}' for cluster '{cluster.cluster_id}' in subscription '{cluster.subscription_id}'."
214
+ )
215
+ if not dry_run:
216
+ ocm.add_subscription_label(
217
+ subscription_id=cluster.subscription_id,
218
+ key=key,
219
+ value=value,
220
+ )
221
+ for key in diff.change:
222
+ value = desired_subscription_labels[key]
223
+ logging.info(
224
+ f"[{spec.name}] Updating label '{key}={value}' for cluster '{cluster.cluster_id}' in subscription '{cluster.subscription_id}'."
225
+ )
226
+ if not dry_run:
227
+ ocm.update_subscription_label(
228
+ subscription_id=cluster.subscription_id,
229
+ key=key,
230
+ value=value,
231
+ )
232
+ # Note, we dont want to enable removal for now - its too dangerous on a broad managed prefix
233
+ # However, if it is needed in the future, we could easily add it here.
234
+
118
235
  def _sync_cluster_inventory(
119
236
  self,
120
237
  ocm: OCMClient,
121
238
  spec: FleetLabelsSpecV1,
122
239
  vcs: VCS,
240
+ clusters_with_duplicate_matches: dict[str, list[ClusterData]],
241
+ all_desired_clusters: dict[str, ClusterData],
123
242
  dry_run: bool,
124
243
  ) -> None:
125
- class ClusterData(BaseModel):
126
- """
127
- Helper structure for synching process
128
- """
129
-
130
- name: str
131
- server_url: str
132
- subscription_id: str
133
- label_default: FleetLabelDefaultV1
134
-
135
244
  all_current_cluster_ids = {cluster.cluster_id for cluster in spec.clusters}
136
- clusters: dict[str, list[ClusterData]] = defaultdict(list)
137
- for label_default in spec.label_defaults:
138
- match_subscription_labels = dict(label_default.match_subscription_labels)
139
- for cluster in ocm.discover_clusters_by_labels(
140
- labels=match_subscription_labels
141
- ):
142
- # TODO: ideally we filter on server side - see TODO in ocm.py
143
- if (
144
- match_subscription_labels.items()
145
- <= cluster.subscription_labels.items()
146
- ):
147
- clusters[cluster.cluster_id].append(
148
- ClusterData(
149
- label_default=label_default,
150
- subscription_id=cluster.subscription_id,
151
- name=cluster.name,
152
- server_url=cluster.server_url,
153
- )
154
- )
155
-
156
- cluster_with_duplicate_matches = {
157
- k: v for k, v in clusters.items() if len(v) > 1
158
- }
159
- for cluster_id, matches in cluster_with_duplicate_matches.items():
245
+ for cluster_id, matches in clusters_with_duplicate_matches.items():
160
246
  label_matches = "\n".join(
161
- str(m.label_default.match_subscription_labels) for m in matches
247
+ str(m.desired_label_default.match_subscription_labels) for m in matches
162
248
  )
163
249
  logging.error(
164
- f"Spec '{spec.name}': Cluster ID {cluster_id} is matched multiple times by different label matchers:\n{label_matches}"
250
+ f"[{spec.name}] Cluster ID {cluster_id} is matched multiple times by different label matchers:\n{label_matches}"
165
251
  )
166
252
  metrics.set_gauge(
167
253
  FleetLabelerDuplicateClusterMatchesGauge(
168
254
  integration=self.name,
169
255
  ocm_name=spec.ocm.name,
170
256
  ),
171
- len(cluster_with_duplicate_matches),
257
+ len(clusters_with_duplicate_matches),
172
258
  )
173
259
 
174
- all_desired_clusters = {k: v[0] for k, v in clusters.items() if len(v) == 1}
175
260
  clusters_to_add = [
176
261
  YamlCluster(
177
262
  cluster_id=cluster_id,
@@ -179,7 +264,7 @@ class FleetLabelerIntegration(QontractReconcileIntegration[NoParams]):
179
264
  name=cluster_info.name,
180
265
  server_url=cluster_info.server_url,
181
266
  subscription_labels_content=self._render_default_labels(
182
- template=cluster_info.label_default.subscription_label_template,
267
+ template=cluster_info.desired_label_default.subscription_label_template,
183
268
  labels=ocm.get_cluster_labels(cluster_id=cluster_id),
184
269
  ),
185
270
  )
@@ -223,4 +308,5 @@ class FleetLabelerIntegration(QontractReconcileIntegration[NoParams]):
223
308
  desired_content = self._render_yaml_file(
224
309
  current_content, cluster_ids_to_delete, sorted_clusters_to_add
225
310
  )
226
- vcs.open_merge_request(path=f"data{spec.path}", content=desired_content)
311
+ if not dry_run:
312
+ vcs.open_merge_request(path=f"data{spec.path}", content=desired_content)
@@ -1,4 +1,4 @@
1
1
  from reconcile.utils.semver_helper import make_semver
2
2
 
3
3
  QONTRACT_INTEGRATION = "fleet-labeler"
4
- QONTRACT_INTEGRATION_VERSION = make_semver(0, 1, 0)
4
+ QONTRACT_INTEGRATION_VERSION = make_semver(1, 0, 0)
@@ -10,7 +10,9 @@ from reconcile.utils.ocm.clusters import (
10
10
  discover_clusters_by_labels,
11
11
  )
12
12
  from reconcile.utils.ocm.labels import (
13
+ add_label,
13
14
  get_cluster_labels_for_cluster_id,
15
+ update_label,
14
16
  )
15
17
  from reconcile.utils.ocm.search_filters import Filter, FilterMode
16
18
  from reconcile.utils.ocm_base_client import (
@@ -24,9 +26,9 @@ Thin abstractions of reconcile.ocm module to reduce coupling.
24
26
 
25
27
  class Cluster(BaseModel):
26
28
  cluster_id: str
29
+ subscription_id: str
27
30
  server_url: str
28
31
  name: str
29
- subscription_id: str
30
32
  subscription_labels: dict[str, str]
31
33
 
32
34
  @staticmethod
@@ -85,3 +87,25 @@ class OCMClient:
85
87
 
86
88
  def get_cluster_labels(self, cluster_id: str) -> dict[str, str]:
87
89
  return get_cluster_labels_for_cluster_id(self._ocm_client, cluster_id)
90
+
91
+ def add_subscription_label(
92
+ self, subscription_id: str, key: str, value: str
93
+ ) -> None:
94
+ # TODO: move href into a utils function
95
+ add_label(
96
+ ocm_api=self._ocm_client,
97
+ label_container_href=f"/api/accounts_mgmt/v1/subscriptions/{subscription_id}/labels",
98
+ label=key,
99
+ value=value,
100
+ )
101
+
102
+ def update_subscription_label(
103
+ self, subscription_id: str, key: str, value: str
104
+ ) -> None:
105
+ # TODO: move href into a utils function
106
+ update_label(
107
+ ocm_api=self._ocm_client,
108
+ label_container_href=f"/api/accounts_mgmt/v1/subscriptions/{subscription_id}/labels",
109
+ label=key,
110
+ value=value,
111
+ )