devspace-sh / devspace

DevSpace - The Fastest Developer Tool for Kubernetes ⚡ Automate your deployment workflow with DevSpace and develop software directly inside Kubernetes.
https://devspace.sh
Apache License 2.0
4.17k stars 351 forks source link

Helm command failure: Kubernetes cluster unreachable: context does not exist #2820

Closed satishweb closed 2 months ago

satishweb commented 3 months ago

What happened?

devspace dev command failed with below error:

$ devspace dev
info Using namespace 'devspace-myname'
info Using kube context 'my-aws-k8s-context-name'
deploy:dev Deploying chart  (devspace-myname) with helm...
create_deployments: error deploying dev: unable to deploy helm chart:  error executing 'helm upgrade devspace-myname --values /tmp/38929297 --install --namespace devspace-myname /path/to/my/chart/ --kube-context my-aws-k8s-context-name': Error: Kubernetes cluster unreachable: context "my-aws-k8s-context-name" does not exist
fatal exit status 1

What did you expect to happen instead?
devspace dev successfully executing helm command to install the helm chart

How can we reproduce the bug? (as minimally and precisely as possible)

  1. Copy this devspace.yaml into your project
  2. Run command devspace dev

My devspace.yaml:

version: v2beta1
name: test
vars:
  IMAGE: "satishweb/devops-tools"
  TAG: "latest"
  OWNER:
    source: "command"
    command: "/bin/bash"
    args:
      - "-c"
      - "git config user.name | awk '{print tolower($0)}'|tr -d ' '"
  WORK_DIR: "/work/source"
  NAMESPACE:
    source: "command"
    command: "/bin/bash"
    args:
      - "-c"
      - "git config user.name | awk '{print tolower($0)}'|tr -d ' '|sed 's/^/devspace-/'"
  FIXUID: "1000"
  FIXGID: "1000"
  HOME: "/work"

deployments:
  dev:
    helm:
      chart:
        name: "devspace"
        repo: "oci://ghcr.io/luminartech/helm-charts-public"
        version: "6.3.12-0"
      releaseName: "devspace-${OWNER}"
      displayOutput: true
      values:
        global:
          IAMRole: arn:aws:iam::123456789012:role/iam-role
          persistentDataDiskSize: 10Gi
          storageClassName: gp3-persistent-125
          images:
            devcontainer: "${IMAGE}:${TAG}"
          env:
            FIXUID: "${FIXUID}"
            FIXGID: "${FIXGID}"
            HOME: "${HOME}"
        service:
          ports:
            - port: 8080
    namespace: ${NAMESPACE}

dev:
  dev:
    command: ["sleep","infinity"] # 8 hours
    devImage: "${IMAGE}:${TAG}"
    labelSelector:
      app.kubernetes.io/part-of: devspace-${OWNER}
      app.kubernetes.io/instance: devspace-${OWNER}
    namespace: "${NAMESPACE}"
    workingDir: ${WORK_DIR}
    logs:
      enabled: true

    attach:
      enabled: false

    ssh:
      enabled: false

    proxyCommands:
      - gitCredentials: true
      - command: "git"
      - command: "gh"
    ports:
      - port: "8080:8080"

    env:
      - name: "OWNER"
        value: "${OWNER}"
      - name : "FIXUID"
        value: "${FIXUID}"
      - name : "FIXGID"
        value: "${FIXGID}"
      - name: "WORK_DIR"
        value: "${WORK_DIR}"
      - name: "NAMESPACE"
        value: "${NAMESPACE}"
      - name: "HOME"
        value: "${HOME}"

    sync:
      - path: "~/.kube/config:${HOME}/.kube/config"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.saml2aws:${HOME}/.saml2aws"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.aws:${HOME}/.aws"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
      - path: "~/.gitconfig:${HOME}/.gitconfig"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.git-credentials:${HOME}/.git-credentials"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.vim:${HOME}/.vim"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
      - path: "~/.tmux.conf:${HOME}/.tmux.conf"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.zsh_history:${HOME}/.zsh_history"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.docker/config.json:${HOME}/.docker/config.json"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
        file: true
      - path: "~/.ssh:${HOME}/.ssh"
        onUpload:
          restartContainer: false
        initialSyncCompareBy: "mtime" # mtime or size
      - path: "${WORK_DIR}:${HOME}/source"
        printLogs: true
        excludePaths:
          - ".git/"
        downloadExcludePaths:
          - ".cache"
        uploadExcludePaths:
          - ".cache"
        startContainer: true
        onUpload:
          restartContainer: false
          exec:
            - name: "Echo Updated Action"
              command: "echo"
              args: ["Files synced from developer's system"]
              onChange:
                - "**/*"
        initialSync: "preferLocal"
        waitInitialSync: true
        initialSyncCompareBy: "mtime" # mtime or size

    terminal:
      command: "./devspace_start.sh"
      workDir: "${WORK_DIR}"
      enabled: true
      disableReplace: true

    resources:
      requests:
        cpu: "4"
        memory: "8Gi"
      limits:
        cpu: "4"
        memory: "8Gi"

Local Environment:

Anything else we need to know?
Apparently, failure was caused by use of HOME var declaration. Renaming HOME to CONTAINER_HOME solved the problem.

It took a while to figure out the root cause. If --debug flag was passed to the helm command, it would have been more easier to find the root cause.

deniseschannon commented 2 months ago

Closing in favor of https://github.com/devspace-sh/devspace/issues/2846

satishweb commented 2 months ago

Root cause: HOME variable was declared in the devspace.yaml and that caused changes to the devspace runtime environment. Removing HOME variable resolved the error

If the debug flag was passed to the helm command, this root cause could have been clearer. #2846 is added for this feature request.