interesting: ApproximatePodTemplateForObject
@@ -151,6 +151,70 @@ func (f *Factory) ExtractFileContents(obj runtime.Object) (map[string][]byte, bo
}
}
+// ApproximatePodTemplateForObject returns a pod template object for the provided source.
+// It may return both an error and a object. It attempt to return the best possible template
+// available at the current time.
+func (f *Factory) ApproximatePodTemplateForObject(object runtime.Object) (*api.PodTemplateSpec, error) {
+ switch t := object.(type) {
+ case *imageapi.ImageStreamTag:
+ // create a minimal pod spec that uses the image referenced by the istag without any introspection
+ // it possible that we could someday do a better job introspecting it
+ return &api.PodTemplateSpec{
+ Spec: api.PodSpec{
+ RestartPolicy: api.RestartPolicyNever,
+ Containers: []api.Container{
+ {Name: "container-00", Image: t.Image.DockerImageReference},
+ },
+ },
+ }, nil
+ case *imageapi.ImageStreamImage:
+ // create a minimal pod spec that uses the image referenced by the istag without any introspection
+ // it possible that we could someday do a better job introspecting it
+ return &api.PodTemplateSpec{
+ Spec: api.PodSpec{
+ RestartPolicy: api.RestartPolicyNever,
+ Containers: []api.Container{
+ {Name: "container-00", Image: t.Image.DockerImageReference},
+ },
+ },
+ }, nil
+ case *deployapi.DeploymentConfig:
+ fallback := t.Spec.Template
+
+ kc, err := f.ClientSet()
+ if err != nil {
+ return fallback, err
+ }
+
+ latestDeploymentName := deployutil.LatestDeploymentNameForConfig(t)
+ deployment, err := kc.Core().ReplicationControllers(t.Namespace).Get(latestDeploymentName, metav1.GetOptions{})
+ if err != nil {
+ return fallback, err
+ }
+
+ fallback = deployment.Spec.Template
+
+ pods, err := kc.Core().Pods(deployment.Namespace).List(metav1.ListOptions{LabelSelector: labels.SelectorFromSet(deployment.Spec.Selector).String()})
+ if err != nil {
+ return fallback, err
+ }
+
+ for i := range pods.Items {
+ pod := &pods.Items[i]
+ if fallback == nil || pod.CreationTimestamp.Before(&fallback.CreationTimestamp) {
+ fallback = &api.PodTemplateSpec{
+ ObjectMeta: pod.ObjectMeta,
+ Spec: pod.Spec,
+ }
+ }
+ }
+ return fallback, nil
+
+ default:
+ return f.ObjectMappingFactory.ApproximatePodTemplateForObject(object)
+ }
+}
+
func (f *Factory) PodForResource(resource string, timeout time.Duration) (string, error) {
sortBy := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) }
namespace, _, err := f.DefaultNamespace()
