2016-11-18 16:23:59 +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.
|
|
|
|
*/
|
|
|
|
|
2018-04-10 14:21:50 +00:00
|
|
|
package create
|
2016-11-18 16:23:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
2018-04-19 21:43:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2017-07-07 04:04:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
2016-11-18 16:23:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-15 03:49:10 +00:00
|
|
|
clusterRoleBindingLong = templates.LongDesc(i18n.T(`
|
|
|
|
Create a ClusterRoleBinding for a particular ClusterRole.`))
|
2016-11-18 16:23:59 +00:00
|
|
|
|
2017-03-15 03:49:10 +00:00
|
|
|
clusterRoleBindingExample = templates.Examples(i18n.T(`
|
2016-11-18 16:23:59 +00:00
|
|
|
# Create a ClusterRoleBinding for user1, user2, and group1 using the cluster-admin ClusterRole
|
2017-03-15 03:49:10 +00:00
|
|
|
kubectl create clusterrolebinding cluster-admin --clusterrole=cluster-admin --user=user1 --user=user2 --group=group1`))
|
2016-11-18 16:23:59 +00:00
|
|
|
)
|
|
|
|
|
2018-04-05 22:39:17 +00:00
|
|
|
type ClusterRoleBindingOpts struct {
|
|
|
|
CreateSubcommandOptions *CreateSubcommandOptions
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:23:59 +00:00
|
|
|
// ClusterRoleBinding is a command to ease creating ClusterRoleBindings.
|
2018-04-19 21:43:28 +00:00
|
|
|
func NewCmdCreateClusterRoleBinding(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
2018-04-05 22:39:17 +00:00
|
|
|
options := &ClusterRoleBindingOpts{
|
2018-04-19 21:43:28 +00:00
|
|
|
CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
|
2018-04-05 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:23:59 +00:00
|
|
|
cmd := &cobra.Command{
|
2017-10-11 06:26:02 +00:00
|
|
|
Use: "clusterrolebinding NAME --clusterrole=NAME [--user=username] [--group=groupname] [--serviceaccount=namespace:serviceaccountname] [--dry-run]",
|
|
|
|
DisableFlagsInUseLine: true,
|
2017-01-25 01:00:32 +00:00
|
|
|
Short: i18n.T("Create a ClusterRoleBinding for a particular ClusterRole"),
|
2016-11-18 16:23:59 +00:00
|
|
|
Long: clusterRoleBindingLong,
|
|
|
|
Example: clusterRoleBindingExample,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2018-04-05 22:39:17 +00:00
|
|
|
cmdutil.CheckErr(options.Complete(cmd, args))
|
|
|
|
cmdutil.CheckErr(options.Run(f))
|
2016-11-18 16:23:59 +00:00
|
|
|
},
|
|
|
|
}
|
2018-04-05 22:39:17 +00:00
|
|
|
|
|
|
|
options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
|
|
|
|
|
2016-11-18 16:23:59 +00:00
|
|
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
|
|
|
cmdutil.AddValidateFlags(cmd)
|
|
|
|
cmdutil.AddGeneratorFlags(cmd, cmdutil.ClusterRoleBindingV1GeneratorName)
|
2017-02-07 06:26:44 +00:00
|
|
|
cmd.Flags().String("clusterrole", "", i18n.T("ClusterRole this ClusterRoleBinding should reference"))
|
2017-06-29 13:12:28 +00:00
|
|
|
cmd.MarkFlagCustom("clusterrole", "__kubectl_get_resource_clusterrole")
|
2017-08-02 15:41:19 +00:00
|
|
|
cmd.Flags().StringArray("user", []string{}, "Usernames to bind to the role")
|
|
|
|
cmd.Flags().StringArray("group", []string{}, "Groups to bind to the role")
|
|
|
|
cmd.Flags().StringArray("serviceaccount", []string{}, "Service accounts to bind to the role, in the format <namespace>:<name>")
|
2016-11-18 16:23:59 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:39:17 +00:00
|
|
|
func (o *ClusterRoleBindingOpts) Complete(cmd *cobra.Command, args []string) error {
|
2016-11-18 16:23:59 +00:00
|
|
|
name, err := NameFromCommandArgs(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-05 22:39:17 +00:00
|
|
|
|
2016-11-18 16:23:59 +00:00
|
|
|
var generator kubectl.StructuredGenerator
|
|
|
|
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
|
|
|
|
case cmdutil.ClusterRoleBindingV1GeneratorName:
|
|
|
|
generator = &kubectl.ClusterRoleBindingGeneratorV1{
|
2016-12-08 19:18:39 +00:00
|
|
|
Name: name,
|
|
|
|
ClusterRole: cmdutil.GetFlagString(cmd, "clusterrole"),
|
2017-03-31 10:03:59 +00:00
|
|
|
Users: cmdutil.GetFlagStringArray(cmd, "user"),
|
|
|
|
Groups: cmdutil.GetFlagStringArray(cmd, "group"),
|
|
|
|
ServiceAccounts: cmdutil.GetFlagStringArray(cmd, "serviceaccount"),
|
2016-11-18 16:23:59 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-05-25 19:44:43 +00:00
|
|
|
return errUnsupportedGenerator(cmd, generatorName)
|
2016-11-18 16:23:59 +00:00
|
|
|
}
|
2018-04-05 22:39:17 +00:00
|
|
|
|
|
|
|
return o.CreateSubcommandOptions.Complete(cmd, args, generator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateClusterRoleBinding is the implementation of the create clusterrolebinding command.
|
|
|
|
func (o *ClusterRoleBindingOpts) Run(f cmdutil.Factory) error {
|
|
|
|
return RunCreateSubcommand(f, o.CreateSubcommandOptions)
|
2016-11-18 16:23:59 +00:00
|
|
|
}
|