Skip to content

Commit

Permalink
refactors kubeclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Rampage1xx committed Jul 9, 2021
1 parent fb93c40 commit 4cacf94
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
4 changes: 1 addition & 3 deletions cmd/run-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import (
"sighupio/permission-manager/internal/config"
"sighupio/permission-manager/internal/kubeclient"
"sighupio/permission-manager/internal/server"
)

func main() {
cfg := config.New()
kc := kubeclient.New()

s := server.New(kc, *cfg)
s := server.New(*cfg)
addr := ":" + cfg.Backend.Port
s.Logger.Fatal(s.Start(addr))
}
3 changes: 1 addition & 2 deletions internal/kubeconfig/create-kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kubeconfig
import (
"context"
"sighupio/permission-manager/internal/config"
"sighupio/permission-manager/internal/kubeclient"
"sighupio/permission-manager/internal/resources"
"testing"

Expand All @@ -18,7 +17,7 @@ func TestCreateKubeconfig(t *testing.T) {
ControlPlaneAddress: "https://100.200.10.200",
}

rs := resources.NewResourceService(kubeclient.New(), context.TODO())
rs := resources.NewResourceService(resources.NewFakeKubeClient(), context.TODO())

got := CreateKubeConfigYAMLForUser(rs, clusterConfig, "gino", "pangolier")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package kubeclient
package resources

import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"log"
runtime "sigs.k8s.io/controller-runtime"
)

// New returns a kubernetes client already configured
func New() kubernetes.Interface {
// NewKubeClient returns a kubernetes client already configured
func NewKubeClient() kubernetes.Interface {
config, err := runtime.GetConfig()

if err != nil {
Expand All @@ -22,3 +23,7 @@ func New() kubernetes.Interface {

return client
}

func NewFakeKubeClient() kubernetes.Interface {
return fake.NewSimpleClientset()
}
3 changes: 1 addition & 2 deletions internal/resources/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes/fake"
)

func TestListNamespaces(t *testing.T) {
kc := fake.NewSimpleClientset()
kc := NewFakeKubeClient()
ctx := context.Background()

svc := NewResourceService(kc, ctx)
Expand Down
2 changes: 0 additions & 2 deletions internal/server/appcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"github.com/labstack/echo"
"k8s.io/client-go/kubernetes"
"net/http"
"sighupio/permission-manager/internal/config"
"sighupio/permission-manager/internal/resources"
Expand All @@ -11,7 +10,6 @@ import (
// AppContext echo context extended with application specific fields
type AppContext struct {
echo.Context
Kubeclient kubernetes.Interface
ResourceService resources.ResourceService
Config config.Config
}
Expand Down
15 changes: 5 additions & 10 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ import (
_ "sighupio/permission-manager/statik"

"github.com/rakyll/statik/fs"
"k8s.io/client-go/kubernetes"
)



func New(kubeclient kubernetes.Interface, cfg config.Config) *echo.Echo {
func New(cfg config.Config) *echo.Echo {
e := echo.New()

e.Validator = &CustomValidator{validator: validator.New()}

addMiddlewareStack(e, kubeclient, cfg)
addMiddlewareStack(e, cfg)

addRoutes(e)

Expand All @@ -36,7 +33,7 @@ func New(kubeclient kubernetes.Interface, cfg config.Config) *echo.Echo {
return e
}

func addMiddlewareStack(e *echo.Echo, kubeclient kubernetes.Interface, cfg config.Config) {
func addMiddlewareStack(e *echo.Echo, cfg config.Config) {
basicAuthPassword := os.Getenv("BASIC_AUTH_PASSWORD")

if basicAuthPassword == "" {
Expand All @@ -62,12 +59,10 @@ func addMiddlewareStack(e *echo.Echo, kubeclient kubernetes.Interface, cfg confi

e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
context := c.Request().Context()
rs := resources.NewResourceService(kubeclient, context)
rs := resources.NewResourceService(resources.NewKubeClient(), c.Request().Context())

customContext := &AppContext{
Context: c,
Kubeclient: kubeclient,
Context: c,
ResourceService: rs,
Config: cfg,
}
Expand Down

0 comments on commit 4cacf94

Please sign in to comment.