Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[d8] Add d8 module options #80

Merged
merged 23 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
k8s.io/component-base v0.29.3
k8s.io/klog/v2 v2.120.1
k8s.io/kubectl v0.29.3
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -562,7 +563,6 @@ require (
k8s.io/klog v1.0.0 // indirect
k8s.io/kube-openapi v0.0.0-20240105020646-a37d4de58910 // indirect
k8s.io/metrics v0.29.3 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
kubevirt.io/api v1.2.0 // indirect
kubevirt.io/containerized-data-importer-api v1.57.0-alpha1 // indirect
kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect
Expand Down
54 changes: 54 additions & 0 deletions internal/platform/cmd/module/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 disable

import (
"fmt"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/operatemodule"

"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/flags"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
)

var disableLong = templates.LongDesc(`
Disable module in the ModuleConfig resource.

© Flant JSC 2025`)

func NewCommand() *cobra.Command {
disableCmd := &cobra.Command{
Use: "disable",
Short: "Disable module.",
Long: disableLong,
ValidArgs: []string{"module_name"},
SilenceErrors: true,
SilenceUsage: true,
RunE: disableModule,
}
flags.AddFlags(disableCmd.Flags())
return disableCmd
}

func disableModule(cmd *cobra.Command, moduleName []string) error {
err := operatemodule.OperateModule(cmd, moduleName[0], false)
if err != nil {
return fmt.Errorf("Error disable module: %w", err)
}
fmt.Printf("Module %s disabled\n", moduleName[0])
return err
}
54 changes: 54 additions & 0 deletions internal/platform/cmd/module/enable/enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 enable

import (
"fmt"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/operatemodule"

"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/flags"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
)

var enableLong = templates.LongDesc(`
Enable module in the ModuleConfig resource.

© Flant JSC 2025`)

func NewCommand() *cobra.Command {
enableCmd := &cobra.Command{
Use: "enable",
Short: "Enable module.",
Long: enableLong,
ValidArgs: []string{"module_name"},
SilenceErrors: true,
SilenceUsage: true,
RunE: enableModule,
}
flags.AddFlags(enableCmd.Flags())
return enableCmd
}

func enableModule(cmd *cobra.Command, moduleName []string) error {
err := operatemodule.OperateModule(cmd, moduleName[0], true)
if err != nil {
return fmt.Errorf("Error enable module: %w", err)
}
fmt.Printf("Module %s enabled\n", moduleName[0])
return err
}
29 changes: 29 additions & 0 deletions internal/platform/cmd/module/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 flags

import (
"github.com/spf13/pflag"
)

func AddFlags(flagSet *pflag.FlagSet) {
flagSet.StringP(
"editor", "e",
"vi",
"Your favourite editor.",
)
}
52 changes: 52 additions & 0 deletions internal/platform/cmd/module/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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/module/operatemodule"

"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/flags"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
)

var listLong = templates.LongDesc(`
List enabled modules in DKP.

© 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 {
err := operatemodule.OptionsModule(cmd, "list.yaml")
if err != nil {
return fmt.Errorf("Error list modules: %w", err)
}
return err
}
53 changes: 53 additions & 0 deletions internal/platform/cmd/module/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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 module

import (
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/enable"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/list"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/snapshots"
"github.com/deckhouse/deckhouse-cli/internal/platform/cmd/module/values"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"

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

var moduleLong = templates.LongDesc(`
Module options for Deckhouse Kubernetes Platform.

© Flant JSC 2025`)

func NewCommand() *cobra.Command {
moduleCmd := &cobra.Command{
Use: "module", Short: "Module options DKP",
Long: moduleLong,
}

moduleCmd.AddCommand(
enable.NewCommand(),
disable.NewCommand(),
list.NewCommand(),
values.NewCommand(),
snapshots.NewCommand(),
)

flags.AddFlags(moduleCmd.Flags())

return moduleCmd
}
113 changes: 113 additions & 0 deletions internal/platform/cmd/module/operatemodule/optionsmodule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package operatemodule

import (
"bytes"
"context"
"fmt"
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
"github.com/spf13/cobra"
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 OptionsModule(cmd *cobra.Command, pathFromOption 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)
}

const (
apiProtocol = "http"
apiEndpoint = "127.0.0.1"
apiPort = "9652"
modulePath = "module"
labelSelector = "leader=true"
namespace = "d8-system"
containerName = "deckhouse"
)

fullEndpointUrl := fmt.Sprintf("%s://%s:%s/%s/%s", apiProtocol, apiEndpoint, apiPort, modulePath, pathFromOption)
getApi := []string{"curl", fullEndpointUrl}
podName, err := getDeckhousePod(kubeCl, namespace, labelSelector, containerName)
executor, err := 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
}

func getDeckhousePod(kubeCl *kubernetes.Clientset, namespace string, labelSelector string, containerName 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
var containerFound bool
for _, c := range pod.Spec.Containers {
if c.Name == containerName {
containerFound = true
break
}
}
if !containerFound {
return "", fmt.Errorf("Container %q not found in pod %q", containerName, podName)
}
return podName, nil
}

func execInPod(config *rest.Config, kubeCl *kubernetes.Clientset, 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
}
Loading