1818import time
1919from collections import OrderedDict
2020from multiprocessing import Process
21- # pylint: disable=E0401
22- from packaging import version as packaging_version # type: ignore
2321
2422RLEC_CONTAINER_NAME = "redis-enterprise-node"
2523OPERATOR_LABEL = "app=redis-enterprise"
2927
3028RS_LOG_FOLDER_PATH = "/var/opt/redislabs/log"
3129logger = logging .getLogger (__name__ )
32- VERSION_LOG_COLLECTOR = "6.2.18-3a "
30+ VERSION_LOG_COLLECTOR = "6.2.18-41 "
3331
3432TIME_FORMAT = time .strftime ("%Y%m%d-%H%M%S" )
3533
8280 "StorageClass"
8381]
8482
83+ SHA_DIGESTS_BEFORE_RESTRICTED_MODE_SUPPORT = [
84+ "0f144922ea1e2d4ea72affb36238258c9f21c39d6ba9ad73da79278dde1eed37" ,
85+ "97ffbde86f27810b1a5e6fee7ec53683f49b650cc33c327696be66d04e10bf31" ,
86+ "53b008cecf3807d51f027f21029b63dc5ad8c002c5278554ce79c008f1b97bbb" ,
87+ "b771ef87bf211c17c37df028c202aac97170fb6d7d5d49b3ccb3410deb8212f6" ,
88+ "2a033d4a4ccabb4963116add69fc8e91770ee627f6b974879d8dd7ddddebce47"
89+ ]
90+
8591
8692def make_dir (directory ):
8793 """
@@ -196,41 +202,92 @@ def detect_k8s_cli(k8s_cli_input=""):
196202 return DEFAULT_K8S_CLI
197203
198204
199- def get_operator_version (k8s_cli , namespaces ):
205+ def parse_operator_deployment (k8s_cli , namespaces ):
200206 """
201207 Compare operator version with the current log_collector version.
202208 """
203209 for namespace in namespaces :
204- cmd = "{} get deployment redis-enterprise-operator -o jsonpath=" \
205- "\" {{.spec.template.spec.containers[0].image}}\" -n {}" .format (k8s_cli , namespace )
206- return_code , out = run_shell_command (cmd )
207- if return_code or not out :
208- continue
209- operator_version = str .split (out , ":" )
210- if len (operator_version ) == 2 :
211- logger .info ("running with operator version: %s and log collector version: %s" ,
212- operator_version [1 ], VERSION_LOG_COLLECTOR )
213- return operator_version [1 ]
210+ try :
211+ cmd = "{} get deployment redis-enterprise-operator -o jsonpath=" \
212+ "\" {{.spec.template.spec.containers[0].image}}\" -n {}" .format (k8s_cli , namespace )
213+ return_code , out = run_shell_command (cmd )
214+ if return_code or not out :
215+ continue
216+
217+ uses_sha = "@sha256" in out
218+
219+ operator_version = str .split (out , ":" )
220+ if len (operator_version ) == 2 :
221+ logger .info ("running with operator version: %s and log collector version: %s" ,
222+ operator_version [1 ], VERSION_LOG_COLLECTOR )
223+ return operator_version [1 ], uses_sha
224+ # pylint: disable=W0703
225+ except Exception :
226+ logger .info ("Failed to parse operator deployment, ignoring ns %s" , namespace )
214227 logger .info ("could not find operator version" )
215- return ""
228+ return "" , False
216229
217230
218- def validate_mode (mode , operator_version ):
231+ def validate_mode (mode , operator_tag , is_sha_digest ):
219232 """
220233 for old versions there is no way to use restricted because resources are missing labels
221234 """
222- if mode == MODE_RESTRICTED and packaging_version . parse ( operator_version ) < packaging_version . parse (
223- FIRST_VERSION_SUPPORTING_RESTRICTED ) :
235+ version_supports_restricted = check_if_tag_supports_restricted ( operator_tag , is_sha_digest )
236+ if mode == MODE_RESTRICTED and not version_supports_restricted :
224237 raise ValueError ("{} is not supported for this version, please use {}" .format (MODE_RESTRICTED , MODE_ALL ))
225238
226239
227- def determine_default_mode (operator_version ):
240+ def is_old_sha (operator_tag ):
241+ """
242+ Check if the sha of the operator is older than the first that supports restricted
243+ Note - we only started using digests recently so there is no need to list all of them
244+ """
245+ return operator_tag in SHA_DIGESTS_BEFORE_RESTRICTED_MODE_SUPPORT
246+
247+
248+ def check_if_tag_supports_restricted (operator_tag , is_sha_digest ):
249+ """
250+ Handle both sha tags and versions and check if they support restricted
251+ """
252+ version_supports_restricted = True
253+ if is_sha_digest :
254+ if is_old_sha (operator_tag ):
255+ version_supports_restricted = False
256+ else :
257+ version_supports_restricted = check_if_version_supports_restricted (operator_tag )
258+ return version_supports_restricted
259+
260+
261+ def check_if_version_supports_restricted (operator_version ):
262+ """
263+ Compare the version to 6.2.18-3 which is the first version supporting restricted
264+ Note - apparently there is no function in Python that is built in and supports that
265+ """
266+ try :
267+ the_version = operator_version .split ("-" )[0 ]
268+
269+ parts = the_version .split ("." )
270+ if int (parts [0 ]) < 6 :
271+ return False
272+ if int (parts [0 ]) >= 7 :
273+ return True
274+ if int (parts [1 ]) > 2 :
275+ return True
276+ if int (parts [1 ]) < 2 :
277+ return False
278+ return int (parts [2 ]) >= 18
279+ # pylint: disable=W0703
280+ except Exception :
281+ logger .info ("issues parsing version %s" , operator_version )
282+ return True
283+
284+
285+ def determine_default_mode (operator_tag , is_sha_digest ):
228286 """
229- check the version of the operator (if it is running)
230- the default mode is ALL before 6.2.18 and RESTRICTED afterwards
287+ determine the default mode based on the version/sha digest
231288 """
232- if operator_version == "" or packaging_version . parse ( operator_version ) >= packaging_version . parse (
233- FIRST_VERSION_SUPPORTING_RESTRICTED ) :
289+ version_supports_restricted = check_if_tag_supports_restricted ( operator_tag , is_sha_digest )
290+ if operator_tag == "" or version_supports_restricted :
234291 return MODE_RESTRICTED
235292
236293 return MODE_ALL
@@ -255,11 +312,11 @@ def run(results):
255312 TIMEOUT = results .timeout
256313
257314 mode = results .mode
258- operator_version = get_operator_version (k8s_cli , namespaces )
315+ operator_tag , is_sha_digest = parse_operator_deployment (k8s_cli , namespaces )
259316 if mode :
260- validate_mode (mode , operator_version )
317+ validate_mode (mode , operator_tag , is_sha_digest )
261318 else :
262- mode = determine_default_mode (operator_version )
319+ mode = determine_default_mode (operator_tag , is_sha_digest )
263320
264321 api_resources = RESTRICTED_MODE_API_RESOURCES
265322 if mode == MODE_ALL :
0 commit comments