Skip to content

Commit

Permalink
Move plugin-related internal packages to pkg (#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec authored Feb 8, 2024
1 parent c627884 commit f6ec67a
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 40 deletions.
4 changes: 2 additions & 2 deletions internal/executor/flux/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package flux

import (
"github.com/kubeshop/botkube/internal/plugin"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/pluginx"
)

// Config holds Flux executor configuration.
Expand All @@ -18,5 +18,5 @@ type Config struct {
} `yaml:"github"`

// Fields not exposed to the user in the JSON schema
TmpDir plugin.TmpDir `yaml:"tmpDir"`
TmpDir pluginx.TmpDir `yaml:"tmpDir"`
}
4 changes: 2 additions & 2 deletions internal/executor/x/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
_ "embed"

"github.com/kubeshop/botkube/internal/executor/x/getter"
"github.com/kubeshop/botkube/internal/plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/pluginx"
)

var (
Expand All @@ -20,7 +20,7 @@ type Config struct {
Logger config.Logger

// Fields not exposed to the user in the JSON schema
TmpDir plugin.TmpDir `yaml:"tmpDir"`
TmpDir pluginx.TmpDir `yaml:"tmpDir"`
}

// GetPluginDependencies returns exec plugin dependencies.
Expand Down
3 changes: 1 addition & 2 deletions internal/executor/x/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/kubeshop/botkube/internal/executor/x/getter"
"github.com/kubeshop/botkube/internal/executor/x/state"
"github.com/kubeshop/botkube/internal/executor/x/template"
"github.com/kubeshop/botkube/internal/plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/executor"
"github.com/kubeshop/botkube/pkg/pluginx"
Expand Down Expand Up @@ -104,7 +103,7 @@ func (i *Runner) getTemplates(ctx context.Context, cfg Config) ([]template.Templ
}

// RunInstalledCommand runs a given user command for already installed CLIs.
func RunInstalledCommand(ctx context.Context, tmp plugin.TmpDir, in string, envs map[string]string) (string, error) {
func RunInstalledCommand(ctx context.Context, tmp pluginx.TmpDir, in string, envs map[string]string) (string, error) {
opts := []pluginx.ExecuteCommandMutation{
pluginx.ExecuteCommandEnvs(envs),
}
Expand Down
4 changes: 2 additions & 2 deletions internal/executor/x/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/kubeshop/botkube/internal/executor/x/getter"
"github.com/kubeshop/botkube/internal/executor/x/state"
"github.com/kubeshop/botkube/internal/executor/x/template"
"github.com/kubeshop/botkube/internal/plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/loggerx"
"github.com/kubeshop/botkube/pkg/pluginx"
)

func TestRunnerRawOutput(t *testing.T) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestRunnerNoExecuteTemplate(t *testing.T) {
Ref: filepath.Join("./testdata/", t.Name()),
},
},
TmpDir: plugin.TmpDir(t.TempDir()),
TmpDir: pluginx.TmpDir(t.TempDir()),
Logger: config.Logger{},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/dustin/go-humanize"

"github.com/kubeshop/botkube/internal/stringx"
"github.com/kubeshop/botkube/pkg/multierror"
"github.com/kubeshop/botkube/pkg/stringx"
)

// Type represents the plugin type.
Expand Down
6 changes: 3 additions & 3 deletions internal/source/argocd/setup_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"

"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/api/source"
"github.com/kubeshop/botkube/pkg/k8sx"
"github.com/kubeshop/botkube/pkg/multierror"
)

Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *Source) getConfigMap(ctx context.Context, k8sCli *dynamic.DynamicClient
}

var cm v1.ConfigMap
err = k8sutil.TransformIntoTypedObject(unstrCM, &cm)
err = k8sx.TransformIntoTypedObject(unstrCM, &cm)
if err != nil {
return v1.ConfigMap{}, fmt.Errorf("while transforming object type %T into type: %T: %w", unstrCM, cm, err)
}
Expand All @@ -176,7 +176,7 @@ func (s *Source) getConfigMap(ctx context.Context, k8sCli *dynamic.DynamicClient
func (s *Source) updateConfigMap(ctx context.Context, k8sCli *dynamic.DynamicClient, cm v1.ConfigMap) error {
s.log.Debug("Updating ConfigMap...")

unstrCM, err := k8sutil.TransformIntoUnstructured(&cm)
unstrCM, err := k8sx.TransformIntoUnstructured(&cm)
if err != nil {
return fmt.Errorf("while transforming object type %T into type: %T: %w", cm, unstrCM, err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/source/kubernetes/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
)

// Event stores data about a given event for Kubernetes object.
Expand Down Expand Up @@ -112,7 +113,7 @@ func New(objectMeta metaV1.ObjectMeta, object interface{}, eventType config.Even
return Event{}, fmt.Errorf("cannot convert type %T into *unstructured.Unstructured", object)
}

err := k8sutil.TransformIntoTypedObject(unstrObj, &eventObj)
err := k8sx.TransformIntoTypedObject(unstrObj, &eventObj)
if err != nil {
return Event{}, fmt.Errorf("while transforming object type %T into type: %T: %w", object, eventObj, err)
}
Expand Down
20 changes: 3 additions & 17 deletions internal/source/kubernetes/k8sutil/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"

"github.com/kubeshop/botkube/pkg/k8sx"
)

// GetObjectMetaData returns metadata of the given object
Expand All @@ -38,7 +39,7 @@ func GetObjectMetaData(ctx context.Context, dynamicCli dynamic.Interface, mapper
}
if GetObjectTypeMetaData(obj).Kind == "Event" {
var eventObj coreV1.Event
err := TransformIntoTypedObject(obj.(*unstructured.Unstructured), &eventObj)
err := k8sx.TransformIntoTypedObject(obj.(*unstructured.Unstructured), &eventObj)
if err != nil {
return metaV1.ObjectMeta{}, fmt.Errorf("while transforming object type: %T into type %T: %w", obj, eventObj, err)
}
Expand Down Expand Up @@ -80,21 +81,6 @@ func GetResourceFromKind(mapper meta.RESTMapper, gvk schema.GroupVersionKind) (s
return mapping.Resource, nil
}

// TransformIntoTypedObject uses unstructured interface and creates a typed object
func TransformIntoTypedObject(obj *unstructured.Unstructured, typedObject interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObject)
}

// TransformIntoUnstructured uses typed object and creates an unstructured interface.
func TransformIntoUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

return &unstructured.Unstructured{Object: out}, nil
}

// extractAnnotationsFromEvent returns annotations of a related resource for the given event.
func extractAnnotationsFromEvent(ctx context.Context, dynamicCli dynamic.Interface, mapper meta.RESTMapper, obj *coreV1.Event) (map[string]string, error) {
gvr, err := GetResourceFromKind(mapper, obj.InvolvedObject.GroupVersionKind())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
"github.com/kubeshop/botkube/pkg/multierror"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func (f *IngressBackendServiceValid) Do(ctx context.Context, event event.Event)
}

var ingress networkingv1.Ingress
err := k8sutil.TransformIntoTypedObject(unstrObj, &ingress)
err := k8sx.TransformIntoTypedObject(unstrObj, &ingress)
if err != nil {
return Result{}, fmt.Errorf("while transforming object type %T into type: %T: %w", event.Object, ingress, err)
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func (f *IngressBackendServiceValid) getService(ctx context.Context, dynamicCli
return coreV1.Service{}, false, err
}
var svc coreV1.Service
err = k8sutil.TransformIntoTypedObject(unstructuredService, &svc)
err = k8sx.TransformIntoTypedObject(unstructuredService, &svc)
if err != nil {
return coreV1.Service{}, false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
"github.com/kubeshop/botkube/pkg/multierror"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func (f *IngressTLSSecretValid) Do(ctx context.Context, event event.Event) (Resu
}

var ingress networkingv1.Ingress
err := k8sutil.TransformIntoTypedObject(unstrObj, &ingress)
err := k8sx.TransformIntoTypedObject(unstrObj, &ingress)
if err != nil {
return Result{}, fmt.Errorf("while transforming object type %T into type: %T: %w", event.Object, ingress, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/source/kubernetes/recommendation/pod_labels_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
)

const podLabelsSetName = "PodLabelsSet"
Expand All @@ -34,7 +34,7 @@ func (f PodLabelsSet) Do(_ context.Context, event event.Event) (Result, error) {
}

var pod coreV1.Pod
err := k8sutil.TransformIntoTypedObject(unstrObj, &pod)
err := k8sx.TransformIntoTypedObject(unstrObj, &pod)
if err != nil {
return Result{}, fmt.Errorf("while transforming object type %T into type: %T: %w", event.Object, pod, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
)

const podNoLatestImageTag = "PodNoLatestImageTag"
Expand All @@ -35,7 +36,7 @@ func (f *PodNoLatestImageTag) Do(_ context.Context, event event.Event) (Result,
}

var pod coreV1.Pod
err := k8sutil.TransformIntoTypedObject(unstrObj, &pod)
err := k8sx.TransformIntoTypedObject(unstrObj, &pod)
if err != nil {
return Result{}, fmt.Errorf("while transforming object type %T into type: %T: %w", event.Object, pod, err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/source/kubernetes/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/internal/source/kubernetes/k8sutil"
"github.com/kubeshop/botkube/pkg/k8sx"
"github.com/kubeshop/botkube/pkg/multierror"
)

Expand Down Expand Up @@ -72,7 +73,7 @@ func (r registration) handleMapped(ctx context.Context, s Source, eventType conf
_, _ = r.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
var eventObj coreV1.Event
err := k8sutil.TransformIntoTypedObject(obj.(*unstructured.Unstructured), &eventObj)
err := k8sx.TransformIntoTypedObject(obj.(*unstructured.Unstructured), &eventObj)
if err != nil {
r.log.Errorf("Unable to transform object type: %v, into type: %v", reflect.TypeOf(obj), reflect.TypeOf(eventObj))
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/go-playground/validator/v10"

"github.com/kubeshop/botkube/internal/stringx"
"github.com/kubeshop/botkube/pkg/multierror"
"github.com/kubeshop/botkube/pkg/stringx"
)

// DecomposePluginKey extract details from plugin key.
Expand Down
21 changes: 21 additions & 0 deletions pkg/k8sx/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package k8sx

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

// TransformIntoTypedObject uses unstructured interface and creates a typed object.
func TransformIntoTypedObject(obj *unstructured.Unstructured, typedObject interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObject)
}

// TransformIntoUnstructured uses typed object and creates an unstructured interface.
func TransformIntoUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

return &unstructured.Unstructured{Object: out}, nil
}
9 changes: 7 additions & 2 deletions internal/plugin/tmp_dir.go → pkg/pluginx/tmp_dir.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package plugin
package pluginx

import (
"os"
"path"

"github.com/kubeshop/botkube/internal/plugin"
)

// TmpDir represents temporary directory.
type TmpDir string

// Get returns temporary directory path.
func (t TmpDir) Get() (string, bool) {
if t != "" {
return string(t), true
}

depDir := os.Getenv(DependencyDirEnvName)
depDir := os.Getenv(plugin.DependencyDirEnvName)
if depDir != "" {
return depDir, false
}

return path.Join(os.TempDir(), "bin"), true
}

// GetDirectory returns temporary directory.
func (t TmpDir) GetDirectory() string {
dir, _ := t.Get()
return dir
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f6ec67a

Please sign in to comment.