kubectl exec의 반환 값을 powershell 스크립트로 가져옵니다.

kubectl exec의 반환 값을 powershell 스크립트로 가져옵니다.

그래서 저는 탱글 테스트를 실행하는 powershell 스크립트를 개발 중입니다. 이 스크립트는 Mongo 데이터베이스가 있는 Kubernetes Pod에 연결됩니다. 목표는 데이터베이스의 컬렉션이 비어 있는지 확인하는 것입니다. "반환 횟수" 줄 앞의 코드에 만족합니다. 쉘에 return 명령이 없다는 것을 알고 있지만 설명을 위해 넣었습니다.

저는 본질적으로 "kubectl exec"의 "count" 값을 powershell 코드로 가져오려고 합니다. 가능합니까?

Context "Foo collection" {
    It "should have no documents"{

        kubectl exec -it $podName -n mongo `
            -- mongosh -u root -p $mongoSecret `
            --eval "`
            db = db.getSiblingDB('thisOne')
            collection = db.getCollection('foo')
            count = collection.countDocuments({}, {limit: 1})

            return count
        "

        $docs = count
        $docs | Should -Be 0
    }
}

답변1

나는 또한 이것을 stackoverflow에 게시했는데 여기에서 해결책을 찾을 수 있는 팁을 얻었습니다.https://stackoverflow.com/questions/73175179/get-return-value-from-kubectl-exec-out-into-powershell-script/73226204#73226204,지금 바로:

반환 값을 kubectl변수에 저장하면 됩니다. eval스크립트에서 최종 명령의 반환 결과를 다음으로 출력합니다.표준 출력. 그러나 --quietmongodb 쉘 단락의 "노이즈" 없이 반환 값을 얻으려면 이 매개 변수가 필요합니다.

Context "Foo collection" {
    It "should have no documents"{

        $count = kubectl exec -it $podName -n mongo `
            -- mongosh --quiet -u root -p $mongoSecret `
            --eval "`
            db = db.getSiblingDB('thisOne')
            collection = db.getCollection('foo')
            collection.countDocuments({}, {limit: 1})
            "

        $count | Should -Be 0
    }
}

관련 정보