Add ContainerID into the ContainerStatus (#337)

This commit is contained in:
Robbie Zhang
2018-08-27 15:38:14 -07:00
committed by GitHub
parent b019ec5549
commit 55bc0c3f75
2 changed files with 147 additions and 68 deletions

View File

@@ -3,7 +3,9 @@ package azure
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -974,7 +976,7 @@ func containerGroupToPod(cg *aci.ContainerGroup) (*v1.Pod, error) {
RestartCount: c.InstanceView.RestartCount, RestartCount: c.InstanceView.RestartCount,
Image: c.Image, Image: c.Image,
ImageID: "", ImageID: "",
ContainerID: "", ContainerID: getContainerID(cg.ID, c.Name),
} }
// Add to containerStatuses // Add to containerStatuses
@@ -1018,6 +1020,19 @@ func containerGroupToPod(cg *aci.ContainerGroup) (*v1.Pod, error) {
return &p, nil return &p, nil
} }
func getContainerID(cgID, containerName string) string {
if cgID == "" {
return ""
}
containerResourceID := fmt.Sprintf("%s/containers/%s", cgID, containerName)
h := sha256.New()
h.Write([]byte(strings.ToUpper(containerResourceID)))
hashBytes := h.Sum(nil)
return fmt.Sprintf("aci://%s", hex.EncodeToString(hashBytes))
}
func aciStateToPodPhase(state string) v1.PodPhase { func aciStateToPodPhase(state string) v1.PodPhase {
switch state { switch state {
case "Running": case "Running":

View File

@@ -7,6 +7,7 @@ package azure
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@@ -364,6 +365,69 @@ func TestGetPodWithoutResourceRequestsLimits(t *testing.T) {
"Containers[0].Resources.Requests.Memory doesn't match") "Containers[0].Resources.Requests.Memory doesn't match")
} }
func TestGetPodWithContainerID(t *testing.T) {
_, aciServerMocker, provider, err := prepareMocks()
if err != nil {
t.Fatal("Unable to prepare the mocks", err)
}
podName := "pod-" + uuid.New().String()
podNamespace := "ns-" + uuid.New().String()
containerName := "c-" + uuid.New().String()
containerImage := "ci-" + uuid.New().String()
cgID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerInstance/containerGroups/%s-%s", fakeSubscription, fakeResourceGroup, podNamespace, podName)
aciServerMocker.OnGetContainerGroup = func(subscription, resourceGroup, containerGroup string) (int, interface{}) {
assert.Equal(t, fakeSubscription, subscription, "Subscription doesn't match")
assert.Equal(t, fakeResourceGroup, resourceGroup, "Resource group doesn't match")
assert.Equal(t, podNamespace+"-"+podName, containerGroup, "Container group name is not expected")
return http.StatusOK, aci.ContainerGroup{
ID: cgID,
Tags: map[string]string{
"NodeName": fakeNodeName,
},
ContainerGroupProperties: aci.ContainerGroupProperties{
ProvisioningState: "Creating",
Containers: []aci.Container{
aci.Container{
Name: containerName,
ContainerProperties: aci.ContainerProperties{
Image: containerImage,
Command: []string{"nginx", "-g", "daemon off;"},
Ports: []aci.ContainerPort{
{
Protocol: aci.ContainerNetworkProtocolTCP,
Port: 80,
},
},
Resources: aci.ResourceRequirements{
Requests: &aci.ResourceRequests{
CPU: 0.99,
MemoryInGB: 1.5,
},
},
},
},
},
},
}
}
pod, err := provider.GetPod(podNamespace, podName)
if err != nil {
t.Fatal("Failed to get pod", err)
}
assert.NotNil(t, pod, "Response pod should not be nil")
assert.Equal(t, 1, len(pod.Status.ContainerStatuses), "1 container status is expected")
assert.Equal(t, containerName, pod.Status.ContainerStatuses[0].Name, "Container name in the container status doesn't match")
assert.Equal(t, containerImage, pod.Status.ContainerStatuses[0].Image, "Container image in the container status doesn't match")
assert.Equal(t, getContainerID(cgID, containerName), pod.Status.ContainerStatuses[0].ContainerID, "Container ID in the container status is not expected")
}
func TestPodToACISecretEnvVar(t *testing.T) { func TestPodToACISecretEnvVar(t *testing.T) {
testKey := "testVar" testKey := "testVar"
@@ -485,13 +549,16 @@ func TestCreatePodWithLivenessProbe(t *testing.T) {
assert.Equal(t, 1, len(cg.ContainerGroupProperties.Containers), "1 Container is expected") assert.Equal(t, 1, len(cg.ContainerGroupProperties.Containers), "1 Container is expected")
assert.Equal(t, "nginx", cg.ContainerGroupProperties.Containers[0].Name, "Container nginx is expected") assert.Equal(t, "nginx", cg.ContainerGroupProperties.Containers[0].Name, "Container nginx is expected")
assert.NotNil(t, cg.Containers[0].LivenessProbe, "Liveness probe expected") assert.NotNil(t, cg.Containers[0].LivenessProbe, "Liveness probe expected")
assert.Equal(t, cg.Containers[0].LivenessProbe.InitialDelaySeconds, 10, "Initial Probe Delay doesn't match") assert.Equal(t, int32(10), cg.Containers[0].LivenessProbe.InitialDelaySeconds, "Initial Probe Delay doesn't match")
assert.Equal(t, cg.Containers[0].LivenessProbe.Period, 5, "Probe Period doesn't match") assert.Equal(t, int32(5), cg.Containers[0].LivenessProbe.Period, "Probe Period doesn't match")
assert.Equal(t, cg.Containers[0].LivenessProbe.TimeoutSeconds, 60, "Probe Timeout doesn't match") assert.Equal(t, int32(60), cg.Containers[0].LivenessProbe.TimeoutSeconds, "Probe Timeout doesn't match")
assert.Equal(t, cg.Containers[0].LivenessProbe.SuccessThreshold, 3, "Probe Success Threshold doesn't match") assert.Equal(t, int32(3), cg.Containers[0].LivenessProbe.SuccessThreshold, "Probe Success Threshold doesn't match")
assert.Equal(t, cg.Containers[0].LivenessProbe.FailureThreshold, 5, "Probe Failure Threshold doesn't match") assert.Equal(t, int32(5), cg.Containers[0].LivenessProbe.FailureThreshold, "Probe Failure Threshold doesn't match")
assert.NotNil(t, cg.Containers[0].LivenessProbe.HTTPGet, "Expected an HTTP Get Probe") assert.NotNil(t, cg.Containers[0].LivenessProbe.HTTPGet, "Expected an HTTP Get Probe")
return http.StatusOK, cg
}
pod := &v1.Pod{ pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: podName,
@@ -522,9 +589,6 @@ func TestCreatePodWithLivenessProbe(t *testing.T) {
if err := provider.CreatePod(pod); err != nil { if err := provider.CreatePod(pod); err != nil {
t.Fatal("Failed to create pod", err) t.Fatal("Failed to create pod", err)
} }
return http.StatusOK, cg
}
} }
func TestCreatePodWithReadinessProbe(t *testing.T) { func TestCreatePodWithReadinessProbe(t *testing.T) {
@@ -547,13 +611,16 @@ func TestCreatePodWithReadinessProbe(t *testing.T) {
assert.Equal(t, 1, len(cg.ContainerGroupProperties.Containers), "1 Container is expected") assert.Equal(t, 1, len(cg.ContainerGroupProperties.Containers), "1 Container is expected")
assert.Equal(t, "nginx", cg.ContainerGroupProperties.Containers[0].Name, "Container nginx is expected") assert.Equal(t, "nginx", cg.ContainerGroupProperties.Containers[0].Name, "Container nginx is expected")
assert.NotNil(t, cg.Containers[0].ReadinessProbe, "Readiness probe expected") assert.NotNil(t, cg.Containers[0].ReadinessProbe, "Readiness probe expected")
assert.Equal(t, cg.Containers[0].ReadinessProbe.InitialDelaySeconds, 10, "Initial Probe Delay doesn't match") assert.Equal(t, int32(10), cg.Containers[0].ReadinessProbe.InitialDelaySeconds, "Initial Probe Delay doesn't match")
assert.Equal(t, cg.Containers[0].ReadinessProbe.Period, 5, "Probe Period doesn't match") assert.Equal(t, int32(5), cg.Containers[0].ReadinessProbe.Period, "Probe Period doesn't match")
assert.Equal(t, cg.Containers[0].ReadinessProbe.TimeoutSeconds, 60, "Probe Timeout doesn't match") assert.Equal(t, int32(60), cg.Containers[0].ReadinessProbe.TimeoutSeconds, "Probe Timeout doesn't match")
assert.Equal(t, cg.Containers[0].ReadinessProbe.SuccessThreshold, 3, "Probe Success Threshold doesn't match") assert.Equal(t, int32(3), cg.Containers[0].ReadinessProbe.SuccessThreshold, "Probe Success Threshold doesn't match")
assert.Equal(t, cg.Containers[0].ReadinessProbe.FailureThreshold, 5, "Probe Failure Threshold doesn't match") assert.Equal(t, int32(5), cg.Containers[0].ReadinessProbe.FailureThreshold, "Probe Failure Threshold doesn't match")
assert.NotNil(t, cg.Containers[0].ReadinessProbe.HTTPGet, "Expected an HTTP Get Probe") assert.NotNil(t, cg.Containers[0].ReadinessProbe.HTTPGet, "Expected an HTTP Get Probe")
return http.StatusOK, cg
}
pod := &v1.Pod{ pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: podName,
@@ -584,7 +651,4 @@ func TestCreatePodWithReadinessProbe(t *testing.T) {
if err := provider.CreatePod(pod); err != nil { if err := provider.CreatePod(pod); err != nil {
t.Fatal("Failed to create pod", err) t.Fatal("Failed to create pod", err)
} }
return http.StatusOK, cg
}
} }