Skip to content

Commit

Permalink
Enable reading workflows from multiple namespaces in tink-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Monsuwe committed Aug 1, 2024
1 parent a3d4371 commit 2e3a171
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
17 changes: 4 additions & 13 deletions internal/server/kubernetes_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ func NewKubeBackedServer(logger logr.Logger, kubeconfig, apiserver, namespace st
return nil, err
}

namespace, _, err = ccfg.Namespace()
if err != nil {
return nil, err
}

if err != nil {
return nil, err
}

return NewKubeBackedServerFromREST(logger, cfg, namespace)
}

Expand All @@ -61,8 +52,10 @@ func NewKubeBackedServerFromREST(logger logr.Logger, config *rest.Config, namesp
clstr, err := cluster.New(config, func(opts *cluster.Options) {
opts.Scheme = controller.DefaultScheme()
opts.Logger = zapr.NewLogger(zap.NewNop())
opts.Cache.DefaultNamespaces = map[string]cache.Config{
namespace: {},
if namespace != "" {
opts.Cache.DefaultNamespaces = map[string]cache.Config{
namespace: {},
}
}
})
if err != nil {
Expand All @@ -89,7 +82,6 @@ func NewKubeBackedServerFromREST(logger logr.Logger, config *rest.Config, namesp
return &KubernetesBackedServer{
logger: logger,
ClientFunc: clstr.GetClient,
namespace: namespace,
nowFunc: time.Now,
}, nil
}
Expand All @@ -98,7 +90,6 @@ func NewKubeBackedServerFromREST(logger logr.Logger, config *rest.Config, namesp
type KubernetesBackedServer struct {
logger logr.Logger
ClientFunc func() client.Client
namespace string

nowFunc func() time.Time
}
Expand Down
12 changes: 7 additions & 5 deletions internal/server/kubernetes_api_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"strings"

"github.com/pkg/errors"
"github.com/tinkerbell/tink/api/v1alpha1"
Expand All @@ -24,7 +25,7 @@ const (

func getWorkflowContext(wf v1alpha1.Workflow) *proto.WorkflowContext {
return &proto.WorkflowContext{
WorkflowId: wf.Name,
WorkflowId: wf.Namespace + "/" + wf.Name,
CurrentWorker: wf.GetCurrentWorker(),
CurrentTask: wf.GetCurrentTask(),
CurrentAction: wf.GetCurrentAction(),
Expand Down Expand Up @@ -52,9 +53,10 @@ func (s *KubernetesBackedServer) getCurrentAssignedNonTerminalWorkflowsForWorker
return wfs, nil
}

func (s *KubernetesBackedServer) getWorkflowByName(ctx context.Context, workflowID, namespace string) (*v1alpha1.Workflow, error) {
func (s *KubernetesBackedServer) getWorkflowByName(ctx context.Context, workflowID string) (*v1alpha1.Workflow, error) {
workflowNamespace, workflowName, _ := strings.Cut(workflowID, "/")
wflw := &v1alpha1.Workflow{}
err := s.ClientFunc().Get(ctx, types.NamespacedName{Name: workflowID, Namespace: namespace}, wflw)
err := s.ClientFunc().Get(ctx, types.NamespacedName{Name: workflowName, Namespace: workflowNamespace}, wflw)
if err != nil {
s.logger.Error(err, "get client", "workflow", workflowID)
return nil, err
Expand Down Expand Up @@ -85,7 +87,7 @@ func (s *KubernetesBackedServer) GetWorkflowActions(ctx context.Context, req *pr
if wfID == "" {
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
}
wf, err := s.getWorkflowByName(ctx, wfID, s.namespace)
wf, err := s.getWorkflowByName(ctx, wfID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -189,7 +191,7 @@ func (s *KubernetesBackedServer) ReportActionStatus(ctx context.Context, req *pr
wfID := req.GetWorkflowId()
l := s.logger.WithValues("actionName", req.GetActionName(), "status", req.GetActionStatus(), "workflowID", req.GetWorkflowId(), "taskName", req.GetTaskName(), "worker", req.WorkerId)

wf, err := s.getWorkflowByName(ctx, wfID, s.namespace)
wf, err := s.getWorkflowByName(ctx, wfID)
if err != nil {
l.Error(err, "get workflow")
return nil, status.Errorf(codes.InvalidArgument, errInvalidWorkflowID)
Expand Down

0 comments on commit 2e3a171

Please sign in to comment.