...

Source file src/k8s.io/kubectl/pkg/cmd/create/create_serviceaccount.go

Documentation: k8s.io/kubectl/pkg/cmd/create

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package create
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/api/meta"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/cli-runtime/pkg/genericclioptions"
    30  	"k8s.io/cli-runtime/pkg/genericiooptions"
    31  	coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
    32  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    33  	"k8s.io/kubectl/pkg/scheme"
    34  	"k8s.io/kubectl/pkg/util"
    35  	"k8s.io/kubectl/pkg/util/i18n"
    36  	"k8s.io/kubectl/pkg/util/templates"
    37  )
    38  
    39  var (
    40  	serviceAccountLong = templates.LongDesc(i18n.T(`
    41  		Create a service account with the specified name.`))
    42  
    43  	serviceAccountExample = templates.Examples(i18n.T(`
    44  	  # Create a new service account named my-service-account
    45  	  kubectl create serviceaccount my-service-account`))
    46  )
    47  
    48  // ServiceAccountOpts holds the options for 'create serviceaccount' sub command
    49  type ServiceAccountOpts struct {
    50  	// PrintFlags holds options necessary for obtaining a printer
    51  	PrintFlags *genericclioptions.PrintFlags
    52  	PrintObj   func(obj runtime.Object) error
    53  	// Name of resource being created
    54  	Name                string
    55  	DryRunStrategy      cmdutil.DryRunStrategy
    56  	ValidationDirective string
    57  	CreateAnnotation    bool
    58  	FieldManager        string
    59  
    60  	Namespace        string
    61  	EnforceNamespace bool
    62  
    63  	Mapper meta.RESTMapper
    64  	Client *coreclient.CoreV1Client
    65  
    66  	genericiooptions.IOStreams
    67  }
    68  
    69  // NewServiceAccountOpts creates a new *ServiceAccountOpts with sane defaults
    70  func NewServiceAccountOpts(ioStreams genericiooptions.IOStreams) *ServiceAccountOpts {
    71  	return &ServiceAccountOpts{
    72  		PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
    73  		IOStreams:  ioStreams,
    74  	}
    75  }
    76  
    77  // NewCmdCreateServiceAccount is a macro command to create a new service account
    78  func NewCmdCreateServiceAccount(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobra.Command {
    79  	o := NewServiceAccountOpts(ioStreams)
    80  
    81  	cmd := &cobra.Command{
    82  		Use:                   "serviceaccount NAME [--dry-run=server|client|none]",
    83  		DisableFlagsInUseLine: true,
    84  		Aliases:               []string{"sa"},
    85  		Short:                 i18n.T("Create a service account with the specified name"),
    86  		Long:                  serviceAccountLong,
    87  		Example:               serviceAccountExample,
    88  		Run: func(cmd *cobra.Command, args []string) {
    89  			cmdutil.CheckErr(o.Complete(f, cmd, args))
    90  			cmdutil.CheckErr(o.Validate())
    91  			cmdutil.CheckErr(o.Run())
    92  		},
    93  	}
    94  
    95  	o.PrintFlags.AddFlags(cmd)
    96  
    97  	cmdutil.AddApplyAnnotationFlags(cmd)
    98  	cmdutil.AddValidateFlags(cmd)
    99  	cmdutil.AddDryRunFlag(cmd)
   100  	cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
   101  	return cmd
   102  }
   103  
   104  // Complete completes all the required options
   105  func (o *ServiceAccountOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
   106  	var err error
   107  	o.Name, err = NameFromCommandArgs(cmd, args)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	restConfig, err := f.ToRESTConfig()
   113  	if err != nil {
   114  		return err
   115  	}
   116  	o.Client, err = coreclient.NewForConfig(restConfig)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	o.CreateAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
   122  
   123  	o.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
   134  
   135  	printer, err := o.PrintFlags.ToPrinter()
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	o.PrintObj = func(obj runtime.Object) error {
   141  		return printer.PrintObj(obj, o.Out)
   142  	}
   143  
   144  	o.ValidationDirective, err = cmdutil.GetValidationDirective(cmd)
   145  	if err != nil {
   146  		return err
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  // Validate checks ServiceAccountOpts to see if there is sufficient information run the command.
   153  func (o *ServiceAccountOpts) Validate() error {
   154  	if len(o.Name) == 0 {
   155  		return fmt.Errorf("name must be specified")
   156  	}
   157  	return nil
   158  }
   159  
   160  // Run makes the api call to the server
   161  func (o *ServiceAccountOpts) Run() error {
   162  	serviceAccount, err := o.createServiceAccount()
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, serviceAccount, scheme.DefaultJSONEncoder()); err != nil {
   168  		return err
   169  	}
   170  
   171  	if o.DryRunStrategy != cmdutil.DryRunClient {
   172  		createOptions := metav1.CreateOptions{}
   173  		if o.FieldManager != "" {
   174  			createOptions.FieldManager = o.FieldManager
   175  		}
   176  		createOptions.FieldValidation = o.ValidationDirective
   177  		if o.DryRunStrategy == cmdutil.DryRunServer {
   178  			createOptions.DryRun = []string{metav1.DryRunAll}
   179  		}
   180  		serviceAccount, err = o.Client.ServiceAccounts(o.Namespace).Create(context.TODO(), serviceAccount, createOptions)
   181  		if err != nil {
   182  			return fmt.Errorf("failed to create serviceaccount: %v", err)
   183  		}
   184  	}
   185  	return o.PrintObj(serviceAccount)
   186  }
   187  
   188  func (o *ServiceAccountOpts) createServiceAccount() (*corev1.ServiceAccount, error) {
   189  	namespace := ""
   190  	if o.EnforceNamespace {
   191  		namespace = o.Namespace
   192  	}
   193  	serviceAccount := &corev1.ServiceAccount{
   194  		TypeMeta: metav1.TypeMeta{APIVersion: corev1.SchemeGroupVersion.String(), Kind: "ServiceAccount"},
   195  		ObjectMeta: metav1.ObjectMeta{
   196  			Name:      o.Name,
   197  			Namespace: namespace,
   198  		},
   199  	}
   200  	serviceAccount.Name = o.Name
   201  	return serviceAccount, nil
   202  }
   203  

View as plain text