mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Add container ports label.
This commit is contained in:
		@@ -42,6 +42,7 @@ const (
 | 
				
			|||||||
	kubernetesContainerRestartCountLabel           = "io.kubernetes.container.restartCount"
 | 
						kubernetesContainerRestartCountLabel           = "io.kubernetes.container.restartCount"
 | 
				
			||||||
	kubernetesContainerTerminationMessagePathLabel = "io.kubernetes.container.terminationMessagePath"
 | 
						kubernetesContainerTerminationMessagePathLabel = "io.kubernetes.container.terminationMessagePath"
 | 
				
			||||||
	kubernetesContainerPreStopHandlerLabel         = "io.kubernetes.container.preStopHandler"
 | 
						kubernetesContainerPreStopHandlerLabel         = "io.kubernetes.container.preStopHandler"
 | 
				
			||||||
 | 
						kubernetesContainerPortsLabel                  = "io.kubernetes.container.ports" // Added in 1.4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO(random-liu): Keep this for old containers, remove this when we drop support for v1.1.
 | 
						// TODO(random-liu): Keep this for old containers, remove this when we drop support for v1.1.
 | 
				
			||||||
	kubernetesPodLabel = "io.kubernetes.pod.data"
 | 
						kubernetesPodLabel = "io.kubernetes.pod.data"
 | 
				
			||||||
@@ -62,6 +63,7 @@ type labelledContainerInfo struct {
 | 
				
			|||||||
	RestartCount              int
 | 
						RestartCount              int
 | 
				
			||||||
	TerminationMessagePath    string
 | 
						TerminationMessagePath    string
 | 
				
			||||||
	PreStopHandler            *api.Handler
 | 
						PreStopHandler            *api.Handler
 | 
				
			||||||
 | 
						Ports                     []api.ContainerPort
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newLabels(container *api.Container, pod *api.Pod, restartCount int, enableCustomMetrics bool) map[string]string {
 | 
					func newLabels(container *api.Container, pod *api.Pod, restartCount int, enableCustomMetrics bool) map[string]string {
 | 
				
			||||||
@@ -89,7 +91,14 @@ func newLabels(container *api.Container, pod *api.Pod, restartCount int, enableC
 | 
				
			|||||||
			labels[kubernetesContainerPreStopHandlerLabel] = string(rawPreStop)
 | 
								labels[kubernetesContainerPreStopHandlerLabel] = string(rawPreStop)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if len(container.Ports) > 0 {
 | 
				
			||||||
 | 
							rawContainerPorts, err := json.Marshal(container.Ports)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								glog.Errorf("Unable to marshal container ports for container %q for pod %q: %v", container.Name, format.Pod(pod), err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								labels[kubernetesContainerPortsLabel] = string(rawContainerPorts)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if enableCustomMetrics {
 | 
						if enableCustomMetrics {
 | 
				
			||||||
		path, err := custommetrics.GetCAdvisorCustomMetricsDefinitionPath(container)
 | 
							path, err := custommetrics.GetCAdvisorCustomMetricsDefinitionPath(container)
 | 
				
			||||||
		if path != nil && err == nil {
 | 
							if path != nil && err == nil {
 | 
				
			||||||
@@ -125,6 +134,12 @@ func getContainerInfoFromLabel(labels map[string]string) *labelledContainerInfo
 | 
				
			|||||||
	} else if found {
 | 
						} else if found {
 | 
				
			||||||
		containerInfo.PreStopHandler = preStopHandler
 | 
							containerInfo.PreStopHandler = preStopHandler
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						containerPorts := []api.ContainerPort{}
 | 
				
			||||||
 | 
						if found, err := getJsonObjectFromLabel(labels, kubernetesContainerPortsLabel, &containerPorts); err != nil {
 | 
				
			||||||
 | 
							logError(containerInfo, kubernetesContainerPortsLabel, err)
 | 
				
			||||||
 | 
						} else if found {
 | 
				
			||||||
 | 
							containerInfo.Ports = containerPorts
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	supplyContainerInfoWithOldLabel(labels, containerInfo)
 | 
						supplyContainerInfoWithOldLabel(labels, containerInfo)
 | 
				
			||||||
	return containerInfo
 | 
						return containerInfo
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,8 +50,23 @@ func TestLabels(t *testing.T) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						containerPorts := []api.ContainerPort{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:          "http",
 | 
				
			||||||
 | 
								HostPort:      80,
 | 
				
			||||||
 | 
								ContainerPort: 8080,
 | 
				
			||||||
 | 
								Protocol:      api.ProtocolTCP,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:          "https",
 | 
				
			||||||
 | 
								HostPort:      443,
 | 
				
			||||||
 | 
								ContainerPort: 6443,
 | 
				
			||||||
 | 
								Protocol:      api.ProtocolTCP,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	container := &api.Container{
 | 
						container := &api.Container{
 | 
				
			||||||
		Name: "test_container",
 | 
							Name:  "test_container",
 | 
				
			||||||
 | 
							Ports: containerPorts,
 | 
				
			||||||
		TerminationMessagePath: "/somepath",
 | 
							TerminationMessagePath: "/somepath",
 | 
				
			||||||
		Lifecycle:              lifecycle,
 | 
							Lifecycle:              lifecycle,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -78,6 +93,7 @@ func TestLabels(t *testing.T) {
 | 
				
			|||||||
		RestartCount:           restartCount,
 | 
							RestartCount:           restartCount,
 | 
				
			||||||
		TerminationMessagePath: container.TerminationMessagePath,
 | 
							TerminationMessagePath: container.TerminationMessagePath,
 | 
				
			||||||
		PreStopHandler:         container.Lifecycle.PreStop,
 | 
							PreStopHandler:         container.Lifecycle.PreStop,
 | 
				
			||||||
 | 
							Ports:                  containerPorts,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Test whether we can get right information from label
 | 
						// Test whether we can get right information from label
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user