feat: pass around logger properly
This commit is contained in:
10 files changed
+109
-92
No files matched your search
@@ -25,6 +25,7 @@ import (
|
||||
type exportConnector struct {
|
||||
repoGetter RepoGetter
|
||||
client *resourceClient
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (*exportConnector) New() runtime.Object {
|
||||
@@ -64,6 +65,7 @@ func (c *exportConnector) Connect(
|
||||
opts runtime.Object,
|
||||
responder rest.Responder,
|
||||
) (http.Handler, error) {
|
||||
logger := c.logger.With("repository_name", name)
|
||||
repo, err := c.repoGetter.GetRepository(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -123,12 +125,12 @@ func (c *exportConnector) Connect(
|
||||
return
|
||||
}
|
||||
|
||||
slog.InfoContext(ctx, "got item in dashboard list",
|
||||
logger.InfoContext(ctx, "got item in dashboard list",
|
||||
"name", item.GetName(),
|
||||
"namespace", item.GetNamespace())
|
||||
name := item.GetName()
|
||||
if namespace := item.GetNamespace(); namespace != ns {
|
||||
slog.DebugContext(ctx, "skipping dashboard in export due to mismatched namespace",
|
||||
logger.DebugContext(ctx, "skipping dashboard in export due to mismatched namespace",
|
||||
"name", name,
|
||||
"namespace", map[string]string{"expected": ns, "actual": namespace})
|
||||
continue
|
||||
@@ -139,7 +141,7 @@ func (c *exportConnector) Connect(
|
||||
delete(item.Object, "metadata")
|
||||
marshalledBody, baseFileName, err := c.marshalPreferredFormat(item.Object, name, repo)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to marshal dashboard into preferred format",
|
||||
logger.ErrorContext(ctx, "failed to marshal dashboard into preferred format",
|
||||
"err", err,
|
||||
"dashboard", name,
|
||||
"namespace", ns)
|
||||
@@ -154,8 +156,8 @@ func (c *exportConnector) Connect(
|
||||
|
||||
fileName := filepath.Join(folder.CreatePath(), baseFileName)
|
||||
// TODO: Upsert
|
||||
if err := repo.Create(ctx, fileName, ref, marshalledBody, "export of dashboard "+name+" in namespace "+ns); err != nil {
|
||||
slog.ErrorContext(ctx, "failed to write dashboard model to repository",
|
||||
if err := repo.Create(ctx, logger, fileName, ref, marshalledBody, "export of dashboard "+name+" in namespace "+ns); err != nil {
|
||||
logger.ErrorContext(ctx, "failed to write dashboard model to repository",
|
||||
"err", err,
|
||||
"repository", repo.Config().GetName(),
|
||||
"dashboard", name,
|
||||
|
||||
@@ -110,7 +110,7 @@ func (s *filesConnector) Connect(ctx context.Context, name string, opts runtime.
|
||||
case http.MethodPut:
|
||||
obj, err = s.doWrite(r.Context(), logger, true, repo, filePath, ref, message, r)
|
||||
case http.MethodDelete:
|
||||
obj, err = s.doDelete(r.Context(), repo, filePath, ref, message)
|
||||
obj, err = s.doDelete(r.Context(), logger, repo, filePath, ref, message)
|
||||
default:
|
||||
err = apierrors.NewMethodNotSupported(provisioning.RepositoryResourceInfo.GroupResource(), r.Method)
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func (s *filesConnector) getParser(repo repository.Repository) (*fileParser, err
|
||||
}
|
||||
|
||||
func (s *filesConnector) doRead(ctx context.Context, logger *slog.Logger, repo repository.Repository, path string, ref string) (int, *provisioning.ResourceWrapper, error) {
|
||||
info, err := repo.Read(ctx, path, ref)
|
||||
info, err := repo.Read(ctx, logger, path, ref)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
@@ -204,9 +204,9 @@ func (s *filesConnector) doWrite(ctx context.Context, logger *slog.Logger, updat
|
||||
}
|
||||
|
||||
if update {
|
||||
err = repo.Update(ctx, path, ref, data, message)
|
||||
err = repo.Update(ctx, logger, path, ref, data, message)
|
||||
} else {
|
||||
err = repo.Create(ctx, path, ref, data, message)
|
||||
err = repo.Create(ctx, logger, path, ref, data, message)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -233,13 +233,13 @@ func (s *filesConnector) doWrite(ctx context.Context, logger *slog.Logger, updat
|
||||
return parsed.AsResourceWrapper(), err
|
||||
}
|
||||
|
||||
func (s *filesConnector) doDelete(ctx context.Context, repo repository.Repository, path string, ref string, message string) (*provisioning.ResourceWrapper, error) {
|
||||
func (s *filesConnector) doDelete(ctx context.Context, logger *slog.Logger, repo repository.Repository, path string, ref string, message string) (*provisioning.ResourceWrapper, error) {
|
||||
settings := repo.Config().Spec.Editing
|
||||
if !settings.Delete {
|
||||
return nil, apierrors.NewForbidden(provisioning.RepositoryResourceInfo.GroupResource(), "deleting is not supported", nil)
|
||||
}
|
||||
|
||||
err := repo.Delete(ctx, path, ref, message)
|
||||
err := repo.Delete(ctx, logger, path, ref, message)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type helloWorldSubresource struct {
|
||||
statusUpdater rest.Updater
|
||||
// for our hacky test access right now
|
||||
parent *ProvisioningAPIBuilder
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (*helloWorldSubresource) New() runtime.Object {
|
||||
@@ -52,6 +53,7 @@ func (*helloWorldSubresource) NewConnectOptions() (runtime.Object, bool, string)
|
||||
}
|
||||
|
||||
func (s *helloWorldSubresource) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
||||
logger := s.logger.With("repository_name", name)
|
||||
obj, err := s.getter.Get(ctx, name, &metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -66,16 +68,16 @@ func (s *helloWorldSubresource) Connect(ctx context.Context, name string, opts r
|
||||
if whom == "" {
|
||||
whom = "World"
|
||||
}
|
||||
logger := logger.With("whom", whom)
|
||||
|
||||
newCommit := r.URL.Query().Get("commit")
|
||||
if newCommit != "" {
|
||||
repo.Status.CurrentGitCommit = newCommit
|
||||
obj, b, err := s.statusUpdater.Update(
|
||||
obj, _, err := s.statusUpdater.Update(
|
||||
ctx,
|
||||
name, // resource name
|
||||
rest.DefaultUpdatedObjectInfo(obj, func(ctx context.Context, newObj, oldObj runtime.Object) (transformedNewObj runtime.Object, err error) {
|
||||
newObj.(*v0alpha1.Repository).Status.CurrentGitCommit = newCommit
|
||||
slog.InfoContext(ctx, "updated the commit", "newObj", newObj, "newCommit", newCommit)
|
||||
return newObj, nil
|
||||
}),
|
||||
func(ctx context.Context, obj runtime.Object) error { return nil }, // createValidation
|
||||
@@ -88,10 +90,9 @@ func (s *helloWorldSubresource) Connect(ctx context.Context, name string, opts r
|
||||
return
|
||||
}
|
||||
repo = obj.(*v0alpha1.Repository)
|
||||
slog.InfoContext(ctx, "the conspicuous boolean", "bool", b)
|
||||
}
|
||||
|
||||
slog.InfoContext(ctx, "Got a repository",
|
||||
logger.InfoContext(ctx, "Got a repository",
|
||||
"name", repo.Name,
|
||||
"type", repo.Spec.Type,
|
||||
"status", repo.Status,
|
||||
|
||||
@@ -149,11 +149,13 @@ func (b *ProvisioningAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserv
|
||||
getter: repositoryStorage,
|
||||
statusUpdater: repositoryStatusStorage,
|
||||
parent: b,
|
||||
logger: b.logger.With("connector", "hello_world"),
|
||||
}
|
||||
|
||||
exportConnector := &exportConnector{
|
||||
repoGetter: b,
|
||||
client: b.client,
|
||||
logger: b.logger.With("connector", "export"),
|
||||
}
|
||||
|
||||
storage := map[string]rest.Storage{}
|
||||
@@ -164,6 +166,7 @@ func (b *ProvisioningAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserv
|
||||
storage[provisioning.RepositoryResourceInfo.StoragePath("webhook")] = &webhookConnector{
|
||||
getter: b,
|
||||
client: b.client.identities,
|
||||
logger: b.logger.With("connector", "webhook"),
|
||||
}
|
||||
storage[provisioning.RepositoryResourceInfo.StoragePath("files")] = &filesConnector{
|
||||
getter: b,
|
||||
@@ -223,7 +226,7 @@ func (b *ProvisioningAPIBuilder) afterCreate(obj runtime.Object, opts *metav1.Cr
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.AfterCreate(ctx); err != nil {
|
||||
if err := repo.AfterCreate(ctx, b.logger); err != nil {
|
||||
b.logger.Error("failed to run after create", "error", err)
|
||||
return
|
||||
}
|
||||
@@ -253,7 +256,7 @@ func (b *ProvisioningAPIBuilder) beginUpdate(ctx context.Context, obj, old runti
|
||||
return nil, fmt.Errorf("failed to ensure the configured folder exists: %w", err)
|
||||
}
|
||||
|
||||
undo, err := repo.BeginUpdate(ctx, oldRepo)
|
||||
undo, err := repo.BeginUpdate(ctx, b.logger, oldRepo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -261,7 +264,7 @@ func (b *ProvisioningAPIBuilder) beginUpdate(ctx context.Context, obj, old runti
|
||||
return func(ctx context.Context, success bool) {
|
||||
if !success && undo != nil {
|
||||
if err := undo(ctx); err != nil {
|
||||
b.logger.Error("failed to undo failed update", "error", err)
|
||||
b.logger.ErrorContext(ctx, "failed to undo failed update", "error", err)
|
||||
}
|
||||
}
|
||||
}, nil
|
||||
@@ -336,7 +339,7 @@ func (b *ProvisioningAPIBuilder) afterDelete(obj runtime.Object, opts *metav1.De
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.AfterDelete(ctx); err != nil {
|
||||
if err := repo.AfterDelete(ctx, b.logger); err != nil {
|
||||
b.logger.Error("failed to run after delete", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func (r *githubRepository) Validate() (list field.ErrorList) {
|
||||
}
|
||||
|
||||
// Test implements provisioning.Repository.
|
||||
func (r *githubRepository) Test(ctx context.Context) error {
|
||||
func (r *githubRepository) Test(ctx context.Context, logger *slog.Logger) error {
|
||||
return &apierrors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "test is not yet implemented",
|
||||
@@ -86,7 +86,7 @@ func (r *githubRepository) Test(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// ReadResource implements provisioning.Repository.
|
||||
func (r *githubRepository) Read(ctx context.Context, filePath, ref string) (*FileInfo, error) {
|
||||
func (r *githubRepository) Read(ctx context.Context, logger *slog.Logger, filePath, ref string) (*FileInfo, error) {
|
||||
if ref == "" {
|
||||
ref = r.config.Spec.GitHub.Branch
|
||||
}
|
||||
@@ -119,7 +119,7 @@ func (r *githubRepository) Read(ctx context.Context, filePath, ref string) (*Fil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *githubRepository) Create(ctx context.Context, path, ref string, data []byte, comment string) error {
|
||||
func (r *githubRepository) Create(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error {
|
||||
owner := r.config.Spec.GitHub.Owner
|
||||
repo := r.config.Spec.GitHub.Repository
|
||||
|
||||
@@ -135,7 +135,7 @@ func (r *githubRepository) Create(ctx context.Context, path, ref string, data []
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *githubRepository) Update(ctx context.Context, path, ref string, data []byte, comment string) error {
|
||||
func (r *githubRepository) Update(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error {
|
||||
owner := r.config.Spec.GitHub.Owner
|
||||
repo := r.config.Spec.GitHub.Repository
|
||||
|
||||
@@ -159,7 +159,7 @@ func (r *githubRepository) Update(ctx context.Context, path, ref string, data []
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *githubRepository) Delete(ctx context.Context, path, ref, comment string) error {
|
||||
func (r *githubRepository) Delete(ctx context.Context, logger *slog.Logger, path, ref, comment string) error {
|
||||
owner := r.config.Spec.GitHub.Owner
|
||||
repo := r.config.Spec.GitHub.Repository
|
||||
|
||||
@@ -180,7 +180,7 @@ func (r *githubRepository) Delete(ctx context.Context, path, ref, comment string
|
||||
}
|
||||
|
||||
// Webhook implements provisioning.Repository.
|
||||
func (r *githubRepository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
func (r *githubRepository) Webhook(ctx context.Context, logger *slog.Logger, responder rest.Responder) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
payload, err := github.ValidatePayload(req, []byte(r.config.Spec.GitHub.WebhookSecret))
|
||||
if err != nil {
|
||||
@@ -197,7 +197,7 @@ func (r *githubRepository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
|
||||
switch event := event.(type) {
|
||||
case *github.PushEvent:
|
||||
if err := r.onPushEvent(req.Context(), event); err != nil {
|
||||
if err := r.onPushEvent(req.Context(), logger, event); err != nil {
|
||||
responder.Error(err)
|
||||
return
|
||||
}
|
||||
@@ -217,7 +217,9 @@ func (r *githubRepository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *githubRepository) onPushEvent(ctx context.Context, event *github.PushEvent) error {
|
||||
func (r *githubRepository) onPushEvent(ctx context.Context, logger *slog.Logger, event *github.PushEvent) error {
|
||||
logger = logger.With("ref", event.GetRef())
|
||||
|
||||
if event.GetRepo() == nil {
|
||||
return fmt.Errorf("missing repository in push event")
|
||||
}
|
||||
@@ -229,41 +231,44 @@ func (r *githubRepository) onPushEvent(ctx context.Context, event *github.PushEv
|
||||
// Skip silently if the event is not for the main/master branch
|
||||
// as we cannot configure the webhook to only publish events for the main branch
|
||||
if event.GetRef() != fmt.Sprintf("refs/heads/%s", r.config.Spec.GitHub.Branch) {
|
||||
r.logger.DebugContext(ctx, "ignoring push event as it is not for the configured branch",
|
||||
"ref", event.GetRef())
|
||||
logger.DebugContext(ctx, "ignoring push event as it is not for the configured branch")
|
||||
return nil
|
||||
}
|
||||
|
||||
beforeRef := event.GetBefore()
|
||||
|
||||
for _, commit := range event.Commits {
|
||||
r.logger.Info("process commit", "commit", commit.GetID(), "message", commit.GetMessage())
|
||||
logger := logger.With("commit", commit.GetID(), "message", commit.GetMessage())
|
||||
logger.InfoContext(ctx, "processing commit")
|
||||
|
||||
for _, file := range commit.Added {
|
||||
info, err := r.Read(ctx, file, commit.GetID())
|
||||
logger := logger.With("change", "added", "file", file)
|
||||
info, err := r.Read(ctx, logger, file, commit.GetID())
|
||||
if err != nil {
|
||||
return fmt.Errorf("read added resource: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("added file", "file", file, "resource", string(info.Data), "commit", commit.GetID())
|
||||
logger.InfoContext(ctx, "added file", "resource", string(info.Data))
|
||||
}
|
||||
|
||||
for _, file := range commit.Modified {
|
||||
info, err := r.Read(ctx, file, commit.GetID())
|
||||
logger := logger.With("change", "modified", "file", file)
|
||||
info, err := r.Read(ctx, logger, file, commit.GetID())
|
||||
if err != nil {
|
||||
return fmt.Errorf("read modified resource: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("modified file", "file", file, "resource", string(info.Data), "commit", commit.GetID())
|
||||
logger.InfoContext(ctx, "modified file", "resource", string(info.Data))
|
||||
}
|
||||
|
||||
for _, file := range commit.Removed {
|
||||
info, err := r.Read(ctx, file, beforeRef)
|
||||
logger := logger.With("change", "removed", "file", file)
|
||||
info, err := r.Read(ctx, logger, file, beforeRef)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read removed resource: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("removed file", "file", file, "resource", string(info.Data), "commit", commit.GetID())
|
||||
logger.InfoContext(ctx, "removed file", "resource", string(info.Data))
|
||||
}
|
||||
|
||||
beforeRef = commit.GetID()
|
||||
@@ -272,7 +277,7 @@ func (r *githubRepository) onPushEvent(ctx context.Context, event *github.PushEv
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *githubRepository) createWebhook(ctx context.Context) error {
|
||||
func (r *githubRepository) createWebhook(ctx context.Context, logger *slog.Logger) error {
|
||||
cfg := pgh.WebhookConfig{
|
||||
URL: r.config.Spec.GitHub.WebhookURL,
|
||||
Secret: r.config.Spec.GitHub.WebhookSecret,
|
||||
@@ -285,11 +290,11 @@ func (r *githubRepository) createWebhook(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
r.logger.Info("webhook created", "url", cfg.URL)
|
||||
logger.InfoContext(ctx, "webhook created", "url", cfg.URL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *githubRepository) updateWebhook(ctx context.Context, oldRepo *githubRepository) (UndoFunc, error) {
|
||||
func (r *githubRepository) updateWebhook(ctx context.Context, logger *slog.Logger, oldRepo *githubRepository) (UndoFunc, error) {
|
||||
owner := r.config.Spec.GitHub.Owner
|
||||
repoName := r.config.Spec.GitHub.Repository
|
||||
|
||||
@@ -304,30 +309,30 @@ func (r *githubRepository) updateWebhook(ctx context.Context, oldRepo *githubRep
|
||||
switch {
|
||||
case newCfg.WebhookURL != oldCfg.WebhookURL:
|
||||
// In this case we cannot find out out which webhook to update, so we delete the old one and create a new one
|
||||
if err := r.createWebhook(ctx); err != nil {
|
||||
if err := r.createWebhook(ctx, logger); err != nil {
|
||||
return nil, fmt.Errorf("create new webhook: %w", err)
|
||||
}
|
||||
|
||||
undoFunc := UndoFunc(func(ctx context.Context) error {
|
||||
if err := r.deleteWebhook(ctx); err != nil {
|
||||
if err := r.deleteWebhook(ctx, logger); err != nil {
|
||||
return fmt.Errorf("revert create new webhook: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("create new webhook reverted", "url", newCfg.WebhookURL)
|
||||
logger.InfoContext(ctx, "create new webhook reverted", "url", newCfg.WebhookURL)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := oldRepo.deleteWebhook(ctx); err != nil {
|
||||
if err := oldRepo.deleteWebhook(ctx, logger); err != nil {
|
||||
return undoFunc, fmt.Errorf("delete old webhook: %w", err)
|
||||
}
|
||||
|
||||
undoFunc = undoFunc.Chain(ctx, func(ctx context.Context) error {
|
||||
if err := oldRepo.createWebhook(ctx); err != nil {
|
||||
if err := oldRepo.createWebhook(ctx, logger); err != nil {
|
||||
return fmt.Errorf("revert delete old webhook: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("delete old webhook reverted", "url", oldCfg.WebhookURL)
|
||||
logger.InfoContext(ctx, "delete old webhook reverted", "url", oldCfg.WebhookURL)
|
||||
|
||||
return nil
|
||||
})
|
||||
@@ -342,7 +347,7 @@ func (r *githubRepository) updateWebhook(ctx context.Context, oldRepo *githubRep
|
||||
return nil, fmt.Errorf("update webhook secret: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("webhook secret updated", "url", newCfg.WebhookURL)
|
||||
logger.InfoContext(ctx, "webhook secret updated", "url", newCfg.WebhookURL)
|
||||
|
||||
return func(ctx context.Context) error {
|
||||
hook.Secret = oldCfg.WebhookSecret
|
||||
@@ -350,7 +355,7 @@ func (r *githubRepository) updateWebhook(ctx context.Context, oldRepo *githubRep
|
||||
return fmt.Errorf("revert webhook secret: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("webhook secret reverted", "url", oldCfg.WebhookURL)
|
||||
logger.InfoContext(ctx, "webhook secret reverted", "url", oldCfg.WebhookURL)
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
@@ -362,7 +367,7 @@ func (r *githubRepository) updateWebhook(ctx context.Context, oldRepo *githubRep
|
||||
}
|
||||
}
|
||||
|
||||
func (r *githubRepository) deleteWebhook(ctx context.Context) error {
|
||||
func (r *githubRepository) deleteWebhook(ctx context.Context, logger *slog.Logger) error {
|
||||
owner := r.config.Spec.GitHub.Owner
|
||||
name := r.config.Spec.GitHub.Repository
|
||||
|
||||
@@ -379,23 +384,23 @@ func (r *githubRepository) deleteWebhook(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
r.logger.Info("webhook deleted", "url", r.config.Spec.GitHub.WebhookURL)
|
||||
logger.InfoContext(ctx, "webhook deleted", "url", r.config.Spec.GitHub.WebhookURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *githubRepository) AfterCreate(ctx context.Context) error {
|
||||
return r.createWebhook(ctx)
|
||||
func (r *githubRepository) AfterCreate(ctx context.Context, logger *slog.Logger) error {
|
||||
return r.createWebhook(ctx, logger)
|
||||
}
|
||||
|
||||
func (r *githubRepository) BeginUpdate(ctx context.Context, old Repository) (UndoFunc, error) {
|
||||
func (r *githubRepository) BeginUpdate(ctx context.Context, logger *slog.Logger, old Repository) (UndoFunc, error) {
|
||||
oldGitRepo, ok := old.(*githubRepository)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("old repository is not a github repository")
|
||||
}
|
||||
|
||||
return r.updateWebhook(ctx, oldGitRepo)
|
||||
return r.updateWebhook(ctx, logger, oldGitRepo)
|
||||
}
|
||||
|
||||
func (r *githubRepository) AfterDelete(ctx context.Context) error {
|
||||
return r.deleteWebhook(ctx)
|
||||
func (r *githubRepository) AfterDelete(ctx context.Context, logger *slog.Logger) error {
|
||||
return r.deleteWebhook(ctx, logger)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -106,7 +107,7 @@ func (r *localRepository) Validate() (fields field.ErrorList) {
|
||||
}
|
||||
|
||||
// Test implements provisioning.Repository.
|
||||
func (r *localRepository) Test(ctx context.Context) error {
|
||||
func (r *localRepository) Test(ctx context.Context, logger *slog.Logger) error {
|
||||
return &apierrors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "test is not yet implemented",
|
||||
@@ -116,7 +117,7 @@ func (r *localRepository) Test(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// ReadResource implements provisioning.Repository.
|
||||
func (r *localRepository) Read(ctx context.Context, path string, ref string) (*FileInfo, error) {
|
||||
func (r *localRepository) Read(ctx context.Context, logger *slog.Logger, path string, ref string) (*FileInfo, error) {
|
||||
if ref != "" {
|
||||
return nil, apierrors.NewBadRequest("local repository does not support ref")
|
||||
}
|
||||
@@ -150,7 +151,7 @@ func (r *localRepository) Read(ctx context.Context, path string, ref string) (*F
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *localRepository) Create(ctx context.Context, path string, ref string, data []byte, comment string) error {
|
||||
func (r *localRepository) Create(ctx context.Context, logger *slog.Logger, path string, ref string, data []byte, comment string) error {
|
||||
if r.path == "" {
|
||||
return &apierrors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
@@ -170,7 +171,7 @@ func (r *localRepository) Create(ctx context.Context, path string, ref string, d
|
||||
return fmt.Errorf("file already exists")
|
||||
}
|
||||
|
||||
func (r *localRepository) Update(ctx context.Context, path string, ref string, data []byte, comment string) error {
|
||||
func (r *localRepository) Update(ctx context.Context, logger *slog.Logger, path string, ref string, data []byte, comment string) error {
|
||||
if r.path == "" {
|
||||
return &apierrors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
@@ -187,7 +188,7 @@ func (r *localRepository) Update(ctx context.Context, path string, ref string, d
|
||||
return os.WriteFile(path, data, 0600)
|
||||
}
|
||||
|
||||
func (r *localRepository) Delete(ctx context.Context, path string, ref string, comment string) error {
|
||||
func (r *localRepository) Delete(ctx context.Context, logger *slog.Logger, path string, ref string, comment string) error {
|
||||
if r.path == "" {
|
||||
return &apierrors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
@@ -201,19 +202,19 @@ func (r *localRepository) Delete(ctx context.Context, path string, ref string, c
|
||||
}
|
||||
|
||||
// Webhook implements provisioning.Repository.
|
||||
func (r *localRepository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
func (r *localRepository) Webhook(ctx context.Context, logger *slog.Logger, responder rest.Responder) http.HandlerFunc {
|
||||
// webhooks are not supported with local
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *localRepository) AfterCreate(ctx context.Context) error {
|
||||
func (r *localRepository) AfterCreate(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *localRepository) BeginUpdate(ctx context.Context, old Repository) (UndoFunc, error) {
|
||||
func (r *localRepository) BeginUpdate(ctx context.Context, logger *slog.Logger, old Repository) (UndoFunc, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *localRepository) AfterDelete(ctx context.Context) error {
|
||||
func (r *localRepository) AfterDelete(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
@@ -49,28 +50,28 @@ type Repository interface {
|
||||
Validate() field.ErrorList
|
||||
|
||||
// Called to check if all connection information actually works
|
||||
Test(ctx context.Context) error
|
||||
Test(ctx context.Context, logger *slog.Logger) error
|
||||
|
||||
// Read a file from the resource
|
||||
// This data will be parsed and validated before it is shown to end users
|
||||
Read(ctx context.Context, path, ref string) (*FileInfo, error)
|
||||
Read(ctx context.Context, logger *slog.Logger, path, ref string) (*FileInfo, error)
|
||||
|
||||
// Write a file to the repository.
|
||||
// The data has already been validated and is ready for save
|
||||
Create(ctx context.Context, path, ref string, data []byte, comment string) error
|
||||
Create(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error
|
||||
|
||||
// Update a file in the remote repository
|
||||
// The data has already been validated and is ready for save
|
||||
Update(ctx context.Context, path, ref string, data []byte, comment string) error
|
||||
Update(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error
|
||||
|
||||
// Delete a file in the remote repository
|
||||
Delete(ctx context.Context, path, ref, comment string) error
|
||||
Delete(ctx context.Context, logger *slog.Logger, path, ref, comment string) error
|
||||
|
||||
// For repositories that support webhooks
|
||||
Webhook(responder rest.Responder) http.HandlerFunc
|
||||
Webhook(ctx context.Context, logger *slog.Logger, responder rest.Responder) http.HandlerFunc
|
||||
|
||||
// Hooks called after the repository has been created, updated or deleted
|
||||
AfterCreate(ctx context.Context) error
|
||||
BeginUpdate(ctx context.Context, old Repository) (UndoFunc, error)
|
||||
AfterDelete(ctx context.Context) error
|
||||
AfterCreate(ctx context.Context, logger *slog.Logger) error
|
||||
BeginUpdate(ctx context.Context, logger *slog.Logger, old Repository) (UndoFunc, error)
|
||||
AfterDelete(ctx context.Context, logger *slog.Logger) error
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -43,7 +44,7 @@ func (r *s3Repository) Validate() (list field.ErrorList) {
|
||||
}
|
||||
|
||||
// Test implements provisioning.Repository.
|
||||
func (r *s3Repository) Test(ctx context.Context) error {
|
||||
func (r *s3Repository) Test(ctx context.Context, logger *slog.Logger) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "test is not yet implemented",
|
||||
@@ -53,7 +54,7 @@ func (r *s3Repository) Test(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// ReadResource implements provisioning.Repository.
|
||||
func (r *s3Repository) Read(ctx context.Context, path string, ref string) (*FileInfo, error) {
|
||||
func (r *s3Repository) Read(ctx context.Context, logger *slog.Logger, path string, ref string) (*FileInfo, error) {
|
||||
return nil, &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "read resource is not yet implemented",
|
||||
@@ -62,7 +63,7 @@ func (r *s3Repository) Read(ctx context.Context, path string, ref string) (*File
|
||||
}
|
||||
}
|
||||
|
||||
func (r *s3Repository) Create(ctx context.Context, path string, ref string, data []byte, comment string) error {
|
||||
func (r *s3Repository) Create(ctx context.Context, logger *slog.Logger, path string, ref string, data []byte, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "write file is not yet implemented",
|
||||
@@ -71,7 +72,7 @@ func (r *s3Repository) Create(ctx context.Context, path string, ref string, data
|
||||
}
|
||||
}
|
||||
|
||||
func (r *s3Repository) Update(ctx context.Context, path string, ref string, data []byte, comment string) error {
|
||||
func (r *s3Repository) Update(ctx context.Context, logger *slog.Logger, path string, ref string, data []byte, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "write file is not yet implemented",
|
||||
@@ -80,7 +81,7 @@ func (r *s3Repository) Update(ctx context.Context, path string, ref string, data
|
||||
}
|
||||
}
|
||||
|
||||
func (r *s3Repository) Delete(ctx context.Context, path string, ref string, comment string) error {
|
||||
func (r *s3Repository) Delete(ctx context.Context, logger *slog.Logger, path string, ref string, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "delete file not yet implemented",
|
||||
@@ -90,19 +91,19 @@ func (r *s3Repository) Delete(ctx context.Context, path string, ref string, comm
|
||||
}
|
||||
|
||||
// Webhook implements provisioning.Repository.
|
||||
func (r *s3Repository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
func (r *s3Repository) Webhook(ctx context.Context, logger *slog.Logger, responder rest.Responder) http.HandlerFunc {
|
||||
// webhooks are not supported with local
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *s3Repository) AfterCreate(ctx context.Context) error {
|
||||
func (r *s3Repository) AfterCreate(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *s3Repository) BeginUpdate(ctx context.Context, old Repository) (UndoFunc, error) {
|
||||
func (r *s3Repository) BeginUpdate(ctx context.Context, logger *slog.Logger, old Repository) (UndoFunc, error) {
|
||||
return func(ctx context.Context) error { return nil }, nil
|
||||
}
|
||||
|
||||
func (r *s3Repository) AfterDelete(ctx context.Context) error {
|
||||
func (r *s3Repository) AfterDelete(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
@@ -38,7 +39,7 @@ func (r *unknownRepository) Validate() (fields field.ErrorList) {
|
||||
}
|
||||
|
||||
// Test implements provisioning.Repository.
|
||||
func (r *unknownRepository) Test(ctx context.Context) error {
|
||||
func (r *unknownRepository) Test(ctx context.Context, logger *slog.Logger) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "test is not yet implemented",
|
||||
@@ -48,7 +49,7 @@ func (r *unknownRepository) Test(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// ReadResource implements provisioning.Repository.
|
||||
func (r *unknownRepository) Read(ctx context.Context, path, ref string) (*FileInfo, error) {
|
||||
func (r *unknownRepository) Read(ctx context.Context, logger *slog.Logger, path, ref string) (*FileInfo, error) {
|
||||
return nil, &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "read resource is not yet implemented",
|
||||
@@ -57,7 +58,7 @@ func (r *unknownRepository) Read(ctx context.Context, path, ref string) (*FileIn
|
||||
}
|
||||
}
|
||||
|
||||
func (r *unknownRepository) Create(ctx context.Context, path, ref string, data []byte, comment string) error {
|
||||
func (r *unknownRepository) Create(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "write file is not yet implemented",
|
||||
@@ -66,7 +67,7 @@ func (r *unknownRepository) Create(ctx context.Context, path, ref string, data [
|
||||
}
|
||||
}
|
||||
|
||||
func (r *unknownRepository) Update(ctx context.Context, path, ref string, data []byte, comment string) error {
|
||||
func (r *unknownRepository) Update(ctx context.Context, logger *slog.Logger, path, ref string, data []byte, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "write file is not yet implemented",
|
||||
@@ -75,7 +76,7 @@ func (r *unknownRepository) Update(ctx context.Context, path, ref string, data [
|
||||
}
|
||||
}
|
||||
|
||||
func (r *unknownRepository) Delete(ctx context.Context, path, ref, comment string) error {
|
||||
func (r *unknownRepository) Delete(ctx context.Context, logger *slog.Logger, path, ref, comment string) error {
|
||||
return &errors.StatusError{
|
||||
ErrStatus: metav1.Status{
|
||||
Message: "delete file not yet implemented",
|
||||
@@ -85,19 +86,19 @@ func (r *unknownRepository) Delete(ctx context.Context, path, ref, comment strin
|
||||
}
|
||||
|
||||
// Webhook implements provisioning.Repository.
|
||||
func (r *unknownRepository) Webhook(responder rest.Responder) http.HandlerFunc {
|
||||
func (r *unknownRepository) Webhook(ctx context.Context, logger *slog.Logger, responder rest.Responder) http.HandlerFunc {
|
||||
// webhooks are not supported with local
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *unknownRepository) AfterCreate(ctx context.Context) error {
|
||||
func (r *unknownRepository) AfterCreate(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *unknownRepository) BeginUpdate(ctx context.Context, old Repository) (UndoFunc, error) {
|
||||
func (r *unknownRepository) BeginUpdate(ctx context.Context, logger *slog.Logger, old Repository) (UndoFunc, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *unknownRepository) AfterDelete(ctx context.Context) error {
|
||||
func (r *unknownRepository) AfterDelete(ctx context.Context, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package provisioning
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
type webhookConnector struct {
|
||||
client auth.BackgroundIdentityService
|
||||
getter RepoGetter
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (*webhookConnector) New() runtime.Object {
|
||||
@@ -69,7 +71,7 @@ func (s *webhookConnector) Connect(ctx context.Context, name string, opts runtim
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhook := repo.Webhook(responder)
|
||||
webhook := repo.Webhook(ctx, s.logger, responder)
|
||||
if webhook == nil {
|
||||
return nil, &errors.StatusError{
|
||||
ErrStatus: v1.Status{
|
||||
|
||||
Reference in new issue
Block a user