Skip to content

Commit a0ea9b5

Browse files
author
OpenShift Bot
authored
Merge pull request #12718 from juanvallejo/jvallejo/improve-oc-idle-output
Merged by openshift-bot
2 parents c8e60fe + 9575d79 commit a0ea9b5

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

pkg/cmd/cli/cmd/idle.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,11 @@ func (o *IdleOptions) RunIdle(f *clientcmd.Factory) error {
515515
hadError := false
516516
nowTime := time.Now().UTC()
517517

518+
dryRunText := ""
519+
if o.dryRun {
520+
dryRunText = "(dry run)"
521+
}
522+
518523
// figure out which endpoints and resources we need to idle
519524
byService, byScalable, err := o.calculateIdlableAnnotationsByService(f)
520525

@@ -564,64 +569,67 @@ func (o *IdleOptions) RunIdle(f *clientcmd.Factory) error {
564569
}
565570
refsWithScale, err := pairScalesWithScaleRefs(serviceName, info.obj.Annotations, info.scaleRefs, replicas)
566571
if err != nil {
567-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
572+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
568573
continue
569574
}
570575

571576
if !o.dryRun {
572577
if len(info.scaleRefs) == 0 {
573-
fmt.Fprintf(o.errOut, "error: no scalable resources marked as idled for service %s, not marking as idled\n", serviceName.String())
578+
fmt.Fprintf(o.errOut, "error: unable to mark the service %q as idled.\n", serviceName.String())
579+
fmt.Fprintf(o.errOut, "Make sure that the service is not already marked as idled and that it is associated with resources that can be scaled.\n")
580+
fmt.Fprintf(o.errOut, "See 'oc idle -h' for help and examples.\n")
574581
hadError = true
575582
continue
576583
}
577584

578585
metadata, err := meta.Accessor(info.obj)
579586
if err != nil {
580-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
587+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
581588
hadError = true
582589
continue
583590
}
584591
gvks, _, err := typer.ObjectKinds(info.obj)
585592
if err != nil {
586-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
593+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
587594
hadError = true
588595
continue
589596
}
590597
oldData, err := json.Marshal(info.obj)
591598
if err != nil {
592-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
599+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
593600
hadError = true
594601
continue
595602
}
596603

597604
mapping, err := mapper.RESTMapping(gvks[0].GroupKind(), gvks[0].Version)
598605
if err != nil {
599-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
606+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
600607
hadError = true
601608
continue
602609
}
603610

604611
if err = setIdleAnnotations(serviceName, info.obj.Annotations, refsWithScale, nowTime); err != nil {
605-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
612+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
606613
hadError = true
607614
continue
608615
}
609616
if _, err := patchObj(info.obj, metadata, oldData, mapping, f); err != nil {
610-
fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
617+
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
611618
hadError = true
612619
continue
613620
}
614621
}
615622

623+
fmt.Fprintf(o.out, "The service %q has been marked as idled %s\n", serviceName.String(), dryRunText)
624+
616625
for _, scaleRef := range refsWithScale {
617-
fmt.Fprintf(o.out, "Marked service %s to unidle resource %s %s/%s (unidle to %v replicas)\n", serviceName.String(), scaleRef.Kind, serviceName.Namespace, scaleRef.Name, scaleRef.Replicas)
626+
fmt.Fprintf(o.out, "The service will unidle %s \"%s/%s\" to %v replicas once it receives traffic %s\n", scaleRef.Kind, serviceName.Namespace, scaleRef.Name, scaleRef.Replicas, dryRunText)
618627
}
619628
}
620629

621630
// actually "idle" the scalable resources by scaling them down to zero
622631
// (scale down to zero *after* we've applied the annotation so that we don't miss any traffic)
623632
for scaleRef, info := range toScale {
624-
idled := ""
625633
if !o.dryRun {
626634
info.scale.Spec.Replicas = 0
627635
scaleUpdater := utilunidling.NewScaleUpdater(f.JSONEncoder(), info.namespace, dcGetter, kclient.Core())
@@ -630,11 +638,9 @@ func (o *IdleOptions) RunIdle(f *clientcmd.Factory) error {
630638
hadError = true
631639
continue
632640
}
633-
} else {
634-
idled = "(dry run)"
635641
}
636642

637-
fmt.Fprintf(o.out, "Idled %s %s/%s %s\n", scaleRef.Kind, info.namespace, scaleRef.Name, idled)
643+
fmt.Fprintf(o.out, "%s \"%s/%s\" has been idled %s\n", scaleRef.Kind, info.namespace, scaleRef.Name, dryRunText)
638644
}
639645

640646
if hadError {

test/cmd/idle.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ EOF
6060
os::test::junit::declare_suite_start "cmd/idle/by-name"
6161
setup_idling_resources
6262
os::cmd::expect_failure "oc idle dc/${dc_name}" # make sure manually passing non-endpoints resources fails
63-
os::cmd::expect_success_and_text 'oc idle idling-echo' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/${dc_name} \(unidle to 2 replicas\)"
63+
os::cmd::expect_success_and_text 'oc idle idling-echo' "The service will unidle DeploymentConfig \"${project}/${dc_name}\" to 2 replicas once it receives traffic"
6464
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
6565
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq '.[] | select(.name == \"${dc_name}\") | (.replicas == 2 and .kind == \"DeploymentConfig\")'" 'true'
6666
os::test::junit::declare_suite_end
6767

6868
os::test::junit::declare_suite_start "cmd/idle/by-label"
6969
setup_idling_resources
70-
os::cmd::expect_success_and_text 'oc idle -l app=idling-echo' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/${dc_name} \(unidle to 2 replicas\)"
70+
os::cmd::expect_success_and_text 'oc idle -l app=idling-echo' "The service will unidle DeploymentConfig \"${project}/${dc_name}\" to 2 replicas once it receives traffic"
7171
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
7272
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq '.[] | select(.name == \"${dc_name}\") | (.replicas == 2 and .kind == \"DeploymentConfig\")'" 'true'
7373
os::test::junit::declare_suite_end
7474

7575
os::test::junit::declare_suite_start "cmd/idle/all"
7676
setup_idling_resources
77-
os::cmd::expect_success_and_text 'oc idle --all' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/${dc_name} \(unidle to 2 replicas\)"
77+
os::cmd::expect_success_and_text 'oc idle --all' "The service will unidle DeploymentConfig \"${project}/${dc_name}\" to 2 replicas once it receives traffic"
7878
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
7979
os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq '.[] | select(.name == \"${dc_name}\") | (.replicas == 2 and .kind == \"DeploymentConfig\")'" 'true'
8080
os::test::junit::declare_suite_end
8181

8282
os::test::junit::declare_suite_start "cmd/idle/check-previous-scale"
8383
setup_idling_resources # scales up to 2 replicas
84-
os::cmd::expect_success_and_text 'oc idle idling-echo' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/${dc_name} \(unidle to 2 replicas\)"
84+
os::cmd::expect_success_and_text 'oc idle idling-echo' "The service will unidle DeploymentConfig \"${project}/${dc_name}\" to 2 replicas once it receives traffic"
8585
os::cmd::expect_success_and_text "oc get dc ${dc_name} -o go-template='${prev_scale_template}'" '2' # we see the result of the initial scale as the previous scale
8686
os::test::junit::declare_suite_end

0 commit comments

Comments
 (0)