Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
d2285 committed Jan 30, 2025
1 parent d5ddccc commit 2f61e8d
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 52 deletions.
54 changes: 3 additions & 51 deletions internal/platform/cmd/module/operatemodule/optionsmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"bytes"
"context"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"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"
Expand All @@ -26,8 +24,8 @@ func OptionsModule(config *rest.Config, kubeCl kubernetes.Interface, pathFromOpt

fullEndpointUrl := fmt.Sprintf("%s://%s:%s/%s/%s", apiProtocol, apiEndpoint, apiPort, modulePath, pathFromOption)
getApi := []string{"curl", fullEndpointUrl}
podName, err := getDeckhousePod(kubeCl, namespace, labelSelector)
executor, err := execInPod(config, kubeCl, getApi, podName, namespace, containerName)
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
Expand All @@ -43,49 +41,3 @@ func OptionsModule(config *rest.Config, kubeCl kubernetes.Interface, pathFromOpt
fmt.Printf("%s\n", stdout.String())
return err
}

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
}
58 changes: 58 additions & 0 deletions internal/platform/cmd/operatepod/operatepod.go
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
}
2 changes: 2 additions & 0 deletions internal/platform/cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module"
queue "github.com/deckhouse/deckhouse-cli/internal/platform/cmd/queue"
"github.com/deckhouse/deckhouse-cli/internal/platform/flags"
)

Expand All @@ -42,6 +43,7 @@ func NewCommand() *cobra.Command {
platformCmd.AddCommand(
edit.NewCommand(),
module.NewCommand(),
queue.NewCommand(),
)

flags.AddPersistentFlags(platformCmd)
Expand Down
45 changes: 45 additions & 0 deletions internal/platform/cmd/queue/flags/flags.go
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.",
)
}
72 changes: 72 additions & 0 deletions internal/platform/cmd/queue/list/list.go
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
}
63 changes: 63 additions & 0 deletions internal/platform/cmd/queue/mainqueue/mainqueue.go
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
}
43 changes: 43 additions & 0 deletions internal/platform/cmd/queue/operatequeue/operatequeue.go
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
}
Loading

0 comments on commit 2f61e8d

Please sign in to comment.