-
Notifications
You must be signed in to change notification settings - Fork 4.8k
new project #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new project #999
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" | ||
| "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| authorizationapi "github.com/openshift/origin/pkg/authorization/api" | ||
| "github.com/openshift/origin/pkg/client" | ||
| cmdutil "github.com/openshift/origin/pkg/cmd/util" | ||
| projectapi "github.com/openshift/origin/pkg/project/api" | ||
| ) | ||
|
|
||
| type newProjectOptions struct { | ||
| projectName string | ||
| displayName string | ||
| description string | ||
| clientConfig clientcmd.ClientConfig | ||
|
|
||
| adminRole string | ||
| masterPolicyNamespace string | ||
| adminUser string | ||
| } | ||
|
|
||
| func NewCmdNewProject(name string) *cobra.Command { | ||
| options := &newProjectOptions{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: name + " <project-name>", | ||
| Short: "create a new project", | ||
| Long: `create a new project`, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| if !options.complete(cmd) { | ||
| return | ||
| } | ||
|
|
||
| err := options.run() | ||
| if err != nil { | ||
| fmt.Printf("%v\n", err) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| // Override global default to https and port 8443 | ||
| clientcmd.DefaultCluster.Server = "https://localhost:8443" | ||
| clientConfig := cmdutil.DefaultClientConfig(cmd.Flags()) | ||
| options.clientConfig = clientConfig | ||
|
|
||
| // TODO remove once we have global policy objects | ||
| cmd.Flags().StringVar(&options.masterPolicyNamespace, "master-policy-namespace", "master", "master policy namespace") | ||
| cmd.Flags().StringVar(&options.adminRole, "admin-role", "admin", "project admin role name in the master policy namespace") | ||
| cmd.Flags().StringVar(&options.adminUser, "admin", "", "project admin username") | ||
| cmd.Flags().StringVar(&options.displayName, "display-name", "", "project display name") | ||
| cmd.Flags().StringVar(&options.description, "description", "", "project description") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (o *newProjectOptions) complete(cmd *cobra.Command) bool { | ||
| args := cmd.Flags().Args() | ||
| if len(args) != 1 { | ||
| cmd.Help() | ||
| return false | ||
| } | ||
|
|
||
| o.projectName = args[0] | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func (o *newProjectOptions) run() error { | ||
| clientConfig, err := o.clientConfig.ClientConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| client, err := client.New(clientConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = client.Projects().Get(o.projectName) | ||
| projectFound := !kerrors.IsNotFound(err) | ||
| if (err != nil) && (projectFound) { | ||
| return err | ||
| } | ||
| if projectFound { | ||
| return fmt.Errorf("project %v already exists", o.projectName) | ||
| } | ||
|
|
||
| project := &projectapi.Project{} | ||
| project.Name = o.projectName | ||
| project.DisplayName = o.displayName | ||
| project.Annotations = make(map[string]string) | ||
| project.Annotations["description"] = o.description | ||
| project, err = client.Projects().Create(project) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(o.adminUser) != 0 { | ||
| adminRoleBinding := &authorizationapi.RoleBinding{} | ||
|
|
||
| adminRoleBinding.Name = "admins" | ||
| adminRoleBinding.RoleRef.Namespace = o.masterPolicyNamespace | ||
| adminRoleBinding.RoleRef.Name = o.adminRole | ||
| adminRoleBinding.UserNames = []string{o.adminUser} | ||
|
|
||
| _, err := client.RoleBindings(project.Name).Create(adminRoleBinding) | ||
| if err != nil { | ||
| fmt.Printf("The project %v was created, but %v could not be added to the %v role.\n", o.projectName, o.adminUser, o.adminRole) | ||
| fmt.Printf("To add the user to the existing project, run\n\n\topenshift ex policy add-user --namespace=%v --role-namespace=%v %v %v\n", o.projectName, o.masterPolicyNamespace, o.adminRole, o.adminUser) | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/spf13/pflag" | ||
|
|
||
| "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" | ||
| ) | ||
|
|
||
| // Copy of kubectl/cmd/DefaultClientConfig, using NewNonInteractiveDeferredLoadingClientConfig | ||
| func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add back the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added back |
||
| loadingRules := clientcmd.NewClientConfigLoadingRules() | ||
| loadingRules.EnvVarPath = os.Getenv(clientcmd.RecommendedConfigPathEnvVar) | ||
| flags.StringVar(&loadingRules.CommandLinePath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.") | ||
|
|
||
| overrides := &clientcmd.ConfigOverrides{} | ||
| overrideFlags := clientcmd.RecommendedConfigOverrideFlags("") | ||
| overrideFlags.ContextOverrideFlags.NamespaceShort = "n" | ||
| clientcmd.BindOverrideFlags(overrides, flags, overrideFlags) | ||
|
|
||
| clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides) | ||
|
|
||
| return clientConfig | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want a TODO to remove this once master policy namespace goes away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.