Skip to content

Commit

Permalink
🎭 k8s client mock (#623)
Browse files Browse the repository at this point in the history
* remove unused merthods

* k8s client mock
  • Loading branch information
petar-cvit authored Oct 10, 2024
1 parent a74945a commit e279463
Show file tree
Hide file tree
Showing 15 changed files with 2,153 additions and 18 deletions.
2 changes: 2 additions & 0 deletions cyclops-ctrl/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bin
cover.out
cover.html
13 changes: 13 additions & 0 deletions cyclops-ctrl/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
quiet: False
disable-version-string: True
with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
outpkg: mocks
dir: mocks
packages:
github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient:
interfaces:
IKubernetesClient:
config:
dir: pkg/mocks
10 changes: 10 additions & 0 deletions cyclops-ctrl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ group-imports:

unit-test:
go test -v -tags musl,dynamic ./...

coverage:
go test -v -coverprofile cover.out ./...
go tool cover -html cover.out -o cover.html
open cover.html

mockery:
GOBIN=$(LOCALBIN) go install github.com/vektra/mockery/v2@latest
./bin/mockery

5 changes: 4 additions & 1 deletion cyclops-ctrl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.15.3
Expand All @@ -27,6 +28,7 @@ require (
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -104,6 +106,7 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc6 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand All @@ -113,6 +116,7 @@ require (
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
Expand Down Expand Up @@ -151,5 +155,4 @@ require (
oras.land/oras-go v1.2.5 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
1 change: 1 addition & 0 deletions cyclops-ctrl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

type Cluster struct {
kubernetesClient *k8sclient.KubernetesClient
kubernetesClient k8sclient.IKubernetesClient
}

func NewClusterController(kubernetes *k8sclient.KubernetesClient) *Cluster {
func NewClusterController(kubernetes k8sclient.IKubernetesClient) *Cluster {
return &Cluster{
kubernetesClient: kubernetes,
}
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

type Modules struct {
kubernetesClient *k8sclient.KubernetesClient
kubernetesClient k8sclient.IKubernetesClient
templatesRepo *template.Repo
renderer *render.Renderer
telemetryClient telemetry.Client
Expand All @@ -33,7 +33,7 @@ type Modules struct {

func NewModulesController(
templatesRepo *template.Repo,
kubernetes *k8sclient.KubernetesClient,
kubernetes k8sclient.IKubernetesClient,
renderer *render.Renderer,
telemetryClient telemetry.Client,
monitor prometheus.Monitor,
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/controller/sse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type Server struct {
k8sClient *k8sclient.KubernetesClient
k8sClient k8sclient.IKubernetesClient
}

// Initialize event and Start procnteessing requests
func NewServer(k8sClient *k8sclient.KubernetesClient) *Server {
func NewServer(k8sClient k8sclient.IKubernetesClient) *Server {
server := &Server{
k8sClient: k8sClient,
}
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/controller/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (

type Templates struct {
templatesRepo *template.Repo
kubernetesClient *k8sclient.KubernetesClient
kubernetesClient k8sclient.IKubernetesClient
telemetryClient telemetry.Client
}

func NewTemplatesController(
templatesRepo *template.Repo,
kubernetes *k8sclient.KubernetesClient,
kubernetes k8sclient.IKubernetesClient,
telemetryClient telemetry.Client,
) *Templates {
return &Templates{
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Handler struct {
router *gin.Engine

templatesRepo *templaterepo.Repo
k8sClient *k8sclient.KubernetesClient
k8sClient k8sclient.IKubernetesClient
renderer *render.Renderer

telemetryClient telemetry.Client
Expand All @@ -26,7 +26,7 @@ type Handler struct {

func New(
templatesRepo *templaterepo.Repo,
kubernetesClient *k8sclient.KubernetesClient,
kubernetesClient k8sclient.IKubernetesClient,
renderer *render.Renderer,
telemetryClient telemetry.Client,
monitor prometheus.Monitor,
Expand Down
6 changes: 3 additions & 3 deletions cyclops-ctrl/internal/modulecontroller/module_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ModuleReconciler struct {
Scheme *runtime.Scheme

templatesRepo *templaterepo.Repo
kubernetesClient *k8sclient.KubernetesClient
kubernetesClient k8sclient.IKubernetesClient
renderer *render.Renderer

telemetryClient telemetry.Client
Expand All @@ -63,7 +63,7 @@ func NewModuleReconciler(
client client.Client,
scheme *runtime.Scheme,
templatesRepo *templaterepo.Repo,
kubernetesClient *k8sclient.KubernetesClient,
kubernetesClient k8sclient.IKubernetesClient,
renderer *render.Renderer,
telemetryClient telemetry.Client,
monitor prometheus.Monitor,
Expand Down Expand Up @@ -226,7 +226,7 @@ func (r *ModuleReconciler) moduleToResources(template *models.Template, module *
}

func (r *ModuleReconciler) generateResources(
kClient *k8sclient.KubernetesClient,
kClient k8sclient.IKubernetesClient,
module cyclopsv1alpha1.Module,
moduleTemplate *models.Template,
) ([]string, []cyclopsv1alpha1.GroupVersionResource, error) {
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/template/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

type Renderer struct {
k8sClient *k8sclient.KubernetesClient
k8sClient k8sclient.IKubernetesClient
}

func NewRenderer(kubernetesClient *k8sclient.KubernetesClient) *Renderer {
func NewRenderer(kubernetesClient k8sclient.IKubernetesClient) *Renderer {
return &Renderer{
k8sClient: kubernetesClient,
}
Expand Down
49 changes: 49 additions & 0 deletions cyclops-ctrl/pkg/cluster/k8sclient/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package k8sclient

import (
"context"

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"

cyclopsv1alpha1 "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/dto"
)

const (
Expand Down Expand Up @@ -53,3 +62,43 @@ func createLocalClient() (*KubernetesClient, error) {
moduleset: moduleSet,
}, nil
}

type IKubernetesClient interface {
GetStreamedPodLogs(ctx context.Context, namespace, container, name string, logCount *int64, logChan chan<- string) error
GetPodLogs(namespace, container, name string, numLogs *int64) ([]string, error)
GetDeploymentLogs(namespace, container, deployment string, numLogs *int64) ([]string, error)
GetStatefulSetsLogs(namespace, container, name string, numLogs *int64) ([]string, error)
ListModules() ([]cyclopsv1alpha1.Module, error)
CreateModule(module cyclopsv1alpha1.Module) error
UpdateModule(module *cyclopsv1alpha1.Module) error
UpdateModuleStatus(module *cyclopsv1alpha1.Module) (*cyclopsv1alpha1.Module, error)
DeleteModule(name string) error
GetModule(name string) (*cyclopsv1alpha1.Module, error)
GetResourcesForModule(name string) ([]dto.Resource, error)
GetWorkloadsForModule(name string) ([]dto.Resource, error)
GetDeletedResources([]dto.Resource, string, string) ([]dto.Resource, error)
GetModuleResourcesHealth(name string) (string, error)
GVKtoAPIResourceName(gv schema.GroupVersion, kind string) (string, error)
VersionInfo() (*version.Info, error)
RestartDeployment(name, namespace string) error
RestartStatefulSet(name, namespace string) error
RestartDaemonSet(name, namespace string) error
GetManifest(group, version, kind, name, namespace string, includeManagedFields bool) (string, error)
Restart(group, version, kind, name, namespace string) error
GetResource(group, version, kind, name, namespace string) (any, error)
Delete(resource dto.Resource) error
CreateDynamic(cyclopsv1alpha1.GroupVersionResource, *unstructured.Unstructured, string) error
ApplyCRD(obj *unstructured.Unstructured) error
ListNodes() ([]apiv1.Node, error)
GetNode(name string) (*apiv1.Node, error)
GetPodsForNode(nodeName string) ([]apiv1.Pod, error)
ListNamespaces() ([]string, error)
WatchResource(group, version, resource, name, namespace string) (watch.Interface, error)
WatchKubernetesResources(gvrs []ResourceWatchSpec, stopCh chan struct{}) (chan *unstructured.Unstructured, error)
ListTemplateAuthRules() ([]cyclopsv1alpha1.TemplateAuthRule, error)
GetTemplateAuthRuleSecret(name, key string) (string, error)
ListTemplateStore() ([]cyclopsv1alpha1.TemplateStore, error)
CreateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error
UpdateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error
DeleteTemplateStore(name string) error
}
5 changes: 3 additions & 2 deletions cyclops-ctrl/pkg/cluster/k8sclient/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package k8sclient
import (
"context"
"fmt"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/pkg/errors"
"strings"
"time"

"github.com/pkg/errors"

"gopkg.in/yaml.v2"

apiv1 "k8s.io/api/core/v1"
Expand All @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/dto"
)

Expand Down
Loading

0 comments on commit e279463

Please sign in to comment.