-
Notifications
You must be signed in to change notification settings - Fork 7
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
9 changed files
with
335 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package operatepod | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/remotecommand" | ||
) | ||
|
||
func GetDeckhousePod(kubeCl kubernetes.Interface, namespace string, labelSelector string) (string, error) { | ||
pods, err := kubeCl.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ | ||
LabelSelector: labelSelector, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("Error listing pods: %w", err) | ||
} | ||
|
||
if len(pods.Items) == 0 { | ||
return "", fmt.Errorf("No pods found with the label: %s", labelSelector) | ||
} | ||
|
||
pod := pods.Items[0] | ||
podName := pod.Name | ||
return podName, nil | ||
} | ||
|
||
func ExecInPod(config *rest.Config, kubeCl kubernetes.Interface, getApi []string, podName string, namespace string, containerName string) (remotecommand.Executor, error) { | ||
scheme := runtime.NewScheme() | ||
parameterCodec := runtime.NewParameterCodec(scheme) | ||
if err := v1.AddToScheme(scheme); err != nil { | ||
return nil, fmt.Errorf("Failed to create parameter codec: %w", err) | ||
} | ||
|
||
req := kubeCl.CoreV1().RESTClient(). | ||
Post(). | ||
Resource("pods"). | ||
Name(podName). | ||
Namespace(namespace). | ||
SubResource("exec"). | ||
VersionedParams(&v1.PodExecOptions{ | ||
Command: getApi, | ||
Container: containerName, | ||
Stdin: false, | ||
Stdout: true, | ||
Stderr: true, | ||
TTY: false, | ||
}, parameterCodec) | ||
|
||
executor, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) | ||
if err != nil { | ||
return nil, fmt.Errorf("Creating SPDY executor for Pod %s: %v", podName, err) | ||
} | ||
return executor, nil | ||
} |
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,45 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
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 flags | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
//"os" | ||
) | ||
|
||
func AddFlags(flagSet *pflag.FlagSet) { | ||
//debug := false | ||
// | ||
//queryParams := []string{} | ||
|
||
//debug := operatequeue.OperateQueue(nil, nil, "list.text?showEmpty=true") | ||
//if p := os.Getenv("KUBECONFIG"); p != "" { | ||
// defaultKubeconfigPath = p | ||
//} | ||
//flagSet.BoolP( | ||
// "show-empty", | ||
// "e", | ||
// false, | ||
// "Show empty queues.", | ||
//) | ||
flagSet.BoolP( | ||
"show-empty", | ||
"e", | ||
false, | ||
"Show empty queues.", | ||
) | ||
} |
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,72 @@ | ||
/* | ||
Copyright 2025 Flant JSC | ||
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 list | ||
|
||
import ( | ||
"fmt" | ||
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/queue/flags" | ||
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/queue/operatequeue" | ||
"github.com/deckhouse/deckhouse-cli/internal/utilk8s" | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
var listLong = templates.LongDesc(` | ||
List enabled Deckhouse Kubernetes Platform modules. | ||
© Flant JSC 2025`) | ||
|
||
func NewCommand() *cobra.Command { | ||
listCmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List enabled modules.", | ||
Long: listLong, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
RunE: listModule, | ||
} | ||
flags.AddFlags(listCmd.Flags()) | ||
return listCmd | ||
} | ||
|
||
func listModule(cmd *cobra.Command, args []string) error { | ||
kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
config, kubeCl, err := utilk8s.SetupK8sClientSet(kubeconfigPath) | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
empty, err := cmd.Flags().GetBool("show-empty") | ||
if err != nil { | ||
return fmt.Errorf("Failed to get editor from --editor flag: %w", err) | ||
} | ||
|
||
pathFromOption := "list.text" | ||
if empty { | ||
pathFromOption = "list.text?showEmpty=true" | ||
} | ||
|
||
err = operatequeue.OperateQueue(config, kubeCl, pathFromOption) | ||
if err != nil { | ||
return fmt.Errorf("Error list queues: %w", err) | ||
} | ||
return err | ||
} |
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,63 @@ | ||
/* | ||
Copyright 2025 Flant JSC | ||
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 mainqueue | ||
|
||
import ( | ||
"fmt" | ||
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/queue/operatequeue" | ||
"github.com/deckhouse/deckhouse-cli/internal/utilk8s" | ||
|
||
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/flags" | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
var mainQueueLong = templates.LongDesc(` | ||
List enabled Deckhouse Kubernetes Platform modules. | ||
© Flant JSC 2025`) | ||
|
||
func NewCommand() *cobra.Command { | ||
listCmd := &cobra.Command{ | ||
Use: "main", | ||
Short: "List enabled modules.", | ||
Long: mainQueueLong, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
RunE: mainQueue, | ||
} | ||
flags.AddFlags(listCmd.Flags()) | ||
return listCmd | ||
} | ||
|
||
func mainQueue(cmd *cobra.Command, args []string) error { | ||
kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
config, kubeCl, err := utilk8s.SetupK8sClientSet(kubeconfigPath) | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
err = operatequeue.OperateQueue(config, kubeCl, "main.text") | ||
if err != nil { | ||
return fmt.Errorf("Error list queues: %w", err) | ||
} | ||
return err | ||
} |
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,43 @@ | ||
package operatequeue | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/operatepod" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/remotecommand" | ||
) | ||
|
||
func OperateQueue(config *rest.Config, kubeCl *kubernetes.Clientset, pathFromOption string) error { | ||
const ( | ||
apiProtocol = "http" | ||
apiEndpoint = "127.0.0.1" | ||
apiPort = "9652" | ||
queuePath = "queue" | ||
|
||
labelSelector = "leader=true" | ||
namespace = "d8-system" | ||
containerName = "deckhouse" | ||
) | ||
|
||
fullEndpointUrl := fmt.Sprintf("%s://%s:%s/%s/%s", apiProtocol, apiEndpoint, apiPort, queuePath, pathFromOption) | ||
getApi := []string{"curl", fullEndpointUrl} | ||
podName, err := operatepod.GetDeckhousePod(kubeCl, namespace, labelSelector) | ||
executor, err := operatepod.ExecInPod(config, kubeCl, getApi, podName, namespace, containerName) | ||
|
||
var stdout bytes.Buffer | ||
var stderr bytes.Buffer | ||
if err = executor.StreamWithContext( | ||
context.Background(), | ||
remotecommand.StreamOptions{ | ||
Stdout: &stdout, | ||
Stderr: &stderr, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", stdout.String()) | ||
return err | ||
} |
Oops, something went wrong.