-
Notifications
You must be signed in to change notification settings - Fork 8
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 edit options cluster\static\provider configurations #64
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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/edit/cmd" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(edit.NewCommand()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
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 ( | ||
"github.com/deckhouse/deckhouse-cli/internal/edit/utilk8s" | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
"log" | ||
) | ||
|
||
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, | ||
PreRunE: ValidateParameters, | ||
RunE: editClusterConfig, | ||
} | ||
|
||
AddFlags(clusterConfigurationCmd.Flags()) | ||
return clusterConfigurationCmd | ||
} | ||
|
||
func editClusterConfig(cmd *cobra.Command, _ []string) error { | ||
err := utilk8s.BaseEditConfigCMD(cmd, "cluster-configuration", "d8-cluster-configuration", "cluster-configuration.yaml") | ||
if err != nil { | ||
log.Fatalf("Error updating secret: %s", err.Error()) | ||
} | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
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 ( | ||
"os" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
func AddFlags(flagSet *pflag.FlagSet) { | ||
|
||
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") | ||
if p := os.Getenv("KUBECONFIG"); p != "" { | ||
defaultKubeconfigPath = p | ||
} | ||
|
||
defaultEditor := os.ExpandEnv("vi") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no reason to use |
||
if e := os.Getenv("EDITOR"); e != "" { | ||
defaultEditor = e | ||
} | ||
|
||
flagSet.StringP( | ||
"kubeconfig", "k", | ||
defaultKubeconfigPath, | ||
"KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", | ||
) | ||
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This flag should probably be "persistent" in terms of cobra, which basically means it must be added to persistent flagset, which is inherited by children subcommands. https://github.com/spf13/cobra/blob/main/site/content/user_guide.md#persistent-flags |
||
|
||
flagSet.StringP( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need this flag when there is already |
||
"editor", "e", | ||
defaultEditor, | ||
"Your favourite editor. (default is $EDITOR when it is set)", | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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" | ||
"os" | ||
) | ||
|
||
func ValidateParameters(cmd *cobra.Command, args []string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is a part of internal logic of edit package and should not be exposed to public as part of this package's interface. |
||
kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
stats, err := os.Stat(kubeconfigPath) | ||
if err != nil { | ||
return fmt.Errorf("Invalid --kubeconfig: %w", err) | ||
} | ||
if !stats.Mode().IsRegular() { | ||
return fmt.Errorf("Invalid --kubeconfig: %s is not a regular file", kubeconfigPath) | ||
} | ||
|
||
return nil | ||
} |
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 edit | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
cluster_config "github.com/deckhouse/deckhouse-cli/internal/edit/cmd/cluster-configuration" | ||
provider_config "github.com/deckhouse/deckhouse-cli/internal/edit/cmd/provider-cluster-configuration" | ||
static_config "github.com/deckhouse/deckhouse-cli/internal/edit/cmd/static-cluster-configuration" | ||
) | ||
|
||
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, | ||
} | ||
|
||
editCmd.AddCommand( | ||
cluster_config.NewCommand(), | ||
static_config.NewCommand(), | ||
provider_config.NewCommand(), | ||
) | ||
return editCmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
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 ( | ||
"os" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
func AddFlags(flagSet *pflag.FlagSet) { | ||
|
||
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") | ||
if p := os.Getenv("KUBECONFIG"); p != "" { | ||
defaultKubeconfigPath = p | ||
} | ||
|
||
defaultEditor := os.ExpandEnv("vi") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, no use for env expanding here |
||
if e := os.Getenv("EDITOR"); e != "" { | ||
defaultEditor = e | ||
} | ||
|
||
flagSet.StringP( | ||
"kubeconfig", "k", | ||
defaultKubeconfigPath, | ||
"KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", | ||
) | ||
|
||
flagSet.StringP( | ||
"editor", "e", | ||
defaultEditor, | ||
"Your favourite editor. (default is $EDITOR when it is set)", | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
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 ( | ||
"github.com/deckhouse/deckhouse-cli/internal/edit/utilk8s" | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
"log" | ||
) | ||
|
||
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, | ||
PreRunE: ValidateParameters, | ||
RunE: editProviderClusterConfig, | ||
} | ||
|
||
AddFlags(providerClusterConfigurationCmd.Flags()) | ||
return providerClusterConfigurationCmd | ||
} | ||
|
||
func editProviderClusterConfig(cmd *cobra.Command, _ []string) error { | ||
err := utilk8s.BaseEditConfigCMD(cmd, "provider-cluster-configuration", "d8-provider-cluster-configuration", "provider-cluster-configuration.yaml") | ||
if err != nil { | ||
log.Fatalf("Error updating secret: %s", err.Error()) | ||
} | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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" | ||
"os" | ||
) | ||
|
||
func ValidateParameters(cmd *cobra.Command, args []string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First, as above, this should not be public-facing. Also, when you see that you have to copy-paste code like that, you should consider moving it into separate package or consolidating it in some other way. Do not copy-paste unless you don't have a choice. |
||
kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") | ||
if err != nil { | ||
return fmt.Errorf("Failed to setup Kubernetes client: %w", err) | ||
} | ||
|
||
stats, err := os.Stat(kubeconfigPath) | ||
if err != nil { | ||
return fmt.Errorf("Invalid --kubeconfig: %w", err) | ||
} | ||
if !stats.Mode().IsRegular() { | ||
return fmt.Errorf("Invalid --kubeconfig: %s is not a regular file", kubeconfigPath) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please format your imports properly. I can show you how to setup automated import fomatting in goland later.