Skip to content

Commit 74d29f0

Browse files
Utkarsh-sharma47Utkarsh-sharma47leonardoce
authored
fix(psql): support Windows execution (#10972)
The psql plugin replaced the current process with a `kubectl exec` invocation through `syscall.Exec`, which is not available on Windows. As a result `kubectl cnpg psql` failed there with "not supported by windows". Execution is now platform-specific: Unix-like systems keep using `syscall.Exec`, while Windows launches `kubectl exec` as a child process via `exec.Command`. Closes #10553 Signed-off-by: Utkarsh-sharma47 <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Co-authored-by: Utkarsh-sharma47 <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]>
1 parent 1dc7d6a commit 74d29f0

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright © contributors to CloudNativePG, established as
5+
CloudNativePG a Series of LF Projects, LLC.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
SPDX-License-Identifier: Apache-2.0
20+
*/
21+
22+
package psql
23+
24+
import (
25+
"os"
26+
"syscall"
27+
)
28+
29+
// execKubectl replaces the current process with kubectl.
30+
// This function does not return on success.
31+
func execKubectl(kubectlPath string, kubectlExec []string) error {
32+
return syscall.Exec(kubectlPath, kubectlExec, os.Environ()) // #nosec
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//go:build windows
2+
3+
/*
4+
Copyright © contributors to CloudNativePG, established as
5+
CloudNativePG a Series of LF Projects, LLC.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
SPDX-License-Identifier: Apache-2.0
20+
*/
21+
22+
package psql
23+
24+
import (
25+
"errors"
26+
"os"
27+
"os/exec"
28+
)
29+
30+
// execKubectl runs kubectl as a child process with interactive stdio.
31+
// On success it exits the process with kubectl's exit code, matching
32+
// the Unix syscall.Exec behavior as closely as possible.
33+
func execKubectl(kubectlPath string, kubectlExec []string) error {
34+
cmd := exec.Command(kubectlPath, kubectlExec[1:]...) //nolint:gosec
35+
cmd.Stdin = os.Stdin
36+
cmd.Stdout = os.Stdout
37+
cmd.Stderr = os.Stderr
38+
39+
if err := cmd.Run(); err != nil {
40+
var exitErr *exec.ExitError
41+
if errors.As(err, &exitErr) {
42+
os.Exit(exitErr.ExitCode())
43+
}
44+
return err
45+
}
46+
47+
os.Exit(0)
48+
return nil
49+
}

‎internal/cmd/plugin/psql/psql.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os"
2626
"os/exec"
2727
"slices"
28-
"syscall"
2928

3029
corev1 "k8s.io/api/core/v1"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -160,15 +159,15 @@ func (psql *Command) getPodName() (string, error) {
160159
return "", &ErrMissingPod{role: targetPodRole}
161160
}
162161

163-
// Exec replaces the current process with a `kubectl Exec` invocation.
164-
// This function won't return
162+
// Exec invokes `kubectl exec` and transfers control to it.
163+
// This function does not return on success.
165164
func (psql *Command) Exec() error {
166165
kubectlExec, err := psql.getKubectlInvocation()
167166
if err != nil {
168167
return err
169168
}
170169

171-
err = syscall.Exec(psql.kubectlPath, kubectlExec, os.Environ()) // #nosec
170+
err = execKubectl(psql.kubectlPath, kubectlExec)
172171
if err != nil {
173172
return err
174173
}

0 commit comments

Comments
 (0)