Skip to content

Commit

Permalink
Fix kubectl.version() when cluster is not accessible
Browse files Browse the repository at this point in the history
Looks like `kubect version` change the behavior - if kubectl cannot
access the cluster, it output the expected json, but fails with non zero
error code.

This causes `drenv stop` to fail since it assumes that stopped cluster
is reported by `kubectl.version()` by reporting version info without
server version.

This issue is easy to reproduce:
1. Start example env
2. Stop one of the vms (using virt-manager or virsh)
3. Run `drenv stop envs/example.yaml`

drenv fails with:

    drenv.commands.Error: Command failed:
       command: ('kubectl', 'version', '--context', 'ex1', '--output', 'json')
       exitcode: 1
       output:
          {
            "clientVersion": {
              "major": "1",
              "minor": "27",
              "gitVersion": "v1.27.2",
              "gitCommit": "7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647",
              "gitTreeState": "clean",
              "buildDate": "2023-05-17T14:20:07Z",
              "goVersion": "go1.20.4",
              "compiler": "gc",
              "platform": "linux/amd64"
            },
            "kustomizeVersion": "v5.0.1"
          }
       error:
          Unable to connect to the server: dial tcp 192.168.39.232:8443:
          connect: no route to host

Fixed by treating the special case of failing with output as success (as
it should be) and returning the output to the caller. The caller can
detect that the cluster is not running since the "serverVersion" is
missing.

Fixes: RamenDR#817
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Jun 7, 2023
1 parent 41ca3d6 commit 5adcc92
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/drenv/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ def version(context=None, output=None):
APIServer.
"""
args = ["--output", output] if output else []
return _run("version", *args, context=context)
try:
return _run("version", *args, context=context)
except commands.Error as e:
# If kubectl provided output this is not really an error and the caller
# can use the output.
if e.output:
return e.output
raise


def config(*args, context=None):
Expand Down

0 comments on commit 5adcc92

Please sign in to comment.