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