7
7
import re
8
8
from azure .cli .core .azclierror import (ValidationError , ResourceNotFoundError , InvalidArgumentValueError ,
9
9
MutuallyExclusiveArgumentError )
10
- from msrestazure .tools import is_valid_resource_id
10
+ from msrestazure .tools import is_valid_resource_id , parse_resource_id
11
11
from knack .log import get_logger
12
12
13
- from ._clients import ContainerAppClient
13
+ from ._clients import ContainerAppClient , ManagedEnvironmentClient
14
14
from ._ssh_utils import ping_container_app
15
15
from ._utils import safe_get , is_registry_msi_system
16
16
from ._constants import ACR_IMAGE_SUFFIX , LOG_TYPE_SYSTEM
@@ -148,6 +148,31 @@ def validate_allow_insecure(namespace):
148
148
raise ValidationError ("Usage error: --allow-insecure is not supported for TCP ingress" )
149
149
150
150
151
+ def _ping_containerapp_if_need (cmd , app ) -> (bool , str ):
152
+ # if container app has no ingress or is internal, not need to ping
153
+ if safe_get (app , "properties" , "configuration" , "ingress" ) is None or safe_get (app , "properties" , "configuration" , "ingress" , "external" ) is False :
154
+ return False , None
155
+
156
+ parsed_env = parse_resource_id (safe_get (app , "properties" , "environmentId" ))
157
+ env_name = parsed_env ['name' ]
158
+ env_rg = parsed_env ['resource_group' ]
159
+ env = ManagedEnvironmentClient .show (cmd , env_rg , env_name )
160
+ # if environment is internal, not need to ping
161
+ if safe_get (env , "properties" , "vnetConfiguration" , "internal" ) is True :
162
+ return False , None
163
+
164
+ try :
165
+ # ping containerapp to activate its replica
166
+ ping_container_app (app ) # needed to get an alive replica
167
+ except Exception as ex : # pylint: disable=broad-except
168
+ warning_message = f"Error encountered while attempting to ping container app.\n " \
169
+ f"Error message: '{ str (ex )} '\n " \
170
+ f"Please ensure there is an alive replica. "
171
+ return True , warning_message
172
+
173
+ return False , None
174
+
175
+
151
176
def _set_ssh_defaults (cmd , namespace ):
152
177
app = ContainerAppClient .show (cmd , namespace .resource_group_name , namespace .name )
153
178
if not app :
@@ -158,16 +183,14 @@ def _set_ssh_defaults(cmd, namespace):
158
183
if not namespace .revision :
159
184
raise ResourceNotFoundError ("Could not find a revision" )
160
185
if not namespace .replica :
161
- # VVV this may not be necessary according to Anthony Chu
162
- try :
163
- ping_container_app (app ) # needed to get an alive replica
164
- except Exception as e : # pylint: disable=broad-except
165
- logger .warning ("Failed to ping container app with error '%s' \n Please ensure there is an alive replica. " , str (e ))
186
+ failed_to_ping , warning_message = _ping_containerapp_if_need (cmd , app )
166
187
replicas = ContainerAppClient .list_replicas (cmd = cmd ,
167
188
resource_group_name = namespace .resource_group_name ,
168
189
container_app_name = namespace .name ,
169
190
revision_name = namespace .revision )
170
191
if not replicas :
192
+ if failed_to_ping :
193
+ logger .warning (warning_message )
171
194
raise ResourceNotFoundError ("Could not find a replica for this app" )
172
195
namespace .replica = replicas [0 ]["name" ]
173
196
if not namespace .container :
0 commit comments