Prefix ACI container groups with namespace name, also fix for container ports in ACI provider

This commit is contained in:
Erik St. Martin
2017-12-05 11:35:42 -06:00
parent 0075e5b0f3
commit 0dadbd7898
5 changed files with 25 additions and 14 deletions

View File

@@ -11,5 +11,11 @@ spec:
requests:
memory: 1G
cpu: 1
ports:
- containerPort: 80
name: http
protocol: TCP
- containerPort: 443
name: https
dnsPolicy: ClusterFirst
nodeName: virtual-kubelet

View File

@@ -124,6 +124,7 @@ func (p *ACIProvider) CreatePod(pod *v1.Pod) error {
podUID := string(pod.UID)
podCreationTimestamp := pod.CreationTimestamp.String()
containerGroup.Tags = map[string]string{
"PodName": pod.Name,
"ClusterName": pod.ClusterName,
"NodeName": pod.Spec.NodeName,
"Namespace": pod.Namespace,
@@ -134,7 +135,7 @@ func (p *ACIProvider) CreatePod(pod *v1.Pod) error {
// TODO(BJK) containergrouprestartpolicy??
_, err = p.aciClient.CreateContainerGroup(
p.resourceGroup,
pod.Name,
fmt.Sprintf("%s-%s", pod.Namespace, pod.Name),
containerGroup,
)
@@ -148,13 +149,13 @@ func (p *ACIProvider) UpdatePod(pod *v1.Pod) error {
// DeletePod deletes the specified pod out of ACI.
func (p *ACIProvider) DeletePod(pod *v1.Pod) error {
return p.aciClient.DeleteContainerGroup(p.resourceGroup, pod.Name)
return p.aciClient.DeleteContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", pod.Namespace, pod.Name))
}
// GetPod returns a pod by name that is running inside ACI
// returns nil if a pod by that name is not found.
func (p *ACIProvider) GetPod(name string) (*v1.Pod, error) {
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, name)
func (p *ACIProvider) GetPod(namespace, name string) (*v1.Pod, error) {
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, name))
if err != nil {
// Trap error for 404 and return gracefully
if strings.Contains(err.Error(), "ResourceNotFound") {
@@ -172,8 +173,8 @@ func (p *ACIProvider) GetPod(name string) (*v1.Pod, error) {
// GetPodStatus returns the status of a pod by name that is running inside ACI
// returns nil if a pod by that name is not found.
func (p *ACIProvider) GetPodStatus(name string) (*v1.PodStatus, error) {
pod, err := p.GetPod(name)
func (p *ACIProvider) GetPodStatus(namespace, name string) (*v1.PodStatus, error) {
pod, err := p.GetPod(namespace, name)
if err != nil {
return nil, err
}
@@ -323,7 +324,7 @@ func (p *ACIProvider) getContainers(pod *v1.Pod) ([]aci.Container, error) {
for _, p := range container.Ports {
c.Ports = append(c.Ports, aci.ContainerPort{
Port: p.HostPort,
Port: p.ContainerPort,
Protocol: getProtocol(p.Protocol),
})
}
@@ -553,7 +554,7 @@ func containerGroupToPod(cg *aci.ContainerGroup) (*v1.Pod, error) {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: cg.Name,
Name: cg.Tags["PodName"],
Namespace: cg.Tags["Namespace"],
ClusterName: cg.Tags["ClusterName"],
UID: types.UID(cg.Tags["UID"]),

View File

@@ -135,13 +135,13 @@ func (p *HyperProvider) DeletePod(pod *v1.Pod) error {
// GetPod returns a pod by name that is running inside hyper.sh
// returns nil if a pod by that name is not found.
func (p *HyperProvider) GetPod(name string) (*v1.Pod, error) {
func (p *HyperProvider) GetPod(namespace, name string) (*v1.Pod, error) {
return nil, nil
}
// GetPodStatus returns the status of a pod by name that is running inside hyper.sh
// returns nil if a pod by that name is not found.
func (p *HyperProvider) GetPodStatus(name string) (*v1.PodStatus, error) {
func (p *HyperProvider) GetPodStatus(namespace, name string) (*v1.PodStatus, error) {
return nil, nil
}

View File

@@ -22,10 +22,10 @@ type Provider interface {
DeletePod(pod *v1.Pod) error
// GetPod retrieves a pod by name from the provider (can be cached).
GetPod(name string) (*v1.Pod, error)
GetPod(namespace, name string) (*v1.Pod, error)
// GetPodStatus retrievesthe status of a pod by name from the provider.
GetPodStatus(name string) (*v1.PodStatus, error)
GetPodStatus(namespace, name string) (*v1.PodStatus, error)
// GetPods retrieves a list of all pods running on the provider (can be cached).
GetPods() ([]*v1.Pod, error)

View File

@@ -240,7 +240,7 @@ func (s *Server) reconcile() {
// Create any pods for k8s pods that don't exist in the provider
pods := s.resourceManager.GetPods()
for _, pod := range pods {
p, err := s.provider.GetPod(pod.Name)
p, err := s.provider.GetPod(pod.Namespace, pod.Name)
if err != nil {
log.Printf("Error retrieving pod '%s' from provider: %s\n", pod.Name, err)
}
@@ -296,6 +296,10 @@ func (s *Server) deletePod(pod *corev1.Pod) error {
if !errors.IsNotFound(delErr) {
var grace int64 = 0
if err := s.k8sClient.CoreV1().Pods(pod.Namespace).Delete(pod.Name, &metav1.DeleteOptions{GracePeriodSeconds: &grace}); err != nil && errors.IsNotFound(err) {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("Failed to delete kubernetes pod: %s", err)
}
@@ -314,7 +318,7 @@ func (s *Server) updatePodStatuses() {
continue
}
status, err := s.provider.GetPodStatus(pod.Name)
status, err := s.provider.GetPodStatus(pod.Namespace, pod.Name)
if err != nil {
log.Printf("Error retrieving pod '%s' status from provider: %s\n", pod.Name, err)
return