Plumb context to providers

This commit is contained in:
Brian Goff
2018-08-20 14:56:39 -07:00
parent 4e20fc40ca
commit 8091b089a2
24 changed files with 277 additions and 253 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/go-autorest/autorest"
"io"
"io/ioutil"
"log"
@@ -13,6 +12,8 @@ import (
"strings"
"time"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
@@ -157,7 +158,7 @@ func NewBatchProviderFromConfig(config *Config, rm *manager.ResourceManager, nod
}
// CreatePod accepts a Pod definition
func (p *Provider) CreatePod(pod *v1.Pod) error {
func (p *Provider) CreatePod(ctx context.Context, pod *v1.Pod) error {
log.Println("Creating pod...")
podCommand, err := pod2docker.GetBashCommand(pod2docker.PodComponents{
InitContainers: pod.Spec.InitContainers,
@@ -200,9 +201,9 @@ func (p *Provider) CreatePod(pod *v1.Pod) error {
}
// GetPodStatus retrieves the status of a given pod by name.
func (p *Provider) GetPodStatus(namespace, name string) (*v1.PodStatus, error) {
func (p *Provider) GetPodStatus(ctx context.Context, namespace, name string) (*v1.PodStatus, error) {
log.Println("Getting pod status ....")
pod, err := p.GetPod(namespace, name)
pod, err := p.GetPod(ctx, namespace, name)
if err != nil {
return nil, err
@@ -214,13 +215,13 @@ func (p *Provider) GetPodStatus(namespace, name string) (*v1.PodStatus, error) {
}
// UpdatePod accepts a Pod definition
func (p *Provider) UpdatePod(pod *v1.Pod) error {
func (p *Provider) UpdatePod(ctx context.Context, pod *v1.Pod) error {
log.Println("Pod Update called: No-op as not implemented")
return nil
}
// DeletePod accepts a Pod definition
func (p *Provider) DeletePod(pod *v1.Pod) error {
func (p *Provider) DeletePod(ctx context.Context, pod *v1.Pod) error {
taskID := getTaskIDForPod(pod.Namespace, pod.Name)
task, err := p.deleteTask(taskID)
if err != nil {
@@ -234,7 +235,7 @@ func (p *Provider) DeletePod(pod *v1.Pod) error {
}
// GetPod returns a pod by name
func (p *Provider) GetPod(namespace, name string) (*v1.Pod, error) {
func (p *Provider) GetPod(ctx context.Context, namespace, name string) (*v1.Pod, error) {
log.Println("Getting Pod ...")
task, err := p.getTask(getTaskIDForPod(namespace, name))
if err != nil {
@@ -257,7 +258,7 @@ func (p *Provider) GetPod(namespace, name string) (*v1.Pod, error) {
}
// GetContainerLogs returns the logs of a container running in a pod by name.
func (p *Provider) GetContainerLogs(namespace, podName, containerName string, tail int) (string, error) {
func (p *Provider) GetContainerLogs(ctx context.Context, namespace, podName, containerName string, tail int) (string, error) {
log.Println("Getting pod logs ....")
taskID := getTaskIDForPod(namespace, podName)
@@ -318,7 +319,7 @@ func (p *Provider) ExecInContainer(name string, uid types.UID, container string,
}
// GetPods retrieves a list of all pods scheduled to run.
func (p *Provider) GetPods() ([]*v1.Pod, error) {
func (p *Provider) GetPods(ctx context.Context) ([]*v1.Pod, error) {
log.Println("Getting pods...")
tasksPtr, err := p.listTasks()
if err != nil {
@@ -342,7 +343,7 @@ func (p *Provider) GetPods() ([]*v1.Pod, error) {
}
// Capacity returns a resource list containing the capacity limits
func (p *Provider) Capacity() v1.ResourceList {
func (p *Provider) Capacity(ctx context.Context) v1.ResourceList {
return v1.ResourceList{
"cpu": resource.MustParse(p.cpu),
"memory": resource.MustParse(p.memory),
@@ -353,7 +354,7 @@ func (p *Provider) Capacity() v1.ResourceList {
// NodeConditions returns a list of conditions (Ready, OutOfDisk, etc), for updates to the node status
// within Kubernetes.
func (p *Provider) NodeConditions() []v1.NodeCondition {
func (p *Provider) NodeConditions(ctx context.Context) []v1.NodeCondition {
return []v1.NodeCondition{
{
Type: "Ready",
@@ -400,7 +401,7 @@ func (p *Provider) NodeConditions() []v1.NodeCondition {
// NodeAddresses returns a list of addresses for the node status
// within Kubernetes.
func (p *Provider) NodeAddresses() []v1.NodeAddress {
func (p *Provider) NodeAddresses(ctx context.Context) []v1.NodeAddress {
// TODO: Make these dynamic and augment with custom ACI specific conditions of interest
return []v1.NodeAddress{
{
@@ -412,7 +413,7 @@ func (p *Provider) NodeAddresses() []v1.NodeAddress {
// NodeDaemonEndpoints returns NodeDaemonEndpoints for the node status
// within Kubernetes.
func (p *Provider) NodeDaemonEndpoints() *v1.NodeDaemonEndpoints {
func (p *Provider) NodeDaemonEndpoints(ctx context.Context) *v1.NodeDaemonEndpoints {
return &v1.NodeDaemonEndpoints{
KubeletEndpoint: v1.DaemonEndpoint{
Port: p.daemonEndpointPort,

View File

@@ -1,16 +1,18 @@
package azurebatch
import (
"context"
"crypto/md5"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
"github.com/Azure/go-autorest/autorest"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
"github.com/Azure/go-autorest/autorest"
apiv1 "k8s.io/api/core/v1"
)
@@ -32,7 +34,7 @@ func Test_deletePod(t *testing.T) {
pod.Name = podName
pod.Namespace = podNamespace
err := provider.DeletePod(pod)
err := provider.DeletePod(context.Background(), pod)
if err != nil {
t.Error(err)
}
@@ -48,7 +50,7 @@ func Test_deletePod_doesntExist(t *testing.T) {
return autorest.Response{}, fmt.Errorf("Task doesn't exist")
}
err := provider.DeletePod(pod)
err := provider.DeletePod(context.Background(), pod)
if err == nil {
t.Error("Expected error but none seen")
}
@@ -72,7 +74,7 @@ func Test_createPod(t *testing.T) {
return autorest.Response{}, nil
}
err := provider.CreatePod(pod)
err := provider.CreatePod(context.Background(), pod)
if err != nil {
t.Errorf("Unexpected error creating pod %v", err)
}
@@ -88,7 +90,7 @@ func Test_createPod_errorResponse(t *testing.T) {
return autorest.Response{}, fmt.Errorf("Failed creating task")
}
err := provider.CreatePod(pod)
err := provider.CreatePod(context.Background(), pod)
if err == nil {
t.Error("Expected error but none seen")
}
@@ -118,7 +120,7 @@ func Test_readLogs_404Response_expectReturnStartupLogs(t *testing.T) {
return batch.ReadCloser{}, fmt.Errorf("Failed in test mock of getFileFromTask")
}
result, err := provider.GetContainerLogs(pod.Namespace, pod.Name, containerName, 0)
result, err := provider.GetContainerLogs(context.Background(), pod.Namespace, pod.Name, containerName, 0)
if err != nil {
t.Errorf("GetContainerLogs return error: %v", err)
}
@@ -152,7 +154,7 @@ func Test_readLogs_JsonResponse_expectFormattedLogs(t *testing.T) {
return batch.ReadCloser{}, fmt.Errorf("Failed in test mock of getFileFromTask")
}
result, err := provider.GetContainerLogs(pod.Namespace, pod.Name, containerName, 0)
result, err := provider.GetContainerLogs(context.Background(), pod.Namespace, pod.Name, containerName, 0)
if err != nil {
t.Errorf("GetContainerLogs return error: %v", err)
}