@@ -28,6 +28,7 @@ import (
28
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29
29
"k8s.io/apimachinery/pkg/runtime"
30
30
"k8s.io/apimachinery/pkg/types"
31
+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
31
32
"k8s.io/apimachinery/pkg/util/sets"
32
33
"k8s.io/cli-runtime/pkg/genericclioptions"
33
34
"k8s.io/cli-runtime/pkg/printers"
@@ -371,53 +372,78 @@ func (o *ApplyOptions) Run() error {
371
372
372
373
// Generates the objects using the resource builder if they have not
373
374
// already been stored by calling "SetObjects()" in the pre-processor.
375
+ errs := []error {}
374
376
infos , err := o .GetObjects ()
375
377
if err != nil {
376
- return err
378
+ errs = append ( errs , err )
377
379
}
378
- if len (infos ) == 0 {
380
+ if len (infos ) == 0 && len ( errs ) == 0 {
379
381
return fmt .Errorf ("no objects passed to apply" )
380
382
}
383
+ // Iterate through all objects, applying each one.
381
384
for _ , info := range infos {
385
+ if err := o .applyOneObject (info ); err != nil {
386
+ errs = append (errs , err )
387
+ }
388
+ }
389
+ // If any errors occurred during apply, then return error (or
390
+ // aggregate of errors).
391
+ if len (errs ) == 1 {
392
+ return errs [0 ]
393
+ }
394
+ if len (errs ) > 1 {
395
+ return utilerrors .NewAggregate (errs )
396
+ }
382
397
383
- o .MarkNamespaceVisited (info )
398
+ if o .PostProcessorFn != nil {
399
+ klog .V (4 ).Infof ("Running apply post-processor function" )
400
+ if err := o .PostProcessorFn (); err != nil {
401
+ return err
402
+ }
403
+ }
404
+
405
+ return nil
406
+ }
407
+
408
+ func (o * ApplyOptions ) applyOneObject (info * resource.Info ) error {
409
+ o .MarkNamespaceVisited (info )
410
+
411
+ if err := o .Recorder .Record (info .Object ); err != nil {
412
+ klog .V (4 ).Infof ("error recording current command: %v" , err )
413
+ }
384
414
385
- if err := o .Recorder .Record (info .Object ); err != nil {
386
- klog .V (4 ).Infof ("error recording current command: %v" , err )
415
+ if o .ServerSideApply {
416
+ // Send the full object to be applied on the server side.
417
+ data , err := runtime .Encode (unstructured .UnstructuredJSONScheme , info .Object )
418
+ if err != nil {
419
+ return cmdutil .AddSourceToErr ("serverside-apply" , info .Source , err )
387
420
}
388
421
389
- if o .ServerSideApply {
390
- // Send the full object to be applied on the server side.
391
- data , err := runtime .Encode (unstructured .UnstructuredJSONScheme , info .Object )
392
- if err != nil {
393
- return cmdutil .AddSourceToErr ("serverside-apply" , info .Source , err )
394
- }
422
+ options := metav1.PatchOptions {
423
+ Force : & o .ForceConflicts ,
424
+ FieldManager : o .FieldManager ,
425
+ }
395
426
396
- options := metav1.PatchOptions {
397
- Force : & o .ForceConflicts ,
398
- FieldManager : o .FieldManager ,
427
+ helper := resource .NewHelper (info .Client , info .Mapping )
428
+ if o .DryRunStrategy == cmdutil .DryRunServer {
429
+ if err := o .DryRunVerifier .HasSupport (info .Mapping .GroupVersionKind ); err != nil {
430
+ return err
399
431
}
400
-
401
- helper := resource .NewHelper (info .Client , info .Mapping )
402
- if o .DryRunStrategy == cmdutil .DryRunServer {
403
- if err := o .DryRunVerifier .HasSupport (info .Mapping .GroupVersionKind ); err != nil {
404
- return err
405
- }
406
- helper .DryRun (true )
432
+ helper .DryRun (true )
433
+ }
434
+ obj , err := helper .Patch (
435
+ info .Namespace ,
436
+ info .Name ,
437
+ types .ApplyPatchType ,
438
+ data ,
439
+ & options ,
440
+ )
441
+ if err != nil {
442
+ if isIncompatibleServerError (err ) {
443
+ err = fmt .Errorf ("Server-side apply not available on the server: (%v)" , err )
407
444
}
408
- obj , err := helper .Patch (
409
- info .Namespace ,
410
- info .Name ,
411
- types .ApplyPatchType ,
412
- data ,
413
- & options ,
414
- )
415
- if err != nil {
416
- if isIncompatibleServerError (err ) {
417
- err = fmt .Errorf ("Server-side apply not available on the server: (%v)" , err )
418
- }
419
- if errors .IsConflict (err ) {
420
- err = fmt .Errorf (`%v
445
+ if errors .IsConflict (err ) {
446
+ err = fmt .Errorf (`%v
421
447
Please review the fields above--they currently have other managers. Here
422
448
are the ways you can resolve this warning:
423
449
* If you intend to manage all of these fields, please re-run the apply
@@ -429,136 +455,128 @@ are the ways you can resolve this warning:
429
455
value; in this case, you'll become the manager if the other manager(s)
430
456
stop managing the field (remove it from their configuration).
431
457
See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts` , err )
432
- }
433
- return err
434
- }
435
-
436
- info .Refresh (obj , true )
437
-
438
- if err := o .MarkObjectVisited (info ); err != nil {
439
- return err
440
458
}
459
+ return err
460
+ }
441
461
442
- if o .shouldPrintObject () {
443
- continue
444
- }
462
+ info .Refresh (obj , true )
445
463
446
- printer , err := o .ToPrinter ("serverside-applied" )
447
- if err != nil {
448
- return err
449
- }
464
+ if err := o .MarkObjectVisited (info ); err != nil {
465
+ return err
466
+ }
450
467
451
- if err = printer .PrintObj (info .Object , o .Out ); err != nil {
452
- return err
453
- }
454
- continue
468
+ if o .shouldPrintObject () {
469
+ return nil
455
470
}
456
471
457
- // Get the modified configuration of the object. Embed the result
458
- // as an annotation in the modified configuration, so that it will appear
459
- // in the patch sent to the server.
460
- modified , err := util .GetModifiedConfiguration (info .Object , true , unstructured .UnstructuredJSONScheme )
472
+ printer , err := o .ToPrinter ("serverside-applied" )
461
473
if err != nil {
462
- return cmdutil . AddSourceToErr ( fmt . Sprintf ( "retrieving modified configuration from: \n %s \n for:" , info . String ()), info . Source , err )
474
+ return err
463
475
}
464
476
465
- if err := info .Get (); err != nil {
466
- if ! errors .IsNotFound (err ) {
467
- return cmdutil .AddSourceToErr (fmt .Sprintf ("retrieving current configuration of:\n %s\n from server for:" , info .String ()), info .Source , err )
468
- }
469
-
470
- // Create the resource if it doesn't exist
471
- // First, update the annotation used by kubectl apply
472
- if err := util .CreateApplyAnnotation (info .Object , unstructured .UnstructuredJSONScheme ); err != nil {
473
- return cmdutil .AddSourceToErr ("creating" , info .Source , err )
474
- }
475
-
476
- if o .DryRunStrategy != cmdutil .DryRunClient {
477
- // Then create the resource and skip the three-way merge
478
- helper := resource .NewHelper (info .Client , info .Mapping )
479
- if o .DryRunStrategy == cmdutil .DryRunServer {
480
- if err := o .DryRunVerifier .HasSupport (info .Mapping .GroupVersionKind ); err != nil {
481
- return cmdutil .AddSourceToErr ("creating" , info .Source , err )
482
- }
483
- helper .DryRun (true )
484
- }
485
- obj , err := helper .Create (info .Namespace , true , info .Object )
486
- if err != nil {
487
- return cmdutil .AddSourceToErr ("creating" , info .Source , err )
488
- }
489
- info .Refresh (obj , true )
490
- }
491
-
492
- if err := o .MarkObjectVisited (info ); err != nil {
493
- return err
494
- }
477
+ if err = printer .PrintObj (info .Object , o .Out ); err != nil {
478
+ return err
479
+ }
480
+ return nil
481
+ }
495
482
496
- if o .shouldPrintObject () {
497
- continue
498
- }
483
+ // Get the modified configuration of the object. Embed the result
484
+ // as an annotation in the modified configuration, so that it will appear
485
+ // in the patch sent to the server.
486
+ modified , err := util .GetModifiedConfiguration (info .Object , true , unstructured .UnstructuredJSONScheme )
487
+ if err != nil {
488
+ return cmdutil .AddSourceToErr (fmt .Sprintf ("retrieving modified configuration from:\n %s\n for:" , info .String ()), info .Source , err )
489
+ }
499
490
500
- printer , err := o .ToPrinter ("created" )
501
- if err != nil {
502
- return err
503
- }
504
- if err = printer .PrintObj (info .Object , o .Out ); err != nil {
505
- return err
506
- }
507
- continue
491
+ if err := info .Get (); err != nil {
492
+ if ! errors .IsNotFound (err ) {
493
+ return cmdutil .AddSourceToErr (fmt .Sprintf ("retrieving current configuration of:\n %s\n from server for:" , info .String ()), info .Source , err )
508
494
}
509
495
510
- if err := o .MarkObjectVisited (info ); err != nil {
511
- return err
496
+ // Create the resource if it doesn't exist
497
+ // First, update the annotation used by kubectl apply
498
+ if err := util .CreateApplyAnnotation (info .Object , unstructured .UnstructuredJSONScheme ); err != nil {
499
+ return cmdutil .AddSourceToErr ("creating" , info .Source , err )
512
500
}
513
501
514
502
if o .DryRunStrategy != cmdutil .DryRunClient {
515
- metadata , _ := meta .Accessor (info .Object )
516
- annotationMap := metadata .GetAnnotations ()
517
- if _ , ok := annotationMap [corev1 .LastAppliedConfigAnnotation ]; ! ok {
518
- fmt .Fprintf (o .ErrOut , warningNoLastAppliedConfigAnnotation , o .cmdBaseName )
519
- }
520
-
521
- patcher , err := newPatcher (o , info )
522
- if err != nil {
523
- return err
503
+ // Then create the resource and skip the three-way merge
504
+ helper := resource .NewHelper (info .Client , info .Mapping )
505
+ if o .DryRunStrategy == cmdutil .DryRunServer {
506
+ if err := o .DryRunVerifier .HasSupport (info .Mapping .GroupVersionKind ); err != nil {
507
+ return cmdutil .AddSourceToErr ("creating" , info .Source , err )
508
+ }
509
+ helper .DryRun (true )
524
510
}
525
- patchBytes , patchedObject , err := patcher . Patch (info .Object , modified , info .Source , info . Namespace , info . Name , o . ErrOut )
511
+ obj , err := helper . Create (info .Namespace , true , info .Object )
526
512
if err != nil {
527
- return cmdutil .AddSourceToErr (fmt . Sprintf ( "applying patch: \n %s \n to: \n %v \n for:" , patchBytes , info ) , info .Source , err )
513
+ return cmdutil .AddSourceToErr ("creating" , info .Source , err )
528
514
}
515
+ info .Refresh (obj , true )
516
+ }
529
517
530
- info .Refresh (patchedObject , true )
531
-
532
- if string (patchBytes ) == "{}" && ! o .shouldPrintObject () {
533
- printer , err := o .ToPrinter ("unchanged" )
534
- if err != nil {
535
- return err
536
- }
537
- if err = printer .PrintObj (info .Object , o .Out ); err != nil {
538
- return err
539
- }
540
- continue
541
- }
518
+ if err := o .MarkObjectVisited (info ); err != nil {
519
+ return err
542
520
}
543
521
544
522
if o .shouldPrintObject () {
545
- continue
523
+ return nil
546
524
}
547
525
548
- printer , err := o .ToPrinter ("configured " )
526
+ printer , err := o .ToPrinter ("created " )
549
527
if err != nil {
550
528
return err
551
529
}
552
530
if err = printer .PrintObj (info .Object , o .Out ); err != nil {
553
531
return err
554
532
}
533
+ return nil
555
534
}
556
535
557
- if o .PostProcessorFn != nil {
558
- klog .V (4 ).Infof ("Running apply post-processor function" )
559
- if err := o .PostProcessorFn (); err != nil {
536
+ if err := o .MarkObjectVisited (info ); err != nil {
537
+ return err
538
+ }
539
+
540
+ if o .DryRunStrategy != cmdutil .DryRunClient {
541
+ metadata , _ := meta .Accessor (info .Object )
542
+ annotationMap := metadata .GetAnnotations ()
543
+ if _ , ok := annotationMap [corev1 .LastAppliedConfigAnnotation ]; ! ok {
544
+ fmt .Fprintf (o .ErrOut , warningNoLastAppliedConfigAnnotation , o .cmdBaseName )
545
+ }
546
+
547
+ patcher , err := newPatcher (o , info )
548
+ if err != nil {
560
549
return err
561
550
}
551
+ patchBytes , patchedObject , err := patcher .Patch (info .Object , modified , info .Source , info .Namespace , info .Name , o .ErrOut )
552
+ if err != nil {
553
+ return cmdutil .AddSourceToErr (fmt .Sprintf ("applying patch:\n %s\n to:\n %v\n for:" , patchBytes , info ), info .Source , err )
554
+ }
555
+
556
+ info .Refresh (patchedObject , true )
557
+
558
+ if string (patchBytes ) == "{}" && ! o .shouldPrintObject () {
559
+ printer , err := o .ToPrinter ("unchanged" )
560
+ if err != nil {
561
+ return err
562
+ }
563
+ if err = printer .PrintObj (info .Object , o .Out ); err != nil {
564
+ return err
565
+ }
566
+ return nil
567
+ }
568
+ }
569
+
570
+ if o .shouldPrintObject () {
571
+ return nil
572
+ }
573
+
574
+ printer , err := o .ToPrinter ("configured" )
575
+ if err != nil {
576
+ return err
577
+ }
578
+ if err = printer .PrintObj (info .Object , o .Out ); err != nil {
579
+ return err
562
580
}
563
581
564
582
return nil
0 commit comments