Skip to content

Commit 54f426e

Browse files
committed
Update set
1 parent 911947b commit 54f426e

13 files changed

+1167
-1250
lines changed

hack/lib/start.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ function os::start::registry() {
631631
# For testing purposes, ensure the quota objects are always up to date in the registry by
632632
# disabling project cache.
633633
oc adm registry --config="${ADMIN_KUBECONFIG}" --images="${USE_IMAGES}" --enforce-quota -o json | \
634-
oc set env --config="${ADMIN_KUBECONFIG}" -f - --output json "REGISTRY_MIDDLEWARE_REPOSITORY_OPENSHIFT_PROJECTCACHETTL=0" | \
634+
oc set env --config="${ADMIN_KUBECONFIG}" --local -f - --output json "REGISTRY_MIDDLEWARE_REPOSITORY_OPENSHIFT_PROJECTCACHETTL=0" | \
635635
oc create --config="${ADMIN_KUBECONFIG}" -f -
636636
}
637637
readonly -f os::start::registry

pkg/oc/cli/cmd/set/buildhook.go

+93-108
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ package set
22

33
import (
44
"fmt"
5-
"io"
6-
"os"
75

86
"github.com/spf13/cobra"
7+
98
"k8s.io/apimachinery/pkg/api/meta"
10-
"k8s.io/apimachinery/pkg/runtime"
119
"k8s.io/apimachinery/pkg/types"
10+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1211
"k8s.io/client-go/dynamic"
1312
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1413
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1514
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
1615
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
16+
"k8s.io/kubernetes/pkg/printers"
1717

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"
2019
"github.com/openshift/origin/pkg/oc/util/ocscheme"
2120
)
2221

@@ -52,126 +51,98 @@ var (
5251
)
5352

5453
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
7655

56+
Selector string
57+
All bool
58+
Local bool
7759
Script string
7860
Entrypoint bool
7961
Remove bool
8062
PostCommit bool
8163

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+
}
8383
}
8484

8585
// NewCmdBuildHook implements the set build-hook command
8686
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)
9188
cmd := &cobra.Command{
9289
Use: "build-hook BUILDCONFIG --post-commit [--command] [--script] -- CMD",
9390
Short: "Update a build hook on a build config",
9491
Long: buildHookLong,
9592
Example: fmt.Sprintf(buildHookExample, fullName),
9693
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())
10697
},
10798
}
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)
121110
kcmdutil.AddDryRunFlag(cmd)
122111

123112
return cmd
124113
}
125114

126115
func (o *BuildHookOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
127-
resources := args
116+
o.Resources = args
128117
if i := cmd.ArgsLenAtDash(); i != -1 {
129-
resources = args[:i]
118+
o.Resources = args[:i]
130119
o.Command = args[i:]
131120
}
132121
if len(o.Filenames) == 0 && len(args) < 1 {
133122
return kcmdutil.UsageErrorf(cmd, "one or more build configs must be specified as <name> or <resource>/<name>")
134123
}
135124

136-
cmdNamespace, explicit, err := f.ToRawKubeConfigLoader().Namespace()
125+
var err error
126+
o.Namespace, o.ExplicitNamespace, err = f.ToRawKubeConfigLoader().Namespace()
137127
if err != nil {
138128
return err
139129
}
140130

141-
o.Cmd = cmd
142-
143-
mapper, err := f.ToRESTMapper()
131+
o.Mapper, err = f.ToRESTMapper()
144132
if err != nil {
145133
return err
146134
}
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
156136

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)")
164140
}
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
169144
}
170145

171-
o.Encoder = kcmdutil.InternalVersionJSONEncoder()
172-
o.ShortOutput = kcmdutil.GetFlagString(cmd, "output") == "name"
173-
o.Mapper = mapper
174-
175146
clientConfig, err := f.ToRESTConfig()
176147
if err != nil {
177148
return err
@@ -185,7 +156,6 @@ func (o *BuildHookOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args
185156
}
186157

187158
func (o *BuildHookOptions) Validate() error {
188-
189159
if !o.PostCommit {
190160
return fmt.Errorf("you must specify a type of hook to set")
191161
}
@@ -212,18 +182,32 @@ func (o *BuildHookOptions) Validate() error {
212182
}
213183

214184
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)
221200
}
222-
infos = loaded
223201
}
224202

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)
227211
if !ok {
228212
return false, nil
229213
}
@@ -235,39 +219,40 @@ func (o *BuildHookOptions) Run() error {
235219
return fmt.Errorf("%s/%s is not a build config", infos[0].Mapping.Resource, infos[0].Name)
236220
}
237221

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{}
243223
for _, patch := range patches {
244224
info := patch.Info
245225
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))
247227
continue
248228
}
249229

250230
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+
}
252239
continue
253240
}
254241

255242
actual, err := o.Client.Resource(info.Mapping.Resource).Namespace(info.Namespace).Patch(info.Name, types.StrategicMergePatchType, patch.Patch)
256243
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))
259245
continue
260246
}
261247

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+
}
266251
}
267-
return nil
252+
return utilerrors.NewAggregate(allErrs)
268253
}
269254

270-
func (o *BuildHookOptions) updateBuildConfig(bc *buildapi.BuildConfig) {
255+
func (o *BuildHookOptions) updateBuildConfig(bc *buildv1.BuildConfig) {
271256
if o.Remove {
272257
bc.Spec.PostCommit.Args = nil
273258
bc.Spec.PostCommit.Command = nil

0 commit comments

Comments
 (0)