Skip to content

Commit

Permalink
fix: prepare config data
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Dec 24, 2024
1 parent a4b76bb commit 5f0ade8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ func main() {
// Initialize event handlers
websocketLoader := ws.NewWebsocketLoader()
if !cfg.DisableWebhooks {
eventsEmitter.Loader.Register(webhook.NewWebhookLoader(log.DefaultLogger, webhooksClient, deprecatedClients, deprecatedRepositories, testWorkflowResultsRepository, metrics, &proContext, envs))
eventsEmitter.Loader.Register(webhook.NewWebhookLoader(log.DefaultLogger, webhooksClient, deprecatedClients, deprecatedRepositories,
testWorkflowResultsRepository, secretClient, metrics, &proContext, envs))
}
eventsEmitter.Loader.Register(websocketLoader)
eventsEmitter.Loader.Register(commons.MustCreateSlackLoader(cfg, envs))
Expand Down
43 changes: 41 additions & 2 deletions pkg/event/kind/webhook/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (
"github.com/kubeshop/testkube/pkg/event/kind/common"
"github.com/kubeshop/testkube/pkg/mapper/webhooks"
"github.com/kubeshop/testkube/pkg/repository/testworkflow"
"github.com/kubeshop/testkube/pkg/secret"
)

var _ common.ListenerLoader = (*WebhooksLoader)(nil)

func NewWebhookLoader(log *zap.SugaredLogger, webhooksClient executorsclientv1.WebhooksInterface, deprecatedClients commons.DeprecatedClients,
deprecatedRepositories commons.DeprecatedRepositories, testWorkflowExecutionResults testworkflow.Repository,
metrics v1.Metrics, proContext *config.ProContext, envs map[string]string,
secretClient secret.Interface, metrics v1.Metrics, proContext *config.ProContext, envs map[string]string,
) *WebhooksLoader {
return &WebhooksLoader{
log: log,
WebhooksClient: webhooksClient,
deprecatedClients: deprecatedClients,
deprecatedRepositories: deprecatedRepositories,
testWorkflowExecutionResults: testWorkflowExecutionResults,
secretClient: secretClient,
metrics: metrics,
proContext: proContext,
envs: envs,
Expand All @@ -40,6 +42,7 @@ type WebhooksLoader struct {
deprecatedClients commons.DeprecatedClients
deprecatedRepositories commons.DeprecatedRepositories
testWorkflowExecutionResults testworkflow.Repository
secretClient secret.Interface
metrics v1.Metrics
proContext *config.ProContext
envs map[string]string
Expand Down Expand Up @@ -92,13 +95,49 @@ func (r WebhooksLoader) Load() (listeners common.Listeners, err error) {

types := webhooks.MapEventArrayToCRDEvents(webhook.Spec.Events)
name := fmt.Sprintf("%s.%s", webhook.ObjectMeta.Namespace, webhook.ObjectMeta.Name)
vars := make(map[string]string)
for key, value := range webhook.Spec.Config {
data := ""
if value.Public != nil {
data = *value.Public
}

if value.Private != nil {
var ns []string
if value.Private.Namespace != "" {
ns = append(ns, value.Private.Namespace)
}

elements, err := r.secretClient.Get(value.Private.Name, ns...)
if err != nil {
r.log.Errorw("error secret loading", "error", err, "name", value.Private.Name)
continue
}

if element, ok := elements[value.Private.Key]; ok {
data = element
} else {
r.log.Errorw("error secret key finding loading", "name", value.Private.Name, "key", value.Private.Key)
continue
}
}

vars[key] = data
}

for key, value := range webhook.Spec.Parameters {
if _, ok := vars[key]; !ok && value.Default_ != nil {
vars[key] = *value.Default_
}
}

listeners = append(
listeners,
NewWebhookListener(
name, webhook.Spec.Uri, webhook.Spec.Selector, types,
webhook.Spec.PayloadObjectField, payloadTemplate, webhook.Spec.Headers, webhook.Spec.Disabled,
r.deprecatedRepositories, r.testWorkflowExecutionResults,
r.metrics, r.proContext, r.envs, nil,
r.metrics, r.proContext, r.envs, vars,
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/event/kind/webhook/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestWebhookLoader(t *testing.T) {
mockDeprecatedClients := commons.NewMockDeprecatedClients(mockCtrl)
mockDeprecatedClients.EXPECT().Templates().Return(mockTemplatesClient).AnyTimes()

webhooksLoader := NewWebhookLoader(zap.NewNop().Sugar(), mockWebhooksClient, mockDeprecatedClients, nil, nil, v1.NewMetrics(), nil, nil)
webhooksLoader := NewWebhookLoader(zap.NewNop().Sugar(), mockWebhooksClient, mockDeprecatedClients, nil, nil, nil, v1.NewMetrics(), nil, nil)
listeners, err := webhooksLoader.Load()

assert.Equal(t, 1, len(listeners))
Expand Down

0 comments on commit 5f0ade8

Please sign in to comment.