Skip to content

Commit 4d30bef

Browse files
ricolinwaipeng
authored andcommitted
Allow update cluster status with admin context
Trust token can be deleted outside of magnum, But when trust token not found, the periodic update status job will stay in inprogress unless another cluster action triggered. Propose to use admin context when trust can not be found in periodic update status job. Story: 2010232 Task: 46031 Change-Id: I9cc9a0e654fb26ebec517e3413a592ac6613777c (cherry picked from commit 1ed78a4)
1 parent 46e1062 commit 4d30bef

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

magnum/drivers/common/driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def get_driver_for_cluster(cls, context, cluster):
141141
context, cluster.cluster_template_id)
142142
return cls.get_driver(ct.server_type, ct.cluster_distro, ct.coe)
143143

144-
def update_cluster_status(self, context, cluster):
144+
def update_cluster_status(self, context, cluster, use_admin_ctx=False):
145145
"""Update the cluster status based on underlying orchestration
146146
147147
This is an optional method if your implementation does not need

magnum/drivers/heat/driver.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ def delete_nodegroup(self, context, cluster, nodegroup):
120120
osc = clients.OpenStackClients(context)
121121
self._delete_stack(context, osc, nodegroup.stack_id)
122122

123-
def update_cluster_status(self, context, cluster):
123+
def update_cluster_status(self, context, cluster, use_admin_ctx=False):
124124
if cluster.stack_id is None:
125125
# NOTE(mgoddard): During cluster creation it is possible to poll
126126
# the cluster before its heat stack has been created. See bug
127127
# 1682058.
128128
return
129-
stack_ctx = mag_ctx.make_cluster_context(cluster)
129+
if use_admin_ctx:
130+
stack_ctx = context
131+
else:
132+
stack_ctx = mag_ctx.make_cluster_context(cluster)
130133
poller = HeatPoller(clients.OpenStackClients(stack_ctx), context,
131134
cluster, self)
132135
poller.poll_and_check()

magnum/service/periodic.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pycadf import cadftaxonomy as taxonomy
2424

2525
from magnum.common import context
26+
from magnum.common import exception
2627
from magnum.common import profiler
2728
from magnum.common import rpc
2829
from magnum.conductor import monitors
@@ -68,7 +69,19 @@ def update_status(self):
6869
# get the driver for the cluster
6970
cdriver = driver.Driver.get_driver_for_cluster(self.ctx, self.cluster)
7071
# ask the driver to sync status
71-
cdriver.update_cluster_status(self.ctx, self.cluster)
72+
try:
73+
cdriver.update_cluster_status(self.ctx, self.cluster)
74+
except exception.AuthorizationFailure as e:
75+
trust_ex = ("Could not find trust: %s" % self.cluster.trust_id)
76+
# Try to use admin context if trust not found.
77+
# This will make sure even with trust got deleted out side of
78+
# Magnum, we still be able to check cluster status
79+
if trust_ex in str(e):
80+
cdriver.update_cluster_status(
81+
self.ctx, self.cluster, use_admin_ctx=True)
82+
else:
83+
raise
84+
7285
LOG.debug("Status for cluster %s updated to %s (%s)",
7386
self.cluster.id, self.cluster.status,
7487
self.cluster.status_reason)

magnum/tests/unit/service/test_periodic.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from oslo_utils import uuidutils
1818

1919
from magnum.common import context
20+
from magnum.common import exception
2021
from magnum.common.rpc_service import CONF
2122
from magnum.db.sqlalchemy import api as dbapi
2223
from magnum.drivers.common import driver
@@ -189,6 +190,32 @@ def _mock_update_status(context, cluster):
189190
self.mock_driver.update_cluster_status.side_effect = (
190191
_mock_update_status)
191192

193+
@mock.patch('magnum.drivers.common.driver.Driver.get_driver_for_cluster')
194+
def test_update_status_non_trusts_error(self, mock_get_driver):
195+
mock_get_driver.return_value = self.mock_driver
196+
trust_ex = ("Unknown Keystone error")
197+
self.mock_driver.update_cluster_status.side_effect = \
198+
exception.AuthorizationFailure(client='keystone', message=trust_ex)
199+
self.assertRaises(
200+
exception.AuthorizationFailure,
201+
periodic.ClusterUpdateJob(
202+
self.context, self.cluster1).update_status
203+
)
204+
self.assertEqual(1, self.mock_driver.update_cluster_status.call_count)
205+
206+
@mock.patch('magnum.drivers.common.driver.Driver.get_driver_for_cluster')
207+
def test_update_status_trusts_not_found(self, mock_get_driver):
208+
mock_get_driver.return_value = self.mock_driver
209+
trust_ex = ("Could not find trust: %s" % self.cluster1.trust_id)
210+
self.mock_driver.update_cluster_status.side_effect = \
211+
exception.AuthorizationFailure(client='keystone', message=trust_ex)
212+
self.assertRaises(
213+
exception.AuthorizationFailure,
214+
periodic.ClusterUpdateJob(
215+
self.context, self.cluster1).update_status
216+
)
217+
self.assertEqual(2, self.mock_driver.update_cluster_status.call_count)
218+
192219
@mock.patch('oslo_service.loopingcall.FixedIntervalLoopingCall',
193220
new=fakes.FakeLoopingCall)
194221
@mock.patch('magnum.drivers.common.driver.Driver.get_driver_for_cluster')

0 commit comments

Comments
 (0)