Browse code

Add the `oc idle` command

The oc idle command takes a list of scalables (RCs and DCs), figures out
the associated services and endpoints (or vice versa), adds those associations
as annotations, and then scales the scalable resources down to zero.

Solly Ross authored on 2015/12/17 04:16:40
Showing 22 changed files
... ...
@@ -879,6 +879,56 @@ _oc_cluster()
879 879
     noun_aliases=()
880 880
 }
881 881
 
882
+_oc_idle()
883
+{
884
+    last_command="oc_idle"
885
+    commands=()
886
+
887
+    flags=()
888
+    two_word_flags=()
889
+    flags_with_completion=()
890
+    flags_completion=()
891
+
892
+    flags+=("--all")
893
+    flags+=("--all-namespaces")
894
+    flags+=("--dry-run")
895
+    flags+=("--resource-names-file=")
896
+    flags_with_completion+=("--resource-names-file")
897
+    flags_completion+=("_filedir")
898
+    flags+=("--selector=")
899
+    two_word_flags+=("-l")
900
+    flags+=("--api-version=")
901
+    flags+=("--as=")
902
+    flags+=("--certificate-authority=")
903
+    flags_with_completion+=("--certificate-authority")
904
+    flags_completion+=("_filedir")
905
+    flags+=("--client-certificate=")
906
+    flags_with_completion+=("--client-certificate")
907
+    flags_completion+=("_filedir")
908
+    flags+=("--client-key=")
909
+    flags_with_completion+=("--client-key")
910
+    flags_completion+=("_filedir")
911
+    flags+=("--cluster=")
912
+    flags+=("--config=")
913
+    flags_with_completion+=("--config")
914
+    flags_completion+=("_filedir")
915
+    flags+=("--context=")
916
+    flags+=("--insecure-skip-tls-verify")
917
+    flags+=("--log-flush-frequency=")
918
+    flags+=("--loglevel=")
919
+    flags+=("--logspec=")
920
+    flags+=("--match-server-version")
921
+    flags+=("--namespace=")
922
+    two_word_flags+=("-n")
923
+    flags+=("--server=")
924
+    flags+=("--token=")
925
+    flags+=("--user=")
926
+
927
+    must_have_one_flag=()
928
+    must_have_one_noun=()
929
+    noun_aliases=()
930
+}
931
+
882 932
 _oc_rollout_history()
883 933
 {
884 934
     last_command="oc_rollout_history"
... ...
@@ -11316,6 +11366,7 @@ _oc()
11316 11316
     commands+=("projects")
11317 11317
     commands+=("explain")
11318 11318
     commands+=("cluster")
11319
+    commands+=("idle")
11319 11320
     commands+=("rollout")
11320 11321
     commands+=("deploy")
11321 11322
     commands+=("rollback")
... ...
@@ -5338,6 +5338,57 @@ _openshift_cli_cluster()
5338 5338
     noun_aliases=()
5339 5339
 }
5340 5340
 
5341
+_openshift_cli_idle()
5342
+{
5343
+    last_command="openshift_cli_idle"
5344
+    commands=()
5345
+
5346
+    flags=()
5347
+    two_word_flags=()
5348
+    flags_with_completion=()
5349
+    flags_completion=()
5350
+
5351
+    flags+=("--all")
5352
+    flags+=("--all-namespaces")
5353
+    flags+=("--dry-run")
5354
+    flags+=("--resource-names-file=")
5355
+    flags_with_completion+=("--resource-names-file")
5356
+    flags_completion+=("_filedir")
5357
+    flags+=("--selector=")
5358
+    two_word_flags+=("-l")
5359
+    flags+=("--api-version=")
5360
+    flags+=("--as=")
5361
+    flags+=("--certificate-authority=")
5362
+    flags_with_completion+=("--certificate-authority")
5363
+    flags_completion+=("_filedir")
5364
+    flags+=("--client-certificate=")
5365
+    flags_with_completion+=("--client-certificate")
5366
+    flags_completion+=("_filedir")
5367
+    flags+=("--client-key=")
5368
+    flags_with_completion+=("--client-key")
5369
+    flags_completion+=("_filedir")
5370
+    flags+=("--cluster=")
5371
+    flags+=("--config=")
5372
+    flags_with_completion+=("--config")
5373
+    flags_completion+=("_filedir")
5374
+    flags+=("--context=")
5375
+    flags+=("--google-json-key=")
5376
+    flags+=("--insecure-skip-tls-verify")
5377
+    flags+=("--log-flush-frequency=")
5378
+    flags+=("--loglevel=")
5379
+    flags+=("--logspec=")
5380
+    flags+=("--match-server-version")
5381
+    flags+=("--namespace=")
5382
+    two_word_flags+=("-n")
5383
+    flags+=("--server=")
5384
+    flags+=("--token=")
5385
+    flags+=("--user=")
5386
+
5387
+    must_have_one_flag=()
5388
+    must_have_one_noun=()
5389
+    noun_aliases=()
5390
+}
5391
+
5341 5392
 _openshift_cli_rollout_history()
5342 5393
 {
5343 5394
     last_command="openshift_cli_rollout_history"
... ...
@@ -15915,6 +15966,7 @@ _openshift_cli()
15915 15915
     commands+=("projects")
15916 15916
     commands+=("explain")
15917 15917
     commands+=("cluster")
15918
+    commands+=("idle")
15918 15919
     commands+=("rollout")
15919 15920
     commands+=("deploy")
15920 15921
     commands+=("rollback")
... ...
@@ -1040,6 +1040,56 @@ _oc_cluster()
1040 1040
     noun_aliases=()
1041 1041
 }
1042 1042
 
1043
+_oc_idle()
1044
+{
1045
+    last_command="oc_idle"
1046
+    commands=()
1047
+
1048
+    flags=()
1049
+    two_word_flags=()
1050
+    flags_with_completion=()
1051
+    flags_completion=()
1052
+
1053
+    flags+=("--all")
1054
+    flags+=("--all-namespaces")
1055
+    flags+=("--dry-run")
1056
+    flags+=("--resource-names-file=")
1057
+    flags_with_completion+=("--resource-names-file")
1058
+    flags_completion+=("_filedir")
1059
+    flags+=("--selector=")
1060
+    two_word_flags+=("-l")
1061
+    flags+=("--api-version=")
1062
+    flags+=("--as=")
1063
+    flags+=("--certificate-authority=")
1064
+    flags_with_completion+=("--certificate-authority")
1065
+    flags_completion+=("_filedir")
1066
+    flags+=("--client-certificate=")
1067
+    flags_with_completion+=("--client-certificate")
1068
+    flags_completion+=("_filedir")
1069
+    flags+=("--client-key=")
1070
+    flags_with_completion+=("--client-key")
1071
+    flags_completion+=("_filedir")
1072
+    flags+=("--cluster=")
1073
+    flags+=("--config=")
1074
+    flags_with_completion+=("--config")
1075
+    flags_completion+=("_filedir")
1076
+    flags+=("--context=")
1077
+    flags+=("--insecure-skip-tls-verify")
1078
+    flags+=("--log-flush-frequency=")
1079
+    flags+=("--loglevel=")
1080
+    flags+=("--logspec=")
1081
+    flags+=("--match-server-version")
1082
+    flags+=("--namespace=")
1083
+    two_word_flags+=("-n")
1084
+    flags+=("--server=")
1085
+    flags+=("--token=")
1086
+    flags+=("--user=")
1087
+
1088
+    must_have_one_flag=()
1089
+    must_have_one_noun=()
1090
+    noun_aliases=()
1091
+}
1092
+
1043 1093
 _oc_rollout_history()
1044 1094
 {
1045 1095
     last_command="oc_rollout_history"
... ...
@@ -11477,6 +11527,7 @@ _oc()
11477 11477
     commands+=("projects")
11478 11478
     commands+=("explain")
11479 11479
     commands+=("cluster")
11480
+    commands+=("idle")
11480 11481
     commands+=("rollout")
11481 11482
     commands+=("deploy")
11482 11483
     commands+=("rollback")
... ...
@@ -5499,6 +5499,57 @@ _openshift_cli_cluster()
5499 5499
     noun_aliases=()
5500 5500
 }
5501 5501
 
5502
+_openshift_cli_idle()
5503
+{
5504
+    last_command="openshift_cli_idle"
5505
+    commands=()
5506
+
5507
+    flags=()
5508
+    two_word_flags=()
5509
+    flags_with_completion=()
5510
+    flags_completion=()
5511
+
5512
+    flags+=("--all")
5513
+    flags+=("--all-namespaces")
5514
+    flags+=("--dry-run")
5515
+    flags+=("--resource-names-file=")
5516
+    flags_with_completion+=("--resource-names-file")
5517
+    flags_completion+=("_filedir")
5518
+    flags+=("--selector=")
5519
+    two_word_flags+=("-l")
5520
+    flags+=("--api-version=")
5521
+    flags+=("--as=")
5522
+    flags+=("--certificate-authority=")
5523
+    flags_with_completion+=("--certificate-authority")
5524
+    flags_completion+=("_filedir")
5525
+    flags+=("--client-certificate=")
5526
+    flags_with_completion+=("--client-certificate")
5527
+    flags_completion+=("_filedir")
5528
+    flags+=("--client-key=")
5529
+    flags_with_completion+=("--client-key")
5530
+    flags_completion+=("_filedir")
5531
+    flags+=("--cluster=")
5532
+    flags+=("--config=")
5533
+    flags_with_completion+=("--config")
5534
+    flags_completion+=("_filedir")
5535
+    flags+=("--context=")
5536
+    flags+=("--google-json-key=")
5537
+    flags+=("--insecure-skip-tls-verify")
5538
+    flags+=("--log-flush-frequency=")
5539
+    flags+=("--loglevel=")
5540
+    flags+=("--logspec=")
5541
+    flags+=("--match-server-version")
5542
+    flags+=("--namespace=")
5543
+    two_word_flags+=("-n")
5544
+    flags+=("--server=")
5545
+    flags+=("--token=")
5546
+    flags+=("--user=")
5547
+
5548
+    must_have_one_flag=()
5549
+    must_have_one_noun=()
5550
+    noun_aliases=()
5551
+}
5552
+
5502 5553
 _openshift_cli_rollout_history()
5503 5554
 {
5504 5555
     last_command="openshift_cli_rollout_history"
... ...
@@ -16076,6 +16127,7 @@ _openshift_cli()
16076 16076
     commands+=("projects")
16077 16077
     commands+=("explain")
16078 16078
     commands+=("cluster")
16079
+    commands+=("idle")
16079 16080
     commands+=("rollout")
16080 16081
     commands+=("deploy")
16081 16082
     commands+=("rollback")
... ...
@@ -1568,6 +1568,19 @@ Display one or many resources
1568 1568
 ====
1569 1569
 
1570 1570
 
1571
+== oc idle
1572
+Idle scalable resources
1573
+
1574
+====
1575
+
1576
+[options="nowrap"]
1577
+----
1578
+  # Idle the scalable controllers associated with the services listed in to-idle.txt
1579
+  $ oc idle -f to-idle.txt
1580
+----
1581
+====
1582
+
1583
+
1571 1584
 == oc import app.json
1572 1585
 Import an app.json definition into OpenShift (experimental)
1573 1586
 
... ...
@@ -134,6 +134,7 @@ oc-export.1
134 134
 oc-expose.1
135 135
 oc-extract.1
136 136
 oc-get.1
137
+oc-idle.1
137 138
 oc-import-app.json.1
138 139
 oc-import-docker-compose.1
139 140
 oc-import-image.1
... ...
@@ -216,6 +216,7 @@ openshift-cli-export.1
216 216
 openshift-cli-expose.1
217 217
 openshift-cli-extract.1
218 218
 openshift-cli-get.1
219
+openshift-cli-idle.1
219 220
 openshift-cli-import-app.json.1
220 221
 openshift-cli-import-docker-compose.1
221 222
 openshift-cli-import-image.1
222 223
new file mode 100644
... ...
@@ -0,0 +1,136 @@
0
+.TH "OC" "1" " Openshift CLI User Manuals" "Openshift" "June 2016"  ""
1
+
2
+
3
+.SH NAME
4
+.PP
5
+oc idle \- Idle scalable resources
6
+
7
+
8
+.SH SYNOPSIS
9
+.PP
10
+\fBoc idle\fP [OPTIONS]
11
+
12
+
13
+.SH DESCRIPTION
14
+.PP
15
+Idle scalable resources.
16
+
17
+.PP
18
+Idling discovers the scalable resources (such as deployment configs and replication controllers)
19
+associated with a series of services by examining the endpoints of the service.
20
+Each service is then marked as idled, the associated resources are recorded, and the resources
21
+are scaled down to zero replicas.
22
+
23
+.PP
24
+Upon receiving network traffic, the services (and any associated routes) will "wake up" the
25
+associated resources by scaling them back up to their previous scale.
26
+
27
+
28
+.SH OPTIONS
29
+.PP
30
+\fB\-\-all\fP=false
31
+    Select all services in the namespace
32
+
33
+.PP
34
+\fB\-\-all\-namespaces\fP=false
35
+    Select services across all namespaces
36
+
37
+.PP
38
+\fB\-\-dry\-run\fP=false
39
+    If true, only print the annotations that would be written, without annotating or idling the relevant objects
40
+
41
+.PP
42
+\fB\-\-resource\-names\-file\fP=""
43
+    file containing list of services whose scalable resources to idle
44
+
45
+.PP
46
+\fB\-l\fP, \fB\-\-selector\fP=""
47
+    Selector (label query) to use to select services
48
+
49
+
50
+.SH OPTIONS INHERITED FROM PARENT COMMANDS
51
+.PP
52
+\fB\-\-api\-version\fP=""
53
+    DEPRECATED: The API version to use when talking to the server
54
+
55
+.PP
56
+\fB\-\-as\fP=""
57
+    Username to impersonate for the operation.
58
+
59
+.PP
60
+\fB\-\-certificate\-authority\fP=""
61
+    Path to a cert. file for the certificate authority.
62
+
63
+.PP
64
+\fB\-\-client\-certificate\fP=""
65
+    Path to a client certificate file for TLS.
66
+
67
+.PP
68
+\fB\-\-client\-key\fP=""
69
+    Path to a client key file for TLS.
70
+
71
+.PP
72
+\fB\-\-cluster\fP=""
73
+    The name of the kubeconfig cluster to use
74
+
75
+.PP
76
+\fB\-\-config\fP=""
77
+    Path to the config file to use for CLI requests.
78
+
79
+.PP
80
+\fB\-\-context\fP=""
81
+    The name of the kubeconfig context to use
82
+
83
+.PP
84
+\fB\-\-google\-json\-key\fP=""
85
+    The Google Cloud Platform Service Account JSON Key to use for authentication.
86
+
87
+.PP
88
+\fB\-\-insecure\-skip\-tls\-verify\fP=false
89
+    If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
90
+
91
+.PP
92
+\fB\-\-log\-flush\-frequency\fP=0
93
+    Maximum number of seconds between log flushes
94
+
95
+.PP
96
+\fB\-\-match\-server\-version\fP=false
97
+    Require server version to match client version
98
+
99
+.PP
100
+\fB\-n\fP, \fB\-\-namespace\fP=""
101
+    If present, the namespace scope for this CLI request.
102
+
103
+.PP
104
+\fB\-\-server\fP=""
105
+    The address and port of the Kubernetes API server
106
+
107
+.PP
108
+\fB\-\-token\fP=""
109
+    Bearer token for authentication to the API server.
110
+
111
+.PP
112
+\fB\-\-user\fP=""
113
+    The name of the kubeconfig user to use
114
+
115
+
116
+.SH EXAMPLE
117
+.PP
118
+.RS
119
+
120
+.nf
121
+  # Idle the scalable controllers associated with the services listed in to\-idle.txt
122
+  $ oc idle \-f to\-idle.txt
123
+
124
+.fi
125
+.RE
126
+
127
+
128
+.SH SEE ALSO
129
+.PP
130
+\fBoc(1)\fP,
131
+
132
+
133
+.SH HISTORY
134
+.PP
135
+June 2016, Ported from the Kubernetes man\-doc generator
... ...
@@ -89,7 +89,7 @@ cluster under the 'adm' subcommand.
89 89
 
90 90
 .SH SEE ALSO
91 91
 .PP
92
-\fBoc\-types(1)\fP, \fBoc\-login(1)\fP, \fBoc\-new\-project(1)\fP, \fBoc\-new\-app(1)\fP, \fBoc\-status(1)\fP, \fBoc\-project(1)\fP, \fBoc\-projects(1)\fP, \fBoc\-explain(1)\fP, \fBoc\-cluster(1)\fP, \fBoc\-rollout(1)\fP, \fBoc\-deploy(1)\fP, \fBoc\-rollback(1)\fP, \fBoc\-new\-build(1)\fP, \fBoc\-start\-build(1)\fP, \fBoc\-cancel\-build(1)\fP, \fBoc\-import\-image(1)\fP, \fBoc\-tag(1)\fP, \fBoc\-get(1)\fP, \fBoc\-describe(1)\fP, \fBoc\-edit(1)\fP, \fBoc\-set(1)\fP, \fBoc\-label(1)\fP, \fBoc\-annotate(1)\fP, \fBoc\-expose(1)\fP, \fBoc\-delete(1)\fP, \fBoc\-scale(1)\fP, \fBoc\-autoscale(1)\fP, \fBoc\-secrets(1)\fP, \fBoc\-serviceaccounts(1)\fP, \fBoc\-logs(1)\fP, \fBoc\-rsh(1)\fP, \fBoc\-rsync(1)\fP, \fBoc\-port\-forward(1)\fP, \fBoc\-debug(1)\fP, \fBoc\-exec(1)\fP, \fBoc\-proxy(1)\fP, \fBoc\-attach(1)\fP, \fBoc\-run(1)\fP, \fBoc\-adm(1)\fP, \fBoc\-create(1)\fP, \fBoc\-replace(1)\fP, \fBoc\-apply(1)\fP, \fBoc\-patch(1)\fP, \fBoc\-process(1)\fP, \fBoc\-export(1)\fP, \fBoc\-extract(1)\fP, \fBoc\-policy(1)\fP, \fBoc\-convert(1)\fP, \fBoc\-import(1)\fP, \fBoc\-logout(1)\fP, \fBoc\-config(1)\fP, \fBoc\-whoami(1)\fP, \fBoc\-completion(1)\fP, \fBoc\-env(1)\fP, \fBoc\-volumes(1)\fP, \fBoc\-build\-logs(1)\fP, \fBoc\-ex(1)\fP, \fBoc\-version(1)\fP, \fBoc\-options(1)\fP,
92
+\fBoc\-types(1)\fP, \fBoc\-login(1)\fP, \fBoc\-new\-project(1)\fP, \fBoc\-new\-app(1)\fP, \fBoc\-status(1)\fP, \fBoc\-project(1)\fP, \fBoc\-projects(1)\fP, \fBoc\-explain(1)\fP, \fBoc\-cluster(1)\fP, \fBoc\-idle(1)\fP, \fBoc\-rollout(1)\fP, \fBoc\-deploy(1)\fP, \fBoc\-rollback(1)\fP, \fBoc\-new\-build(1)\fP, \fBoc\-start\-build(1)\fP, \fBoc\-cancel\-build(1)\fP, \fBoc\-import\-image(1)\fP, \fBoc\-tag(1)\fP, \fBoc\-get(1)\fP, \fBoc\-describe(1)\fP, \fBoc\-edit(1)\fP, \fBoc\-set(1)\fP, \fBoc\-label(1)\fP, \fBoc\-annotate(1)\fP, \fBoc\-expose(1)\fP, \fBoc\-delete(1)\fP, \fBoc\-scale(1)\fP, \fBoc\-autoscale(1)\fP, \fBoc\-secrets(1)\fP, \fBoc\-serviceaccounts(1)\fP, \fBoc\-logs(1)\fP, \fBoc\-rsh(1)\fP, \fBoc\-rsync(1)\fP, \fBoc\-port\-forward(1)\fP, \fBoc\-debug(1)\fP, \fBoc\-exec(1)\fP, \fBoc\-proxy(1)\fP, \fBoc\-attach(1)\fP, \fBoc\-run(1)\fP, \fBoc\-adm(1)\fP, \fBoc\-create(1)\fP, \fBoc\-replace(1)\fP, \fBoc\-apply(1)\fP, \fBoc\-patch(1)\fP, \fBoc\-process(1)\fP, \fBoc\-export(1)\fP, \fBoc\-extract(1)\fP, \fBoc\-policy(1)\fP, \fBoc\-convert(1)\fP, \fBoc\-import(1)\fP, \fBoc\-logout(1)\fP, \fBoc\-config(1)\fP, \fBoc\-whoami(1)\fP, \fBoc\-completion(1)\fP, \fBoc\-env(1)\fP, \fBoc\-volumes(1)\fP, \fBoc\-build\-logs(1)\fP, \fBoc\-ex(1)\fP, \fBoc\-version(1)\fP, \fBoc\-options(1)\fP,
93 93
 
94 94
 
95 95
 .SH HISTORY
96 96
new file mode 100644
... ...
@@ -0,0 +1,136 @@
0
+.TH "OPENSHIFT CLI" "1" " Openshift CLI User Manuals" "Openshift" "June 2016"  ""
1
+
2
+
3
+.SH NAME
4
+.PP
5
+openshift cli idle \- Idle scalable resources
6
+
7
+
8
+.SH SYNOPSIS
9
+.PP
10
+\fBopenshift cli idle\fP [OPTIONS]
11
+
12
+
13
+.SH DESCRIPTION
14
+.PP
15
+Idle scalable resources.
16
+
17
+.PP
18
+Idling discovers the scalable resources (such as deployment configs and replication controllers)
19
+associated with a series of services by examining the endpoints of the service.
20
+Each service is then marked as idled, the associated resources are recorded, and the resources
21
+are scaled down to zero replicas.
22
+
23
+.PP
24
+Upon receiving network traffic, the services (and any associated routes) will "wake up" the
25
+associated resources by scaling them back up to their previous scale.
26
+
27
+
28
+.SH OPTIONS
29
+.PP
30
+\fB\-\-all\fP=false
31
+    Select all services in the namespace
32
+
33
+.PP
34
+\fB\-\-all\-namespaces\fP=false
35
+    Select services across all namespaces
36
+
37
+.PP
38
+\fB\-\-dry\-run\fP=false
39
+    If true, only print the annotations that would be written, without annotating or idling the relevant objects
40
+
41
+.PP
42
+\fB\-\-resource\-names\-file\fP=""
43
+    file containing list of services whose scalable resources to idle
44
+
45
+.PP
46
+\fB\-l\fP, \fB\-\-selector\fP=""
47
+    Selector (label query) to use to select services
48
+
49
+
50
+.SH OPTIONS INHERITED FROM PARENT COMMANDS
51
+.PP
52
+\fB\-\-api\-version\fP=""
53
+    DEPRECATED: The API version to use when talking to the server
54
+
55
+.PP
56
+\fB\-\-as\fP=""
57
+    Username to impersonate for the operation.
58
+
59
+.PP
60
+\fB\-\-certificate\-authority\fP=""
61
+    Path to a cert. file for the certificate authority.
62
+
63
+.PP
64
+\fB\-\-client\-certificate\fP=""
65
+    Path to a client certificate file for TLS.
66
+
67
+.PP
68
+\fB\-\-client\-key\fP=""
69
+    Path to a client key file for TLS.
70
+
71
+.PP
72
+\fB\-\-cluster\fP=""
73
+    The name of the kubeconfig cluster to use
74
+
75
+.PP
76
+\fB\-\-config\fP=""
77
+    Path to the config file to use for CLI requests.
78
+
79
+.PP
80
+\fB\-\-context\fP=""
81
+    The name of the kubeconfig context to use
82
+
83
+.PP
84
+\fB\-\-google\-json\-key\fP=""
85
+    The Google Cloud Platform Service Account JSON Key to use for authentication.
86
+
87
+.PP
88
+\fB\-\-insecure\-skip\-tls\-verify\fP=false
89
+    If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
90
+
91
+.PP
92
+\fB\-\-log\-flush\-frequency\fP=0
93
+    Maximum number of seconds between log flushes
94
+
95
+.PP
96
+\fB\-\-match\-server\-version\fP=false
97
+    Require server version to match client version
98
+
99
+.PP
100
+\fB\-n\fP, \fB\-\-namespace\fP=""
101
+    If present, the namespace scope for this CLI request.
102
+
103
+.PP
104
+\fB\-\-server\fP=""
105
+    The address and port of the Kubernetes API server
106
+
107
+.PP
108
+\fB\-\-token\fP=""
109
+    Bearer token for authentication to the API server.
110
+
111
+.PP
112
+\fB\-\-user\fP=""
113
+    The name of the kubeconfig user to use
114
+
115
+
116
+.SH EXAMPLE
117
+.PP
118
+.RS
119
+
120
+.nf
121
+  # Idle the scalable controllers associated with the services listed in to\-idle.txt
122
+  $ openshift cli idle \-f to\-idle.txt
123
+
124
+.fi
125
+.RE
126
+
127
+
128
+.SH SEE ALSO
129
+.PP
130
+\fBopenshift\-cli(1)\fP,
131
+
132
+
133
+.SH HISTORY
134
+.PP
135
+June 2016, Ported from the Kubernetes man\-doc generator
... ...
@@ -91,7 +91,7 @@ cluster under the 'adm' subcommand.
91 91
 
92 92
 .SH SEE ALSO
93 93
 .PP
94
-\fBopenshift(1)\fP, \fBopenshift\-cli\-types(1)\fP, \fBopenshift\-cli\-login(1)\fP, \fBopenshift\-cli\-new\-project(1)\fP, \fBopenshift\-cli\-new\-app(1)\fP, \fBopenshift\-cli\-status(1)\fP, \fBopenshift\-cli\-project(1)\fP, \fBopenshift\-cli\-projects(1)\fP, \fBopenshift\-cli\-explain(1)\fP, \fBopenshift\-cli\-cluster(1)\fP, \fBopenshift\-cli\-rollout(1)\fP, \fBopenshift\-cli\-deploy(1)\fP, \fBopenshift\-cli\-rollback(1)\fP, \fBopenshift\-cli\-new\-build(1)\fP, \fBopenshift\-cli\-start\-build(1)\fP, \fBopenshift\-cli\-cancel\-build(1)\fP, \fBopenshift\-cli\-import\-image(1)\fP, \fBopenshift\-cli\-tag(1)\fP, \fBopenshift\-cli\-get(1)\fP, \fBopenshift\-cli\-describe(1)\fP, \fBopenshift\-cli\-edit(1)\fP, \fBopenshift\-cli\-set(1)\fP, \fBopenshift\-cli\-label(1)\fP, \fBopenshift\-cli\-annotate(1)\fP, \fBopenshift\-cli\-expose(1)\fP, \fBopenshift\-cli\-delete(1)\fP, \fBopenshift\-cli\-scale(1)\fP, \fBopenshift\-cli\-autoscale(1)\fP, \fBopenshift\-cli\-secrets(1)\fP, \fBopenshift\-cli\-serviceaccounts(1)\fP, \fBopenshift\-cli\-logs(1)\fP, \fBopenshift\-cli\-rsh(1)\fP, \fBopenshift\-cli\-rsync(1)\fP, \fBopenshift\-cli\-port\-forward(1)\fP, \fBopenshift\-cli\-debug(1)\fP, \fBopenshift\-cli\-exec(1)\fP, \fBopenshift\-cli\-proxy(1)\fP, \fBopenshift\-cli\-attach(1)\fP, \fBopenshift\-cli\-run(1)\fP, \fBopenshift\-cli\-adm(1)\fP, \fBopenshift\-cli\-create(1)\fP, \fBopenshift\-cli\-replace(1)\fP, \fBopenshift\-cli\-apply(1)\fP, \fBopenshift\-cli\-patch(1)\fP, \fBopenshift\-cli\-process(1)\fP, \fBopenshift\-cli\-export(1)\fP, \fBopenshift\-cli\-extract(1)\fP, \fBopenshift\-cli\-policy(1)\fP, \fBopenshift\-cli\-convert(1)\fP, \fBopenshift\-cli\-import(1)\fP, \fBopenshift\-cli\-logout(1)\fP, \fBopenshift\-cli\-config(1)\fP, \fBopenshift\-cli\-whoami(1)\fP, \fBopenshift\-cli\-completion(1)\fP, \fBopenshift\-cli\-env(1)\fP, \fBopenshift\-cli\-volumes(1)\fP, \fBopenshift\-cli\-build\-logs(1)\fP, \fBopenshift\-cli\-ex(1)\fP, \fBopenshift\-cli\-options(1)\fP,
94
+\fBopenshift(1)\fP, \fBopenshift\-cli\-types(1)\fP, \fBopenshift\-cli\-login(1)\fP, \fBopenshift\-cli\-new\-project(1)\fP, \fBopenshift\-cli\-new\-app(1)\fP, \fBopenshift\-cli\-status(1)\fP, \fBopenshift\-cli\-project(1)\fP, \fBopenshift\-cli\-projects(1)\fP, \fBopenshift\-cli\-explain(1)\fP, \fBopenshift\-cli\-cluster(1)\fP, \fBopenshift\-cli\-idle(1)\fP, \fBopenshift\-cli\-rollout(1)\fP, \fBopenshift\-cli\-deploy(1)\fP, \fBopenshift\-cli\-rollback(1)\fP, \fBopenshift\-cli\-new\-build(1)\fP, \fBopenshift\-cli\-start\-build(1)\fP, \fBopenshift\-cli\-cancel\-build(1)\fP, \fBopenshift\-cli\-import\-image(1)\fP, \fBopenshift\-cli\-tag(1)\fP, \fBopenshift\-cli\-get(1)\fP, \fBopenshift\-cli\-describe(1)\fP, \fBopenshift\-cli\-edit(1)\fP, \fBopenshift\-cli\-set(1)\fP, \fBopenshift\-cli\-label(1)\fP, \fBopenshift\-cli\-annotate(1)\fP, \fBopenshift\-cli\-expose(1)\fP, \fBopenshift\-cli\-delete(1)\fP, \fBopenshift\-cli\-scale(1)\fP, \fBopenshift\-cli\-autoscale(1)\fP, \fBopenshift\-cli\-secrets(1)\fP, \fBopenshift\-cli\-serviceaccounts(1)\fP, \fBopenshift\-cli\-logs(1)\fP, \fBopenshift\-cli\-rsh(1)\fP, \fBopenshift\-cli\-rsync(1)\fP, \fBopenshift\-cli\-port\-forward(1)\fP, \fBopenshift\-cli\-debug(1)\fP, \fBopenshift\-cli\-exec(1)\fP, \fBopenshift\-cli\-proxy(1)\fP, \fBopenshift\-cli\-attach(1)\fP, \fBopenshift\-cli\-run(1)\fP, \fBopenshift\-cli\-adm(1)\fP, \fBopenshift\-cli\-create(1)\fP, \fBopenshift\-cli\-replace(1)\fP, \fBopenshift\-cli\-apply(1)\fP, \fBopenshift\-cli\-patch(1)\fP, \fBopenshift\-cli\-process(1)\fP, \fBopenshift\-cli\-export(1)\fP, \fBopenshift\-cli\-extract(1)\fP, \fBopenshift\-cli\-policy(1)\fP, \fBopenshift\-cli\-convert(1)\fP, \fBopenshift\-cli\-import(1)\fP, \fBopenshift\-cli\-logout(1)\fP, \fBopenshift\-cli\-config(1)\fP, \fBopenshift\-cli\-whoami(1)\fP, \fBopenshift\-cli\-completion(1)\fP, \fBopenshift\-cli\-env(1)\fP, \fBopenshift\-cli\-volumes(1)\fP, \fBopenshift\-cli\-build\-logs(1)\fP, \fBopenshift\-cli\-ex(1)\fP, \fBopenshift\-cli\-options(1)\fP,
95 95
 
96 96
 
97 97
 .SH HISTORY
98 98
new file mode 100644
... ...
@@ -0,0 +1,123 @@
0
+.TH "OC" "1" " Openshift CLI User Manuals" "Openshift" "June 2016"  ""
1
+
2
+
3
+.SH NAME
4
+.PP
5
+oc idle \- Idle scalable resources
6
+
7
+
8
+.SH SYNOPSIS
9
+.PP
10
+\fBoc idle\fP [OPTIONS]
11
+
12
+
13
+.SH DESCRIPTION
14
+.PP
15
+Idle scalable resources.
16
+
17
+.PP
18
+This command idles the provides list of resources by scaling scalable resources (e.g. RCs and
19
+DCs) down to zero replicas, and then marking associated services so that that the scalables
20
+will be "woken up" when traffic occurs on those services.
21
+
22
+.PP
23
+Only DCs and RCs with associated services will be idled \-\- services that only point to pods,
24
+as well as RCs and DCs with no services, will not be idled.
25
+
26
+
27
+.SH OPTIONS
28
+.PP
29
+\fB\-\-dry\-run\fP=false
30
+    If true, only print the annotations that would be written, without annotating or idling the relevant objects
31
+
32
+.PP
33
+\fB\-f\fP, \fB\-\-filename\fP=""
34
+    file containing list of services whose scalables will be idled
35
+
36
+
37
+.SH OPTIONS INHERITED FROM PARENT COMMANDS
38
+.PP
39
+\fB\-\-api\-version\fP=""
40
+    DEPRECATED: The API version to use when talking to the server
41
+
42
+.PP
43
+\fB\-\-as\fP=""
44
+    Username to impersonate for the operation.
45
+
46
+.PP
47
+\fB\-\-certificate\-authority\fP=""
48
+    Path to a cert. file for the certificate authority.
49
+
50
+.PP
51
+\fB\-\-client\-certificate\fP=""
52
+    Path to a client certificate file for TLS.
53
+
54
+.PP
55
+\fB\-\-client\-key\fP=""
56
+    Path to a client key file for TLS.
57
+
58
+.PP
59
+\fB\-\-cluster\fP=""
60
+    The name of the kubeconfig cluster to use
61
+
62
+.PP
63
+\fB\-\-config\fP=""
64
+    Path to the config file to use for CLI requests.
65
+
66
+.PP
67
+\fB\-\-context\fP=""
68
+    The name of the kubeconfig context to use
69
+
70
+.PP
71
+\fB\-\-google\-json\-key\fP=""
72
+    The Google Cloud Platform Service Account JSON Key to use for authentication.
73
+
74
+.PP
75
+\fB\-\-insecure\-skip\-tls\-verify\fP=false
76
+    If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
77
+
78
+.PP
79
+\fB\-\-log\-flush\-frequency\fP=0
80
+    Maximum number of seconds between log flushes
81
+
82
+.PP
83
+\fB\-\-match\-server\-version\fP=false
84
+    Require server version to match client version
85
+
86
+.PP
87
+\fB\-n\fP, \fB\-\-namespace\fP=""
88
+    If present, the namespace scope for this CLI request.
89
+
90
+.PP
91
+\fB\-\-server\fP=""
92
+    The address and port of the Kubernetes API server
93
+
94
+.PP
95
+\fB\-\-token\fP=""
96
+    Bearer token for authentication to the API server.
97
+
98
+.PP
99
+\fB\-\-user\fP=""
100
+    The name of the kubeconfig user to use
101
+
102
+
103
+.SH EXAMPLE
104
+.PP
105
+.RS
106
+
107
+.nf
108
+  # Idle the scalable controllers associated with some services listed in to\-idle.txt
109
+  $ oc idle \-f to\-idle.txt
110
+
111
+.fi
112
+.RE
113
+
114
+
115
+.SH SEE ALSO
116
+.PP
117
+\fBoc(1)\fP,
118
+
119
+
120
+.SH HISTORY
121
+.PP
122
+June 2016, Ported from the Kubernetes man\-doc generator
0 123
new file mode 100644
... ...
@@ -0,0 +1,123 @@
0
+.TH "OPENSHIFT CLI" "1" " Openshift CLI User Manuals" "Openshift" "June 2016"  ""
1
+
2
+
3
+.SH NAME
4
+.PP
5
+openshift cli idle \- Idle scalable resources
6
+
7
+
8
+.SH SYNOPSIS
9
+.PP
10
+\fBopenshift cli idle\fP [OPTIONS]
11
+
12
+
13
+.SH DESCRIPTION
14
+.PP
15
+Idle scalable resources.
16
+
17
+.PP
18
+This command idles the provides list of resources by scaling scalable resources (e.g. RCs and
19
+DCs) down to zero replicas, and then marking associated services so that that the scalables
20
+will be "woken up" when traffic occurs on those services.
21
+
22
+.PP
23
+Only DCs and RCs with associated services will be idled \-\- services that only point to pods,
24
+as well as RCs and DCs with no services, will not be idled.
25
+
26
+
27
+.SH OPTIONS
28
+.PP
29
+\fB\-\-dry\-run\fP=false
30
+    If true, only print the annotations that would be written, without annotating or idling the relevant objects
31
+
32
+.PP
33
+\fB\-f\fP, \fB\-\-filename\fP=""
34
+    file containing list of services whose scalables will be idled
35
+
36
+
37
+.SH OPTIONS INHERITED FROM PARENT COMMANDS
38
+.PP
39
+\fB\-\-api\-version\fP=""
40
+    DEPRECATED: The API version to use when talking to the server
41
+
42
+.PP
43
+\fB\-\-as\fP=""
44
+    Username to impersonate for the operation.
45
+
46
+.PP
47
+\fB\-\-certificate\-authority\fP=""
48
+    Path to a cert. file for the certificate authority.
49
+
50
+.PP
51
+\fB\-\-client\-certificate\fP=""
52
+    Path to a client certificate file for TLS.
53
+
54
+.PP
55
+\fB\-\-client\-key\fP=""
56
+    Path to a client key file for TLS.
57
+
58
+.PP
59
+\fB\-\-cluster\fP=""
60
+    The name of the kubeconfig cluster to use
61
+
62
+.PP
63
+\fB\-\-config\fP=""
64
+    Path to the config file to use for CLI requests.
65
+
66
+.PP
67
+\fB\-\-context\fP=""
68
+    The name of the kubeconfig context to use
69
+
70
+.PP
71
+\fB\-\-google\-json\-key\fP=""
72
+    The Google Cloud Platform Service Account JSON Key to use for authentication.
73
+
74
+.PP
75
+\fB\-\-insecure\-skip\-tls\-verify\fP=false
76
+    If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
77
+
78
+.PP
79
+\fB\-\-log\-flush\-frequency\fP=0
80
+    Maximum number of seconds between log flushes
81
+
82
+.PP
83
+\fB\-\-match\-server\-version\fP=false
84
+    Require server version to match client version
85
+
86
+.PP
87
+\fB\-n\fP, \fB\-\-namespace\fP=""
88
+    If present, the namespace scope for this CLI request.
89
+
90
+.PP
91
+\fB\-\-server\fP=""
92
+    The address and port of the Kubernetes API server
93
+
94
+.PP
95
+\fB\-\-token\fP=""
96
+    Bearer token for authentication to the API server.
97
+
98
+.PP
99
+\fB\-\-user\fP=""
100
+    The name of the kubeconfig user to use
101
+
102
+
103
+.SH EXAMPLE
104
+.PP
105
+.RS
106
+
107
+.nf
108
+  # Idle the scalable controllers associated with some services listed in to\-idle.txt
109
+  $ openshift cli idle \-f to\-idle.txt
110
+
111
+.fi
112
+.RE
113
+
114
+
115
+.SH SEE ALSO
116
+.PP
117
+\fBopenshift\-cli(1)\fP,
118
+
119
+
120
+.SH HISTORY
121
+.PP
122
+June 2016, Ported from the Kubernetes man\-doc generator
... ...
@@ -102,6 +102,7 @@ func NewCommandCLI(name, fullName string, in io.Reader, out, errout io.Writer) *
102 102
 				cmd.NewCmdProjects(fullName, f, out),
103 103
 				cmd.NewCmdExplain(fullName, f, out),
104 104
 				cluster.NewCmdCluster(cluster.ClusterRecommendedName, fullName+" "+cluster.ClusterRecommendedName, f, out),
105
+				cmd.NewCmdIdle(fullName, f, out, errout),
105 106
 			},
106 107
 		},
107 108
 		{
108 109
new file mode 100644
... ...
@@ -0,0 +1,630 @@
0
+package cmd
1
+
2
+import (
3
+	"bufio"
4
+	"encoding/json"
5
+	"fmt"
6
+	"io"
7
+	"os"
8
+	"time"
9
+
10
+	"github.com/spf13/cobra"
11
+
12
+	cmdutil "github.com/openshift/origin/pkg/cmd/util"
13
+	utilerrors "github.com/openshift/origin/pkg/util/errors"
14
+	kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
15
+
16
+	osclient "github.com/openshift/origin/pkg/client"
17
+	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
18
+	deployapi "github.com/openshift/origin/pkg/deploy/api"
19
+	deployclient "github.com/openshift/origin/pkg/deploy/client/clientset_generated/internalclientset/typed/core/unversioned"
20
+	unidlingapi "github.com/openshift/origin/pkg/unidling/api"
21
+	utilunidling "github.com/openshift/origin/pkg/unidling/util"
22
+	"k8s.io/kubernetes/pkg/api"
23
+	"k8s.io/kubernetes/pkg/api/meta"
24
+	"k8s.io/kubernetes/pkg/api/unversioned"
25
+	"k8s.io/kubernetes/pkg/apis/extensions"
26
+	clientset "k8s.io/kubernetes/pkg/client/unversioned/adapters/internalclientset"
27
+	"k8s.io/kubernetes/pkg/controller"
28
+	"k8s.io/kubernetes/pkg/kubectl/resource"
29
+	"k8s.io/kubernetes/pkg/runtime"
30
+	"k8s.io/kubernetes/pkg/types"
31
+	"k8s.io/kubernetes/pkg/util/strategicpatch"
32
+)
33
+
34
+const (
35
+	idleLong = `
36
+Idle scalable resources.
37
+
38
+Idling discovers the scalable resources (such as deployment configs and replication controllers)
39
+associated with a series of services by examining the endpoints of the service.
40
+Each service is then marked as idled, the associated resources are recorded, and the resources
41
+are scaled down to zero replicas.
42
+
43
+Upon receiving network traffic, the services (and any associated routes) will "wake up" the
44
+associated resources by scaling them back up to their previous scale.`
45
+
46
+	idleExample = `  # Idle the scalable controllers associated with the services listed in to-idle.txt
47
+  $ %[1]s idle -f to-idle.txt`
48
+)
49
+
50
+// NewCmdStatus implements the OpenShift cli status command
51
+func NewCmdIdle(fullName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
52
+	o := &IdleOptions{
53
+		out:    out,
54
+		errOut: errOut,
55
+	}
56
+
57
+	cmd := &cobra.Command{
58
+		Use:     "idle (SERVICES... | -l label | --all | --resource-names-file FILENAME)",
59
+		Short:   "Idle scalable resources",
60
+		Long:    idleLong,
61
+		Example: fmt.Sprintf(idleExample, fullName),
62
+		Run: func(cmd *cobra.Command, args []string) {
63
+			kcmdutil.CheckErr(o.Complete(f, cmd, args))
64
+			err := o.RunIdle(f)
65
+			if err == cmdutil.ErrExit {
66
+				os.Exit(1)
67
+			}
68
+			kcmdutil.CheckErr(err)
69
+		},
70
+	}
71
+
72
+	cmd.Flags().BoolVar(&o.dryRun, "dry-run", false, "If true, only print the annotations that would be written, without annotating or idling the relevant objects")
73
+	cmd.Flags().StringVar(&o.filename, "resource-names-file", o.filename, "file containing list of services whose scalable resources to idle")
74
+	cmd.Flags().StringVarP(&o.selector, "selector", "l", o.selector, "Selector (label query) to use to select services")
75
+	cmd.Flags().BoolVar(&o.all, "all", o.all, "Select all services in the namespace")
76
+	cmd.Flags().BoolVar(&o.allNamespaces, "all-namespaces", o.allNamespaces, "Select services across all namespaces")
77
+	cmd.MarkFlagFilename("resource-names-file")
78
+
79
+	// TODO: take the `-o name` argument, and only print out names instead of the summary
80
+
81
+	return cmd
82
+}
83
+
84
+type IdleOptions struct {
85
+	out, errOut io.Writer
86
+
87
+	dryRun bool
88
+
89
+	filename      string
90
+	all           bool
91
+	selector      string
92
+	allNamespaces bool
93
+	resources     string
94
+
95
+	nowTime    time.Time
96
+	svcBuilder *resource.Builder
97
+}
98
+
99
+func (o *IdleOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
100
+	namespace, _, err := f.DefaultNamespace()
101
+	if err != nil {
102
+		return err
103
+	}
104
+
105
+	o.nowTime = time.Now().UTC()
106
+
107
+	// NB: our filename arg is different from usual, since it's just a list of service names
108
+	if o.filename != "" && (o.selector != "" || len(args) > 0 || o.all) {
109
+		return fmt.Errorf("resource names, selectors, and the all flag may not be be specified if a filename is specified")
110
+	}
111
+
112
+	mapper, typer := f.Object(false)
113
+	o.svcBuilder = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), api.Codecs.UniversalDecoder()).
114
+		ContinueOnError().
115
+		NamespaceParam(namespace).DefaultNamespace().AllNamespaces(o.allNamespaces).
116
+		Flatten().
117
+		SingleResourceType()
118
+
119
+	if o.filename != "" {
120
+		targetServiceNames, err := scanLinesFromFile(o.filename)
121
+		if err != nil {
122
+			return err
123
+		}
124
+		o.svcBuilder.ResourceNames("endpoints", targetServiceNames...)
125
+	} else {
126
+		// NB: this is a bit weird because the resource builder will complain if we use ResourceTypes and ResourceNames when len(args) > 0
127
+		if o.selector != "" {
128
+			o.svcBuilder.SelectorParam(o.selector).ResourceTypes("endpoints")
129
+		}
130
+
131
+		o.svcBuilder.ResourceNames("endpoints", args...)
132
+
133
+		if o.all {
134
+			o.svcBuilder.ResourceTypes("endpoints").SelectAllParam(o.all)
135
+		}
136
+	}
137
+
138
+	return nil
139
+}
140
+
141
+// scanLinesFromFile loads lines from either standard in or a file
142
+func scanLinesFromFile(filename string) ([]string, error) {
143
+	var targetsInput io.Reader
144
+	if filename == "-" {
145
+		targetsInput = os.Stdin
146
+	} else if filename == "" {
147
+		return nil, fmt.Errorf("you must specify an list of resources to idle")
148
+	} else {
149
+		inputFile, err := os.Open(filename)
150
+		if err != nil {
151
+			return nil, err
152
+		}
153
+		defer inputFile.Close()
154
+		targetsInput = inputFile
155
+	}
156
+
157
+	lines := []string{}
158
+
159
+	// grab the raw resources from the file
160
+	lineScanner := bufio.NewScanner(targetsInput)
161
+	for lineScanner.Scan() {
162
+		line := lineScanner.Text()
163
+		if line == "" {
164
+			// skip empty lines
165
+			continue
166
+		}
167
+		lines = append(lines, line)
168
+	}
169
+	if err := lineScanner.Err(); err != nil {
170
+		return nil, err
171
+	}
172
+
173
+	return lines, nil
174
+}
175
+
176
+// idleUpdateInfo contains the required info to annotate an endpoints object
177
+// with the scalable resources that it should unidle
178
+type idleUpdateInfo struct {
179
+	obj       *api.Endpoints
180
+	scaleRefs map[unidlingapi.CrossGroupObjectReference]struct{}
181
+}
182
+
183
+// calculateIdlableAnnotationsByService calculates the list of objects involved in the idling process from a list of services in a file.
184
+// Using the list of services, it figures out the associated scalable objects, and returns a map from the endpoints object for the services to
185
+// the list of scalable resources associated with that endpoints object, as well as a map from CrossGroupObjectReferences to scale to 0 to the
186
+// name of the associated service.
187
+func (o *IdleOptions) calculateIdlableAnnotationsByService(f *clientcmd.Factory) (map[types.NamespacedName]idleUpdateInfo, map[unidlingapi.CrossGroupObjectReference]types.NamespacedName, error) {
188
+	// load our set of services
189
+	client, err := f.Client()
190
+	if err != nil {
191
+		return nil, nil, err
192
+	}
193
+
194
+	mapper, _ := f.Object(false)
195
+
196
+	podsLoaded := make(map[api.ObjectReference]*api.Pod)
197
+	getPod := func(ref api.ObjectReference) (*api.Pod, error) {
198
+		if pod, ok := podsLoaded[ref]; ok {
199
+			return pod, nil
200
+		}
201
+		pod, err := client.Pods(ref.Namespace).Get(ref.Name)
202
+		if err != nil {
203
+			return nil, err
204
+		}
205
+
206
+		podsLoaded[ref] = pod
207
+
208
+		return pod, nil
209
+	}
210
+
211
+	controllersLoaded := make(map[api.ObjectReference]runtime.Object)
212
+	helpers := make(map[unversioned.GroupKind]*resource.Helper)
213
+	getController := func(ref api.ObjectReference) (runtime.Object, error) {
214
+		if controller, ok := controllersLoaded[ref]; ok {
215
+			return controller, nil
216
+		}
217
+		gv, err := unversioned.ParseGroupVersion(ref.APIVersion)
218
+		if err != nil {
219
+			return nil, err
220
+		}
221
+		// just get the unversioned version of this
222
+		gk := unversioned.GroupKind{Group: gv.Group, Kind: ref.Kind}
223
+		helper, ok := helpers[gk]
224
+		if !ok {
225
+			var mapping *meta.RESTMapping
226
+			mapping, err = mapper.RESTMapping(unversioned.GroupKind{Group: gv.Group, Kind: ref.Kind}, "")
227
+			if err != nil {
228
+				return nil, err
229
+			}
230
+			var client resource.RESTClient
231
+			client, err = f.ClientForMapping(mapping)
232
+			if err != nil {
233
+				return nil, err
234
+			}
235
+			helper = resource.NewHelper(client, mapping)
236
+			helpers[gk] = helper
237
+		}
238
+
239
+		var controller runtime.Object
240
+		controller, err = helper.Get(ref.Namespace, ref.Name, false)
241
+		if err != nil {
242
+			return nil, err
243
+		}
244
+
245
+		controllersLoaded[ref] = controller
246
+
247
+		return controller, nil
248
+	}
249
+
250
+	targetScaleRefs := make(map[unidlingapi.CrossGroupObjectReference]types.NamespacedName)
251
+	endpointsInfo := make(map[types.NamespacedName]idleUpdateInfo)
252
+
253
+	decoder := f.Decoder(true)
254
+	err = o.svcBuilder.Do().Visit(func(info *resource.Info, err error) error {
255
+		endpoints := info.Object.(*api.Endpoints)
256
+		endpointsName := types.NamespacedName{
257
+			Namespace: endpoints.Namespace,
258
+			Name:      endpoints.Name,
259
+		}
260
+		scaleRefs, err := findScalableResourcesForEndpoints(endpoints, decoder, getPod, getController)
261
+		if err != nil {
262
+			return fmt.Errorf("unable to calculate scalable resources for service %s/%s: %v", endpoints.Namespace, endpoints.Name, err)
263
+		}
264
+
265
+		for ref := range scaleRefs {
266
+			targetScaleRefs[ref] = endpointsName
267
+		}
268
+
269
+		idleInfo := idleUpdateInfo{
270
+			obj:       endpoints,
271
+			scaleRefs: scaleRefs,
272
+		}
273
+
274
+		endpointsInfo[endpointsName] = idleInfo
275
+
276
+		return nil
277
+	})
278
+
279
+	return endpointsInfo, targetScaleRefs, err
280
+}
281
+
282
+// getControllerRef returns a subresource reference to the owning controller of the given object.
283
+// It will use both the CreatedByAnnotation from Kubernetes, as well as the DeploymentConfigAnnotation
284
+// from Origin to look this up.  If neither are found, it will return nil.
285
+func getControllerRef(obj runtime.Object, decoder runtime.Decoder) (*api.ObjectReference, error) {
286
+	objMeta, err := meta.Accessor(obj)
287
+	if err != nil {
288
+		return nil, err
289
+	}
290
+
291
+	annotations := objMeta.GetAnnotations()
292
+
293
+	creatorRefRaw, creatorListed := annotations[controller.CreatedByAnnotation]
294
+	if !creatorListed {
295
+		// if we don't have a creator listed, try the openshift-specific Deployment annotation
296
+		dcName, dcNameListed := annotations[deployapi.DeploymentConfigAnnotation]
297
+		if !dcNameListed {
298
+			return nil, nil
299
+		}
300
+
301
+		return &api.ObjectReference{
302
+			Name:      dcName,
303
+			Namespace: objMeta.GetNamespace(),
304
+			Kind:      "DeploymentConfig",
305
+		}, nil
306
+	}
307
+
308
+	serializedRef := &api.SerializedReference{}
309
+	if err := runtime.DecodeInto(decoder, []byte(creatorRefRaw), serializedRef); err != nil {
310
+		return nil, fmt.Errorf("could not decoded pod's creator reference: %v", err)
311
+	}
312
+
313
+	return &serializedRef.Reference, nil
314
+}
315
+
316
+func makeCrossGroupObjRef(ref *api.ObjectReference) (unidlingapi.CrossGroupObjectReference, error) {
317
+	gv, err := unversioned.ParseGroupVersion(ref.APIVersion)
318
+	if err != nil {
319
+		return unidlingapi.CrossGroupObjectReference{}, err
320
+	}
321
+
322
+	return unidlingapi.CrossGroupObjectReference{
323
+		Kind:  ref.Kind,
324
+		Name:  ref.Name,
325
+		Group: gv.Group,
326
+	}, nil
327
+}
328
+
329
+// findScalableResourcesForEndpoints takes an Endpoints object and looks for the associated
330
+// scalable objects by checking each address in each subset to see if it has a pod
331
+// reference, and the following that pod reference to find the owning controller,
332
+// and returning the unique set of controllers found this way.
333
+func findScalableResourcesForEndpoints(endpoints *api.Endpoints, decoder runtime.Decoder, getPod func(api.ObjectReference) (*api.Pod, error), getController func(api.ObjectReference) (runtime.Object, error)) (map[unidlingapi.CrossGroupObjectReference]struct{}, error) {
334
+	// To find all RCs and DCs for an endpoint, we first figure out which pods are pointed to by that endpoint...
335
+	podRefs := map[api.ObjectReference]*api.Pod{}
336
+	for _, subset := range endpoints.Subsets {
337
+		for _, addr := range subset.Addresses {
338
+			if addr.TargetRef != nil && addr.TargetRef.Kind == "Pod" {
339
+				pod, err := getPod(*addr.TargetRef)
340
+				if utilerrors.TolerateNotFoundError(err) != nil {
341
+					return nil, fmt.Errorf("unable to find controller for pod %s/%s: %v", addr.TargetRef.Namespace, addr.TargetRef.Name, err)
342
+				}
343
+
344
+				if pod != nil {
345
+					podRefs[*addr.TargetRef] = pod
346
+				}
347
+			}
348
+		}
349
+	}
350
+
351
+	// ... then, for each pod, we check the controller, and find the set of unique controllers...
352
+	immediateControllerRefs := make(map[api.ObjectReference]struct{})
353
+	for _, pod := range podRefs {
354
+		controllerRef, err := getControllerRef(pod, decoder)
355
+		if err != nil {
356
+			return nil, fmt.Errorf("unable to find controller for pod %s/%s: %v", pod.Namespace, pod.Name, err)
357
+		} else if controllerRef == nil {
358
+			return nil, fmt.Errorf("unable to find controller for pod %s/%s: no creator reference listed", pod.Namespace, pod.Name)
359
+		}
360
+
361
+		immediateControllerRefs[*controllerRef] = struct{}{}
362
+	}
363
+
364
+	// ... finally, for each controller, we load it, and see if there is a corresponding owner (to cover cases like DCs, Deployments, etc)
365
+	controllerRefs := make(map[unidlingapi.CrossGroupObjectReference]struct{})
366
+	for controllerRef := range immediateControllerRefs {
367
+		controller, err := getController(controllerRef)
368
+		if utilerrors.TolerateNotFoundError(err) != nil {
369
+			return nil, fmt.Errorf("unable to load %s %q: %v", controllerRef.Kind, controllerRef.Name, err)
370
+		}
371
+
372
+		if controller != nil {
373
+			var parentControllerRef *api.ObjectReference
374
+			parentControllerRef, err = getControllerRef(controller, decoder)
375
+			if err != nil {
376
+				return nil, fmt.Errorf("unable to load the creator of %s %q: %v", controllerRef.Kind, controllerRef.Name, err)
377
+			}
378
+
379
+			var crossGroupObjRef unidlingapi.CrossGroupObjectReference
380
+			if parentControllerRef == nil {
381
+				// if this is just a plain RC, use it
382
+				crossGroupObjRef, err = makeCrossGroupObjRef(&controllerRef)
383
+			} else {
384
+				crossGroupObjRef, err = makeCrossGroupObjRef(parentControllerRef)
385
+			}
386
+
387
+			if err != nil {
388
+				return nil, fmt.Errorf("unable to load the creator of %s %q: %v", controllerRef.Kind, controllerRef.Name, err)
389
+			}
390
+			controllerRefs[crossGroupObjRef] = struct{}{}
391
+		}
392
+	}
393
+
394
+	return controllerRefs, nil
395
+}
396
+
397
+// pairScalesWithScaleRefs takes some subresource references, a map of new scales for those subresource references,
398
+// and annotations from an existing object.  It merges the scales and references found in the existing annotations
399
+// with the new data (using the new scale in case of conflict if present and not 0, and the old scale otherwise),
400
+// and returns a slice of RecordedScaleReferences suitable for using as the new annotation value.
401
+func pairScalesWithScaleRefs(serviceName types.NamespacedName, annotations map[string]string, rawScaleRefs map[unidlingapi.CrossGroupObjectReference]struct{}, scales map[unidlingapi.CrossGroupObjectReference]int32) ([]unidlingapi.RecordedScaleReference, error) {
402
+	oldTargetsRaw, hasOldTargets := annotations[unidlingapi.UnidleTargetAnnotation]
403
+
404
+	scaleRefs := make([]unidlingapi.RecordedScaleReference, 0, len(rawScaleRefs))
405
+
406
+	// initialize the list of new annotations
407
+	for rawScaleRef := range rawScaleRefs {
408
+		scaleRefs = append(scaleRefs, unidlingapi.RecordedScaleReference{
409
+			CrossGroupObjectReference: rawScaleRef,
410
+			Replicas:                  0,
411
+		})
412
+	}
413
+
414
+	// if the new preserved scale would be 0, see if we have an old scale that we can use instead
415
+	if hasOldTargets {
416
+		var oldTargets []unidlingapi.RecordedScaleReference
417
+		oldTargetsSet := make(map[unidlingapi.CrossGroupObjectReference]int)
418
+		if err := json.Unmarshal([]byte(oldTargetsRaw), &oldTargets); err != nil {
419
+			return nil, fmt.Errorf("unable to extract existing scale information from endpoints %s: %v", serviceName.String(), err)
420
+		}
421
+
422
+		for i, target := range oldTargets {
423
+			oldTargetsSet[target.CrossGroupObjectReference] = i
424
+		}
425
+
426
+		// figure out which new targets were already present...
427
+		for _, newScaleRef := range scaleRefs {
428
+			if oldTargetInd, ok := oldTargetsSet[newScaleRef.CrossGroupObjectReference]; ok {
429
+				if newScale, ok := scales[newScaleRef.CrossGroupObjectReference]; !ok || newScale == 0 {
430
+					scales[newScaleRef.CrossGroupObjectReference] = oldTargets[oldTargetInd].Replicas
431
+				}
432
+				delete(oldTargetsSet, newScaleRef.CrossGroupObjectReference)
433
+			}
434
+		}
435
+
436
+		// ...and add in any existing targets not already on the new list to the new list
437
+		for _, ind := range oldTargetsSet {
438
+			scaleRefs = append(scaleRefs, oldTargets[ind])
439
+		}
440
+	}
441
+
442
+	for i := range scaleRefs {
443
+		scaleRef := &scaleRefs[i]
444
+		newScale, ok := scales[scaleRef.CrossGroupObjectReference]
445
+		if !ok || newScale == 0 {
446
+			newScale = 1
447
+			if scaleRef.Replicas != 0 {
448
+				newScale = scaleRef.Replicas
449
+			}
450
+		}
451
+
452
+		scaleRef.Replicas = newScale
453
+	}
454
+
455
+	return scaleRefs, nil
456
+}
457
+
458
+// setIdleAnnotations sets the given annotation on the given object to the marshaled list of CrossGroupObjectReferences
459
+func setIdleAnnotations(serviceName types.NamespacedName, annotations map[string]string, scaleRefs []unidlingapi.RecordedScaleReference, nowTime time.Time) error {
460
+	var scaleRefsBytes []byte
461
+	var err error
462
+	if scaleRefsBytes, err = json.Marshal(scaleRefs); err != nil {
463
+		return err
464
+	}
465
+
466
+	annotations[unidlingapi.UnidleTargetAnnotation] = string(scaleRefsBytes)
467
+	annotations[unidlingapi.IdledAtAnnotation] = nowTime.Format(time.RFC3339)
468
+
469
+	return nil
470
+}
471
+
472
+// patchObj patches calculates a patch between the given new object and the existing marshaled object
473
+func patchObj(obj runtime.Object, metadata meta.Object, oldData []byte, mapping *meta.RESTMapping, f *clientcmd.Factory) (runtime.Object, error) {
474
+	newData, err := json.Marshal(obj)
475
+	if err != nil {
476
+		return nil, err
477
+	}
478
+
479
+	patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
480
+	if err != nil {
481
+		return nil, err
482
+	}
483
+
484
+	client, err := f.ClientForMapping(mapping)
485
+	if err != nil {
486
+		return nil, err
487
+	}
488
+	helper := resource.NewHelper(client, mapping)
489
+
490
+	return helper.Patch(metadata.GetNamespace(), metadata.GetName(), api.StrategicMergePatchType, patchBytes)
491
+}
492
+
493
+type scaleInfo struct {
494
+	namespace string
495
+	scale     *extensions.Scale
496
+	obj       runtime.Object
497
+}
498
+
499
+// RunIdle runs the idling command logic, taking a list of resources or services in a file, scaling the associated
500
+// scalable resources to zero, and annotating the associated endpoints objects with the scalable resources to unidle
501
+// when they receive traffic.
502
+func (o *IdleOptions) RunIdle(f *clientcmd.Factory) error {
503
+	hadError := false
504
+	nowTime := time.Now().UTC()
505
+
506
+	// figure out which endpoints and resources we need to idle
507
+	byService, byScalable, err := o.calculateIdlableAnnotationsByService(f)
508
+
509
+	if err != nil {
510
+		if len(byService) == 0 || len(byScalable) == 0 {
511
+			return fmt.Errorf("no valid scalable resources found to idle: %v", err)
512
+		}
513
+		fmt.Fprintf(o.errOut, "warning: continuing on for valid scalable resources, but an error occured while finding scalable resources to idle: %v", err)
514
+	}
515
+
516
+	oclient, kclient, err := f.Clients()
517
+	if err != nil {
518
+		return err
519
+	}
520
+
521
+	delegScaleGetter := osclient.NewDelegatingScaleNamespacer(oclient, kclient)
522
+	dcGetter := deployclient.New(oclient.RESTClient)
523
+	rcGetter := clientset.FromUnversionedClient(kclient)
524
+
525
+	scaleAnnotater := utilunidling.NewScaleAnnotater(delegScaleGetter, dcGetter, rcGetter, func(annotations map[string]string) {
526
+		annotations[unidlingapi.IdledAtAnnotation] = nowTime.UTC().Format(time.RFC3339)
527
+	})
528
+
529
+	replicas := make(map[unidlingapi.CrossGroupObjectReference]int32, len(byScalable))
530
+	toScale := make(map[unidlingapi.CrossGroupObjectReference]scaleInfo)
531
+
532
+	mapper, typer := f.Object(false)
533
+
534
+	// first, collect the scale info
535
+	for scaleRef, svcName := range byScalable {
536
+		obj, scale, err := scaleAnnotater.GetObjectWithScale(svcName.Namespace, scaleRef)
537
+		if err != nil {
538
+			fmt.Fprintf(o.errOut, "error: unable to get scale for %s %s/%s, not marking that scalable as idled\n", scaleRef.Kind, svcName.Namespace, scaleRef.Name)
539
+			svcInfo := byService[svcName]
540
+			delete(svcInfo.scaleRefs, scaleRef)
541
+			hadError = true
542
+			continue
543
+		}
544
+		replicas[scaleRef] = scale.Spec.Replicas
545
+		toScale[scaleRef] = scaleInfo{scale: scale, obj: obj, namespace: svcName.Namespace}
546
+	}
547
+
548
+	// annotate the endpoints objects to indicate which scalable resources need to be unidled on traffic
549
+	for serviceName, info := range byService {
550
+		if info.obj.Annotations == nil {
551
+			info.obj.Annotations = make(map[string]string)
552
+		}
553
+		refsWithScale, err := pairScalesWithScaleRefs(serviceName, info.obj.Annotations, info.scaleRefs, replicas)
554
+		if err != nil {
555
+			fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
556
+			continue
557
+		}
558
+
559
+		if !o.dryRun {
560
+			if len(info.scaleRefs) == 0 {
561
+				fmt.Fprintf(o.errOut, "error: no scalable resources marked as idled for service %s, not marking as idled\n", serviceName.String())
562
+				hadError = true
563
+				continue
564
+			}
565
+
566
+			metadata, err := meta.Accessor(info.obj)
567
+			if err != nil {
568
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
569
+				hadError = true
570
+				continue
571
+			}
572
+			gvks, _, err := typer.ObjectKinds(info.obj)
573
+			if err != nil {
574
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
575
+				hadError = true
576
+				continue
577
+			}
578
+			oldData, err := json.Marshal(info.obj)
579
+			if err != nil {
580
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
581
+				hadError = true
582
+				continue
583
+			}
584
+
585
+			mapping, err := mapper.RESTMapping(gvks[0].GroupKind(), gvks[0].Version)
586
+			if err != nil {
587
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
588
+				hadError = true
589
+				continue
590
+			}
591
+
592
+			if err = setIdleAnnotations(serviceName, info.obj.Annotations, refsWithScale, nowTime); err != nil {
593
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
594
+				hadError = true
595
+				continue
596
+			}
597
+			if _, err := patchObj(info.obj, metadata, oldData, mapping, f); err != nil {
598
+				fmt.Fprintf(o.errOut, "error: unable to mark service %s as idled: %v", serviceName.String(), err)
599
+				hadError = true
600
+				continue
601
+			}
602
+		}
603
+
604
+		for _, scaleRef := range refsWithScale {
605
+			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)
606
+		}
607
+	}
608
+
609
+	// actually "idle" the scalable resources by scaling them down to zero
610
+	// (scale down to zero *after* we've applied the annotation so that we don't miss any traffic)
611
+	for scaleRef, info := range toScale {
612
+		if !o.dryRun {
613
+			info.scale.Spec.Replicas = 0
614
+			if err := scaleAnnotater.UpdateObjectScale(info.namespace, scaleRef, info.obj, info.scale); err != nil {
615
+				fmt.Fprintf(o.errOut, "error: unable to scale %s %s/%s to 0, but still listed as target for unidling\n", scaleRef.Kind, info.namespace, scaleRef.Name)
616
+				hadError = true
617
+				continue
618
+			}
619
+		}
620
+
621
+		fmt.Fprintf(o.out, "Idled %s %s/%s\n", scaleRef.Kind, info.namespace, scaleRef.Name)
622
+	}
623
+
624
+	if hadError {
625
+		return cmdutil.ErrExit
626
+	}
627
+
628
+	return nil
629
+}
0 630
new file mode 100644
... ...
@@ -0,0 +1,317 @@
0
+package cmd
1
+
2
+import (
3
+	"encoding/json"
4
+	"testing"
5
+
6
+	deployapi "github.com/openshift/origin/pkg/deploy/api"
7
+	unidlingapi "github.com/openshift/origin/pkg/unidling/api"
8
+
9
+	kapi "k8s.io/kubernetes/pkg/api"
10
+	kerrors "k8s.io/kubernetes/pkg/api/errors"
11
+	kunversioned "k8s.io/kubernetes/pkg/api/unversioned"
12
+	kcontroller "k8s.io/kubernetes/pkg/controller"
13
+	kruntime "k8s.io/kubernetes/pkg/runtime"
14
+	ktypes "k8s.io/kubernetes/pkg/types"
15
+
16
+	// install all APIs
17
+	_ "github.com/openshift/origin/pkg/api/install"
18
+	_ "k8s.io/kubernetes/pkg/api/install"
19
+)
20
+
21
+func makePod(name, rcName string, t *testing.T) kapi.Pod {
22
+	// this snippet is from kube's code to set the created-by annotation
23
+	// (which itself does not do quite what we want here)
24
+
25
+	codec := kapi.Codecs.LegacyCodec(kunversioned.GroupVersion{Group: kapi.GroupName, Version: "v1"})
26
+
27
+	createdByRefJson, err := kruntime.Encode(codec, &kapi.SerializedReference{
28
+		Reference: kapi.ObjectReference{
29
+			Kind:      "ReplicationController",
30
+			Name:      rcName,
31
+			Namespace: "somens",
32
+		},
33
+	})
34
+
35
+	if err != nil {
36
+		t.Fatalf("Unexpected error: %v", err)
37
+	}
38
+
39
+	return kapi.Pod{
40
+		ObjectMeta: kapi.ObjectMeta{
41
+			Name:      name,
42
+			Namespace: "somens",
43
+			Annotations: map[string]string{
44
+				kcontroller.CreatedByAnnotation: string(createdByRefJson),
45
+			},
46
+		},
47
+	}
48
+}
49
+
50
+func makeRC(name, dcName, createdByDCName string, t *testing.T) *kapi.ReplicationController {
51
+	rc := kapi.ReplicationController{
52
+		ObjectMeta: kapi.ObjectMeta{
53
+			Name:        name,
54
+			Namespace:   "somens",
55
+			Annotations: make(map[string]string),
56
+		},
57
+	}
58
+
59
+	if createdByDCName != "" {
60
+		codec := kapi.Codecs.LegacyCodec(kunversioned.GroupVersion{Group: kapi.GroupName, Version: "v1"})
61
+		createdByRefJson, err := kruntime.Encode(codec, &kapi.SerializedReference{
62
+			Reference: kapi.ObjectReference{
63
+				Kind:      "DeploymentConfig",
64
+				Name:      createdByDCName,
65
+				Namespace: "somens",
66
+			},
67
+		})
68
+
69
+		if err != nil {
70
+			t.Fatalf("Unexpected error: %v", err)
71
+		}
72
+
73
+		rc.Annotations[kcontroller.CreatedByAnnotation] = string(createdByRefJson)
74
+	}
75
+
76
+	if dcName != "" {
77
+		rc.Annotations[deployapi.DeploymentConfigAnnotation] = dcName
78
+	}
79
+
80
+	return &rc
81
+}
82
+
83
+func makePodRef(name string) *kapi.ObjectReference {
84
+	return &kapi.ObjectReference{
85
+		Kind:      "Pod",
86
+		Name:      name,
87
+		Namespace: "somens",
88
+	}
89
+}
90
+
91
+func makeRCRef(name string) *kapi.ObjectReference {
92
+	return &kapi.ObjectReference{
93
+		Kind:      "ReplicationController",
94
+		Name:      name,
95
+		Namespace: "somens",
96
+	}
97
+}
98
+
99
+func TestFindIdlablesForEndpoints(t *testing.T) {
100
+	endpoints := &kapi.Endpoints{
101
+		Subsets: []kapi.EndpointSubset{
102
+			{
103
+				Addresses: []kapi.EndpointAddress{
104
+					{
105
+						TargetRef: makePodRef("somepod1"),
106
+					},
107
+					{
108
+						TargetRef: makePodRef("somepod2"),
109
+					},
110
+					{
111
+						TargetRef: &kapi.ObjectReference{
112
+							Kind:      "Cheese",
113
+							Name:      "cheddar",
114
+							Namespace: "somens",
115
+						},
116
+					},
117
+				},
118
+			},
119
+			{
120
+				Addresses: []kapi.EndpointAddress{
121
+					{},
122
+					{
123
+						TargetRef: makePodRef("somepod3"),
124
+					},
125
+					{
126
+						TargetRef: makePodRef("somepod4"),
127
+					},
128
+					{
129
+						TargetRef: makePodRef("somepod5"),
130
+					},
131
+					{
132
+						TargetRef: makePodRef("missingpod"),
133
+					},
134
+				},
135
+			},
136
+		},
137
+	}
138
+
139
+	pods := map[kapi.ObjectReference]kapi.Pod{
140
+		*makePodRef("somepod1"): makePod("somepod1", "somerc1", t),
141
+		*makePodRef("somepod2"): makePod("somepod2", "somerc2", t),
142
+		*makePodRef("somepod3"): makePod("somepod3", "somerc1", t),
143
+		*makePodRef("somepod4"): makePod("somepod4", "somerc3", t),
144
+		*makePodRef("somepod5"): makePod("somepod5", "somerc4", t),
145
+	}
146
+
147
+	getPod := func(ref kapi.ObjectReference) (*kapi.Pod, error) {
148
+		if pod, ok := pods[ref]; ok {
149
+			return &pod, nil
150
+		}
151
+		return nil, kerrors.NewNotFound(kunversioned.GroupResource{Group: kapi.GroupName, Resource: "Pod"}, ref.Name)
152
+	}
153
+
154
+	controllers := map[kapi.ObjectReference]kruntime.Object{
155
+		// prefer CreatedByAnnotation to DeploymentConfigAnnotation
156
+		*makeRCRef("somerc1"): makeRC("somerc1", "nonsense-value", "somedc1", t),
157
+		*makeRCRef("somerc2"): makeRC("somerc2", "", "", t),
158
+		*makeRCRef("somerc3"): makeRC("somerc3", "somedc2", "", t),
159
+		*makeRCRef("somerc4"): makeRC("somerc4", "", "somedc2", t),
160
+	}
161
+
162
+	getController := func(ref kapi.ObjectReference) (kruntime.Object, error) {
163
+		if controller, ok := controllers[ref]; ok {
164
+			return controller, nil
165
+		}
166
+
167
+		// NB: this GroupResource declaration plays fast and loose with various distinctions
168
+		// but is good enough for being an error in a test
169
+		return nil, kerrors.NewNotFound(kunversioned.GroupResource{Group: kapi.GroupName, Resource: ref.Kind}, ref.Name)
170
+
171
+	}
172
+
173
+	codec := kapi.Codecs.LegacyCodec(kunversioned.GroupVersion{Group: kapi.GroupName, Version: "v1"})
174
+	refSet, err := findScalableResourcesForEndpoints(endpoints, codec, getPod, getController)
175
+
176
+	if err != nil {
177
+		t.Fatalf("Unexpected error while finding idlables: %v", err)
178
+	}
179
+
180
+	expectedRefs := []unidlingapi.CrossGroupObjectReference{
181
+		{
182
+			Kind: "DeploymentConfig",
183
+			Name: "somedc1",
184
+		},
185
+		{
186
+			Kind: "DeploymentConfig",
187
+			Name: "somedc2",
188
+		},
189
+		{
190
+			Kind: "ReplicationController",
191
+			Name: "somerc2",
192
+		},
193
+	}
194
+
195
+	if len(refSet) != len(expectedRefs) {
196
+		t.Errorf("Expected to get somedc1, somedc2, somerc2, instead got %#v", refSet)
197
+	}
198
+
199
+	for _, ref := range expectedRefs {
200
+		if _, ok := refSet[ref]; !ok {
201
+			t.Errorf("expected ReplicationController %q to be present, but was not", ref.Name)
202
+		}
203
+	}
204
+}
205
+
206
+func TestPairScalesWithIdlables(t *testing.T) {
207
+	oldScaleRefs := []unidlingapi.RecordedScaleReference{
208
+		{
209
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
210
+				Kind: "ReplicationController",
211
+				Name: "somerc1",
212
+			},
213
+			Replicas: 5,
214
+		},
215
+		{
216
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
217
+				Kind: "DeploymentConfig",
218
+				Name: "somedc1",
219
+			},
220
+			Replicas: 3,
221
+		},
222
+	}
223
+
224
+	oldScaleRefBytes, err := json.Marshal(oldScaleRefs)
225
+	if err != nil {
226
+		t.Fatalf("Unexpected error: %v", err)
227
+	}
228
+	oldAnnotations := map[string]string{
229
+		unidlingapi.UnidleTargetAnnotation: string(oldScaleRefBytes),
230
+	}
231
+
232
+	newRawRefs := map[unidlingapi.CrossGroupObjectReference]struct{}{
233
+		{
234
+			Kind: "ReplicationController",
235
+			Name: "somerc1",
236
+		}: {},
237
+		{
238
+			Kind: "ReplicationController",
239
+			Name: "somerc2",
240
+		}: {},
241
+		{
242
+			Kind: "DeploymentConfig",
243
+			Name: "somedc1",
244
+		}: {},
245
+		{
246
+			Kind: "DeploymentConfig",
247
+			Name: "somedc2",
248
+		}: {},
249
+	}
250
+
251
+	scales := map[unidlingapi.CrossGroupObjectReference]int32{
252
+		{
253
+			Kind: "ReplicationController",
254
+			Name: "somerc1",
255
+		}: 2,
256
+		{
257
+			Kind: "ReplicationController",
258
+			Name: "somerc2",
259
+		}: 5,
260
+		{
261
+			Kind: "DeploymentConfig",
262
+			Name: "somedc1",
263
+		}: 0,
264
+		{
265
+			Kind: "DeploymentConfig",
266
+			Name: "somedc2",
267
+		}: 0,
268
+	}
269
+
270
+	newScaleRefs, err := pairScalesWithScaleRefs(ktypes.NamespacedName{Name: "somesvc"}, oldAnnotations, newRawRefs, scales)
271
+
272
+	expectedScaleRefs := map[unidlingapi.RecordedScaleReference]struct{}{
273
+		{
274
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
275
+				Kind: "ReplicationController",
276
+				Name: "somerc1",
277
+			},
278
+			Replicas: 2,
279
+		}: {},
280
+		{
281
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
282
+				Kind: "ReplicationController",
283
+				Name: "somerc2",
284
+			},
285
+			Replicas: 5,
286
+		}: {},
287
+		{
288
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
289
+				Kind: "DeploymentConfig",
290
+				Name: "somedc1",
291
+			},
292
+			Replicas: 3,
293
+		}: {},
294
+		{
295
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
296
+				Kind: "DeploymentConfig",
297
+				Name: "somedc2",
298
+			},
299
+			Replicas: 1,
300
+		}: {},
301
+	}
302
+
303
+	if err != nil {
304
+		t.Fatalf("Unexpected error while generating new annotation value: %v", err)
305
+	}
306
+
307
+	if len(newScaleRefs) != len(expectedScaleRefs) {
308
+		t.Fatalf("Expected new recorded scale references of %#v, got %#v", expectedScaleRefs, newScaleRefs)
309
+	}
310
+
311
+	for _, scaleRef := range newScaleRefs {
312
+		if _, wasPresent := expectedScaleRefs[scaleRef]; !wasPresent {
313
+			t.Errorf("Unexpected recorded scale reference %#v found in the output", scaleRef)
314
+		}
315
+	}
316
+}
0 317
new file mode 100755
... ...
@@ -0,0 +1,79 @@
0
+#!/bin/bash
1
+
2
+set -o errexit
3
+set -o nounset
4
+set -o pipefail
5
+
6
+OS_ROOT=$(dirname "${BASH_SOURCE}")/../..
7
+source "${OS_ROOT}/hack/lib/init.sh"
8
+os::log::stacktrace::install
9
+trap os::test::junit::reconcile_output EXIT
10
+
11
+# Cleanup cluster resources created by this test
12
+(
13
+  set +e
14
+  oc delete all,templates --all
15
+  exit 0
16
+) &>/dev/null
17
+
18
+project=$(oc project -q)
19
+idled_at_annotation='idling.alpha.openshift.io/idled-at'
20
+unidle_target_annotation='idling.alpha.openshift.io/unidle-targets'
21
+idled_at_template="{{index .metadata.annotations \"${idled_at_annotation}\"}}"
22
+unidle_target_template="{{index .metadata.annotations \"${unidle_target_annotation}\"}}"
23
+
24
+setup_idling_resources() {
25
+    os::cmd::expect_success 'oc delete all --all'
26
+
27
+    # set up resources for the idle command
28
+    os::cmd::expect_success 'oc create -f test/extended/testdata/idling-echo-server.yaml'
29
+    os::cmd::expect_success 'oc describe deploymentconfigs idling-echo'
30
+    os::cmd::try_until_success 'oc describe endpoints idling-echo'
31
+    local endpoints_json=$(oc get endpoints idling-echo -o json)
32
+    os::cmd::expect_success 'oc delete service idling-echo'
33
+    os::cmd::expect_success "echo '${endpoints_json}' | oc create -f -"
34
+    os::cmd::expect_success 'oc describe endpoints idling-echo'
35
+    # deployer pod won't work, so just scale up the rc ourselves
36
+    os::cmd::expect_success 'oc scale replicationcontroller idling-echo-1 --replicas=2'
37
+    pod_name=$(oc get pod -l app=idling-echo -o go-template='{{ (index .items 0).metadata.name }}')
38
+    fake_endpoints_patch=$(cat <<EOF
39
+{
40
+    "subsets": [{
41
+        "addresses": [{
42
+            "ip": "1.2.3.4",
43
+            "targetRef": {
44
+                "kind": "Pod",
45
+                "name": "${pod_name}",
46
+                "namespace": "${project}"
47
+            }
48
+        }],
49
+        "ports": [{"name": "foo", "port": 80}]
50
+    }]
51
+}
52
+EOF
53
+)
54
+
55
+    os::cmd::expect_success "oc patch endpoints idling-echo -p '${fake_endpoints_patch}'"
56
+    os::cmd::try_until_text 'oc get endpoints idling-echo -o go-template="{{ len .subsets }}"' '1'
57
+}
58
+
59
+os::test::junit::declare_suite_start "cmd/idle/by-name"
60
+setup_idling_resources
61
+os::cmd::expect_success_and_text 'oc idle idling-echo' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/idling-echo \(unidle to 2 replicas\)"
62
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
63
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq 'length == 1 and (.[0] | .replicas == 2 and .name == \"idling-echo\" and .kind == \"DeploymentConfig\")'" 'true'
64
+os::test::junit::declare_suite_end
65
+
66
+os::test::junit::declare_suite_start "cmd/idle/by-label"
67
+setup_idling_resources
68
+os::cmd::expect_success_and_text 'oc idle -l app=idling-echo' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/idling-echo \(unidle to 2 replicas\)"
69
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
70
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq 'length == 1 and (.[0] | .replicas == 2 and .name == \"idling-echo\" and .kind == \"DeploymentConfig\")'" 'true'
71
+os::test::junit::declare_suite_end
72
+
73
+os::test::junit::declare_suite_start "cmd/idle/all"
74
+setup_idling_resources
75
+os::cmd::expect_success_and_text 'oc idle --all' "Marked service ${project}/idling-echo to unidle resource DeploymentConfig ${project}/idling-echo \(unidle to 2 replicas\)"
76
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${idled_at_template}'" '.'
77
+os::cmd::expect_success_and_text "oc get endpoints idling-echo -o go-template='${unidle_target_template}' | jq 'length == 1 and (.[0] | .replicas == 2 and .name == \"idling-echo\" and .kind == \"DeploymentConfig\")'" 'true'
78
+os::test::junit::declare_suite_end
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	_ "github.com/openshift/origin/test/extended/cli"
10 10
 	_ "github.com/openshift/origin/test/extended/deployments"
11 11
 	_ "github.com/openshift/origin/test/extended/dns"
12
+	_ "github.com/openshift/origin/test/extended/idling"
12 13
 	_ "github.com/openshift/origin/test/extended/images"
13 14
 	_ "github.com/openshift/origin/test/extended/jenkins"
14 15
 	_ "github.com/openshift/origin/test/extended/jobs"
15 16
new file mode 100644
... ...
@@ -0,0 +1,158 @@
0
+package idling
1
+
2
+import (
3
+	"encoding/json"
4
+	"fmt"
5
+	"io/ioutil"
6
+	"os"
7
+	"strings"
8
+	"time"
9
+
10
+	g "github.com/onsi/ginkgo"
11
+	o "github.com/onsi/gomega"
12
+
13
+	unidlingapi "github.com/openshift/origin/pkg/unidling/api"
14
+	exutil "github.com/openshift/origin/test/extended/util"
15
+)
16
+
17
+func createFixture(oc *exutil.CLI, path string) ([]string, []string, error) {
18
+	output, err := oc.Run("create").Args("-f", path, "-o", "name").Output()
19
+	if err != nil {
20
+		return nil, nil, err
21
+	}
22
+
23
+	lines := strings.Split(output, "\n")
24
+
25
+	resources := make([]string, 0, len(lines)-1)
26
+	names := make([]string, 0, len(lines)-1)
27
+
28
+	for _, line := range lines {
29
+		if line == "" {
30
+			continue
31
+		}
32
+		parts := strings.Split(line, "/")
33
+		if len(parts) != 2 {
34
+			return nil, nil, fmt.Errorf("expected type/name syntax, got: %q", line)
35
+		}
36
+		resources = append(resources, parts[0])
37
+		names = append(names, parts[1])
38
+	}
39
+
40
+	return resources, names, nil
41
+}
42
+
43
+func checkSingleIdle(oc *exutil.CLI, idlingFile string, resources map[string][]string, resourceName string, kind string) {
44
+	g.By("Idling the service")
45
+	_, err := oc.Run("idle").Args("--resource-names-file", idlingFile).Output()
46
+	o.Expect(err).ToNot(o.HaveOccurred())
47
+
48
+	g.By("Ensuring the scale is zero")
49
+	objName := resources[resourceName][0]
50
+	replicas, err := oc.Run("get").Args(resourceName+"/"+objName, "--output=jsonpath=\"{.spec.replicas}\"").Output()
51
+	o.Expect(err).ToNot(o.HaveOccurred())
52
+	o.Expect(replicas).To(o.ContainSubstring("0"))
53
+
54
+	g.By("Fetching the service and checking the annotations are present")
55
+	serviceName := resources["service"][0]
56
+	endpoints, err := oc.KubeREST().Endpoints(oc.Namespace()).Get(serviceName)
57
+	o.Expect(err).NotTo(o.HaveOccurred())
58
+
59
+	o.Expect(endpoints.Annotations).To(o.HaveKey(unidlingapi.IdledAtAnnotation))
60
+	o.Expect(endpoints.Annotations).To(o.HaveKey(unidlingapi.UnidleTargetAnnotation))
61
+
62
+	g.By("Checking the idled-at time")
63
+	idledAtAnnotation := endpoints.Annotations[unidlingapi.IdledAtAnnotation]
64
+	idledAtTime, err := time.Parse(time.RFC3339, idledAtAnnotation)
65
+	o.Expect(err).ToNot(o.HaveOccurred())
66
+	o.Expect(idledAtTime).To(o.BeTemporally("~", time.Now(), 5*time.Minute))
67
+
68
+	g.By("Checking the idle targets")
69
+	unidleTargetAnnotation := endpoints.Annotations[unidlingapi.UnidleTargetAnnotation]
70
+	unidleTargets := []unidlingapi.RecordedScaleReference{}
71
+	err = json.Unmarshal([]byte(unidleTargetAnnotation), &unidleTargets)
72
+	o.Expect(err).ToNot(o.HaveOccurred())
73
+	o.Expect(unidleTargets).To(o.Equal([]unidlingapi.RecordedScaleReference{
74
+		{
75
+			Replicas: 2,
76
+			CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
77
+				Name: resources[resourceName][0],
78
+				Kind: kind,
79
+			},
80
+		},
81
+	}))
82
+}
83
+
84
+var _ = g.Describe("idling and uidling", func() {
85
+	defer g.GinkgoRecover()
86
+	var (
87
+		oc                  = exutil.NewCLI("cli-idling", exutil.KubeConfigPath()).Verbose()
88
+		echoServerFixture   = exutil.FixturePath("testdata", "idling-echo-server.yaml")
89
+		echoServerRcFixture = exutil.FixturePath("testdata", "idling-echo-server-rc.yaml")
90
+		framework           = oc.KubeFramework()
91
+	)
92
+
93
+	// path to the fixture
94
+	var fixture string
95
+
96
+	// path to the idling file
97
+	var idlingFile string
98
+
99
+	// map of all resources created from the fixtures
100
+	var resources map[string][]string
101
+
102
+	g.JustBeforeEach(func() {
103
+		g.By("Creating the resources")
104
+		rawResources, rawResourceNames, err := createFixture(oc, fixture)
105
+		o.Expect(err).ToNot(o.HaveOccurred())
106
+
107
+		resources = make(map[string][]string)
108
+		for i, resource := range rawResources {
109
+			resources[resource] = append(resources[resource], rawResourceNames[i])
110
+		}
111
+
112
+		g.By("Creating the idling file")
113
+		serviceNames := resources["service"]
114
+
115
+		targetFile, err := ioutil.TempFile(exutil.TestContext.OutputDir, "idling-services-")
116
+		o.Expect(err).ToNot(o.HaveOccurred())
117
+		defer targetFile.Close()
118
+		idlingFile = targetFile.Name()
119
+		_, err = targetFile.Write([]byte(strings.Join(serviceNames, "\n")))
120
+		o.Expect(err).ToNot(o.HaveOccurred())
121
+
122
+		g.By("Waiting for the endpoints to exist")
123
+		serviceName := resources["service"][0]
124
+		g.By("Waiting for endpoints to be up")
125
+		err = waitForEndpointsAvailable(oc, serviceName)
126
+		o.Expect(err).ToNot(o.HaveOccurred())
127
+	})
128
+
129
+	g.AfterEach(func() {
130
+		g.By("Cleaning up the idling file")
131
+		os.Remove(idlingFile)
132
+	})
133
+
134
+	g.Describe("idling", func() {
135
+		g.Context("with a single service and DeploymentConfig", func() {
136
+			g.BeforeEach(func() {
137
+				framework.BeforeEach()
138
+				fixture = echoServerFixture
139
+			})
140
+
141
+			g.It("should idle the service and DeploymentConfig properly", func() {
142
+				checkSingleIdle(oc, idlingFile, resources, "deploymentconfig", "DeploymentConfig")
143
+			})
144
+		})
145
+
146
+		g.Context("with a single service and ReplicationController", func() {
147
+			g.BeforeEach(func() {
148
+				framework.BeforeEach()
149
+				fixture = echoServerRcFixture
150
+			})
151
+
152
+			g.It("should idle the service and ReplicationController properly", func() {
153
+				checkSingleIdle(oc, idlingFile, resources, "replicationcontroller", "ReplicationController")
154
+			})
155
+		})
156
+	})
157
+})
0 158
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package idling
1
+
2
+import (
3
+	"time"
4
+
5
+	"github.com/openshift/origin/pkg/util/errors"
6
+	exutil "github.com/openshift/origin/test/extended/util"
7
+	"k8s.io/kubernetes/pkg/util/wait"
8
+)
9
+
10
+func waitForEndpointsAvailable(oc *exutil.CLI, serviceName string) error {
11
+	return wait.Poll(200*time.Millisecond, 2*time.Minute, func() (bool, error) {
12
+		ep, err := oc.KubeREST().Endpoints(oc.Namespace()).Get(serviceName)
13
+		// Tolerate NotFound b/c it could take a moment for the endpoints to be created
14
+		if errors.TolerateNotFoundError(err) != nil {
15
+			return false, err
16
+		}
17
+
18
+		return (len(ep.Subsets) > 0) && (len(ep.Subsets[0].Addresses) > 0), nil
19
+	})
20
+}
0 21
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+apiVersion: v1
1
+kind: List
2
+items:
3
+- apiVersion: v1
4
+  kind: ReplicationController
5
+  metadata:
6
+    name: idling-echo-rc
7
+  spec:
8
+    replicas: 2
9
+    selector:
10
+      app: idling-echo
11
+      replicationcontroller: idling-echo
12
+    template:
13
+      metadata:
14
+        labels:
15
+          app: idling-echo
16
+          replicationcontroller: idling-echo
17
+      spec:
18
+        containers:
19
+        - image: openshift/node
20
+          name: idling-echo
21
+          command:
22
+            - /usr/bin/socat
23
+            - TCP4-LISTEN:8675,reuseaddr,fork
24
+            - EXEC:'/bin/cat'
25
+          ports:
26
+          - containerPort: 8675
27
+            protocol: TCP
28
+        dnsPolicy: ClusterFirst
29
+        restartPolicy: Always
30
+        securityContext: {}
31
+- apiVersion: v1
32
+  kind: Service
33
+  metadata:
34
+    name: idling-echo
35
+  spec:
36
+    selector:
37
+      app: idling-echo
38
+    ports:
39
+      - port: 8675
40
+- apiVersion: v1
41
+  kind: Route
42
+  metadata:
43
+    name: idling-echo
44
+  spec:
45
+    to:
46
+      kind: Service
47
+      name: idling-echo
48
+
0 49
new file mode 100644
... ...
@@ -0,0 +1,67 @@
0
+apiVersion: v1
1
+kind: List
2
+metadata: {}
3
+items:
4
+- apiVersion: v1
5
+  kind: DeploymentConfig
6
+  metadata:
7
+    name: idling-echo
8
+  spec:
9
+    replicas: 2
10
+    selector:
11
+      app: idling-echo
12
+      deploymentconfig: idling-echo
13
+    strategy:
14
+      type: Rolling
15
+    template:
16
+      metadata:
17
+        labels:
18
+          app: idling-echo
19
+          deploymentconfig: idling-echo
20
+      spec:
21
+        containers:
22
+        - image: openshift/node
23
+          name: idling-tcp-echo
24
+          command:
25
+            - /usr/bin/socat
26
+            - TCP4-LISTEN:8675,reuseaddr,fork
27
+            - EXEC:'/bin/cat'
28
+          ports:
29
+          - containerPort: 8675
30
+            protocol: TCP
31
+        - image: openshift/node
32
+          name: idling-udp-echo
33
+          command:
34
+            - /usr/bin/socat
35
+            - UDP4-LISTEN:3090,reuseaddr,fork
36
+            - EXEC:'/bin/cat'
37
+          ports:
38
+          - containerPort: 3090
39
+            protocol: UDP
40
+        dnsPolicy: ClusterFirst
41
+        restartPolicy: Always
42
+        securityContext: {}
43
+- apiVersion: v1
44
+  kind: Service
45
+  metadata:
46
+    name: idling-echo
47
+    labels:
48
+      app: idling-echo
49
+  spec:
50
+    selector:
51
+      app: idling-echo
52
+    ports:
53
+      - port: 8675
54
+        name: tcp-echo
55
+        protocol: TCP
56
+      - port: 3090
57
+        name: udp-echo
58
+        protocol: UDP
59
+- apiVersion: v1
60
+  kind: Route
61
+  metadata:
62
+    name: idling-echo
63
+  spec:
64
+    to:
65
+      kind: Service
66
+      name: idling-echo