Improve handling kubectl errors

Implementation is correcting unexpected behavior. Before, any warning
in the "kubectl apply/delete -k" command aborted the apply process,
even if the command had been successfully executed. Now, the warning
will be shown, but it will not cancel the apply process.

Test Plan:
PASS: Upload/apply/remove/delete kubevirt-app
PASS: Upload/apply/remove/delete metrics-server
PASS: Upload/apply/remove/delete vault
PASS: If "kubectl apply -k" generates a warning the apply process
is not aborted
PASS: If "kubectl apply -k" generates an error, the apply process
is aborted

Closes-bug: 2025511

Change-Id: Ief4bab661aed29ba51f6ea75a1582109741d3599
Signed-off-by: David Barbosa Bastos <david.barbosabastos@windriver.com>
This commit is contained in:
David Barbosa Bastos 2023-07-17 15:11:16 -03:00
parent 31257a7bc2
commit b5db54b0d9
2 changed files with 30 additions and 24 deletions

View File

@ -1834,6 +1834,11 @@ APP_UPDATE_IN_PROGRESS = 'updating'
APP_RECOVER_IN_PROGRESS = 'recovering'
APP_RESTORE_REQUESTED = 'restore-requested'
# Kubectl kustomize operations
KUBECTL_KUSTOMIZE_APPLY = 'apply'
KUBECTL_KUSTOMIZE_DELETE = 'delete'
KUBECTL_KUSTOMIZE_VALIDATE = 'validate'
# Operation constants
APP_VALIDATE_OP = 'validate'
APP_UPLOAD_OP = 'upload'

View File

@ -3867,39 +3867,40 @@ class FluxCDHelper(object):
rc = False
return rc
def _apply(self, manifest_dir):
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
'apply', '-k', manifest_dir]
_, stderr = cutils.trycmd(*cmd)
def run_kubectl_kustomize(self, operation_type, manifest_dir):
if operation_type == constants.KUBECTL_KUSTOMIZE_VALIDATE:
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
constants.KUBECTL_KUSTOMIZE_APPLY, '-k', manifest_dir, '--dry-run=server']
elif operation_type == constants.KUBECTL_KUSTOMIZE_DELETE:
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
operation_type, '-k', manifest_dir, '--ignore-not-found=true']
else:
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
operation_type, '-k', manifest_dir]
if stderr:
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
_, stderr = process.communicate()
if process.returncode == 0 and stderr:
LOG.warning("Command: %s; %s" % (' '.join(cmd), stderr))
if process.returncode != 0:
LOG.error("Command: %s; Error: %s" % (' '.join(cmd), stderr))
return False
return True
def _apply(self, manifest_dir):
return self.run_kubectl_kustomize(constants.KUBECTL_KUSTOMIZE_APPLY, manifest_dir)
def _delete(self, manifest_dir):
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
'delete', '-k', manifest_dir, '--ignore-not-found=true']
_, stderr = cutils.trycmd(*cmd)
if stderr:
LOG.error("Command: %s; Error: %s" % (' '.join(cmd), stderr))
return False
return True
return self.run_kubectl_kustomize(constants.KUBECTL_KUSTOMIZE_DELETE, manifest_dir)
def _validate(self, manifest_dir):
cmd = ['kubectl', '--kubeconfig', kubernetes.KUBERNETES_ADMIN_CONF,
'apply', '-k', manifest_dir, '--dry-run=server']
_, stderr = cutils.trycmd(*cmd)
if stderr:
LOG.error("Command: %s; Error: %s" % (' '.join(cmd), stderr))
return False
return True
return self.run_kubectl_kustomize(constants.KUBECTL_KUSTOMIZE_VALIDATE, manifest_dir)
def _rollback(self, manifest_dir):
pass