2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 create
|
|
|
|
|
|
|
|
import (
|
2021-03-18 22:40:29 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
2021-03-18 22:40:29 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
|
|
coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
|
2019-09-27 21:51:53 +00:00
|
|
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
2021-03-18 22:40:29 +00:00
|
|
|
"k8s.io/kubectl/pkg/scheme"
|
|
|
|
"k8s.io/kubectl/pkg/util"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/kubectl/pkg/util/i18n"
|
|
|
|
"k8s.io/kubectl/pkg/util/templates"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
serviceAccountLong = templates.LongDesc(i18n.T(`
|
|
|
|
Create a service account with the specified name.`))
|
|
|
|
|
|
|
|
serviceAccountExample = templates.Examples(i18n.T(`
|
|
|
|
# Create a new service account named my-service-account
|
|
|
|
kubectl create serviceaccount my-service-account`))
|
|
|
|
)
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// ServiceAccountOpts holds the options for 'create serviceaccount' sub command
|
2019-01-12 04:58:27 +00:00
|
|
|
type ServiceAccountOpts struct {
|
2021-03-18 22:40:29 +00:00
|
|
|
// PrintFlags holds options necessary for obtaining a printer
|
|
|
|
PrintFlags *genericclioptions.PrintFlags
|
|
|
|
PrintObj func(obj runtime.Object) error
|
|
|
|
// Name of resource being created
|
|
|
|
Name string
|
|
|
|
DryRunStrategy cmdutil.DryRunStrategy
|
|
|
|
DryRunVerifier *resource.DryRunVerifier
|
|
|
|
CreateAnnotation bool
|
|
|
|
FieldManager string
|
|
|
|
|
|
|
|
Namespace string
|
|
|
|
EnforceNamespace bool
|
|
|
|
|
|
|
|
Mapper meta.RESTMapper
|
|
|
|
Client *coreclient.CoreV1Client
|
|
|
|
|
|
|
|
genericclioptions.IOStreams
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceAccountOpts creates a new *ServiceAccountOpts with sane defaults
|
|
|
|
func NewServiceAccountOpts(ioStreams genericclioptions.IOStreams) *ServiceAccountOpts {
|
|
|
|
return &ServiceAccountOpts{
|
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
|
|
|
|
IOStreams: ioStreams,
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCmdCreateServiceAccount is a macro command to create a new service account
|
|
|
|
func NewCmdCreateServiceAccount(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
2021-03-18 22:40:29 +00:00
|
|
|
o := NewServiceAccountOpts(ioStreams)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2020-03-26 21:07:15 +00:00
|
|
|
Use: "serviceaccount NAME [--dry-run=server|client|none]",
|
2019-01-12 04:58:27 +00:00
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
Aliases: []string{"sa"},
|
|
|
|
Short: i18n.T("Create a service account with the specified name"),
|
|
|
|
Long: serviceAccountLong,
|
|
|
|
Example: serviceAccountExample,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-03-18 22:40:29 +00:00
|
|
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
|
|
|
cmdutil.CheckErr(o.Validate())
|
|
|
|
cmdutil.CheckErr(o.Run())
|
2019-01-12 04:58:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
o.PrintFlags.AddFlags(cmd)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
|
|
|
cmdutil.AddValidateFlags(cmd)
|
2021-03-18 22:40:29 +00:00
|
|
|
cmdutil.AddDryRunFlag(cmd)
|
|
|
|
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
|
2019-01-12 04:58:27 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// Complete completes all the required options
|
2019-01-12 04:58:27 +00:00
|
|
|
func (o *ServiceAccountOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
2021-03-18 22:40:29 +00:00
|
|
|
var err error
|
|
|
|
o.Name, err = NameFromCommandArgs(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
restConfig, err := f.ToRESTConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Client, err = coreclient.NewForConfig(restConfig)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
o.CreateAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
|
|
|
|
|
|
|
o.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dynamicClient, err := f.DynamicClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.DryRunVerifier = resource.NewDryRunVerifier(dynamicClient, f.OpenAPIGetter())
|
|
|
|
|
|
|
|
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
|
|
|
|
|
|
|
printer, err := o.PrintFlags.ToPrinter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.PrintObj = func(obj runtime.Object) error {
|
|
|
|
return printer.PrintObj(obj, o.Out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// Validate checks ServiceAccountOpts to see if there is sufficient information run the command.
|
|
|
|
func (o *ServiceAccountOpts) Validate() error {
|
|
|
|
if len(o.Name) == 0 {
|
|
|
|
return fmt.Errorf("name must be specified")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run makes the api call to the server
|
2019-01-12 04:58:27 +00:00
|
|
|
func (o *ServiceAccountOpts) Run() error {
|
2021-03-18 22:40:29 +00:00
|
|
|
serviceAccount, err := o.createServiceAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, serviceAccount, scheme.DefaultJSONEncoder()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DryRunStrategy != cmdutil.DryRunClient {
|
|
|
|
createOptions := metav1.CreateOptions{}
|
|
|
|
if o.FieldManager != "" {
|
|
|
|
createOptions.FieldManager = o.FieldManager
|
|
|
|
}
|
|
|
|
if o.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
if err := o.DryRunVerifier.HasSupport(serviceAccount.GroupVersionKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createOptions.DryRun = []string{metav1.DryRunAll}
|
|
|
|
}
|
|
|
|
serviceAccount, err = o.Client.ServiceAccounts(o.Namespace).Create(context.TODO(), serviceAccount, createOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create serviceaccount: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o.PrintObj(serviceAccount)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
func (o *ServiceAccountOpts) createServiceAccount() (*corev1.ServiceAccount, error) {
|
|
|
|
namespace := ""
|
|
|
|
if o.EnforceNamespace {
|
|
|
|
namespace = o.Namespace
|
|
|
|
}
|
|
|
|
serviceAccount := &corev1.ServiceAccount{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: corev1.SchemeGroupVersion.String(), Kind: "ServiceAccount"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: o.Name,
|
|
|
|
Namespace: namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
serviceAccount.Name = o.Name
|
|
|
|
return serviceAccount, nil
|
2020-12-01 01:06:26 +00:00
|
|
|
}
|