generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
283 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package passthrough | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
passthroughTypes = []string{"workload", "wl", "clusterqueue", "cq", "localqueue", "lq"} | ||
passthroughCmds = []string{"get", "delete", "edit", "describe", "patch"} | ||
) | ||
|
||
func NewCommands() ([]*cobra.Command, error) { | ||
kubectlPath, err := exec.LookPath("kubectl") | ||
if err != nil { | ||
return nil, fmt.Errorf("pass-through commands are not available: %w, PATH=%q", err, os.Getenv("PATH")) | ||
} | ||
|
||
commands := make([]*cobra.Command, len(passthroughCmds)) | ||
for i, pCmd := range passthroughCmds { | ||
commands[i] = newCmd(kubectlPath, pCmd, passthroughTypes) | ||
} | ||
return commands, nil | ||
} | ||
|
||
func newCmd(kubectlPath string, command string, subcommands []string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: command, | ||
Short: fmt.Sprintf("Pass-through %q to kubectl", command), | ||
} | ||
|
||
for _, subcommand := range subcommands { | ||
cmd.AddCommand(newSubcommand(kubectlPath, command, subcommand)) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func newSubcommand(kubectlPath string, command, subcommand string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: subcommand, | ||
Short: fmt.Sprintf("Pass-through \"%s %s\" to kubectl", command, subcommand), | ||
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
// prepare the args | ||
args := os.Args | ||
args[0] = kubectlPath | ||
|
||
// go in kubectl | ||
return syscall.Exec(kubectlPath, args, os.Environ()) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
site/content/en/docs/reference/kubectl-kueue/commands/passthrough.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: "Pass-through commands" | ||
linkTitle: "Pass-through" | ||
date: 2024-05-14 | ||
weight: 100 | ||
description: > | ||
Commands delegated to `kubectl` | ||
--- | ||
|
||
### Usage: | ||
|
||
``` | ||
kubectl kueue <command> <type> [FLAGS] | ||
``` | ||
|
||
### Examples: | ||
|
||
```bash | ||
# Edit a local queue | ||
kubectl kueue edit localqueue my-local-queue | ||
|
||
# Delete a cluster queue | ||
kubectl kueue delete cq my-cluster-queue | ||
``` | ||
|
||
## Supported commands | ||
|
||
The following table includes a list of commands that are passed through to `kubectl`. | ||
|
||
| Name | Short | | ||
|------------|--------------------------------------------------------------------------------------------------| | ||
| get | Display one or many resources | ||
| describe | Show details of a specific resource or group of resources | ||
| edit | Edit a resource on the server | ||
| patch | Update fields of a resource | ||
| delete | Delete resources by file names, stdin, resources and names, or by resources and label selector | ||
|
||
## Resource types | ||
|
||
The following table includes a list of all the supported resource types and their abbreviated aliases: | ||
|
||
| Name | Short | API version | Namespaced | Kind | | ||
|--------------|-------|------------------------|------------|--------------| | ||
| workload | wl | kueue.x-k8s.io/v1beta1 | true | Workload | | ||
| clusterqueue | cq | kueue.x-k8s.io/v1beta1 | false | ClusterQueue | | ||
| localqueue | lq | kueue.x-k8s.io/v1beta1 | true | LocalQueue | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kueuectl | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/set" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/kueue/pkg/util/testing" | ||
"sigs.k8s.io/kueue/test/util" | ||
) | ||
|
||
func makePassThroughWorkload(ns string) client.Object { | ||
return testing.MakeWorkload("pass-through-wl", ns).Obj() | ||
} | ||
|
||
func makePassThroughCQ(_ string) client.Object { | ||
return testing.MakeClusterQueue("pass-through-cq").Obj() | ||
} | ||
|
||
func makePassThroughLQ(ns string) client.Object { | ||
return testing.MakeLocalQueue("pass-through-lq", ns).ClusterQueue("queue").Obj() | ||
} | ||
|
||
func setupEnv(c *exec.Cmd, kassetsPath string, kubeconfigPath string) { | ||
c.Env = os.Environ() | ||
cmdPath := os.Getenv("PATH") | ||
if cmdPath == "" { | ||
cmdPath = kassetsPath | ||
} else if !set.New(filepath.SplitList(cmdPath)...).Has(kassetsPath) { | ||
cmdPath = fmt.Sprintf("%s%c%s", kassetsPath, os.PathListSeparator, cmdPath) | ||
} | ||
|
||
c.Env = append(c.Env, "PATH="+cmdPath) | ||
c.Env = append(c.Env, "KUBECONFIG="+kubeconfigPath) | ||
} | ||
|
||
var _ = ginkgo.Describe("Kueuectl Pass-through", ginkgo.Ordered, ginkgo.ContinueOnFailure, func() { | ||
var ( | ||
ns *corev1.Namespace | ||
) | ||
|
||
ginkgo.BeforeEach(func() { | ||
ns = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "ns-"}} | ||
gomega.Expect(k8sClient.Create(ctx, ns)).To(gomega.Succeed()) | ||
}) | ||
|
||
ginkgo.AfterEach(func() { | ||
gomega.Expect(util.DeleteNamespace(ctx, k8sClient, ns)).To(gomega.Succeed()) | ||
}) | ||
|
||
ginkgo.DescribeTable("Pass-through commands", | ||
func(oType string, namespaced bool, makeObject func(ns string) client.Object, getPath string, expectGet string, patch string, expectGetAfterPatch string) { | ||
obj := makeObject(ns.Name) | ||
gomega.Expect(k8sClient.Create(ctx, obj)).To(gomega.Succeed()) | ||
key := client.ObjectKeyFromObject(obj) | ||
|
||
identityArgs := []string{oType, key.Name} | ||
if namespaced { | ||
identityArgs = append(identityArgs, "-n", key.Namespace) | ||
} | ||
|
||
// get it | ||
args := append([]string{"get"}, identityArgs...) | ||
args = append(args, fmt.Sprintf("-o=jsonpath='%s'", getPath)) | ||
cmd := exec.Command(kueuectlPath, args...) | ||
setupEnv(cmd, kassetsPath, kubeconfigPath) | ||
out, err := cmd.CombinedOutput() | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "%q", string(out)) | ||
gomega.Expect(string(out)).To(gomega.BeComparableTo(expectGet)) | ||
|
||
// patch it | ||
if patch != "" { | ||
args = append([]string{"patch"}, identityArgs...) | ||
args = append(args, "--type=merge", "-p", patch) | ||
cmd = exec.Command(kueuectlPath, args...) | ||
setupEnv(cmd, kassetsPath, kubeconfigPath) | ||
_, err = cmd.CombinedOutput() | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "%q", string(out)) | ||
|
||
args = append([]string{"get"}, identityArgs...) | ||
args = append(args, fmt.Sprintf("-o=jsonpath='%s'", getPath)) | ||
cmd = exec.Command(kueuectlPath, args...) | ||
setupEnv(cmd, kassetsPath, kubeconfigPath) | ||
out, err = cmd.CombinedOutput() | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "%q", string(out)) | ||
gomega.Expect(string(out)).To(gomega.BeComparableTo(expectGetAfterPatch)) | ||
} | ||
// delete it | ||
args = []string{"delete", oType} | ||
if namespaced { | ||
args = append(args, "-n", key.Namespace) | ||
} | ||
args = append(args, key.Name) | ||
cmd = exec.Command(kueuectlPath, args...) | ||
setupEnv(cmd, kassetsPath, kubeconfigPath) | ||
_, err = cmd.CombinedOutput() | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "%q", string(out)) | ||
|
||
gomega.Eventually(func() error { | ||
return k8sClient.Get(ctx, key, obj) | ||
}, util.Timeout, util.Interval).Should(testing.BeNotFoundError()) | ||
}, | ||
ginkgo.Entry("Workload", "workload", true, makePassThroughWorkload, "{.spec.active}", "'true'", `{"spec":{"active":false}}`, "'false'"), | ||
ginkgo.Entry("Workload(short)", "wl", true, makePassThroughWorkload, "{.spec.active}", "'true'", `{"spec":{"active":false}}`, "'false'"), | ||
ginkgo.Entry("Cluster Queue", "clusterqueue", false, makePassThroughCQ, "{.spec.stopPolicy}", "'None'", `{"spec":{"stopPolicy":"Hold"}}`, "'Hold'"), | ||
ginkgo.Entry("Cluster Queue(short)", "cq", false, makePassThroughCQ, "{.spec.stopPolicy}", "'None'", `{"spec":{"stopPolicy":"Hold"}}`, "'Hold'"), | ||
ginkgo.Entry("Local Queue", "localqueue", true, makePassThroughLQ, "{.spec.clusterQueue}", "'queue'", ``, ""), | ||
ginkgo.Entry("Local Queue (short)", "lq", true, makePassThroughLQ, "{.spec.clusterQueue}", "'queue'", ``, ""), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters