| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | package object
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2020-05-15 12:47:29 -04:00
										 |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	api "k8s.io/api/core/v1"
 | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/runtime"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Pod is a stripped down api.Pod with only the items we need for CoreDNS.
 | 
					
						
							|  |  |  | type Pod struct {
 | 
					
						
							| 
									
										
										
										
											2019-06-01 08:39:52 +01:00
										 |  |  | 	// Don't add new fields to this struct without talking to the CoreDNS maintainers.
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	Version   string
 | 
					
						
							|  |  |  | 	PodIP     string
 | 
					
						
							|  |  |  | 	Name      string
 | 
					
						
							|  |  |  | 	Namespace string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*Empty
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 12:47:29 -04:00
										 |  |  | var errPodTerminating = errors.New("pod terminating")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 17:48:43 +02:00
										 |  |  | // ToPod returns a function that converts an api.Pod to a *Pod.
 | 
					
						
							|  |  |  | func ToPod(skipCleanup bool) ToFunc {
 | 
					
						
							| 
									
										
										
										
											2020-05-15 12:47:29 -04:00
										 |  |  | 	return func(obj interface{}) (interface{}, error) {
 | 
					
						
							|  |  |  | 		apiPod, ok := obj.(*api.Pod)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("unexpected object %v", obj)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		pod := toPod(skipCleanup, apiPod)
 | 
					
						
							|  |  |  | 		t := apiPod.ObjectMeta.DeletionTimestamp
 | 
					
						
							|  |  |  | 		if t != nil && !(*t).Time.IsZero() {
 | 
					
						
							|  |  |  | 			// if the pod is in the process of termination, return an error so it can be ignored
 | 
					
						
							|  |  |  | 			// during add/update event processing
 | 
					
						
							|  |  |  | 			return pod, errPodTerminating
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return pod, nil
 | 
					
						
							| 
									
										
										
										
											2019-10-04 17:48:43 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 12:47:29 -04:00
										 |  |  | func toPod(skipCleanup bool, pod *api.Pod) *Pod {
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	p := &Pod{
 | 
					
						
							|  |  |  | 		Version:   pod.GetResourceVersion(),
 | 
					
						
							|  |  |  | 		PodIP:     pod.Status.PodIP,
 | 
					
						
							|  |  |  | 		Namespace: pod.GetNamespace(),
 | 
					
						
							|  |  |  | 		Name:      pod.GetName(),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 17:48:43 +02:00
										 |  |  | 	if !skipCleanup {
 | 
					
						
							|  |  |  | 		*pod = api.Pod{}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return p
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var _ runtime.Object = &Pod{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeepCopyObject implements the ObjectKind interface.
 | 
					
						
							|  |  |  | func (p *Pod) DeepCopyObject() runtime.Object {
 | 
					
						
							|  |  |  | 	p1 := &Pod{
 | 
					
						
							|  |  |  | 		Version:   p.Version,
 | 
					
						
							|  |  |  | 		PodIP:     p.PodIP,
 | 
					
						
							|  |  |  | 		Namespace: p.Namespace,
 | 
					
						
							|  |  |  | 		Name:      p.Name,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return p1
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetNamespace implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) GetNamespace() string { return p.Namespace }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetNamespace implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) SetNamespace(namespace string) {}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetName implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) GetName() string { return p.Name }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetName implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) SetName(name string) {}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetResourceVersion implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) GetResourceVersion() string { return p.Version }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetResourceVersion implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (p *Pod) SetResourceVersion(version string) {}
 |