Untitled

 avatar
unknown
plain_text
a month ago
1.5 kB
4
Indexable
function setup_kubernetes() {
    log "Setting up Kubernetes access..."
    
    # First try environment variable
    if [ -z "${RUNAI_KUBECONFIG:-}" ]; then
        # Try to get from user's kubeconfig
        if [ -f "$HOME/.kube/config" ]; then
            export KUBECONFIG="$HOME/.kube/config"
        else
            handle_error "No valid kubeconfig found"
        fi
    else
        export KUBECONFIG="$RUNAI_KUBECONFIG"
    fi

    log "Using kubeconfig: $KUBECONFIG"

    # Extract token from kubeconfig if available
    if [ -f "$KUBECONFIG" ]; then
        TOKEN=$(kubectl config view --minify --flatten -o jsonpath='{.users[0].user.token}')
        if [ ! -z "$TOKEN" ]; then
            export KUBE_TOKEN="$TOKEN"
            log "Found and exported token from kubeconfig"
        else
            # Try to get token from oidc
            OIDC_TOKEN=$(kubectl config view --minify --flatten -o jsonpath='{.users[0].user.auth-provider.config.id-token}')
            if [ ! -z "$OIDC_TOKEN" ]; then
                export KUBE_TOKEN="$OIDC_TOKEN"
                log "Found and exported OIDC token"
            fi
        fi
    fi

    # Verify cluster access
    log "Verifying cluster access..."
    if ! kubectl cluster-info &> /dev/null; then
        handle_error "Unable to access Kubernetes cluster"
    fi

    # Create test namespace if it doesn't exist
    if ! kubectl get namespace "$NAMESPACE" &> /dev/null; then
        kubectl create namespace "$NAMESPACE"
    fi

    log "Kubernetes setup complete."
}
Editor is loading...
Leave a Comment