@@ -2,21 +2,20 @@ package set
2
2
3
3
import (
4
4
"fmt"
5
- "io"
6
- "os"
7
5
8
6
"github.com/spf13/cobra"
7
+
9
8
"k8s.io/apimachinery/pkg/api/meta"
10
- "k8s.io/apimachinery/pkg/runtime"
11
9
"k8s.io/apimachinery/pkg/types"
10
+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
12
11
"k8s.io/client-go/dynamic"
13
12
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
14
13
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
15
14
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
16
15
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
16
+ "k8s.io/kubernetes/pkg/printers"
17
17
18
- buildapi "github.com/openshift/origin/pkg/build/apis/build"
19
- "github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
18
+ buildv1 "github.com/openshift/api/build/v1"
20
19
"github.com/openshift/origin/pkg/oc/util/ocscheme"
21
20
)
22
21
@@ -52,126 +51,99 @@ var (
52
51
)
53
52
54
53
type BuildHookOptions struct {
55
- Out io.Writer
56
- Err io.Writer
57
-
58
- Builder * resource.Builder
59
- Infos []* resource.Info
60
-
61
- Encoder runtime.Encoder
62
-
63
- Filenames []string
64
- Selector string
65
- All bool
66
- Output string
67
-
68
- Cmd * cobra.Command
69
-
70
- Local bool
71
- ShortOutput bool
72
- Mapper meta.RESTMapper
73
- Client dynamic.Interface
74
-
75
- PrintObject func ([]* resource.Info ) error
54
+ PrintFlags * genericclioptions.PrintFlags
76
55
56
+ Selector string
57
+ All bool
58
+ Output string
59
+ Local bool
77
60
Script string
78
61
Entrypoint bool
79
62
Remove bool
80
63
PostCommit bool
81
64
82
- Command []string
65
+ Mapper meta.RESTMapper
66
+ Client dynamic.Interface
67
+ Printer printers.ResourcePrinter
68
+ Builder func () * resource.Builder
69
+ Namespace string
70
+ ExplicitNamespace bool
71
+ Command []string
72
+ Resources []string
73
+ DryRun bool
74
+
75
+ resource.FilenameOptions
76
+ genericclioptions.IOStreams
77
+ }
78
+
79
+ func NewBuildHookOptions (streams genericclioptions.IOStreams ) * BuildHookOptions {
80
+ return & BuildHookOptions {
81
+ PrintFlags : genericclioptions .NewPrintFlags ("hooks updated" ).WithTypeSetter (ocscheme .PrintingInternalScheme ),
82
+ IOStreams : streams ,
83
+ }
83
84
}
84
85
85
86
// NewCmdBuildHook implements the set build-hook command
86
87
func NewCmdBuildHook (fullName string , f kcmdutil.Factory , streams genericclioptions.IOStreams ) * cobra.Command {
87
- options := & BuildHookOptions {
88
- Out : streams .Out ,
89
- Err : streams .ErrOut ,
90
- }
88
+ o := NewBuildHookOptions (streams )
91
89
cmd := & cobra.Command {
92
90
Use : "build-hook BUILDCONFIG --post-commit [--command] [--script] -- CMD" ,
93
91
Short : "Update a build hook on a build config" ,
94
92
Long : buildHookLong ,
95
93
Example : fmt .Sprintf (buildHookExample , fullName ),
96
94
Run : func (cmd * cobra.Command , args []string ) {
97
- kcmdutil .CheckErr (options .Complete (f , cmd , args ))
98
- kcmdutil .CheckErr (options .Validate ())
99
- if err := options .Run (); err != nil {
100
- // TODO: move me to kcmdutil
101
- if err == kcmdutil .ErrExit {
102
- os .Exit (1 )
103
- }
104
- kcmdutil .CheckErr (err )
105
- }
95
+ kcmdutil .CheckErr (o .Complete (f , cmd , args ))
96
+ kcmdutil .CheckErr (o .Validate ())
97
+ kcmdutil .CheckErr (o .Run ())
106
98
},
107
99
}
108
-
109
- kcmdutil .AddPrinterFlags (cmd )
110
- cmd .Flags ().StringVarP (& options .Selector , "selector" , "l" , options .Selector , "Selector (label query) to filter build configs" )
111
- cmd .Flags ().BoolVar (& options .All , "all" , options .All , "If true, select all build configs in the namespace" )
112
- cmd .Flags ().StringSliceVarP (& options .Filenames , "filename" , "f" , options .Filenames , "Filename, directory, or URL to file to use to edit the resource." )
113
-
114
- cmd .Flags ().BoolVar (& options .PostCommit , "post-commit" , options .PostCommit , "If true, set the post-commit build hook on a build config" )
115
- cmd .Flags ().BoolVar (& options .Entrypoint , "command" , options .Entrypoint , "If true, set the entrypoint of the hook container to the given command" )
116
- cmd .Flags ().StringVar (& options .Script , "script" , options .Script , "Specify a script to run for the build-hook" )
117
- cmd .Flags ().BoolVar (& options .Remove , "remove" , options .Remove , "If true, remove the build hook." )
118
- cmd .Flags ().BoolVar (& options .Local , "local" , false , "If true, set image will NOT contact api-server but run locally." )
119
-
120
- cmd .MarkFlagFilename ("filename" , "yaml" , "yml" , "json" )
100
+ usage := "to use to edit the resource"
101
+ kcmdutil .AddFilenameOptionFlags (cmd , & o .FilenameOptions , usage )
102
+ cmd .Flags ().StringVarP (& o .Selector , "selector" , "l" , o .Selector , "Selector (label query) to filter build configs" )
103
+ cmd .Flags ().BoolVar (& o .All , "all" , o .All , "If true, select all build configs in the namespace" )
104
+ cmd .Flags ().BoolVar (& o .PostCommit , "post-commit" , o .PostCommit , "If true, set the post-commit build hook on a build config" )
105
+ cmd .Flags ().BoolVar (& o .Entrypoint , "command" , o .Entrypoint , "If true, set the entrypoint of the hook container to the given command" )
106
+ cmd .Flags ().StringVar (& o .Script , "script" , o .Script , "Specify a script to run for the build-hook" )
107
+ cmd .Flags ().BoolVar (& o .Remove , "remove" , o .Remove , "If true, remove the build hook." )
108
+ cmd .Flags ().BoolVar (& o .Local , "local" , o .Local , "If true, set image will NOT contact api-server but run locally." )
109
+
110
+ o .PrintFlags .AddFlags (cmd )
121
111
kcmdutil .AddDryRunFlag (cmd )
122
112
123
113
return cmd
124
114
}
125
115
126
116
func (o * BuildHookOptions ) Complete (f kcmdutil.Factory , cmd * cobra.Command , args []string ) error {
127
- resources : = args
117
+ o . Resources = args
128
118
if i := cmd .ArgsLenAtDash (); i != - 1 {
129
- resources = args [:i ]
119
+ o . Resources = args [:i ]
130
120
o .Command = args [i :]
131
121
}
132
122
if len (o .Filenames ) == 0 && len (args ) < 1 {
133
123
return kcmdutil .UsageErrorf (cmd , "one or more build configs must be specified as <name> or <resource>/<name>" )
134
124
}
135
125
136
- cmdNamespace , explicit , err := f .ToRawKubeConfigLoader ().Namespace ()
126
+ var err error
127
+ o .Namespace , o .ExplicitNamespace , err = f .ToRawKubeConfigLoader ().Namespace ()
137
128
if err != nil {
138
129
return err
139
130
}
140
131
141
- o .Cmd = cmd
142
-
143
- mapper , err := f .ToRESTMapper ()
132
+ o .DryRun = kcmdutil .GetDryRunFlag (cmd )
133
+ o .Mapper , err = f .ToRESTMapper ()
144
134
if err != nil {
145
135
return err
146
136
}
147
- o .Builder = f .NewBuilder ().
148
- WithScheme (ocscheme .ReadingInternalScheme ).
149
- LocalParam (o .Local ).
150
- ContinueOnError ().
151
- NamespaceParam (cmdNamespace ).DefaultNamespace ().
152
- FilenameParam (explicit , & resource.FilenameOptions {Recursive : false , Filenames : o .Filenames }).
153
- LabelSelectorParam (o .Selector ).
154
- ResourceNames ("buildconfigs" , resources ... ).
155
- Flatten ()
137
+ o .Builder = f .NewBuilder
156
138
157
- if ! o .Local {
158
- o .Builder = o .Builder .
159
- LabelSelectorParam (o .Selector ).
160
- ResourceNames ("buildconfigs" , resources ... )
161
- if o .All {
162
- o .Builder .ResourceTypes ("buildconfigs" ).SelectAllParam (o .All )
163
- }
139
+ if o .DryRun {
140
+ o .PrintFlags .Complete ("%s (dry run)" )
164
141
}
165
-
166
- o .Output = kcmdutil .GetFlagString (cmd , "output" )
167
- o .PrintObject = func (infos []* resource.Info ) error {
168
- return clientcmd .PrintResourceInfos (cmd , infos , o .Out )
142
+ o .Printer , err = o .PrintFlags .ToPrinter ()
143
+ if err != nil {
144
+ return err
169
145
}
170
146
171
- o .Encoder = kcmdutil .InternalVersionJSONEncoder ()
172
- o .ShortOutput = kcmdutil .GetFlagString (cmd , "output" ) == "name"
173
- o .Mapper = mapper
174
-
175
147
clientConfig , err := f .ToRESTConfig ()
176
148
if err != nil {
177
149
return err
@@ -185,7 +157,6 @@ func (o *BuildHookOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args
185
157
}
186
158
187
159
func (o * BuildHookOptions ) Validate () error {
188
-
189
160
if ! o .PostCommit {
190
161
return fmt .Errorf ("you must specify a type of hook to set" )
191
162
}
@@ -212,18 +183,32 @@ func (o *BuildHookOptions) Validate() error {
212
183
}
213
184
214
185
func (o * BuildHookOptions ) Run () error {
215
- infos := o .Infos
216
- singleItemImplied := len (o .Infos ) <= 1
217
- if o .Builder != nil {
218
- loaded , err := o .Builder .Do ().IntoSingleItemImplied (& singleItemImplied ).Infos ()
219
- if err != nil {
220
- return err
186
+ b := o .Builder ().
187
+ WithScheme (ocscheme .ReadingInternalScheme ).
188
+ LocalParam (o .Local ).
189
+ ContinueOnError ().
190
+ NamespaceParam (o .Namespace ).DefaultNamespace ().
191
+ FilenameParam (o .ExplicitNamespace , & o .FilenameOptions ).
192
+ Flatten ()
193
+
194
+ if ! o .Local {
195
+ b = b .
196
+ LabelSelectorParam (o .Selector ).
197
+ ResourceNames ("buildconfigs" , o .Resources ... ).
198
+ Latest ()
199
+ if o .All {
200
+ b = b .ResourceTypes ("buildconfigs" ).SelectAllParam (o .All )
221
201
}
222
- infos = loaded
223
202
}
224
203
225
- patches := CalculatePatches (infos , o .Encoder , func (info * resource.Info ) (bool , error ) {
226
- bc , ok := info .Object .(* buildapi.BuildConfig )
204
+ singleItemImplied := false
205
+ infos , err := b .Do ().IntoSingleItemImplied (& singleItemImplied ).Infos ()
206
+ if err != nil {
207
+ return err
208
+ }
209
+
210
+ patches := CalculatePatchesExternal (infos , func (info * resource.Info ) (bool , error ) {
211
+ bc , ok := info .Object .(* buildv1.BuildConfig )
227
212
if ! ok {
228
213
return false , nil
229
214
}
@@ -235,39 +220,40 @@ func (o *BuildHookOptions) Run() error {
235
220
return fmt .Errorf ("%s/%s is not a build config" , infos [0 ].Mapping .Resource , infos [0 ].Name )
236
221
}
237
222
238
- if len (o .Output ) > 0 || o .Local || kcmdutil .GetDryRunFlag (o .Cmd ) {
239
- return o .PrintObject (infos )
240
- }
241
-
242
- failed := false
223
+ allErrs := []error {}
243
224
for _ , patch := range patches {
244
225
info := patch .Info
245
226
if patch .Err != nil {
246
- fmt . Fprintf ( o . Err , "error: %s/%s %v\n " , info .Mapping .Resource , info .Name , patch .Err )
227
+ allErrs = append ( allErrs , fmt . Errorf ( "error: %s/%s %v\n " , info .Mapping .Resource , info .Name , patch .Err ) )
247
228
continue
248
229
}
249
230
250
231
if string (patch .Patch ) == "{}" || len (patch .Patch ) == 0 {
251
- fmt .Fprintf (o .Err , "info: %s %q was not changed\n " , info .Mapping .Resource , info .Name )
232
+ fmt .Fprintf (o .ErrOut , "info: %s %q was not changed\n " , info .Mapping .Resource , info .Name )
233
+ continue
234
+ }
235
+
236
+ if o .Local || o .DryRun {
237
+ if err := o .Printer .PrintObj (info .Object , o .Out ); err != nil {
238
+ allErrs = append (allErrs , err )
239
+ }
252
240
continue
253
241
}
254
242
255
243
actual , err := o .Client .Resource (info .Mapping .Resource ).Namespace (info .Namespace ).Patch (info .Name , types .StrategicMergePatchType , patch .Patch )
256
244
if err != nil {
257
- fmt .Fprintf (o .Err , "error: %v\n " , err )
258
- failed = true
245
+ allErrs = append (allErrs , fmt .Errorf ("failed to patch build hook: %v\n " , err ))
259
246
continue
260
247
}
261
248
262
- kcmdutil .PrintSuccess (o .ShortOutput , o .Out , actual , false , "updated" )
263
- }
264
- if failed {
265
- return kcmdutil .ErrExit
249
+ if err := o .Printer .PrintObj (actual , o .Out ); err != nil {
250
+ allErrs = append (allErrs , err )
251
+ }
266
252
}
267
- return nil
253
+ return utilerrors . NewAggregate ( allErrs )
268
254
}
269
255
270
- func (o * BuildHookOptions ) updateBuildConfig (bc * buildapi .BuildConfig ) {
256
+ func (o * BuildHookOptions ) updateBuildConfig (bc * buildv1 .BuildConfig ) {
271
257
if o .Remove {
272
258
bc .Spec .PostCommit .Args = nil
273
259
bc .Spec .PostCommit .Command = nil
0 commit comments