-
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.
Signed-off-by: Dmitry Denisenko <dmitry.denisenko@flant.com>
- Loading branch information
Showing
9 changed files
with
476 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
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 cmd | ||
|
||
import ( | ||
edit "github.com/deckhouse/deckhouse-cli/internal/platform/cmd" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(platform.NewCommand()) | ||
} |
51 changes: 51 additions & 0 deletions
51
internal/platform/cmd/edit/cluster-configuration/cluster-configuration.go
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,51 @@ | ||
/* | ||
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 cluster_config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/deckhouse/deckhouse-cli/internal/edit" | ||
) | ||
|
||
var clusterConfigurationLong = templates.LongDesc(` | ||
Edit cluster-configuration in Kubernetes cluster. | ||
© Flant JSC 2024`) | ||
|
||
func NewCommand() *cobra.Command { | ||
clusterConfigurationCmd := &cobra.Command{ | ||
Use: "cluster-configuration", | ||
Short: "Edit cluster-configuration.", | ||
Long: clusterConfigurationLong, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
RunE: editClusterConfig, | ||
} | ||
return clusterConfigurationCmd | ||
} | ||
|
||
func editClusterConfig(cmd *cobra.Command, _ []string) error { | ||
err := edit.BaseEditConfigCMD(cmd, "cluster-configuration", "d8-cluster-configuration", "cluster-configuration.yaml") | ||
if err != nil { | ||
return fmt.Errorf("Error updating secret: %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,50 @@ | ||
/* | ||
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 edit | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
cluster_config "github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/cluster-configuration" | ||
provider_config "github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/provider-cluster-configuration" | ||
static_config "github.com/deckhouse/deckhouse-cli/internal/platform/cmd/edit/static-cluster-configuration" | ||
"github.com/deckhouse/deckhouse-cli/internal/platform/edit/flags" | ||
) | ||
|
||
var editLong = templates.LongDesc(` | ||
Change configuration files in Kubernetes cluster conveniently and safely. | ||
© Flant JSC 2024`) | ||
|
||
func NewCommand() *cobra.Command { | ||
editCmd := &cobra.Command{ | ||
Use: "edit", Short: "Edit configuration files", | ||
Long: editLong, | ||
PreRunE: flags.ValidateParameters, | ||
} | ||
|
||
editCmd.AddCommand( | ||
cluster_config.NewCommand(), | ||
static_config.NewCommand(), | ||
provider_config.NewCommand(), | ||
) | ||
|
||
flags.AddPersistentFlags(editCmd) | ||
|
||
return editCmd | ||
} |
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,124 @@ | ||
package edit | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/deckhouse/deckhouse-cli/internal/utilk8s" | ||
) | ||
|
||
func BaseEditConfigCMD(cmd *cobra.Command, name, secret, dataKey string) error { | ||
editor, err := cmd.Flags().GetString("editor") | ||
if err != nil { | ||
return fmt.Errorf("Failed to get editor from --editor flag: %w", err) | ||
} | ||
|
||
kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
_, kubeCl, err := utilk8s.SetupK8sClientSet(kubeconfigPath) | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
secretConfig, err := kubeCl.CoreV1(). | ||
Secrets("kube-system"). | ||
Get(context.Background(), secret, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching secret: %w", err) | ||
} | ||
|
||
tempFile, err := writeSecretTmp(secretConfig, dataKey) | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(tempFile.Name()) | ||
|
||
cmdExec := exec.Command(editor, tempFile.Name()) | ||
cmdExec.Stdin = os.Stdin | ||
cmdExec.Stdout = os.Stdout | ||
cmdExec.Stderr = os.Stderr | ||
err = cmdExec.Run() | ||
if err != nil { | ||
return fmt.Errorf("Error opening in editor: %w", err) | ||
} | ||
|
||
updatedContent, contentNotChanged, err := openSecretTmp(tempFile, secretConfig, dataKey) | ||
if err != nil { | ||
return fmt.Errorf("Cannot open edited temp file: %w", err) | ||
} | ||
|
||
if contentNotChanged { | ||
return nil | ||
} | ||
|
||
encodedValue, err := encodeSecretTmp(updatedContent, dataKey) | ||
_, err = kubeCl.CoreV1(). | ||
Secrets("kube-system"). | ||
Patch(context.TODO(), secret, types.MergePatchType, encodedValue, metav1.PatchOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("Error updating secret: %w", err) | ||
} | ||
|
||
fmt.Println("Secret updated successfully") | ||
return err | ||
} | ||
|
||
func writeSecretTmp(secretConfig *v1.Secret, dataKey string) (*os.File, error) { | ||
tempFile, err := os.CreateTemp(os.TempDir(), "secret.*.yaml") | ||
if err != nil { | ||
return nil, fmt.Errorf("Can't save cluster configuration: %w\n", err) | ||
} | ||
|
||
_, err = tempFile.Write(secretConfig.Data[dataKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error writing decoded data to file: %w", err) | ||
} | ||
return tempFile, nil | ||
} | ||
|
||
func openSecretTmp(tempFile *os.File, secretConfig *v1.Secret, dataKey string) ([]byte, bool, error) { | ||
if _, err := tempFile.Seek(0, 0); err != nil { | ||
return nil, false, fmt.Errorf("Error reading updated file: %w", err) | ||
} | ||
updatedContent, err := io.ReadAll(tempFile) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("Error reading updated file: %w", err) | ||
} | ||
|
||
if sha256.Sum256(secretConfig.Data[dataKey]) == sha256.Sum256(updatedContent) { | ||
fmt.Println("Configurations are equal. Nothing to update.") | ||
return nil, true, nil | ||
} | ||
|
||
return updatedContent, false, nil | ||
} | ||
|
||
func encodeSecretTmp(updatedContent []byte, dataKey string) ([]byte, error) { | ||
encodedValue := base64.StdEncoding.EncodeToString(updatedContent) | ||
patchData := map[string]interface{}{ | ||
"data": map[string]interface{}{ | ||
dataKey: encodedValue, | ||
}, | ||
} | ||
|
||
patchBytes, err := json.Marshal(patchData) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error convert to json updated data: %w", err) | ||
} | ||
|
||
return patchBytes, nil | ||
} |
51 changes: 51 additions & 0 deletions
51
internal/platform/cmd/edit/provider-cluster-configuration/provider-cluster-configuration.go
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,51 @@ | ||
/* | ||
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 provider_config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/deckhouse/deckhouse-cli/internal/edit" | ||
) | ||
|
||
var providerClusterConfigurationLong = templates.LongDesc(` | ||
Edit provider-cluster-configuration in Kubernetes cluster. | ||
© Flant JSC 2024`) | ||
|
||
func NewCommand() *cobra.Command { | ||
providerClusterConfigurationCmd := &cobra.Command{ | ||
Use: "provider-cluster-configuration", | ||
Short: "Edit provider-cluster-configuration.", | ||
Long: providerClusterConfigurationLong, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
RunE: editProviderClusterConfig, | ||
} | ||
return providerClusterConfigurationCmd | ||
} | ||
|
||
func editProviderClusterConfig(cmd *cobra.Command, _ []string) error { | ||
err := edit.BaseEditConfigCMD(cmd, "provider-cluster-configuration", "d8-provider-cluster-configuration", "provider-cluster-configuration.yaml") | ||
if err != nil { | ||
return fmt.Errorf("Error updating secret: %w", err) | ||
} | ||
return err | ||
} |
51 changes: 51 additions & 0 deletions
51
internal/platform/cmd/edit/static-cluster-configuration/static-cluster-configuration.go
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,51 @@ | ||
/* | ||
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 static_config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/deckhouse/deckhouse-cli/internal/edit" | ||
) | ||
|
||
var staticClusterConfigurationLong = templates.LongDesc(` | ||
Edit static-cluster-configuration in Kubernetes cluster. | ||
© Flant JSC 2024`) | ||
|
||
func NewCommand() *cobra.Command { | ||
staticClusterConfigurationCmd := &cobra.Command{ | ||
Use: "static-cluster-configuration", | ||
Short: "Edit static-cluster-configuration.", | ||
Long: staticClusterConfigurationLong, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
RunE: editStaticClusterConfig, | ||
} | ||
return staticClusterConfigurationCmd | ||
} | ||
|
||
func editStaticClusterConfig(cmd *cobra.Command, _ []string) error { | ||
err := edit.BaseEditConfigCMD(cmd, "static-cluster-configuration", "d8-static-cluster-configuration", "static-cluster-configuration.yaml") | ||
if err != nil { | ||
return fmt.Errorf("Error updating secret: %w", err) | ||
} | ||
return err | ||
} |
Oops, something went wrong.