diff options
author | 2018-10-09 21:56:09 +0100 | |
---|---|---|
committer | 2018-10-09 21:56:09 +0100 | |
commit | 830e97f8008ed83abb6b293a12a87f3fce34e423 (patch) | |
tree | d1a020aa3e7b232a52018479ab7972d350bd74e7 /plugin/kubernetes/object/service.go | |
parent | 298b860a97481630ec65698ab0465675bf58b43a (diff) | |
download | coredns-830e97f8008ed83abb6b293a12a87f3fce34e423.tar.gz coredns-830e97f8008ed83abb6b293a12a87f3fce34e423.tar.zst coredns-830e97f8008ed83abb6b293a12a87f3fce34e423.zip |
plugin/kubernetes: allow trimming down of cached items. (#2128)
* Convert to runtime.Object to smaller structs
This adds conversion for all the objects we want to keep in the cache.
It keeps the minimum for CoreDNS to function and throws away the rest.
The conversion:
api.Endpoints -> object.Endpoints
api.Pod -> object.Pod
api.Serivce -> object.Service
We needed to copy some client-go stuff to insert a conversion function
into NewIndexInformers.
Some unrelated cleanups in the watch functionality as that needed to be
touched because of the above translation of objects.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Reduce test line-count
Signed-off-by: Miek Gieben <miek@miek.nl>
* ....and fix test
Signed-off-by: Miek Gieben <miek@miek.nl>
* Drop use of append
Signed-off-by: Miek Gieben <miek@miek.nl>
* cosmetic changes
Signed-off-by: Miek Gieben <miek@miek.nl>
* that was a typo
Signed-off-by: Miek Gieben <miek@miek.nl>
* re-introduce append here
We can't really use len() here because we don't know the number before
hand.
Signed-off-by: Miek Gieben <miek@miek.nl>
* comment in better place
Signed-off-by: Miek Gieben <miek@miek.nl>
* Make the timestamp a bool; thats where it is used for
Signed-off-by: Miek Gieben <miek@miek.nl>
* Set incoming object to nil
Explicataliy discard the converted object; we did a deep copy it's
not needed anymore.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Per Chris's comment
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/kubernetes/object/service.go')
-rw-r--r-- | plugin/kubernetes/object/service.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/plugin/kubernetes/object/service.go b/plugin/kubernetes/object/service.go new file mode 100644 index 000000000..be010e96b --- /dev/null +++ b/plugin/kubernetes/object/service.go @@ -0,0 +1,89 @@ +package object + +import ( + api "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +// Service is a stripped down api.Service with only the items we need for CoreDNS. +type Service struct { + Version string + Name string + Namespace string + Index string + ClusterIP string + Type api.ServiceType + ExternalName string + Ports []api.ServicePort + + *Empty +} + +// ServiceKey return a string using for the index. +func ServiceKey(name, namespace string) string { return name + "." + namespace } + +// ToService converts an api.Service to a *Service. +func ToService(obj interface{}) interface{} { + svc, ok := obj.(*api.Service) + if !ok { + return nil + } + + s := &Service{ + Version: svc.GetResourceVersion(), + Name: svc.GetName(), + Namespace: svc.GetNamespace(), + Index: ServiceKey(svc.GetName(), svc.GetNamespace()), + ClusterIP: svc.Spec.ClusterIP, + Type: svc.Spec.Type, + ExternalName: svc.Spec.ExternalName, + } + + if len(svc.Spec.Ports) == 0 { + // Add sentinal if there are no ports. + s.Ports = []api.ServicePort{{Port: -1}} + } else { + s.Ports = make([]api.ServicePort, len(svc.Spec.Ports)) + copy(s.Ports, svc.Spec.Ports) + } + + *svc = api.Service{} + + return s +} + +var _ runtime.Object = &Service{} + +// DeepCopyObject implements the ObjectKind interface. +func (s *Service) DeepCopyObject() runtime.Object { + s1 := &Service{ + Version: s.Version, + Name: s.Name, + Namespace: s.Namespace, + Index: s.Index, + ClusterIP: s.ClusterIP, + Type: s.Type, + ExternalName: s.ExternalName, + Ports: make([]api.ServicePort, len(s.Ports)), + } + copy(s1.Ports, s.Ports) + return s1 +} + +// GetNamespace implements the metav1.Object interface. +func (s *Service) GetNamespace() string { return s.Namespace } + +// SetNamespace implements the metav1.Object interface. +func (s *Service) SetNamespace(namespace string) {} + +// GetName implements the metav1.Object interface. +func (s *Service) GetName() string { return s.Name } + +// SetName implements the metav1.Object interface. +func (s *Service) SetName(name string) {} + +// GetResourceVersion implements the metav1.Object interface. +func (s *Service) GetResourceVersion() string { return s.Version } + +// SetResourceVersion implements the metav1.Object interface. +func (s *Service) SetResourceVersion(version string) {} |