Files
poseidon/internal/runner/kubernetes_runner.go
2024-09-18 10:43:38 +02:00

108 lines
3.3 KiB
Go

package runner
import (
"context"
"fmt"
"github.com/openHPI/poseidon/internal/kubernetes"
"github.com/openHPI/poseidon/internal/nomad"
"github.com/openHPI/poseidon/pkg/dto"
"github.com/openHPI/poseidon/pkg/monitoring"
"github.com/openHPI/poseidon/pkg/storage"
"io"
v1 "k8s.io/api/core/v1"
"net/http"
"time"
)
// NomadJob is an abstraction to communicate with Nomad environments.
type KubernetesDeployment struct {
InactivityTimer
executions storage.Storage[*dto.ExecutionRequest]
id string
portMappings []v1.ContainerPort
api kubernetes.ExecutorAPI
onDestroy DestroyRunnerHandler
ctx context.Context
cancel context.CancelFunc
}
func (r *KubernetesDeployment) MappedPorts() []*dto.MappedPort {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) StoreExecution(id string, executionRequest *dto.ExecutionRequest) {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) ExecutionExists(id string) bool {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) ExecuteInteractively(id string, stdin io.ReadWriter, stdout, stderr io.Writer, ctx context.Context) (exit <-chan ExitInfo, cancel context.CancelFunc, err error) {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) ListFileSystem(path string, recursive bool, result io.Writer, privilegedExecution bool, ctx context.Context) error {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) UpdateFileSystem(request *dto.UpdateFileSystemRequest, ctx context.Context) error {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) GetFileContent(path string, content http.ResponseWriter, privilegedExecution bool, ctx context.Context) error {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) Destroy(reason DestroyReason) error {
//TODO implement me
panic("implement me")
}
func (r *KubernetesDeployment) ID() string {
return r.id
}
func (r *KubernetesDeployment) Environment() dto.EnvironmentID {
id, err := nomad.EnvironmentIDFromRunnerID(r.ID())
if err != nil {
log.WithError(err).Error("Runners must have correct IDs")
}
return id
}
// NewNomadJob creates a new NomadJob with the provided id.
// The InactivityTimer is used actively. It executes onDestroy when it has expired.
// The InactivityTimer is persisted in Nomad by the runner manager's Claim Function.
func NewKubernetesDeployment(id string, portMappings []v1.ContainerPort,
apiClient kubernetes.ExecutorAPI, onDestroy DestroyRunnerHandler,
) *KubernetesDeployment {
ctx := context.WithValue(context.Background(), dto.ContextKey(dto.KeyRunnerID), id)
ctx, cancel := context.WithCancel(ctx)
job := &KubernetesDeployment{
id: id,
portMappings: portMappings,
api: apiClient,
onDestroy: onDestroy,
ctx: ctx,
cancel: cancel,
}
job.executions = storage.NewMonitoredLocalStorage[*dto.ExecutionRequest](
monitoring.MeasurementExecutionsNomad, monitorExecutionsRunnerID(job.Environment(), id), time.Minute, ctx)
job.InactivityTimer = NewInactivityTimer(job, func(r Runner) error {
err := r.Destroy(ErrorRunnerInactivityTimeout)
if err != nil {
err = fmt.Errorf("NomadJob: %w", err)
}
return err
})
return job
}