apiserver.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Reads the pod configuration from the Kubernetes apiserver.
  14. package config
  15. import (
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/fields"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/client-go/tools/cache"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  25. )
  26. // NewSourceApiserver creates a config source that watches and pulls from the apiserver.
  27. func NewSourceApiserver(c clientset.Interface, nodeName types.NodeName, updates chan<- interface{}) {
  28. lw := cache.NewListWatchFromClient(c.CoreV1().RESTClient(), "pods", metav1.NamespaceAll, fields.OneTermEqualSelector(api.PodHostField, string(nodeName)))
  29. newSourceApiserverFromLW(lw, updates)
  30. }
  31. // newSourceApiserverFromLW holds creates a config source that watches and pulls from the apiserver.
  32. func newSourceApiserverFromLW(lw cache.ListerWatcher, updates chan<- interface{}) {
  33. send := func(objs []interface{}) {
  34. var pods []*v1.Pod
  35. for _, o := range objs {
  36. pods = append(pods, o.(*v1.Pod))
  37. }
  38. updates <- kubetypes.PodUpdate{Pods: pods, Op: kubetypes.SET, Source: kubetypes.ApiserverSource}
  39. }
  40. r := cache.NewReflector(lw, &v1.Pod{}, cache.NewUndeltaStore(send, cache.MetaNamespaceKeyFunc), 0)
  41. go r.Run(wait.NeverStop)
  42. }