Skip to content

Commit

Permalink
Rename to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed May 6, 2021
1 parent df44b91 commit 784dff7
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion internal/kubernetes/copy.go → internal/backend/copy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion internal/kubernetes/exec.go → internal/backend/exec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/kubernetes/logs.go → internal/backend/logs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"io"
Expand Down
12 changes: 6 additions & 6 deletions internal/kubernetes/main.go → internal/backend/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"io"
Expand All @@ -10,8 +10,8 @@ import (
"github.com/joyrex2001/kubedock/internal/model/types"
)

// Kubernetes is the interface to orchestrate and manage kubernetes objects.
type Kubernetes interface {
// Backend is the interface to orchestrate and manage kubernetes objects.
type Backend interface {
StartContainer(*types.Container) error
DeleteContainer(*types.Container) error
DeleteContainersOlderThan(time.Duration) error
Expand All @@ -22,15 +22,15 @@ type Kubernetes interface {
GetLogs(*types.Container, bool, io.Writer) error
}

// instance is the internal representation of the Kubernetes object.
// instance is the internal representation of the Backend object.
type instance struct {
cli kubernetes.Interface
cfg *rest.Config
initImage string
namespace string
}

// Config is the structure to instantiate a Kubernetes object
// Config is the structure to instantiate a Backend object
type Config struct {
// Client is the kubernetes clientset
Client kubernetes.Interface
Expand All @@ -43,7 +43,7 @@ type Config struct {
}

// New will return an ContainerFactory instance.
func New(cfg Config) Kubernetes {
func New(cfg Config) Backend {
return &instance{
cli: cfg.Client,
cfg: cfg.RestConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubernetes
package backend

import (
"testing"
Expand Down
18 changes: 9 additions & 9 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package internal
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"

"github.com/joyrex2001/kubedock/internal/backend"
"github.com/joyrex2001/kubedock/internal/config"
"github.com/joyrex2001/kubedock/internal/kubernetes"
"github.com/joyrex2001/kubedock/internal/reaper"
"github.com/joyrex2001/kubedock/internal/server"
)

// Main is the main entry point for starting this service, based the settings
// initiated by cmd.
func Main(cmd *cobra.Command, args []string) {
kub, err := getKubernetes()
kub, err := getBackend()
if err != nil {
klog.Fatalf("error instantiating kubernetes: %s", err)
}

rpr, err := reaper.New(reaper.Config{
KeepMax: viper.GetDuration("reaper.keepmax"),
Kubernetes: kub,
KeepMax: viper.GetDuration("reaper.keepmax"),
Backend: kub,
})
if err != nil {
klog.Fatalf("error instantiating reaper: %s", err)
Expand All @@ -35,17 +35,17 @@ func Main(cmd *cobra.Command, args []string) {
}
}

// getKubernetes will instantiate a the kubedock kubernetes object.
func getKubernetes() (kubernetes.Kubernetes, error) {
// getBackend will instantiate a the kubedock kubernetes object.
func getBackend() (backend.Backend, error) {
cfg, err := config.GetKubernetes()
if err != nil {
return nil, err
}
cli, err := clientset.NewForConfig(cfg)
cli, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
kub := kubernetes.New(kubernetes.Config{
kub := backend.New(backend.Config{
Client: cli,
RestConfig: cfg,
Namespace: viper.GetString("kubernetes.namespace"),
Expand Down
8 changes: 4 additions & 4 deletions internal/reaper/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (

"k8s.io/client-go/kubernetes/fake"

"github.com/joyrex2001/kubedock/internal/kubernetes"
"github.com/joyrex2001/kubedock/internal/backend"
"github.com/joyrex2001/kubedock/internal/model/types"
"github.com/spf13/viper"
)

func TestCleanContainers(t *testing.T) {
kub := kubernetes.New(kubernetes.Config{
kub := backend.New(backend.Config{
Client: fake.NewSimpleClientset(),
Namespace: viper.GetString("kubernetes.namespace"),
InitImage: viper.GetString("kubernetes.initimage"),
})
rp, _ := New(Config{
KeepMax: 20 * time.Millisecond,
Kubernetes: kub,
KeepMax: 20 * time.Millisecond,
Backend: kub,
})
rp.db.SaveContainer(&types.Container{})
if err := rp.CleanContainers(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions internal/reaper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
"time"

"github.com/joyrex2001/kubedock/internal/kubernetes"
"github.com/joyrex2001/kubedock/internal/backend"
"github.com/joyrex2001/kubedock/internal/model"
"k8s.io/klog"
)
Expand All @@ -13,7 +13,7 @@ import (
type Reaper struct {
db *model.Database
keepMax time.Duration
kub kubernetes.Kubernetes
kub backend.Backend
quit chan struct{}
}

Expand All @@ -22,10 +22,10 @@ var once sync.Once

// Config is the configuration to be used for the Reaper proces.
type Config struct {
// KeepMax is the maximum age of resources, older resources are deleted
// KeepMax is the maximum age of resources, older resources are deleted.
KeepMax time.Duration
// Kubernetes is the kubedock kubernetes helper object
Kubernetes kubernetes.Kubernetes
// Backend is the kubedock backend object.
Backend backend.Backend
}

// New will create return the singleton Reaper instance.
Expand All @@ -36,7 +36,7 @@ func New(cfg Config) (*Reaper, error) {
instance = &Reaper{}
db, err = model.New()
instance.db = db
instance.kub = cfg.Kubernetes
instance.kub = cfg.Backend
instance.keepMax = cfg.KeepMax
})
return instance, err
Expand All @@ -53,7 +53,7 @@ func (in *Reaper) Stop() {
in.quit <- struct{}{}
}

// Reaper will reap all lingering resources at a steady interval.
// runloop will reap all lingering resources at a steady interval.
func (in *Reaper) runloop() {
go func() {
for {
Expand Down
6 changes: 3 additions & 3 deletions internal/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (

"k8s.io/klog"

"github.com/joyrex2001/kubedock/internal/kubernetes"
"github.com/joyrex2001/kubedock/internal/backend"
"github.com/joyrex2001/kubedock/internal/server/httputil"
"github.com/joyrex2001/kubedock/internal/server/routes"
)

// Server is the API server.
type Server struct {
kub kubernetes.Kubernetes
kub backend.Backend
}

// New will instantiate a Server object.
func New(kub kubernetes.Kubernetes) *Server {
func New(kub backend.Backend) *Server {
return &Server{kub: kub}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/server/routes/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (cr *Router) PutArchive(c *gin.Context) {
}

// hmm... how to do this without a running container...
running, _ := cr.kubernetes.IsContainerRunning(tainr)
running, _ := cr.kub.IsContainerRunning(tainr)
if !running {
if err := cr.kubernetes.StartContainer(tainr); err != nil {
if err := cr.kub.StartContainer(tainr); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand All @@ -55,7 +55,7 @@ func (cr *Router) PutArchive(c *gin.Context) {
return
}

if err := cr.kubernetes.CopyToContainer(tainr, archive, path); err != nil {
if err := cr.kub.CopyToContainer(tainr, archive, path); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/server/routes/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (cr *Router) ContainerStart(c *gin.Context) {
httputil.Error(c, http.StatusNotFound, err)
return
}
running, _ := cr.kubernetes.IsContainerRunning(tainr)
running, _ := cr.kub.IsContainerRunning(tainr)
if !running {
if err := cr.kubernetes.StartContainer(tainr); err != nil {
if err := cr.kub.StartContainer(tainr); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand All @@ -74,7 +74,7 @@ func (cr *Router) ContainerDelete(c *gin.Context) {
return
}
tainr.SignalStop()
if err := cr.kubernetes.DeleteContainer(tainr); err != nil {
if err := cr.kub.DeleteContainer(tainr); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand All @@ -96,7 +96,7 @@ func (cr *Router) ContainerInfo(c *gin.Context) {
return
}

status, err := cr.kubernetes.GetContainerStatus(tainr)
status, err := cr.kub.GetContainerStatus(tainr)
if err != nil {
httputil.Error(c, http.StatusNotFound, err)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/server/routes/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (cr *Router) ExecStart(c *gin.Context) {
w := c.Writer
w.WriteHeader(http.StatusOK)

if err := cr.kubernetes.ExecContainer(tainr, exec, w); err != nil {
if err := cr.kub.ExecContainer(tainr, exec, w); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand Down
6 changes: 3 additions & 3 deletions internal/server/routes/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (cr *Router) ContainerLogs(c *gin.Context) {
return
}

running, err := cr.kubernetes.IsContainerRunning(tainr)
running, err := cr.kub.IsContainerRunning(tainr)
if err != nil {
httputil.Error(c, http.StatusNotFound, err)
return
Expand All @@ -43,7 +43,7 @@ func (cr *Router) ContainerLogs(c *gin.Context) {
w.WriteHeader(http.StatusOK)

if !follow {
if err := cr.kubernetes.GetLogs(tainr, follow, w); err != nil {
if err := cr.kub.GetLogs(tainr, follow, w); err != nil {
httputil.Error(c, http.StatusInternalServerError, err)
return
}
Expand All @@ -70,7 +70,7 @@ func (cr *Router) ContainerLogs(c *gin.Context) {
}
fmt.Fprint(out, "\r\n")

if err := cr.kubernetes.GetLogs(tainr, follow, out); err != nil {
if err := cr.kub.GetLogs(tainr, follow, out); err != nil {
klog.Errorf("error retrieving logs: %s", err)
return
}
Expand Down
12 changes: 6 additions & 6 deletions internal/server/routes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package routes
import (
"github.com/gin-gonic/gin"

"github.com/joyrex2001/kubedock/internal/kubernetes"
"github.com/joyrex2001/kubedock/internal/backend"
"github.com/joyrex2001/kubedock/internal/model"
"github.com/joyrex2001/kubedock/internal/server/httputil"
)

// Router is the object that facilitates the kubedock API endpoints.
type Router struct {
db *model.Database
kubernetes kubernetes.Kubernetes
db *model.Database
kub backend.Backend
}

// New will instantiate a containerRouter object.
func New(router *gin.Engine, kube kubernetes.Kubernetes) (*Router, error) {
func New(router *gin.Engine, kub backend.Backend) (*Router, error) {
db, err := model.New()
if err != nil {
return nil, err
}
cr := &Router{
db: db,
kubernetes: kube,
db: db,
kub: kub,
}
cr.initRoutes(router)
return cr, nil
Expand Down

0 comments on commit 784dff7

Please sign in to comment.