Skip to content

Commit

Permalink
Use PreRunE for sub-commands (#165)
Browse files Browse the repository at this point in the history
By default, PersistentPreRun hooks are not chained. If a child command
overwrites this, the parent hook is not executed.

This caused the global permissions check to be overwritten in the
sub-commands and caused misleading error messages when running commands
without root permission.
  • Loading branch information
bschimke95 authored and eaudetcobello committed Feb 28, 2024
1 parent 6439355 commit d0788ac
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_add_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var (

func newAddNodeCmd() *cobra.Command {
addNodeCmd := &cobra.Command{
Use: "add-node <name>",
Short: "Create a connection token for a node to join the cluster",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "add-node <name>",
Short: "Create a connection token for a node to join the cluster",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
return fmt.Errorf("too many arguments: provide only the node name to add")
Expand Down
8 changes: 4 additions & 4 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var (

func newBootstrapCmd() *cobra.Command {
bootstrapCmd := &cobra.Command{
Use: "bootstrap",
Short: "Bootstrap a k8s cluster on this node.",
Long: "Initialize the necessary folders, permissions, service arguments, certificates and start up the Kubernetes services.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "bootstrap",
Short: "Bootstrap a k8s cluster on this node.",
Long: "Initialize the necessary folders, permissions, service arguments, certificates and start up the Kubernetes services.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, bootstrapCmdErrorMsgs)

Expand Down
8 changes: 4 additions & 4 deletions src/k8s/cmd/k8s/k8s_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

func newKubeConfigCmd() *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Generate a kubeconfig that can be used to access the Kubernetes cluster",
Hidden: true,
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "config",
Short: "Generate a kubeconfig that can be used to access the Kubernetes cluster",
Hidden: true,
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableDNSCmd() *cobra.Command {
return &cobra.Command{
Use: "dns",
Short: "Disable the DNS component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "dns",
Short: "Disable the DNS component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableGatewayCmd() *cobra.Command {
return &cobra.Command{
Use: "gateway",
Short: "Disable the Gateway component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "gateway",
Short: "Disable the Gateway component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableIngressCmd() *cobra.Command {
return &cobra.Command{
Use: "ingress",
Short: "Disable the Ingress component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "ingress",
Short: "Disable the Ingress component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableLoadBalancerCmd() *cobra.Command {
return &cobra.Command{
Use: "loadbalancer",
Short: "Disable the LoadBalancer component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "loadbalancer",
Short: "Disable the LoadBalancer component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableMetricsServerCmd() *cobra.Command {
return &cobra.Command{
Use: "metrics-server",
Short: "Disable the Metrics-Server component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "metrics-server",
Short: "Disable the Metrics-Server component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableNetworkCmd() *cobra.Command {
return &cobra.Command{
Use: "network",
Short: "Disable the Network component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "network",
Short: "Disable the Network component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_disable_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newDisableStorageCmd() *cobra.Command {
return &cobra.Command{
Use: "storage",
Short: "Disable the Network component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "storage",
Short: "Disable the Network component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var enableDNSCmdConfig struct {

func newEnableDNSCmd() *cobra.Command {
enableDNSCmd := &cobra.Command{
Use: "dns",
Short: "Enable the DNS component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "dns",
Short: "Enable the DNS component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newEnableGatewayCmd() *cobra.Command {
return &cobra.Command{
Use: "gateway",
Short: "Enable the Gateway component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "gateway",
Short: "Enable the Gateway component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var enableIngressCmdOpts struct {

func newEnableIngressCmd() *cobra.Command {
enableIngressCmd := &cobra.Command{
Use: "ingress",
Short: "Enable the Ingress component in the cluster",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "ingress",
Short: "Enable the Ingress component in the cluster",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var enableLoadBalancerCmdOpts struct {

func newEnableLoadBalancerCmd() *cobra.Command {
enableLoadBalancerCmd := &cobra.Command{
Use: "loadbalancer",
Short: "Enable the LoadBalancer component in the cluster",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "loadbalancer",
Short: "Enable the LoadBalancer component in the cluster",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newEnableMetricsServerCmd() *cobra.Command {
return &cobra.Command{
Use: "metrics-server",
Short: "Enable the Metrics-Server component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "metrics-server",
Short: "Enable the Metrics-Server component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newEnableNetworkCmd() *cobra.Command {
return &cobra.Command{
Use: "network",
Short: "Enable the Network component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "network",
Short: "Enable the Network component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_enable_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func newEnableStorageCmd() *cobra.Command {
return &cobra.Command{
Use: "storage",
Short: "Enable the Storage component in the cluster.",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "storage",
Short: "Enable the Storage component in the cluster.",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
8 changes: 4 additions & 4 deletions src/k8s/cmd/k8s/k8s_generate_auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ var (

func newGenerateAuthTokenCmd() *cobra.Command {
generateAuthTokenCmd := &cobra.Command{
Use: "generate-auth-token --username <user> [--groups <group1>,<group2>]",
Short: "Generate an auth token for Kubernetes",
Hidden: true,
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "generate-auth-token --username <user> [--groups <group1>,<group2>]",
Short: "Generate an auth token for Kubernetes",
Hidden: true,
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_join_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ var (

func newJoinNodeCmd() *cobra.Command {
joinNodeCmd := &cobra.Command{
Use: "join-cluster <token>",
Short: "Join a cluster",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "join-cluster <token>",
Short: "Join a cluster",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
return fmt.Errorf("too many arguments: provide only the token that was generated with `sudo k8s add-node <node-name>`")
Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_remove_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var (

func newRemoveNodeCmd() *cobra.Command {
removeNodeCmd := &cobra.Command{
Use: "remove-node <name>",
Short: "Remove a node from the cluster",
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "remove-node <name>",
Short: "Remove a node from the cluster",
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
return fmt.Errorf("too many arguments: provide only the name of the node to remove")
Expand Down
8 changes: 4 additions & 4 deletions src/k8s/cmd/k8s/k8s_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ var (

func newStatusCmd() *cobra.Command {
statusCmd := &cobra.Command{
Use: "status",
Short: "Retrieve the current status of the cluster",
Hidden: true,
PersistentPreRunE: chainPreRunHooks(hookSetupClient),
Use: "status",
Short: "Retrieve the current status of the cluster",
Hidden: true,
PreRunE: chainPreRunHooks(hookSetupClient),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer errors.Transform(&err, nil)

Expand Down

0 comments on commit d0788ac

Please sign in to comment.