@@ -25,24 +25,25 @@ import (
2525 "strconv"
2626 "strings"
2727
28- "github.com/spf13/cobra"
2928 cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
29+ "github.com/spf13/cobra"
3030 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3232 "k8s.io/apimachinery/pkg/runtime"
3333 "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
3434 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3535 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
36+ _ "k8s.io/client-go/plugin/pkg/client/auth"
3637 "k8s.io/client-go/rest"
3738 "k8s.io/client-go/tools/clientcmd"
3839 ctrl "sigs.k8s.io/controller-runtime"
3940 "sigs.k8s.io/controller-runtime/pkg/client"
40- _ "k8s.io/client-go/plugin/pkg/client/auth"
4141)
4242
4343var addCmdFlags struct {
44- files []string
45- kubeconfig string
44+ files []string
45+ kubeconfig string
46+ allowPrivileged bool
4647}
4748
4849var addCmd = & cobra.Command {
@@ -59,7 +60,7 @@ Multiple -f flags can be specified, and they can point to files or directories.`
5960 // Collect package names from arguments and files
6061 packageNames := make (map [string ]bool )
6162 packagesFromFiles := make (map [string ]string ) // packageName -> filePath
62-
63+
6364 for _ , arg := range args {
6465 packageNames [arg ] = true
6566 }
@@ -119,7 +120,7 @@ Multiple -f flags can be specified, and they can point to files or directories.`
119120 }
120121 // If failed, fall back to interactive installation
121122 }
122-
123+
123124 // Interactive installation from PackageSource
124125 if err := installPackage (ctx , k8sClient , packageName ); err != nil {
125126 return err
@@ -178,7 +179,7 @@ func readPackagesFromYAMLFile(filePath string) ([]string, error) {
178179
179180 // Split YAML documents (in case of multiple resources)
180181 documents := strings .Split (string (data ), "---" )
181-
182+
182183 for _ , doc := range documents {
183184 doc = strings .TrimSpace (doc )
184185 if doc == "" {
@@ -240,7 +241,7 @@ func buildDependencyTree(ctx context.Context, k8sClient client.Client, rootName
240241 tree := make (map [string ][]string )
241242 dependencyRequesters := make (map [string ]string ) // dep -> requester
242243 visited := make (map [string ]bool )
243-
244+
244245 // Ensure root is in tree even if it has no dependencies
245246 tree [rootName ] = []string {}
246247
@@ -354,7 +355,7 @@ func createPackageFromFile(ctx context.Context, k8sClient client.Client, filePat
354355
355356 // Split YAML documents
356357 documents := strings .Split (string (data ), "---" )
357-
358+
358359 for _ , doc := range documents {
359360 doc = strings .TrimSpace (doc )
360361 if doc == "" {
@@ -462,6 +463,18 @@ func installPackage(ctx context.Context, k8sClient client.Client, packageSourceN
462463 return fmt .Errorf ("failed to select variant for %s: %w" , pkgName , err )
463464 }
464465
466+ // A privileged component runs with elevated access; require an explicit
467+ // confirmation (or --allow-privileged) before installing it.
468+ if privileged := privilegedComponents (ps , variant ); len (privileged ) > 0 && ! addCmdFlags .allowPrivileged {
469+ ok , err := confirmPrivileged (pkgName , variant , privileged )
470+ if err != nil {
471+ return err
472+ }
473+ if ! ok {
474+ return fmt .Errorf ("aborted: %s variant %s has privileged component(s) %v; re-run with --allow-privileged to install" , pkgName , variant , privileged )
475+ }
476+ }
477+
465478 packageVariants [pkgName ] = variant
466479 }
467480
@@ -541,9 +554,44 @@ func selectVariantInteractive(ps *cozyv1alpha1.PackageSource) (string, error) {
541554 }
542555}
543556
557+ // privilegedComponents returns the names of components in the given variant
558+ // that declare install.privileged: true.
559+ func privilegedComponents (ps * cozyv1alpha1.PackageSource , variantName string ) []string {
560+ var names []string
561+ for _ , v := range ps .Spec .Variants {
562+ if v .Name != variantName {
563+ continue
564+ }
565+ for _ , c := range v .Components {
566+ if c .Install != nil && c .Install .Privileged {
567+ names = append (names , c .Name )
568+ }
569+ }
570+ }
571+ return names
572+ }
573+
574+ // confirmPrivileged prompts the operator to confirm installing privileged
575+ // components. It defaults to No.
576+ func confirmPrivileged (pkgName , variant string , components []string ) (bool , error ) {
577+ fmt .Fprintf (os .Stderr , "\n ⚠ %s variant %s installs privileged component(s): %s\n " , pkgName , variant , strings .Join (components , ", " ))
578+ fmt .Fprint (os .Stderr , "These run with elevated cluster access. Install anyway? [y/N]: " )
579+ reader := bufio .NewReader (os .Stdin )
580+ input , err := reader .ReadString ('\n' )
581+ if err != nil {
582+ return false , fmt .Errorf ("failed to read confirmation: %w" , err )
583+ }
584+ switch strings .ToLower (strings .TrimSpace (input )) {
585+ case "y" , "yes" :
586+ return true , nil
587+ default :
588+ return false , nil
589+ }
590+ }
591+
544592func init () {
545593 rootCmd .AddCommand (addCmd )
594+ addCmd .Flags ().BoolVar (& addCmdFlags .allowPrivileged , "allow-privileged" , false , "Install privileged components without an interactive confirmation" )
546595 addCmd .Flags ().StringArrayVarP (& addCmdFlags .files , "file" , "f" , []string {}, "Read packages from file or directory (can be specified multiple times)" )
547596 addCmd .Flags ().StringVar (& addCmdFlags .kubeconfig , "kubeconfig" , "" , "Path to kubeconfig file (defaults to ~/.kube/config or KUBECONFIG env var)" )
548597}
549-
0 commit comments