// RunProject contains all the necessary functionality for the OpenShift cli project command func (o ProjectOptions) RunProject() error { config := o.Config clientCfg := o.ClientConfig out := o.Out // No argument provided, we will just print info if len(o.ProjectName) == 0 { currentContext := config.Contexts[config.CurrentContext] currentProject := currentContext.Namespace if len(currentProject) > 0 { _, err := o.Client.Projects().Get(currentProject) if err != nil { if kapierrors.IsNotFound(err) { return fmt.Errorf("the project %q specified in your config does not exist.", currentProject) } if clientcmd.IsForbidden(err) { return fmt.Errorf("you do not have rights to view project %q.", currentProject) } return err } if config.CurrentContext != currentProject { if len(currentProject) > 0 { fmt.Fprintf(out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host) } else { fmt.Fprintf(out, "Using context named %q on server %q.\n", config.CurrentContext, clientCfg.Host) } } else { fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host) } } else { fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n") } return nil } // We have an argument that can be either a context or project argument := o.ProjectName contextInUse := "" namespaceInUse := "" contextNameIsGenerated := false // Check if argument is an existing context, if so just set it as the context in use. // If not a context then we will try to handle it as a project. if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists { contextInUse = argument namespaceInUse = context.Namespace config.CurrentContext = argument } else { project, err := o.Client.Projects().Get(argument) if err != nil { if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden { var msg string if isForbidden { msg = fmt.Sprintf("You are not a member of project %q.", argument) } else { msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host) } projects, err := getProjects(o.Client) if err == nil { switch len(projects) { case 0: msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command." case 1: msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0])) default: msg += "\nYour projects are:" for _, project := range projects { msg += fmt.Sprintf("\n* %s", api.DisplayNameAndNameForProject(&project)) } } } if hasMultipleServers(config) { msg += "\nTo see projects on another server, pass '--server=<server>'." } return errors.New(msg) } return err } kubeconfig, err := cliconfig.CreateConfig(project.Name, o.ClientConfig) if err != nil { return err } merged, err := cliconfig.MergeConfig(config, *kubeconfig) if err != nil { return err } config = *merged namespaceInUse = project.Name contextInUse = merged.CurrentContext contextNameIsGenerated = true } if err := kubecmdconfig.ModifyConfig(o.PathOptions, config); err != nil { return err } if contextInUse != namespaceInUse && !contextNameIsGenerated { if len(namespaceInUse) > 0 { fmt.Fprintf(out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host) } else { fmt.Fprintf(out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host) } } else { fmt.Fprintf(out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host) } return nil }
// Describe returns the description of a project func (d *ProjectStatusDescriber) Describe(namespace, name string) (string, error) { project, err := d.C.Projects().Get(namespace) if err != nil { return "", err } svcs, err := d.K.Services(namespace).List(labels.Everything()) if err != nil { return "", err } bcs, err := d.C.BuildConfigs(namespace).List(labels.Everything(), fields.Everything()) if err != nil { return "", err } dcs, err := d.C.DeploymentConfigs(namespace).List(labels.Everything(), fields.Everything()) if err != nil { return "", err } builds := &buildapi.BuildList{} if len(bcs.Items) > 0 { if b, err := d.C.Builds(namespace).List(labels.Everything(), fields.Everything()); err == nil { builds = b } } rcs, err := d.K.ReplicationControllers(namespace).List(labels.Everything()) if err != nil { rcs = &kapi.ReplicationControllerList{} } g := graph.New() for i := range bcs.Items { build := graph.BuildConfig(g, &bcs.Items[i]) graph.JoinBuilds(build.(*graph.BuildConfigNode), builds.Items) } for i := range dcs.Items { deploy := graph.DeploymentConfig(g, &dcs.Items[i]) graph.JoinDeployments(deploy.(*graph.DeploymentConfigNode), rcs.Items) } for i := range svcs.Items { graph.Service(g, &svcs.Items[i]) } groups := graph.ServiceAndDeploymentGroups(graph.CoverServices(g)) return tabbedString(func(out *tabwriter.Writer) error { indent := " " fmt.Fprintf(out, "In project %s\n", projectapi.DisplayNameAndNameForProject(project)) for _, group := range groups { if len(group.Builds) != 0 { for _, build := range group.Builds { fmt.Fprintln(out) printLines(out, indent, 0, describeStandaloneBuildGroup(build, namespace)...) printLines(out, indent, 1, describeAdditionalBuildDetail(build.Build, true)...) } continue } if len(group.Services) == 0 { for _, deploy := range group.Deployments { fmt.Fprintln(out) printLines(out, indent, 0, describeDeploymentInServiceGroup(deploy)...) } continue } fmt.Fprintln(out) for _, svc := range group.Services { printLines(out, indent, 0, describeServiceInServiceGroup(svc)...) } for _, deploy := range group.Deployments { printLines(out, indent, 1, describeDeploymentInServiceGroup(deploy)...) } } if len(groups) == 0 { fmt.Fprintln(out, "\nYou have no Services, DeploymentConfigs, or BuildConfigs. 'oc new-app' can be used to create applications from scratch from existing Docker images and templates.") } else { fmt.Fprintln(out, "\nTo see more information about a Service or DeploymentConfig, use 'oc describe service <name>' or 'oc describe dc <name>'.") fmt.Fprintln(out, "You can use 'oc get all' to see lists of each of the types described above.") } return nil }) }