@@ -11,18 +11,18 @@ import (
11
11
"github.com/spf13/cobra"
12
12
13
13
utilerrors "github.com/openshift/origin/pkg/util/errors"
14
+ corev1 "k8s.io/api/core/v1"
14
15
extensions "k8s.io/api/extensions/v1beta1"
15
16
"k8s.io/apimachinery/pkg/api/meta"
16
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18
"k8s.io/apimachinery/pkg/runtime"
18
19
"k8s.io/apimachinery/pkg/runtime/schema"
19
20
"k8s.io/apimachinery/pkg/types"
20
21
"k8s.io/apimachinery/pkg/util/strategicpatch"
22
+ "k8s.io/client-go/kubernetes"
21
23
kextensionsclient "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
22
24
"k8s.io/client-go/rest"
23
25
"k8s.io/kubernetes/pkg/api/legacyscheme"
24
- kapi "k8s.io/kubernetes/pkg/apis/core"
25
- kinternalclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
26
26
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
27
27
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
28
28
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
@@ -54,23 +54,23 @@ var (
54
54
)
55
55
56
56
type IdleOptions struct {
57
- dryRun bool
58
-
57
+ dryRun bool
59
58
filename string
60
59
all bool
61
60
selector string
62
61
allNamespaces bool
63
- resources string
62
+ resources [] string
64
63
65
64
cmdFullName string
66
65
67
66
ClientForMappingFn func (* meta.RESTMapping ) (resource.RESTClient , error )
68
67
ClientConfig * rest.Config
69
- ClientSet kinternalclientset .Interface
68
+ ClientSet kubernetes .Interface
70
69
Mapper meta.RESTMapper
71
70
72
- nowTime time.Time
73
- svcBuilder * resource.Builder
71
+ Builder func () * resource.Builder
72
+ Namespace string
73
+ nowTime time.Time
74
74
75
75
genericclioptions.IOStreams
76
76
}
@@ -110,7 +110,8 @@ func NewCmdIdle(fullName string, f kcmdutil.Factory, streams genericclioptions.I
110
110
}
111
111
112
112
func (o * IdleOptions ) Complete (f kcmdutil.Factory , cmd * cobra.Command , args []string ) error {
113
- namespace , _ , err := f .ToRawKubeConfigLoader ().Namespace ()
113
+ var err error
114
+ o .Namespace , _ , err = f .ToRawKubeConfigLoader ().Namespace ()
114
115
if err != nil {
115
116
return err
116
117
}
@@ -127,7 +128,7 @@ func (o *IdleOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
127
128
return err
128
129
}
129
130
130
- o .ClientSet , err = f . ClientSet ( )
131
+ o .ClientSet , err = kubernetes . NewForConfig ( o . ClientConfig )
131
132
if err != nil {
132
133
return err
133
134
}
@@ -138,32 +139,9 @@ func (o *IdleOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
138
139
}
139
140
140
141
o .ClientForMappingFn = f .ClientForMapping
142
+ o .Builder = f .NewBuilder
141
143
142
- o .svcBuilder = f .NewBuilder ().
143
- WithScheme (ocscheme .ReadingInternalScheme ).
144
- ContinueOnError ().
145
- NamespaceParam (namespace ).DefaultNamespace ().AllNamespaces (o .allNamespaces ).
146
- Flatten ().
147
- SingleResourceType ()
148
-
149
- if len (o .filename ) > 0 {
150
- targetServiceNames , err := scanLinesFromFile (o .filename )
151
- if err != nil {
152
- return err
153
- }
154
- o .svcBuilder .ResourceNames ("endpoints" , targetServiceNames ... )
155
- } else {
156
- // NB: this is a bit weird because the resource builder will complain if we use ResourceTypes and ResourceNames when len(args) > 0
157
- if o .selector != "" {
158
- o .svcBuilder .LabelSelectorParam (o .selector ).ResourceTypes ("endpoints" )
159
- }
160
-
161
- o .svcBuilder .ResourceNames ("endpoints" , args ... )
162
-
163
- if o .all {
164
- o .svcBuilder .ResourceTypes ("endpoints" ).SelectAllParam (o .all )
165
- }
166
- }
144
+ o .resources = args
167
145
168
146
return nil
169
147
}
@@ -206,7 +184,7 @@ func scanLinesFromFile(filename string) ([]string, error) {
206
184
// idleUpdateInfo contains the required info to annotate an endpoints object
207
185
// with the scalable resources that it should unidle
208
186
type idleUpdateInfo struct {
209
- obj * kapi .Endpoints
187
+ obj * corev1 .Endpoints
210
188
scaleRefs map [unidlingapi.CrossGroupObjectReference ]struct {}
211
189
}
212
190
@@ -224,9 +202,9 @@ type controllerRef struct {
224
202
// Using the list of services, it figures out the associated scalable objects, and returns a map from the endpoints object for the services to
225
203
// the list of scalable resources associated with that endpoints object, as well as a map from CrossGroupObjectReferences to scale to 0 to the
226
204
// name of the associated service.
227
- func (o * IdleOptions ) calculateIdlableAnnotationsByService () (map [types.NamespacedName ]idleUpdateInfo , map [namespacedCrossGroupObjectReference ]types.NamespacedName , error ) {
228
- podsLoaded := make (map [kapi .ObjectReference ]* kapi .Pod )
229
- getPod := func (ref kapi .ObjectReference ) (* kapi .Pod , error ) {
205
+ func (o * IdleOptions ) calculateIdlableAnnotationsByService (infoVisitor func (resource. VisitorFunc ) error ) (map [types.NamespacedName ]idleUpdateInfo , map [namespacedCrossGroupObjectReference ]types.NamespacedName , error ) {
206
+ podsLoaded := make (map [corev1 .ObjectReference ]* corev1 .Pod )
207
+ getPod := func (ref corev1 .ObjectReference ) (* corev1 .Pod , error ) {
230
208
if pod , ok := podsLoaded [ref ]; ok {
231
209
return pod , nil
232
210
}
@@ -287,12 +265,12 @@ func (o *IdleOptions) calculateIdlableAnnotationsByService() (map[types.Namespac
287
265
targetScaleRefs := make (map [namespacedCrossGroupObjectReference ]types.NamespacedName )
288
266
endpointsInfo := make (map [types.NamespacedName ]idleUpdateInfo )
289
267
290
- err := o . svcBuilder . Do (). Visit (func (info * resource.Info , err error ) error {
268
+ err := infoVisitor (func (info * resource.Info , err error ) error {
291
269
if err != nil {
292
270
return err
293
271
}
294
272
295
- endpoints , isEndpoints := info .Object .(* kapi .Endpoints )
273
+ endpoints , isEndpoints := info .Object .(* corev1 .Endpoints )
296
274
if ! isEndpoints {
297
275
return fmt .Errorf ("you must specify endpoints, not %v (view available endpoints with \" %s get endpoints\" )." , info .Mapping .Resource , o .cmdFullName )
298
276
}
@@ -372,9 +350,9 @@ func normalizedNSOwnerRef(namespace string, ownerRef *metav1.OwnerReference) nam
372
350
// scalable objects by checking each address in each subset to see if it has a pod
373
351
// reference, and the following that pod reference to find the owning controller,
374
352
// and returning the unique set of controllers found this way.
375
- func findScalableResourcesForEndpoints (endpoints * kapi .Endpoints , getPod func (kapi .ObjectReference ) (* kapi .Pod , error ), getController func (namespacedOwnerReference ) (metav1.Object , error )) (map [namespacedCrossGroupObjectReference ]struct {}, error ) {
353
+ func findScalableResourcesForEndpoints (endpoints * corev1 .Endpoints , getPod func (corev1 .ObjectReference ) (* corev1 .Pod , error ), getController func (namespacedOwnerReference ) (metav1.Object , error )) (map [namespacedCrossGroupObjectReference ]struct {}, error ) {
376
354
// To find all RCs and DCs for an endpoint, we first figure out which pods are pointed to by that endpoint...
377
- podRefs := map [kapi .ObjectReference ]* kapi .Pod {}
355
+ podRefs := map [corev1 .ObjectReference ]* corev1 .Pod {}
378
356
for _ , subset := range endpoints .Subsets {
379
357
for _ , addr := range subset .Addresses {
380
358
if addr .TargetRef != nil && addr .TargetRef .Kind == "Pod" {
@@ -547,6 +525,32 @@ type scaleInfo struct {
547
525
// scalable resources to zero, and annotating the associated endpoints objects with the scalable resources to unidle
548
526
// when they receive traffic.
549
527
func (o * IdleOptions ) RunIdle () error {
528
+ b := o .Builder ().
529
+ WithScheme (ocscheme .ReadingInternalScheme , ocscheme .ReadingInternalScheme .PrioritizedVersionsAllGroups ()... ).
530
+ ContinueOnError ().
531
+ NamespaceParam (o .Namespace ).DefaultNamespace ().AllNamespaces (o .allNamespaces ).
532
+ Flatten ().
533
+ SingleResourceType ()
534
+
535
+ if len (o .filename ) > 0 {
536
+ targetServiceNames , err := scanLinesFromFile (o .filename )
537
+ if err != nil {
538
+ return err
539
+ }
540
+ b .ResourceNames ("endpoints" , targetServiceNames ... )
541
+ } else {
542
+ // NB: this is a bit weird because the resource builder will complain if we use ResourceTypes and ResourceNames when len(args) > 0
543
+ if o .selector != "" {
544
+ b .LabelSelectorParam (o .selector ).ResourceTypes ("endpoints" )
545
+ }
546
+
547
+ b .ResourceNames ("endpoints" , o .resources ... )
548
+
549
+ if o .all {
550
+ b .ResourceTypes ("endpoints" ).SelectAllParam (o .all )
551
+ }
552
+ }
553
+
550
554
hadError := false
551
555
nowTime := time .Now ().UTC ()
552
556
@@ -556,7 +560,7 @@ func (o *IdleOptions) RunIdle() error {
556
560
}
557
561
558
562
// figure out which endpoints and resources we need to idle
559
- byService , byScalable , err := o .calculateIdlableAnnotationsByService ()
563
+ byService , byScalable , err := o .calculateIdlableAnnotationsByService (b . Do (). Visit )
560
564
561
565
if err != nil {
562
566
if len (byService ) == 0 || len (byScalable ) == 0 {
0 commit comments