Skip to content

Commit 70c2279

Browse files
committed
Provide easy delete of resources for new-app and generate
1 parent 2d38380 commit 70c2279

File tree

7 files changed

+92
-10
lines changed

7 files changed

+92
-10
lines changed

hack/test-cmd.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,18 @@ echo "ex registry: ok"
449449
[ "$(openshift ex build-chain --all -o dot | grep 'graph')" ]
450450
echo "ex build-chain: ok"
451451

452-
osc get minions,pods
453-
454452
osadm new-project example --admin="createuser"
455453
osc project example
456454
osc create -f test/fixtures/app-scenarios
457455
osc status
458-
echo "complex-scenarios: ok"
456+
echo "complex-scenarios: ok"
457+
458+
# Clean-up everything before testing cleaning up everything...
459+
osc delete all -l template=application-template-stibuild
460+
osc process -f examples/sample-app/application-template-stibuild.json -l name=mytemplate | osc create -f -
461+
osc delete all -l name=mytemplate
462+
osc new-app https://github.com/openshift/ruby-hello-world -l name=hello-world
463+
osc delete all -l name=hello-world
464+
openshift ex generate https://github.com/openshift/ruby-hello-world -l name=hello-world | osc create -f -
465+
osc delete all -l name=hello-world
466+
echo "delete all: ok"

pkg/api/latest/latest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ var originTypes = []string{
7979
"Role", "RoleBinding", "Policy", "PolicyBinding", "ResourceAccessReview", "SubjectAccessReview",
8080
}
8181

82+
// UserResources is a group of user-defined resources
83+
var UserResources = []string{"bc", "builds", "services", "imageStreams", "pods", "routes", "dc", "rc"}
84+
8285
// OriginKind returns true if OpenShift owns the kind described in a given apiVersion.
8386
// TODO: make this based on scheme information or other behavior
8487
func OriginKind(kind, apiVersion string) bool {

pkg/api/meta/multimapper.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ func (m MultiRESTMapper) RESTMapping(kind string, versions ...string) (mapping *
3232
}
3333
return
3434
}
35+
36+
// AliasesForResource returns whether a resource has an alias or not. This implementation
37+
// supports multiple REST schemas and return the first match.
38+
func (m MultiRESTMapper) AliasesForResource(resource string) (aliases []string, has bool) {
39+
for _, t := range m {
40+
aliases, has = t.AliasesForResource(resource)
41+
if has {
42+
return
43+
}
44+
}
45+
return
46+
}

pkg/cmd/cli/cmd/newapp.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
9+
ctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
910
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
1011
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
1112
"github.com/golang/glog"
@@ -17,6 +18,7 @@ import (
1718
configcmd "github.com/openshift/origin/pkg/config/cmd"
1819
newcmd "github.com/openshift/origin/pkg/generate/app/cmd"
1920
imageapi "github.com/openshift/origin/pkg/image/api"
21+
"github.com/openshift/origin/pkg/util"
2022
)
2123

2224
type usage interface {
@@ -46,6 +48,9 @@ Examples:
4648
# Use a MySQL image in a private registry to create an app
4749
$ %[1]s new-app myregistry.com/mycompany/mysql
4850
51+
# Create an application from the remote repository using the specified label
52+
$ %[1]s new-app https://github.com/openshift/ruby-hello-world -l name=hello-world
53+
4954
If you specify source code, you may need to run a build with 'start-build' after the
5055
application is created.
5156
@@ -76,6 +81,7 @@ func NewCmdNewApplication(fullName string, f *clientcmd.Factory, out io.Writer)
7681
cmd.Flags().Var(&config.Groups, "group", "Indicate components that should be grouped together as <comp1>+<comp2>.")
7782
cmd.Flags().VarP(&config.Environment, "env", "e", "Specify key value pairs of environment variables to set into each container.")
7883
cmd.Flags().StringVar(&config.TypeOfBuild, "build", "", "Specify the type of build to use if you don't want to detect (docker|source)")
84+
cmd.Flags().StringP("labels", "l", "", "Label to set in all resources for this application")
7985

8086
cmdutil.AddPrinterFlags(cmd)
8187

@@ -121,6 +127,17 @@ func RunNewApplication(f *clientcmd.Factory, out io.Writer, c *cobra.Command, ar
121127
return err
122128
}
123129

130+
label := cmdutil.GetFlagString(c, "labels")
131+
if len(label) != 0 {
132+
lbl := ctl.ParseLabels(label)
133+
for _, object := range result.List.Items {
134+
err = util.AddObjectLabels(object, lbl)
135+
if err != nil {
136+
return err
137+
}
138+
}
139+
}
140+
124141
if len(cmdutil.GetFlagString(c, "output")) != 0 {
125142
return f.Factory.PrintObject(c, result.List, out)
126143
}

pkg/cmd/cli/cmd/process.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
99
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
1010
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
11-
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
11+
ctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
12+
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
1213
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
1314
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
1415
"github.com/golang/glog"
@@ -23,7 +24,7 @@ import (
2324
// injectUserVars injects user specified variables into the Template
2425
func injectUserVars(cmd *cobra.Command, t *api.Template) {
2526
values := util.StringList{}
26-
values.Set(cmdutil.GetFlagString(cmd, "value"))
27+
values.Set(kcmdutil.GetFlagString(cmd, "value"))
2728
for _, keypair := range values {
2829
p := strings.SplitN(keypair, "=", 2)
2930
if len(p) != 2 {
@@ -49,6 +50,9 @@ Examples:
4950
# Convert template.json file into resource list
5051
$ %[1]s process -f template.json
5152
53+
# Process template while passing a user-defined label
54+
$ %[1]s process -f template.json -l name=mytemplate
55+
5256
# Convert stored template into resource list
5357
$ %[1]s process foo
5458
@@ -64,15 +68,16 @@ func NewCmdProcess(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.
6468
Long: fmt.Sprintf(processLongDesc, fullName),
6569
Run: func(cmd *cobra.Command, args []string) {
6670
err := RunProcess(f, out, cmd, args)
67-
cmdutil.CheckErr(err)
71+
kcmdutil.CheckErr(err)
6872
},
6973
}
7074

71-
cmdutil.AddPrinterFlags(cmd)
75+
kcmdutil.AddPrinterFlags(cmd)
7276

7377
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
7478
cmd.Flags().StringP("value", "v", "", "Specify a list of key-value pairs (eg. -v FOO=BAR,BAR=FOO) to set/override parameter values")
7579
cmd.Flags().BoolP("parameters", "", false, "Do not process but only print available parameters")
80+
cmd.Flags().StringP("labels", "l", "", "Label to set in all resources for this template")
7681
return cmd
7782
}
7883

@@ -82,9 +87,9 @@ func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []
8287
storedTemplate = args[0]
8388
}
8489

85-
filename := cmdutil.GetFlagString(cmd, "filename")
90+
filename := kcmdutil.GetFlagString(cmd, "filename")
8691
if len(storedTemplate) == 0 && len(filename) == 0 {
87-
return cmdutil.UsageError(cmd, "Must pass a filename or name of stored template")
92+
return kcmdutil.UsageError(cmd, "Must pass a filename or name of stored template")
8893
}
8994

9095
namespace, err := f.DefaultNamespace()
@@ -158,14 +163,22 @@ func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []
158163

159164
// If 'parameters' flag is set it does not do processing but only print
160165
// the template parameters to console for inspection.
161-
if cmdutil.GetFlagBool(cmd, "parameters") == true {
166+
if kcmdutil.GetFlagBool(cmd, "parameters") == true {
162167
err = describe.PrintTemplateParameters(templateObj.Parameters, out)
163168
if err != nil {
164169
return err
165170
}
166171
return nil
167172
}
168173

174+
label := kcmdutil.GetFlagString(cmd, "labels")
175+
if len(label) != 0 {
176+
lbl := ctl.ParseLabels(label)
177+
for key, value := range lbl {
178+
templateObj.ObjectLabels[key] = value
179+
}
180+
}
181+
169182
result, err := client.TemplateConfigs(namespace).Create(templateObj)
170183
if err != nil {
171184
return err

pkg/cmd/experimental/generate/generate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
10+
ctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
1011
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
1112
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
1213
"github.com/fsouza/go-dockerclient"
@@ -21,6 +22,7 @@ import (
2122
genapp "github.com/openshift/origin/pkg/generate/app"
2223
gen "github.com/openshift/origin/pkg/generate/generator"
2324
"github.com/openshift/origin/pkg/generate/source"
25+
"github.com/openshift/origin/pkg/util"
2426
)
2527

2628
const longDescription = `
@@ -50,6 +52,9 @@ Examples:
5052
# Find a git repository in the current directory and build artifacts based on detection
5153
$ openshift ex generate
5254
55+
# Build artifacts from a git repository in the current directory using the specified label
56+
$ openshift ex generate -l name=hello-world
57+
5358
# Specify the directory for the repository to use
5459
$ openshift ex generate ./repo/dir
5560
@@ -97,6 +102,7 @@ func NewCmdGenerate(f *clientcmd.Factory, parentName, name string, out io.Writer
97102
flag.StringVarP(&input.port, "port", "p", "", "Port to expose on pod deployment")
98103
flag.StringVarP(&environment, "environment", "e", "", "Comma-separated list of environment variables to add to the deployment."+
99104
"Should be in the form of var1=value1,var2=value2,...")
105+
flag.StringP("labels", "l", "", "Label to set in all resources for this configuration")
100106
kcmdutil.AddPrinterFlags(c)
101107
dockerHelper.InstallFlags(flag)
102108
return c
@@ -150,6 +156,17 @@ func RunGenerate(f *clientcmd.Factory, out io.Writer, c *cobra.Command, args []s
150156
return err
151157
}
152158

159+
label := kcmdutil.GetFlagString(c, "labels")
160+
if len(label) != 0 {
161+
lbl := ctl.ParseLabels(label)
162+
for _, object := range list.Items {
163+
err = util.AddObjectLabels(object, lbl)
164+
if err != nil {
165+
return err
166+
}
167+
}
168+
}
169+
153170
// Output config
154171
setDefaultPrinter(c)
155172
return f.Factory.PrintObject(c, list, out)

pkg/cmd/util/clientcmd/factory.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ func (e ShortcutExpander) VersionAndKindForResource(resource string) (defaultVer
134134
return e.RESTMapper.VersionAndKindForResource(resource)
135135
}
136136

137+
// AliasesForResource returns whether a resource has an alias or not
138+
func (e ShortcutExpander) AliasesForResource(resource string) ([]string, bool) {
139+
aliases := map[string][]string{
140+
"all": latest.UserResources,
141+
}
142+
143+
if res, ok := aliases[resource]; ok {
144+
return res, true
145+
}
146+
return nil, false
147+
}
148+
137149
// expandResourceShortcut will return the expanded version of resource
138150
// (something that a pkg/api/meta.RESTMapper can understand), if it is
139151
// indeed a shortcut. Otherwise, will return resource unmodified.

0 commit comments

Comments
 (0)