Skip to content

Commit 018f859

Browse files
weshayutinclaude
andcommitted
add config for nonadmin
* if nonadmin: true * only allow the cli to present `oc oadp nonadmin` commands Signed-off-by: Wesley Hayutin <weshayutin@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent a6d2ac5 commit 018f859

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

cmd/root.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
"github.com/vmware-tanzu/velero/pkg/cmd/cli/create"
5050
"github.com/vmware-tanzu/velero/pkg/cmd/cli/datamover"
5151
"github.com/vmware-tanzu/velero/pkg/cmd/cli/debug"
52-
"github.com/vmware-tanzu/velero/pkg/cmd/cli/delete"
52+
veldelete "github.com/vmware-tanzu/velero/pkg/cmd/cli/delete"
5353
"github.com/vmware-tanzu/velero/pkg/cmd/cli/describe"
5454
"github.com/vmware-tanzu/velero/pkg/cmd/cli/get"
5555
"github.com/vmware-tanzu/velero/pkg/cmd/cli/repo"
@@ -349,6 +349,24 @@ func wrapPreRunE(existing func(*cobra.Command, []string) error, additional func(
349349
}
350350
}
351351

352+
// isNonadminEnabled checks if nonadmin mode is enabled in the VeleroConfig.
353+
// Handles both boolean and string representations since
354+
// `oc oadp client config set nonadmin=true` stores the value as a string.
355+
func isNonadminEnabled(config clientcmd.VeleroConfig) bool {
356+
val, ok := config["nonadmin"]
357+
if !ok {
358+
return false
359+
}
360+
switch v := val.(type) {
361+
case bool:
362+
return v
363+
case string:
364+
return strings.EqualFold(v, "true")
365+
default:
366+
return false
367+
}
368+
}
369+
352370
// NewVeleroRootCommand returns a root command with all Velero CLI subcommands attached.
353371
func NewVeleroRootCommand(baseName string) *cobra.Command {
354372

@@ -357,6 +375,13 @@ func NewVeleroRootCommand(baseName string) *cobra.Command {
357375
fmt.Fprintf(os.Stderr, "WARNING: Error reading config file: %v\n", err)
358376
}
359377

378+
// When nonadmin mode is enabled, remove the namespace override so the
379+
// factory uses the current kubeconfig context namespace instead of an
380+
// admin namespace like openshift-adp.
381+
if isNonadminEnabled(config) {
382+
delete(config, clientcmd.ConfigKeyNamespace)
383+
}
384+
360385
// Declare cmdFeatures and cmdColorzied here so we can access them in the PreRun hooks
361386
// without doing a chain of calls into the command's FlagSet
362387
var cmdFeatures veleroflag.StringArray
@@ -401,7 +426,7 @@ func NewVeleroRootCommand(baseName string) *cobra.Command {
401426
get.NewCommand(f),
402427
describe.NewCommand(f),
403428
create.NewCommand(f),
404-
delete.NewCommand(f),
429+
veldelete.NewCommand(f),
405430
cliclient.NewCommand(),
406431
completion.NewCommand(),
407432
repo.NewCommand(f),
@@ -437,6 +462,21 @@ func NewVeleroRootCommand(baseName string) *cobra.Command {
437462
renameTimeoutFlag(cmd)
438463
}
439464

465+
// When nonadmin mode is enabled, hide all admin commands so only
466+
// nonadmin and client (for toggling the config) are visible.
467+
if isNonadminEnabled(config) {
468+
allowedCmds := map[string]bool{
469+
"nonadmin": true,
470+
"client": true,
471+
"completion": true,
472+
}
473+
for _, cmd := range c.Commands() {
474+
if !allowedCmds[cmd.Use] {
475+
cmd.Hidden = true
476+
}
477+
}
478+
}
479+
440480
// Set custom usage template to show "oc oadp" instead of just "oadp"
441481
usageTemplate := c.UsageTemplate()
442482
usageTemplate = strings.ReplaceAll(usageTemplate, "{{.CommandPath}}", "oc {{.CommandPath}}")

cmd/shared/factories.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,32 @@ import (
2121
"fmt"
2222
"os"
2323
"path/filepath"
24+
"strings"
2425

2526
"github.com/vmware-tanzu/velero/pkg/client"
2627
)
2728

2829
// ClientConfig represents the structure of the Velero client configuration file
2930
type ClientConfig struct {
30-
Namespace string `json:"namespace"`
31+
Namespace string `json:"namespace"`
32+
NonAdmin interface{} `json:"nonadmin,omitempty"`
33+
}
34+
35+
// IsNonAdmin returns true if the nonadmin configuration is enabled.
36+
// Handles both boolean and string representations since
37+
// `oc oadp client config set nonadmin=true` stores the value as a string.
38+
func (c *ClientConfig) IsNonAdmin() bool {
39+
if c == nil {
40+
return false
41+
}
42+
switch v := c.NonAdmin.(type) {
43+
case bool:
44+
return v
45+
case string:
46+
return strings.EqualFold(v, "true")
47+
default:
48+
return false
49+
}
3150
}
3251

3352
// CreateVeleroFactory creates a client factory for Velero operations (admin-scoped)

0 commit comments

Comments
 (0)