Provisioning: Manage repo jobs as single sub-resource (#103090)

This commit is contained in:
Ryan McKinley
2025-04-01 13:22:47 +03:00
committed by GitHub
parent 1ead18d856
commit ca35a89916
25 changed files with 691 additions and 1177 deletions
+3 -2
View File
@@ -81,10 +81,11 @@ func (j JobState) Finished() bool {
}
type JobSpec struct {
Action JobAction `json:"action"`
Action JobAction `json:"action,omitempty"`
// The the repository reference (for now also in labels)
Repository string `json:"repository"`
// This value is required, but will be popuplated from the job making the request
Repository string `json:"repository,omitempty"`
// Pull request options
PullRequest *PullRequestJobOptions `json:"pr,omitempty"`
@@ -725,7 +725,6 @@ func schema_pkg_apis_provisioning_v0alpha1_JobSpec(ref common.ReferenceCallback)
"action": {
SchemaProps: spec.SchemaProps{
Description: "Possible enum values:\n - `\"migrate\"` acts like JobActionExport, then JobActionPull. It also tries to preserve the history.\n - `\"pr\"` adds additional useful information to a PR, such as comments with preview links and rendered images.\n - `\"pull\"` replicates the remote branch in the local copy of the repository.\n - `\"push\"` replicates the local copy of the repository in the remote branch.",
Default: "",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"migrate", "pr", "pull", "push"},
@@ -733,8 +732,7 @@ func schema_pkg_apis_provisioning_v0alpha1_JobSpec(ref common.ReferenceCallback)
},
"repository": {
SchemaProps: spec.SchemaProps{
Description: "The the repository reference (for now also in labels)",
Default: "",
Description: "The the repository reference (for now also in labels) This value is required, but will be popuplated from the job making the request",
Type: []string{"string"},
Format: "",
},
@@ -764,7 +762,6 @@ func schema_pkg_apis_provisioning_v0alpha1_JobSpec(ref common.ReferenceCallback)
},
},
},
Required: []string{"action", "repository"},
},
},
Dependencies: []string{
@@ -361,15 +361,10 @@ func (rc *RepositoryController) determineSyncStrategy(ctx context.Context, obj *
}
func (rc *RepositoryController) addSyncJob(ctx context.Context, obj *provisioning.Repository, syncOptions *provisioning.SyncJobOptions) error {
job, err := rc.jobs.Insert(ctx, &provisioning.Job{
ObjectMeta: v1.ObjectMeta{
Namespace: obj.Namespace,
},
Spec: provisioning.JobSpec{
Repository: obj.GetName(),
Action: provisioning.JobActionPull,
Pull: syncOptions,
},
job, err := rc.jobs.Insert(ctx, obj.Namespace, provisioning.JobSpec{
Repository: obj.GetName(),
Action: provisioning.JobActionPull,
Pull: syncOptions,
})
if apierrors.IsAlreadyExists(err) {
logging.FromContext(ctx).Info("sync job already exists, nothing triggered")
-83
View File
@@ -1,83 +0,0 @@
package provisioning
import (
"context"
"net/http"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
)
type exportConnector struct {
repoGetter RepoGetter
jobs jobs.Queue
}
func (*exportConnector) New() runtime.Object {
return &provisioning.Job{}
}
func (*exportConnector) Destroy() {}
func (*exportConnector) ProducesMIMETypes(verb string) []string {
return []string{"application/json"}
}
func (c *exportConnector) ProducesObject(verb string) any {
return c.New()
}
func (*exportConnector) ConnectMethods() []string {
return []string{http.MethodPost}
}
func (*exportConnector) NewConnectOptions() (runtime.Object, bool, string) {
return nil, false, ""
}
func (c *exportConnector) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
repo, err := c.repoGetter.GetRepository(ctx, name)
if err != nil {
return nil, err
}
cfg := repo.Config()
if err := repository.IsWriteAllowed(cfg, ""); err != nil {
return nil, err
}
return withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
options := &provisioning.ExportJobOptions{}
if err := unmarshalJSON(r, defaultMaxBodySize, options); err != nil {
responder.Error(apierrors.NewBadRequest(err.Error()))
return
}
job, err := c.jobs.Insert(ctx, &provisioning.Job{
ObjectMeta: v1.ObjectMeta{
Namespace: cfg.Namespace,
},
Spec: provisioning.JobSpec{
Action: provisioning.JobActionPush,
Repository: cfg.Name,
Push: options,
},
})
if err != nil {
responder.Error(err)
} else {
responder.Object(http.StatusAccepted, job)
}
}), 30*time.Second), nil
}
var (
_ rest.Connecter = (*exportConnector)(nil)
_ rest.Storage = (*exportConnector)(nil)
_ rest.StorageMetadata = (*exportConnector)(nil)
)
+110
View File
@@ -0,0 +1,110 @@
package provisioning
import (
"context"
"fmt"
"net/http"
"strings"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs"
)
type jobsConnector struct {
repoGetter RepoGetter
jobs jobs.Queue
historic jobs.History
}
func (*jobsConnector) New() runtime.Object {
return &provisioning.Repository{}
}
func (*jobsConnector) Destroy() {}
func (*jobsConnector) ProducesMIMETypes(verb string) []string {
return []string{"application/json"}
}
func (c *jobsConnector) ProducesObject(verb string) any {
return &provisioning.Job{}
}
func (*jobsConnector) ConnectMethods() []string {
return []string{http.MethodPost, http.MethodGet}
}
func (*jobsConnector) NewConnectOptions() (runtime.Object, bool, string) {
return nil, true, "" // path -> uid
}
func (c *jobsConnector) Connect(
ctx context.Context,
name string,
opts runtime.Object,
responder rest.Responder,
) (http.Handler, error) {
repo, err := c.repoGetter.GetHealthyRepository(ctx, name)
if err != nil {
return nil, err
}
cfg := repo.Config()
return withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx = r.Context()
prefix := fmt.Sprintf("/%s/jobs/", name)
idx := strings.Index(r.URL.Path, prefix)
if r.Method == http.MethodGet {
if idx > 0 {
jobUID := r.URL.Path[idx+len(prefix):]
if !validBlobID(jobUID) {
responder.Error(apierrors.NewBadRequest(fmt.Sprintf("invalid job uid: %s", jobUID)))
return
}
job, err := c.historic.GetJob(ctx, cfg.Namespace, name, jobUID)
if err != nil {
responder.Error(err)
return
}
responder.Object(http.StatusOK, job)
return
}
recent, err := c.historic.RecentJobs(ctx, cfg.Namespace, name)
if err != nil {
responder.Error(err)
return
}
responder.Object(http.StatusOK, recent)
return
}
if idx > 0 {
responder.Error(apierrors.NewBadRequest("can not post to a job UID"))
return
}
spec := provisioning.JobSpec{}
if err := unmarshalJSON(r, defaultMaxBodySize, &spec); err != nil {
responder.Error(apierrors.NewBadRequest("error decoding provisioning.Job from request"))
return
}
spec.Repository = name
job, err := c.jobs.Insert(ctx, cfg.Namespace, spec)
if err != nil {
responder.Error(err)
return
}
responder.Object(http.StatusAccepted, job)
}), 30*time.Second), nil
}
var (
_ rest.Connecter = (*jobsConnector)(nil)
_ rest.Storage = (*jobsConnector)(nil)
_ rest.StorageMetadata = (*jobsConnector)(nil)
)
@@ -0,0 +1,125 @@
package jobs
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
)
// History keeps track of completed jobs
type History interface {
// Adds a job to the history
WriteJob(ctx context.Context, job *provisioning.Job) error
// Gets recent jobs for a repository
RecentJobs(ctx context.Context, namespace, repo string) (*provisioning.JobList, error)
// find a specific job from the original UID
GetJob(ctx context.Context, namespace, repo, uid string) (*provisioning.Job, error)
}
// NewStorageBackedHistory creates a History client backed by unified storage
// This should be replaced by loki when running in cloud
func NewStorageBackedHistory(store rest.Storage) (History, error) {
var ok bool
history := &storageBackedHistory{}
history.creator, ok = store.(rest.Creater)
if !ok {
return nil, fmt.Errorf("storage does not implement rest.Creater")
}
history.lister, ok = store.(rest.Lister)
if !ok {
return nil, fmt.Errorf("storage does not implement rest.Lister")
}
return history, nil
}
type storageBackedHistory struct {
creator rest.Creater
lister rest.Lister
}
// Write implements History.
func (s *storageBackedHistory) WriteJob(ctx context.Context, job *provisioning.Job) error {
if job.UID == "" {
return fmt.Errorf("missing UID in job '%s'", job.GetName())
}
if job.Labels == nil {
job.Labels = make(map[string]string)
}
job.Labels[LabelRepository] = job.Spec.Repository
job.Labels[LabelJobOriginalUID] = string(job.UID)
// Generate a new name based on the input job
job.GenerateName = job.Name + "-"
job.Name = ""
// We also reset the UID as this is not the same object.
job.ObjectMeta.UID = ""
// We aren't allowed to write with ResourceVersion set.
job.ResourceVersion = ""
_, err := s.creator.Create(ctx, &provisioning.HistoricJob{
ObjectMeta: job.ObjectMeta,
Spec: job.Spec,
Status: job.Status,
}, nil, &metav1.CreateOptions{})
return err
}
func (s *storageBackedHistory) getJobs(ctx context.Context, namespace string, labels labels.Set) (*provisioning.JobList, error) {
ctx = request.WithNamespace(ctx, namespace)
obj, err := s.lister.List(ctx, &internalversion.ListOptions{
LabelSelector: labels.AsSelector(),
})
if err != nil {
return nil, err
}
historic, ok := obj.(*provisioning.HistoricJobList)
if !ok {
return nil, fmt.Errorf("expected HistoricJobList, found %T", historic)
}
jobs := &provisioning.JobList{
ListMeta: historic.ListMeta,
}
for _, job := range historic.Items {
jobs.Items = append(jobs.Items, provisioning.Job{
ObjectMeta: job.ObjectMeta,
Spec: job.Spec,
Status: job.Status,
})
}
return jobs, nil
}
// Recent implements History.
func (s *storageBackedHistory) RecentJobs(ctx context.Context, namespace, repo string) (*provisioning.JobList, error) {
return s.getJobs(ctx, namespace, labels.Set{
LabelRepository: repo,
})
}
// GetJob implements History.
func (s *storageBackedHistory) GetJob(ctx context.Context, namespace, repo, job string) (*provisioning.Job, error) {
jobs, err := s.getJobs(ctx, namespace, labels.Set{
LabelJobOriginalUID: job,
})
if err != nil {
return nil, err
}
if len(jobs.Items) == 1 {
return &jobs.Items[0], nil
}
return nil, apierrors.NewNotFound(provisioning.JobResourceInfo.GroupResource(), job)
}
@@ -8,10 +8,6 @@ import (
"strconv"
"time"
"github.com/grafana/grafana-app-sdk/logging"
"github.com/grafana/grafana/pkg/apimachinery/identity"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/apifmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -20,6 +16,11 @@ import (
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"github.com/grafana/grafana-app-sdk/logging"
"github.com/grafana/grafana/pkg/apimachinery/identity"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/apifmt"
)
const (
@@ -27,8 +28,8 @@ const (
// The label must be formatted as milliseconds from Epoch. This grants a natural ordering, allowing for less-than operators in label selectors.
// The natural ordering would be broken if the number rolls over into 1 more digit. This won't happen before Nov, 2286.
LabelJobClaim = "provisioning.grafana.app/claim"
// LabelJobOriginalName contains the Job's name as a label. This allows for label selectors to find the archived version of a job.
LabelJobOriginalName = "provisioning.grafana.app/original-name"
// LabelJobOriginalUID contains the Job's original uid as a label. This allows for label selectors to find the archived version of a job.
LabelJobOriginalUID = "provisioning.grafana.app/original-uid"
// LabelRepository contains the repository name as a label. This allows for label selectors to find the archived version of a job.
LabelRepository = "provisioning.grafana.app/repository"
)
@@ -60,7 +61,7 @@ type Queue interface {
// The job name is not honoured. It will be overwritten with a name that fits the job.
//
// This saves it if it is a new job, or fails with `apierrors.IsAlreadyExists(err) == true` if one already exists.
Insert(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error)
Insert(ctx context.Context, namespace string, spec provisioning.JobSpec) (*provisioning.Job, error)
}
var _ Queue = (*persistentStore)(nil)
@@ -77,8 +78,8 @@ type jobStorage interface {
// When persistentStore claims a job, it will update the status of it. This does a ResourceVersion check to ensure it is atomic; if the job has been claimed by another worker, the claim will fail.
// When a job is completed, it is moved to the historic job store by first deleting it from the job store and then creating it in the historic job store. We are fine with the job being lost if the historic job store fails to create it.
type persistentStore struct {
jobStore jobStorage
historicJobStore rest.Creater
jobStore jobStorage
historicJobs History
// clock is a function that returns the current time.
clock func() time.Time
@@ -94,7 +95,7 @@ type persistentStore struct {
func NewStore(
jobStore jobStorage,
historicJobStore rest.Creater,
historicJobs History,
expiry time.Duration,
) (*persistentStore, error) {
if expiry <= 0 {
@@ -102,8 +103,8 @@ func NewStore(
}
return &persistentStore{
jobStore: jobStore,
historicJobStore: historicJobStore,
jobStore: jobStore,
historicJobs: historicJobs,
clock: time.Now,
expiry: expiry,
@@ -273,27 +274,13 @@ func (s *persistentStore) Complete(ctx context.Context, job *provisioning.Job) e
job.Labels = make(map[string]string)
}
delete(job.Labels, LabelJobClaim)
// We also need a new, unique name.
job.Labels[LabelJobOriginalName] = job.GetName()
job.Labels[LabelRepository] = job.Spec.Repository
job.GenerateName = job.Name + "-"
job.Name = ""
// We also reset the UID as this is not the same object.
job.ObjectMeta.UID = ""
// We aren't allowed to write with ResourceVersion set.
job.ResourceVersion = ""
historicJob := &provisioning.HistoricJob{
ObjectMeta: job.ObjectMeta,
Spec: job.Spec,
Status: job.Status,
}
_, err = s.historicJobStore.Create(ctx, historicJob, nil, &metav1.CreateOptions{})
err = s.historicJobs.WriteJob(ctx, job)
if err != nil {
// We're not going to return this as it is not critical. Not ideal, but not critical.
logger.Warn("failed to create historic job", "historic_job", *historicJob, "error", err)
logger.Warn("failed to create historic job", "historic_job", *job, "error", err)
} else {
logger.Debug("created historic job", "historic_job", *historicJob)
logger.Debug("created historic job", "historic_job", *job)
}
logger.Debug("job completion done")
@@ -376,7 +363,23 @@ func (s *persistentStore) cleanupClaims(ctx context.Context) error {
return nil
}
func (s *persistentStore) Insert(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error) {
func (s *persistentStore) Insert(ctx context.Context, namespace string, spec provisioning.JobSpec) (*provisioning.Job, error) {
if spec.Repository == "" {
return nil, errors.New("missing repository in job")
}
job := &provisioning.Job{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Labels: map[string]string{
LabelRepository: spec.Repository,
},
},
Spec: spec,
}
if err := mutateJobAction(job); err != nil {
return nil, err
}
s.generateJobName(job) // Side-effect: updates the job's name.
ctx = request.WithNamespace(ctx, job.GetNamespace())
@@ -423,3 +426,28 @@ func (s *persistentStore) generateJobName(job *provisioning.Job) {
job.Name = fmt.Sprintf("%s-%s", job.Spec.Repository, job.Spec.Action)
}
}
func mutateJobAction(job *provisioning.Job) error {
kinds := map[provisioning.JobAction]any{}
spec := job.Spec
if spec.Migrate != nil {
job.Spec.Action = provisioning.JobActionMigrate
kinds[provisioning.JobActionMigrate] = spec.Migrate
}
if spec.Pull != nil {
job.Spec.Action = provisioning.JobActionPull
kinds[provisioning.JobActionPull] = spec.Pull
}
if spec.Push != nil {
job.Spec.Action = provisioning.JobActionPush
kinds[provisioning.JobActionPush] = spec.Push
}
if spec.PullRequest != nil {
job.Spec.Action = provisioning.JobActionPullRequest
kinds[provisioning.JobActionPullRequest] = spec.PullRequest
}
if len(kinds) > 1 {
return apierrors.NewBadRequest("multiple job types found")
}
return nil
}
-89
View File
@@ -1,89 +0,0 @@
package provisioning
import (
"context"
"net/http"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs"
"github.com/grafana/grafana/pkg/storage/legacysql/dualwrite"
)
// TODO: should we have merge migrate and sync connectors and have a single repository job connector?
type migrateConnector struct {
dual dualwrite.Service
repoGetter RepoGetter
jobs jobs.Queue
}
func (*migrateConnector) New() runtime.Object {
return &provisioning.Job{}
}
func (*migrateConnector) Destroy() {}
func (*migrateConnector) ProducesMIMETypes(verb string) []string {
return []string{"application/json"}
}
func (c *migrateConnector) ProducesObject(verb string) any {
return c.New()
}
func (*migrateConnector) ConnectMethods() []string {
return []string{http.MethodPost}
}
func (*migrateConnector) NewConnectOptions() (runtime.Object, bool, string) {
return nil, false, ""
}
func (c *migrateConnector) Connect(
ctx context.Context,
name string,
opts runtime.Object,
responder rest.Responder,
) (http.Handler, error) {
repo, err := c.repoGetter.GetHealthyRepository(ctx, name)
if err != nil {
return nil, err
}
cfg := repo.Config()
return withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var options provisioning.MigrateJobOptions
if err := unmarshalJSON(r, defaultMaxBodySize, &options); err != nil {
responder.Error(apierrors.NewBadRequest("error decoding MigrateJobOptions from request"))
return
}
job, err := c.jobs.Insert(ctx, &provisioning.Job{
ObjectMeta: v1.ObjectMeta{
Namespace: cfg.Namespace,
},
Spec: provisioning.JobSpec{
Action: provisioning.JobActionMigrate,
Repository: cfg.Name,
Migrate: &options,
},
})
if err != nil {
responder.Error(err)
return
}
responder.Object(http.StatusAccepted, job)
}), 30*time.Second), nil
}
var (
_ rest.Connecter = (*migrateConnector)(nil)
_ rest.Storage = (*migrateConnector)(nil)
_ rest.StorageMetadata = (*migrateConnector)(nil)
)
+96 -56
View File
@@ -335,15 +335,20 @@ func (b *APIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupI
return fmt.Errorf("failed to create historic job storage: %w", err)
}
b.jobs, err = jobs.NewStore(realJobStore, historicJobStore, time.Second*30)
jobHistory, err := jobs.NewStorageBackedHistory(historicJobStore)
if err != nil {
return fmt.Errorf("failed to create historic job wrapper: %w", err)
}
b.jobs, err = jobs.NewStore(realJobStore, jobHistory, time.Second*30)
if err != nil {
return fmt.Errorf("failed to create job store: %w", err)
}
storage := map[string]rest.Storage{}
// Although we never interact with these resources via the API, we want them to be readable from the API.
// Although we never interact with jobs via the API, we want them to be readable (watchable!) from the API.
storage[provisioning.JobResourceInfo.StoragePath()] = readonly.Wrap(realJobStore)
storage[provisioning.HistoricJobResourceInfo.StoragePath()] = readonly.Wrap(historicJobStore)
storage[provisioning.RepositoryResourceInfo.StoragePath()] = repositoryStorage
storage[provisioning.RepositoryResourceInfo.StoragePath("status")] = repositoryStatusStorage
@@ -369,18 +374,10 @@ func (b *APIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupI
storage[provisioning.RepositoryResourceInfo.StoragePath("history")] = &historySubresource{
repoGetter: b,
}
storage[provisioning.RepositoryResourceInfo.StoragePath("sync")] = &syncConnector{
storage[provisioning.RepositoryResourceInfo.StoragePath("jobs")] = &jobsConnector{
repoGetter: b,
jobs: b.jobs,
}
storage[provisioning.RepositoryResourceInfo.StoragePath("export")] = &exportConnector{
repoGetter: b,
jobs: b.jobs,
}
storage[provisioning.RepositoryResourceInfo.StoragePath("migrate")] = &migrateConnector{
repoGetter: b,
jobs: b.jobs,
dual: b.storageStatus,
historic: jobHistory,
}
storage[provisioning.RepositoryResourceInfo.StoragePath("render")] = &renderConnector{
blob: b.unified,
@@ -628,6 +625,7 @@ func (b *APIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, err
defs := b.GetOpenAPIDefinitions()(func(path string) spec.Ref { return spec.Ref{} })
defsBase := "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1."
refsBase := "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1."
sub := oas.Paths.Paths[repoprefix+"/test"]
if sub != nil {
@@ -804,66 +802,71 @@ spec:
sub.Put.RequestBody = sub.Post.RequestBody
}
sub = oas.Paths.Paths[repoprefix+"/sync"]
sub = oas.Paths.Paths[repoprefix+"/jobs"]
if sub != nil {
optionsSchema := defs[defsBase+"SyncJobOptions"].Schema
sub.Post.Description = "Sync from repository into Grafana"
sub.Post.Description = "Register a job for this repository"
sub.Post.Responses = getJSONResponse("#/components/schemas/" + refsBase + "Job")
sub.Post.RequestBody = &spec3.RequestBody{
RequestBodyProps: spec3.RequestBodyProps{
Content: map[string]*spec3.MediaType{
"application/json": {
MediaTypeProps: spec3.MediaTypeProps{
Schema: &optionsSchema,
Example: &provisioning.SyncJobOptions{
Incremental: false,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/components/schemas/" + refsBase + "JobSpec"),
},
},
Examples: map[string]*spec3.Example{
"incremental": {
ExampleProps: spec3.ExampleProps{
Summary: "Pull (incremental)",
Description: "look for changes since the last sync",
Value: provisioning.JobSpec{
Pull: &provisioning.SyncJobOptions{
Incremental: true,
},
},
},
},
"pull": {
ExampleProps: spec3.ExampleProps{
Summary: "Pull from repository",
Description: "pull all files",
Value: provisioning.JobSpec{
Pull: &provisioning.SyncJobOptions{
Incremental: false,
},
},
},
},
},
},
},
},
},
}
sub.Get.Description = "List recent jobs"
sub.Get.Responses = getJSONResponse("#/components/schemas/" + refsBase + "JobList")
}
sub = oas.Paths.Paths[repoprefix+"/export"]
sub = oas.Paths.Paths[repoprefix+"/jobs/{path}"]
if sub != nil {
optionsSchema := defs[defsBase+"ExportJobOptions"].Schema
sub.Post.Description = "Export from grafana into the remote repository"
sub.Post.RequestBody = &spec3.RequestBody{
RequestBodyProps: spec3.RequestBodyProps{
Content: map[string]*spec3.MediaType{
"application/json": {
MediaTypeProps: spec3.MediaTypeProps{
Schema: &optionsSchema,
Example: &provisioning.ExportJobOptions{
Folder: "grafan-folder-ref",
Branch: "target-branch",
Path: "path/in/tree",
},
},
},
},
},
}
}
sub.Post = nil
sub.Get.Description = "Get job by UID"
sub.Get.Responses = getJSONResponse("#/components/schemas/" + refsBase + "Job")
sub = oas.Paths.Paths[repoprefix+"/migrate"]
if sub != nil {
optionsSchema := defs[defsBase+"MigrateJobOptions"].Schema
sub.Post.Description = "Export from grafana into the remote repository"
sub.Post.RequestBody = &spec3.RequestBody{
RequestBodyProps: spec3.RequestBodyProps{
Content: map[string]*spec3.MediaType{
"application/json": {
MediaTypeProps: spec3.MediaTypeProps{
Schema: &optionsSchema,
Example: &provisioning.MigrateJobOptions{
History: true,
},
},
},
},
},
// Replace {path} with {uid} (it is a UID query, but all k8s sub-resources are called path)
for _, v := range sub.Parameters {
if v.Name == "path" {
v.Name = "uid"
v.Description = "Original Job UID"
break
}
}
delete(oas.Paths.Paths, repoprefix+"/jobs/{path}")
oas.Paths.Paths[repoprefix+"/jobs/{uid}"] = sub
}
delete(oas.Paths.Paths, repoprefix+"/render")
@@ -884,6 +887,18 @@ spec:
},
},
}
// Replace {path} with {guid} (it is a GUID, but all k8s sub-resources are called path)
for _, v := range sub.Parameters {
if v.Name == "path" {
v.Name = "guid"
v.Description = "Image GUID"
break
}
}
delete(oas.Paths.Paths, repoprefix+"/render/{path}")
oas.Paths.Paths[repoprefix+"/render/{guid}"] = sub
}
// Add any missing definitions
@@ -1124,3 +1139,28 @@ func (b *APIBuilder) AsRepository(ctx context.Context, r *provisioning.Repositor
return nil, fmt.Errorf("unknown repository type (%s)", r.Spec.Type)
}
}
func getJSONResponse(ref string) *spec3.Responses {
return &spec3.Responses{
ResponsesProps: spec3.ResponsesProps{
StatusCodeResponses: map[int]*spec3.Response{
200: {
ResponseProps: spec3.ResponseProps{
Content: map[string]*spec3.MediaType{
"application/json": {
MediaTypeProps: spec3.MediaTypeProps{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef(ref),
},
},
},
},
},
Description: "OK",
},
},
},
},
}
}
-87
View File
@@ -1,87 +0,0 @@
package provisioning
import (
"context"
"net/http"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs"
)
// TODO: should we have merge migrate and sync connectors and have a single repository job connector?
type syncConnector struct {
repoGetter RepoGetter
jobs jobs.Queue
}
func (*syncConnector) New() runtime.Object {
return &provisioning.Job{}
}
func (*syncConnector) Destroy() {}
func (*syncConnector) ProducesMIMETypes(verb string) []string {
return []string{"application/json"}
}
func (c *syncConnector) ProducesObject(verb string) any {
return c.New()
}
func (*syncConnector) ConnectMethods() []string {
return []string{http.MethodPost}
}
func (*syncConnector) NewConnectOptions() (runtime.Object, bool, string) {
return nil, false, ""
}
func (c *syncConnector) Connect(
ctx context.Context,
name string,
opts runtime.Object,
responder rest.Responder,
) (http.Handler, error) {
repo, err := c.repoGetter.GetHealthyRepository(ctx, name)
if err != nil {
return nil, err
}
cfg := repo.Config()
return withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var options provisioning.SyncJobOptions
if err := unmarshalJSON(r, defaultMaxBodySize, &options); err != nil {
responder.Error(apierrors.NewBadRequest("error decoding SyncJobOptions from request"))
return
}
job, err := c.jobs.Insert(ctx, &provisioning.Job{
ObjectMeta: v1.ObjectMeta{
Namespace: cfg.Namespace,
},
Spec: provisioning.JobSpec{
Action: provisioning.JobActionPull,
Repository: cfg.Name,
Pull: &options,
},
})
if err != nil {
responder.Error(err)
return
}
responder.Object(http.StatusAccepted, job)
}), 30*time.Second), nil
}
var (
_ rest.Connecter = (*syncConnector)(nil)
_ rest.Storage = (*syncConnector)(nil)
_ rest.StorageMetadata = (*syncConnector)(nil)
)
+2 -12
View File
@@ -7,7 +7,6 @@ import (
"time"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
@@ -95,17 +94,8 @@ func (s *webhookConnector) Connect(ctx context.Context, name string, opts runtim
return
}
if rsp.Job != nil {
// Add the job to the job queue
job := &provisioning.Job{
ObjectMeta: v1.ObjectMeta{
Namespace: namespace,
Labels: map[string]string{
"repository": name,
},
},
Spec: *rsp.Job,
}
job, err := s.jobs.Insert(ctx, job)
rsp.Job.Repository = name
job, err := s.jobs.Insert(ctx, namespace, *rsp.Job)
if err != nil {
responder.Error(err)
return
@@ -36,232 +36,6 @@
}
}
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/historicjobs": {
"get": {
"tags": [
"HistoricJob"
],
"description": "list or watch objects of kind HistoricJob",
"operationId": "listHistoricJob",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList"
}
},
"application/json;stream=watch": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList"
}
},
"application/vnd.kubernetes.protobuf": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList"
}
},
"application/vnd.kubernetes.protobuf;stream=watch": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList"
}
},
"application/yaml": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList"
}
}
}
}
},
"x-kubernetes-action": "list",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "HistoricJob"
}
},
"parameters": [
{
"name": "allowWatchBookmarks",
"in": "query",
"description": "allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored.",
"schema": {
"type": "boolean",
"uniqueItems": true
}
},
{
"name": "continue",
"in": "query",
"description": "The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".\n\nThis field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "fieldSelector",
"in": "query",
"description": "A selector to restrict the list of returned objects by their fields. Defaults to everything.",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "labelSelector",
"in": "query",
"description": "A selector to restrict the list of returned objects by their labels. Defaults to everything.",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "limit",
"in": "query",
"description": "limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.\n\nThe server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.",
"schema": {
"type": "integer",
"uniqueItems": true
}
},
{
"name": "namespace",
"in": "path",
"description": "object name and auth scope, such as for teams and projects",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "pretty",
"in": "query",
"description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "resourceVersion",
"in": "query",
"description": "resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "resourceVersionMatch",
"in": "query",
"description": "resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "sendInitialEvents",
"in": "query",
"description": "`sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic \"Bookmark\" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `\"k8s.io/initial-events-end\": \"true\"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.\n\nWhen `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan\n is interpreted as \"data at least as new as the provided `resourceVersion`\"\n and the bookmark event is send when the state is synced\n to a `resourceVersion` at least as fresh as the one provided by the ListOptions.\n If `resourceVersion` is unset, this is interpreted as \"consistent read\" and the\n bookmark event is send when the state is synced at least to the moment\n when request started being processed.\n- `resourceVersionMatch` set to any other value or unset\n Invalid error is returned.\n\nDefaults to true if `resourceVersion=\"\"` or `resourceVersion=\"0\"` (for backward compatibility reasons) and to false otherwise.",
"schema": {
"type": "boolean",
"uniqueItems": true
}
},
{
"name": "timeoutSeconds",
"in": "query",
"description": "Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.",
"schema": {
"type": "integer",
"uniqueItems": true
}
},
{
"name": "watch",
"in": "query",
"description": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.",
"schema": {
"type": "boolean",
"uniqueItems": true
}
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/historicjobs/{name}": {
"get": {
"tags": [
"HistoricJob"
],
"description": "read the specified HistoricJob",
"operationId": "getHistoricJob",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJob"
}
},
"application/vnd.kubernetes.protobuf": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJob"
}
},
"application/yaml": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJob"
}
}
}
}
},
"x-kubernetes-action": "get",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "HistoricJob"
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "name of the HistoricJob",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "namespace",
"in": "path",
"description": "object name and auth scope, such as for teams and projects",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "pretty",
"in": "query",
"description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).",
"schema": {
"type": "string",
"uniqueItems": true
}
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/jobs": {
"get": {
"tags": [
@@ -1318,92 +1092,6 @@
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/export": {
"post": {
"tags": [
"Repository"
],
"description": "Export from grafana into the remote repository",
"operationId": "createRepositoryExport",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"identifier"
],
"properties": {
"branch": {
"description": "Target branch for export (only git)",
"type": "string"
},
"folder": {
"description": "The source folder (or empty) to export",
"type": "string"
},
"identifier": {
"description": "Include the identifier in the exported metadata",
"type": "boolean",
"default": false
},
"path": {
"description": "Prefix in target file system",
"type": "string"
}
}
},
"example": {
"folder": "grafan-folder-ref",
"branch": "target-branch",
"path": "path/in/tree",
"identifier": false
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
}
}
}
}
},
"x-kubernetes-action": "connect",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "Job"
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "name of the Job",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "namespace",
"in": "path",
"description": "object name and auth scope, such as for teams and projects",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/files/": {
"get": {
"tags": [
@@ -1982,36 +1670,63 @@
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/migrate": {
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/jobs": {
"get": {
"tags": [
"Repository"
],
"description": "List recent jobs",
"operationId": "getRepositoryJobs",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
}
}
}
}
},
"x-kubernetes-action": "connect",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "Repository"
}
},
"post": {
"tags": [
"Repository"
],
"description": "Export from grafana into the remote repository",
"operationId": "createRepositoryMigrate",
"description": "Register a job for this repository",
"operationId": "createRepositoryJobs",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"identifier"
],
"properties": {
"history": {
"description": "Preserve history (if possible)",
"type": "boolean"
},
"identifier": {
"description": "Include the identifier in the exported metadata",
"type": "boolean",
"default": false
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec"
},
"examples": {
"incremental": {
"summary": "Pull (incremental)",
"description": "look for changes since the last sync",
"value": {
"pull": {
"incremental": true
}
}
},
"pull": {
"summary": "Pull from repository",
"description": "pull all files",
"value": {
"pull": {
"incremental": false
}
}
}
},
"example": {
"history": true,
"identifier": false
}
}
}
@@ -2020,7 +1735,7 @@
"200": {
"description": "OK",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
}
@@ -2032,14 +1747,14 @@
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "Job"
"kind": "Repository"
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "name of the Job",
"description": "name of the Repository",
"required": true,
"schema": {
"type": "string",
@@ -2058,7 +1773,66 @@
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/render/{path}": {
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/jobs/{uid}": {
"get": {
"tags": [
"Repository"
],
"description": "Get job by UID",
"operationId": "getRepositoryJobsWithPath",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
}
}
}
}
},
"x-kubernetes-action": "connect",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "Repository"
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "name of the Repository",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "namespace",
"in": "path",
"description": "object name and auth scope, such as for teams and projects",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "uid",
"in": "path",
"description": "Original Job UID",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/render/{guid}": {
"get": {
"tags": [
"Repository"
@@ -2102,9 +1876,9 @@
}
},
{
"name": "path",
"name": "guid",
"in": "path",
"description": "path to the resource",
"description": "Image GUID",
"required": true,
"schema": {
"type": "string",
@@ -2452,77 +2226,6 @@
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/sync": {
"post": {
"tags": [
"Repository"
],
"description": "Sync from repository into Grafana",
"operationId": "createRepositorySync",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"incremental"
],
"properties": {
"incremental": {
"description": "Incremental synchronization for versioned repositories",
"type": "boolean",
"default": false
}
}
},
"example": {
"incremental": false
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
}
}
}
}
},
"x-kubernetes-action": "connect",
"x-kubernetes-group-version-kind": {
"group": "provisioning.grafana.app",
"version": "v0alpha1",
"kind": "Job"
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "name of the Job",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
},
{
"name": "namespace",
"in": "path",
"description": "object name and auth scope, such as for teams and projects",
"required": true,
"schema": {
"type": "string",
"uniqueItems": true
}
}
]
},
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories/{name}/test": {
"post": {
"tags": [
@@ -2919,42 +2622,15 @@
"type": "string"
},
"metadata": {
"default": {},
"allOf": [
{
"$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"
}
]
"default": {}
},
"spec": {
"default": {},
"allOf": [
{
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec"
}
]
"default": {}
},
"status": {
"default": {},
"allOf": [
{
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobStatus"
}
]
"default": {}
}
},
"x-kubernetes-group-version-kind": [
{
"group": "provisioning.grafana.app",
"kind": "HistoricJob",
"version": "__internal"
},
{
"group": "provisioning.grafana.app",
"kind": "HistoricJob",
"version": "v0alpha1"
}
]
}
},
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJobList": {
"type": "object",
@@ -2966,12 +2642,7 @@
"items": {
"type": "array",
"items": {
"default": {},
"allOf": [
{
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoricJob"
}
]
"default": {}
}
},
"kind": {
@@ -2979,26 +2650,9 @@
"type": "string"
},
"metadata": {
"default": {},
"allOf": [
{
"$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"
}
]
"default": {}
}
},
"x-kubernetes-group-version-kind": [
{
"group": "provisioning.grafana.app",
"kind": "HistoricJobList",
"version": "__internal"
},
{
"group": "provisioning.grafana.app",
"kind": "HistoricJobList",
"version": "v0alpha1"
}
]
}
},
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.HistoryItem": {
"type": "object",
@@ -3200,15 +2854,10 @@
},
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec": {
"type": "object",
"required": [
"action",
"repository"
],
"properties": {
"action": {
"description": "Possible enum values:\n - `\"migrate\"` acts like JobActionExport, then JobActionPull. It also tries to preserve the history.\n - `\"pr\"` adds additional useful information to a PR, such as comments with preview links and rendered images.\n - `\"pull\"` replicates the remote branch in the local copy of the repository.\n - `\"push\"` replicates the local copy of the repository in the remote branch.",
"type": "string",
"default": "",
"enum": [
"migrate",
"pr",
@@ -3249,9 +2898,8 @@
]
},
"repository": {
"description": "The the repository reference (for now also in labels)",
"type": "string",
"default": ""
"description": "The the repository reference (for now also in labels) This value is required, but will be popuplated from the job making the request",
"type": "string"
}
}
},
+36 -34
View File
@@ -45,7 +45,6 @@ type provisioningTestHelper struct {
Repositories *apis.K8sResourceClient
Jobs *apis.K8sResourceClient
HistoricJobs *apis.K8sResourceClient
Folders *apis.K8sResourceClient
Dashboards *apis.K8sResourceClient
AdminREST *rest.RESTClient
@@ -55,17 +54,19 @@ type provisioningTestHelper struct {
func (h *provisioningTestHelper) SyncAndWait(t *testing.T, repo string, options *provisioning.SyncJobOptions) {
t.Helper()
var opts provisioning.SyncJobOptions
if options != nil {
opts = *options
if options == nil {
options = &provisioning.SyncJobOptions{}
}
body := asJSON(opts)
body := asJSON(&provisioning.JobSpec{
Action: provisioning.JobActionPull,
Pull: options,
})
result := h.AdminREST.Post().
Namespace("default").
Resource("repositories").
Name(repo).
SubResource("sync").
SubResource("jobs").
Body(body).
SetHeader("Content-Type", "application/json").
Do(t.Context())
@@ -84,30 +85,33 @@ func (h *provisioningTestHelper) SyncAndWait(t *testing.T, repo string, options
name := unstruct.GetName()
require.NotEmpty(t, name, "expecting name to be set")
h.AwaitJobSuccess(t, t.Context(), name)
h.AwaitJobSuccess(t, t.Context(), unstruct)
}
func (h *provisioningTestHelper) AwaitJobSuccess(t *testing.T, ctx context.Context, jobName string) {
func (h *provisioningTestHelper) AwaitJobSuccess(t *testing.T, ctx context.Context, job *unstructured.Unstructured) {
t.Helper()
if !assert.EventuallyWithT(t, func(collect *assert.CollectT) {
jobs, err := h.HistoricJobs.Resource.List(ctx, metav1.ListOptions{
LabelSelector: jobs.LabelJobOriginalName + "=" + jobName,
})
if assert.NoError(collect, err) && assert.NotEmpty(collect, jobs.Items, "no historic jobs found yet") {
for _, job := range jobs.Items {
state := mustNestedString(job.Object, "status", "state")
if state == "" {
// The job hasn't gotten its state yet. We do two requests: one to insert the job, one to set the status.
assert.Fail(collect, "job '%s' has no state yet", jobName)
}
// We can fail fast once the job is here: HistoricJobs are immutable.
require.Equal(t, string(provisioning.JobStateSuccess), state, "historic job '%s' was not successful", jobName)
}
repo := job.GetLabels()[jobs.LabelRepository]
require.NotEmpty(t, repo)
if !assert.EventuallyWithT(t, func(collect *assert.CollectT) {
result, err := h.Repositories.Resource.Get(ctx, repo, metav1.GetOptions{},
"jobs", string(job.GetUID()))
if apierrors.IsNotFound(err) {
assert.Fail(collect, "job '%s' not found yet yet", job.GetName())
return // continue trying
}
// Can fail fast here -- the jobs are immutable
require.NoError(t, err)
require.NotNil(t, result)
state := mustNestedString(result.Object, "status", "state")
require.Equal(t, string(provisioning.JobStateSuccess), state,
"historic job '%s' was not successful", job.GetName())
}, time.Second*5, time.Millisecond*20) {
// We also want to add the job details to the error when it fails.
job, err := h.Jobs.Resource.Get(ctx, jobName, metav1.GetOptions{})
job, err := h.Jobs.Resource.Get(ctx, job.GetName(), metav1.GetOptions{})
if err != nil {
t.Logf("failed to get job details for further help: %v", err)
} else {
@@ -135,14 +139,18 @@ func (h *provisioningTestHelper) AwaitJobs(t *testing.T, repoName string) {
}, time.Second*5, time.Millisecond*20)
// Then, as all jobs are now historic jobs, we make sure they are successful.
list, err := h.HistoricJobs.Resource.List(context.Background(), metav1.ListOptions{})
result, err := h.Repositories.Resource.Get(context.Background(), repoName, metav1.GetOptions{}, "jobs")
require.NoError(t, err, "failed to list historic jobs")
list, err := result.ToList()
require.NoError(t, err, "results should be a list")
require.NotEmpty(t, list.Items, "expect at least one job")
for _, elem := range list.Items {
// TODO: Use the spec field of the job.
if elem.GetLabels()["repository"] == repoName {
require.Equal(t, string(provisioning.JobStateSuccess), mustNestedString(elem.Object, "status", "state"), "job %s failed: %+v", elem.GetName(), elem.Object)
}
require.Equal(t, repoName, elem.GetLabels()[jobs.LabelRepository], "should have repo label")
state := mustNestedString(elem.Object, "status", "state")
require.Equal(t, string(provisioning.JobStateSuccess), state, "job %s failed: %+v", elem.GetName(), elem.Object)
}
}
@@ -238,11 +246,6 @@ func runGrafana(t *testing.T, options ...grafanaOption) *provisioningTestHelper
Namespace: "default", // actually org1
GVR: provisioning.JobResourceInfo.GroupVersionResource(),
})
historicJobs := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
GVR: provisioning.HistoricJobResourceInfo.GroupVersionResource(),
})
folders := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
@@ -289,7 +292,6 @@ func runGrafana(t *testing.T, options ...grafanaOption) *provisioningTestHelper
AdminREST: restClient,
ViewerREST: viewerClient,
Jobs: jobs,
HistoricJobs: historicJobs,
Folders: folders,
Dashboards: dashboards,
}
@@ -306,12 +306,14 @@ func TestProvisioning_ExportUnifiedToRepository(t *testing.T) {
Namespace("default").
Resource("repositories").
Name(repo).
SubResource("export").
SubResource("jobs").
SetHeader("Content-Type", "application/json").
Body(asJSON(&provisioning.ExportJobOptions{
Folder: "", // export entire instance
Path: "", // no prefix necessary for testing
Identifier: true, // doesn't _really_ matter, but handy for debugging.
Body(asJSON(&provisioning.JobSpec{
Push: &provisioning.ExportJobOptions{
Folder: "", // export entire instance
Path: "", // no prefix necessary for testing
Identifier: true, // doesn't _really_ matter, but handy for debugging.
},
})).
Do(ctx)
require.NoError(t, result.Error())
@@ -1,39 +1,11 @@
import { api } from './baseAPI';
export const addTagTypes = ['HistoricJob', 'Job', 'Repository', 'Provisioning'] as const;
export const addTagTypes = ['Job', 'Repository', 'Provisioning'] as const;
const injectedRtkApi = api
.enhanceEndpoints({
addTagTypes,
})
.injectEndpoints({
endpoints: (build) => ({
listHistoricJob: build.query<ListHistoricJobApiResponse, ListHistoricJobApiArg>({
query: (queryArg) => ({
url: `/historicjobs`,
params: {
allowWatchBookmarks: queryArg.allowWatchBookmarks,
continue: queryArg['continue'],
fieldSelector: queryArg.fieldSelector,
labelSelector: queryArg.labelSelector,
limit: queryArg.limit,
pretty: queryArg.pretty,
resourceVersion: queryArg.resourceVersion,
resourceVersionMatch: queryArg.resourceVersionMatch,
sendInitialEvents: queryArg.sendInitialEvents,
timeoutSeconds: queryArg.timeoutSeconds,
watch: queryArg.watch,
},
}),
providesTags: ['HistoricJob'],
}),
getHistoricJob: build.query<GetHistoricJobApiResponse, GetHistoricJobApiArg>({
query: (queryArg) => ({
url: `/historicjobs/${queryArg.name}`,
params: {
pretty: queryArg.pretty,
},
}),
providesTags: ['HistoricJob'],
}),
listJob: build.query<ListJobApiResponse, ListJobApiArg>({
query: (queryArg) => ({
url: `/jobs`,
@@ -159,10 +131,6 @@ const injectedRtkApi = api
}),
invalidatesTags: ['Repository'],
}),
createRepositoryExport: build.mutation<CreateRepositoryExportApiResponse, CreateRepositoryExportApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/export`, method: 'POST', body: queryArg.body }),
invalidatesTags: ['Repository'],
}),
getRepositoryFiles: build.query<GetRepositoryFilesApiResponse, GetRepositoryFilesApiArg>({
query: (queryArg) => ({
url: `/repositories/${queryArg.name}/files/`,
@@ -246,15 +214,23 @@ const injectedRtkApi = api
}),
providesTags: ['Repository'],
}),
createRepositoryMigrate: build.mutation<CreateRepositoryMigrateApiResponse, CreateRepositoryMigrateApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/migrate`, method: 'POST', body: queryArg.body }),
getRepositoryJobs: build.query<GetRepositoryJobsApiResponse, GetRepositoryJobsApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/jobs` }),
providesTags: ['Repository'],
}),
createRepositoryJobs: build.mutation<CreateRepositoryJobsApiResponse, CreateRepositoryJobsApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/jobs`, method: 'POST', body: queryArg.jobSpec }),
invalidatesTags: ['Repository'],
}),
getRepositoryJobsWithPath: build.query<GetRepositoryJobsWithPathApiResponse, GetRepositoryJobsWithPathApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/jobs/${queryArg.uid}` }),
providesTags: ['Repository'],
}),
getRepositoryRenderWithPath: build.query<
GetRepositoryRenderWithPathApiResponse,
GetRepositoryRenderWithPathApiArg
>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/render/${queryArg.path}` }),
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/render/${queryArg.guid}` }),
providesTags: ['Repository'],
}),
getRepositoryResources: build.query<GetRepositoryResourcesApiResponse, GetRepositoryResourcesApiArg>({
@@ -284,10 +260,6 @@ const injectedRtkApi = api
}),
invalidatesTags: ['Repository'],
}),
createRepositorySync: build.mutation<CreateRepositorySyncApiResponse, CreateRepositorySyncApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/sync`, method: 'POST', body: queryArg.body }),
invalidatesTags: ['Repository'],
}),
createRepositoryTest: build.mutation<CreateRepositoryTestApiResponse, CreateRepositoryTestApiArg>({
query: (queryArg) => ({ url: `/repositories/${queryArg.name}/test`, method: 'POST', body: queryArg.body }),
invalidatesTags: ['Repository'],
@@ -312,58 +284,6 @@ const injectedRtkApi = api
overrideExisting: false,
});
export { injectedRtkApi as generatedAPI };
export type ListHistoricJobApiResponse = /** status 200 OK */ HistoricJobList;
export type ListHistoricJobApiArg = {
/** allowWatchBookmarks requests watch events with type "BOOKMARK". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. */
allowWatchBookmarks?: boolean;
/** The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the "next key".
This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. */
continue?: string;
/** A selector to restrict the list of returned objects by their fields. Defaults to everything. */
fieldSelector?: string;
/** A selector to restrict the list of returned objects by their labels. Defaults to everything. */
labelSelector?: string;
/** limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.
The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. */
limit?: number;
/** If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). */
pretty?: string;
/** resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.
Defaults to unset */
resourceVersion?: string;
/** resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.
Defaults to unset */
resourceVersionMatch?: string;
/** `sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic "Bookmark" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `"k8s.io/initial-events-end": "true"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.
When `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan
is interpreted as "data at least as new as the provided `resourceVersion`"
and the bookmark event is send when the state is synced
to a `resourceVersion` at least as fresh as the one provided by the ListOptions.
If `resourceVersion` is unset, this is interpreted as "consistent read" and the
bookmark event is send when the state is synced at least to the moment
when request started being processed.
- `resourceVersionMatch` set to any other value or unset
Invalid error is returned.
Defaults to true if `resourceVersion=""` or `resourceVersion="0"` (for backward compatibility reasons) and to false otherwise. */
sendInitialEvents?: boolean;
/** Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity. */
timeoutSeconds?: number;
/** Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion. */
watch?: boolean;
};
export type GetHistoricJobApiResponse = /** status 200 OK */ HistoricJob;
export type GetHistoricJobApiArg = {
/** name of the HistoricJob */
name: string;
/** If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). */
pretty?: string;
};
export type ListJobApiResponse = /** status 200 OK */ JobList;
export type ListJobApiArg = {
/** allowWatchBookmarks requests watch events with type "BOOKMARK". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. */
@@ -565,21 +485,6 @@ export type DeleteRepositoryApiArg = {
/** Whether and how garbage collection will be performed. Either this field or OrphanDependents may be set, but not both. The default policy is decided by the existing finalizer set in the metadata.finalizers and the resource-specific default policy. Acceptable values are: 'Orphan' - orphan the dependents; 'Background' - allow the garbage collector to delete the dependents in the background; 'Foreground' - a cascading policy that deletes all dependents in the foreground. */
propagationPolicy?: string;
};
export type CreateRepositoryExportApiResponse = /** status 200 OK */ Job;
export type CreateRepositoryExportApiArg = {
/** name of the Job */
name: string;
body: {
/** Target branch for export (only git) */
branch?: string;
/** The source folder (or empty) to export */
folder?: string;
/** Include the identifier in the exported metadata */
identifier: boolean;
/** Prefix in target file system */
path?: string;
};
};
export type GetRepositoryFilesApiResponse = /** status 200 OK */ {
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: string;
@@ -658,23 +563,30 @@ export type GetRepositoryHistoryWithPathApiArg = {
/** branch or commit hash */
ref?: string;
};
export type CreateRepositoryMigrateApiResponse = /** status 200 OK */ Job;
export type CreateRepositoryMigrateApiArg = {
/** name of the Job */
export type GetRepositoryJobsApiResponse = /** status 200 OK */ JobList;
export type GetRepositoryJobsApiArg = {
/** name of the Repository */
name: string;
body: {
/** Preserve history (if possible) */
history?: boolean;
/** Include the identifier in the exported metadata */
identifier: boolean;
};
};
export type CreateRepositoryJobsApiResponse = /** status 200 OK */ Job;
export type CreateRepositoryJobsApiArg = {
/** name of the Repository */
name: string;
jobSpec: JobSpec;
};
export type GetRepositoryJobsWithPathApiResponse = /** status 200 OK */ Job;
export type GetRepositoryJobsWithPathApiArg = {
/** name of the Repository */
name: string;
/** Original Job UID */
uid: string;
};
export type GetRepositoryRenderWithPathApiResponse = unknown;
export type GetRepositoryRenderWithPathApiArg = {
/** name of the Repository */
name: string;
/** path to the resource */
path: string;
/** Image GUID */
guid: string;
};
export type GetRepositoryResourcesApiResponse = /** status 200 OK */ ResourceList;
export type GetRepositoryResourcesApiArg = {
@@ -702,15 +614,6 @@ export type ReplaceRepositoryStatusApiArg = {
fieldValidation?: string;
repository: Repository;
};
export type CreateRepositorySyncApiResponse = /** status 200 OK */ Job;
export type CreateRepositorySyncApiArg = {
/** name of the Job */
name: string;
body: {
/** Incremental synchronization for versioned repositories */
incremental: boolean;
};
};
export type CreateRepositoryTestApiResponse = /** status 200 OK */ TestResults;
export type CreateRepositoryTestApiArg = {
/** name of the TestResults */
@@ -856,7 +759,7 @@ export type JobSpec = {
- `"pr"` adds additional useful information to a PR, such as comments with preview links and rendered images.
- `"pull"` replicates the remote branch in the local copy of the repository.
- `"push"` replicates the local copy of the repository in the remote branch. */
action: 'migrate' | 'pr' | 'pull' | 'push';
action?: 'migrate' | 'pr' | 'pull' | 'push';
/** Required when the action is `migrate` */
migrate?: MigrateJobOptions;
/** Pull request options */
@@ -865,8 +768,8 @@ export type JobSpec = {
pull?: SyncJobOptions;
/** Required when the action is `push` */
push?: ExportJobOptions;
/** The the repository reference (for now also in labels) */
repository: string;
/** The the repository reference (for now also in labels) This value is required, but will be popuplated from the job making the request */
repository?: string;
};
export type JobResourceSummary = {
create?: number;
@@ -899,7 +802,7 @@ export type JobStatus = {
/** Summary of processed actions */
summary?: JobResourceSummary[];
};
export type HistoricJob = {
export type Job = {
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: string;
/** Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds */
@@ -918,23 +821,6 @@ export type ListMeta = {
/** Deprecated: selfLink is a legacy read-only field that is no longer populated by the system. */
selfLink?: string;
};
export type HistoricJobList = {
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: string;
items?: HistoricJob[];
/** Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds */
kind?: string;
metadata?: ListMeta;
};
export type Job = {
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: string;
/** Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds */
kind?: string;
metadata?: ObjectMeta;
spec?: JobSpec;
status?: JobStatus;
};
export type JobList = {
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: string;
@@ -1287,8 +1173,6 @@ export type ResourceStats = {
metadata?: any;
};
export const {
useListHistoricJobQuery,
useGetHistoricJobQuery,
useListJobQuery,
useGetJobQuery,
useListRepositoryQuery,
@@ -1297,7 +1181,6 @@ export const {
useGetRepositoryQuery,
useReplaceRepositoryMutation,
useDeleteRepositoryMutation,
useCreateRepositoryExportMutation,
useGetRepositoryFilesQuery,
useGetRepositoryFilesWithPathQuery,
useReplaceRepositoryFilesWithPathMutation,
@@ -1305,12 +1188,13 @@ export const {
useDeleteRepositoryFilesWithPathMutation,
useGetRepositoryHistoryQuery,
useGetRepositoryHistoryWithPathQuery,
useCreateRepositoryMigrateMutation,
useGetRepositoryJobsQuery,
useCreateRepositoryJobsMutation,
useGetRepositoryJobsWithPathQuery,
useGetRepositoryRenderWithPathQuery,
useGetRepositoryResourcesQuery,
useGetRepositoryStatusQuery,
useReplaceRepositoryStatusMutation,
useCreateRepositorySyncMutation,
useCreateRepositoryTestMutation,
useGetRepositoryWebhookQuery,
useCreateRepositoryWebhookMutation,
@@ -8,8 +8,6 @@ import {
JobList,
Repository,
RepositoryList,
HistoricJob,
HistoricJobList,
} from './endpoints.gen';
import { createOnCacheEntryAdded } from './utils/createOnCacheEntryAdded';
@@ -24,15 +22,6 @@ export const provisioningAPI = generatedAPI.enhanceEndpoints({
});
endpoint.onCacheEntryAdded = createOnCacheEntryAdded<JobSpec, JobStatus, Job, JobList>('jobs');
},
listHistoricJob(endpoint) {
endpoint.query = ({ watch, ...queryArg }) => ({
url: `/historicjobs`,
params: queryArg,
});
endpoint.onCacheEntryAdded = createOnCacheEntryAdded<JobSpec, JobStatus, HistoricJob, HistoricJobList>(
'historicjobs'
);
},
listRepository(endpoint) {
endpoint.query = ({ watch, ...queryArg }) => ({
url: `/repositories`,
@@ -44,6 +44,8 @@ export type Preferences = {
homeDashboardUID?: string;
/** Selected language (beta) */
language?: string;
/** Selected locale (beta) */
locale?: string;
navbar?: NavbarPreference;
queryHistory?: QueryHistoryPreference;
/** light, dark, empty is default */
@@ -74,6 +76,7 @@ export type PatchPrefsCmd = {
homeDashboardId?: number;
homeDashboardUID?: string;
language?: string;
locale?: string;
navbar?: NavbarPreference;
queryHistory?: QueryHistoryPreference;
theme?: 'light' | 'dark';
@@ -86,6 +89,7 @@ export type UpdatePrefsCmd = {
homeDashboardId?: number;
homeDashboardUID?: string;
language?: string;
locale?: string;
navbar?: NavbarPreference;
queryHistory?: QueryHistoryPreference;
theme?: 'light' | 'dark' | 'system';
@@ -2,37 +2,48 @@ import { skipToken } from '@reduxjs/toolkit/query';
import { useEffect } from 'react';
import { Alert, ControlledCollapse, LinkButton, Spinner, Stack, Text } from '@grafana/ui';
import { useGetRepositoryQuery } from 'app/api/clients/provisioning';
import {
Job,
useGetRepositoryJobsWithPathQuery,
useGetRepositoryQuery,
useListJobQuery,
} from 'app/api/clients/provisioning';
import { Trans, t } from 'app/core/internationalization';
import ProgressBar from '../Shared/ProgressBar';
import { useRepositoryAllJobs } from '../hooks/useRepositoryAllJobs';
import { getRepoHref } from '../utils/git';
import { JobSummary } from './JobSummary';
export interface JobStatusProps {
name: string;
watch: Job;
onStatusChange?: (success: boolean) => void;
onRunningChange?: (isRunning: boolean) => void;
onErrorChange?: (error: string | null) => void;
}
export function JobStatus({ name, onStatusChange, onRunningChange, onErrorChange }: JobStatusProps) {
const [jobs, activeQuery, historicQuery] = useRepositoryAllJobs({ jobName: name, watch: true });
const job = jobs?.[0];
export function JobStatus({ watch, onStatusChange, onRunningChange, onErrorChange }: JobStatusProps) {
const activeQuery = useListJobQuery({
fieldSelector: `metadata.name=${watch.metadata?.name}`,
watch: true,
});
const activeJob = activeQuery?.data?.items?.[0];
const finishedQuery = useGetRepositoryJobsWithPathQuery(
activeJob
? skipToken
: {
name: watch.metadata?.labels?.['provisioning.grafana.app/repository']!,
uid: watch.metadata?.uid!,
}
);
const job = activeJob || finishedQuery.data;
useEffect(() => {
if (onRunningChange) {
onRunningChange(
activeQuery.isLoading ||
historicQuery.isLoading ||
!job ||
job.status?.state === 'working' ||
job.status?.state === 'pending'
);
if (!job) {
finishedQuery.refetch();
}
}, [activeQuery.isLoading, historicQuery.isLoading, job, onRunningChange, onErrorChange]);
}, [finishedQuery, job]);
useEffect(() => {
if (onStatusChange && job?.status?.state === 'success') {
@@ -49,7 +60,7 @@ export function JobStatus({ name, onStatusChange, onRunningChange, onErrorChange
}
}, [job, onStatusChange, onErrorChange, onRunningChange]);
if (!name || activeQuery.isLoading || historicQuery.isLoading || !job) {
if (!job || activeQuery.isLoading) {
return (
<Stack direction="row" alignItems="center" justifyContent="center" gap={2}>
<Spinner size={24} />
@@ -2,7 +2,7 @@ import { useMemo } from 'react';
import { intervalToAbbreviatedDurationString, TraceKeyValuePair } from '@grafana/data';
import { Alert, Badge, Box, Card, Icon, InteractiveTable, Spinner, Stack, Text } from '@grafana/ui';
import { HistoricJob, Job, Repository, SyncStatus } from 'app/api/clients/provisioning';
import { Job, Repository, SyncStatus } from 'app/api/clients/provisioning';
import { Trans, t } from 'app/core/internationalization';
import KeyValuesTable from 'app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/KeyValuesTable';
@@ -17,7 +17,7 @@ interface Props {
type JobCell = {
row: {
original: Job | HistoricJob;
original: Job;
};
};
@@ -184,9 +184,7 @@ export function RecentJobs({ repo }: Props) {
// TODO: Decide on whether we want to wait on historic jobs to show the current ones.
// Gut feeling is that current jobs are far more important to show than historic ones.
const [jobs, activeQuery, historicQuery] = useRepositoryAllJobs({
repositoryName: repo.metadata?.name,
watch: true,
sort: 'active-first',
repositoryName: repo.metadata?.name ?? 'x',
});
const jobColumns = useMemo(() => getJobColumns(), []);
@@ -4,7 +4,7 @@ import { useNavigate } from 'react-router-dom-v5-compat';
import { AppEvents } from '@grafana/data';
import { getAppEvents } from '@grafana/runtime';
import { Button, ConfirmModal } from '@grafana/ui';
import { Repository, useCreateRepositorySyncMutation } from 'app/api/clients/provisioning';
import { Repository, useCreateRepositoryJobsMutation } from 'app/api/clients/provisioning';
import { Trans, t } from 'app/core/internationalization';
import { PROVISIONING_URL } from '../constants';
@@ -14,34 +14,38 @@ interface Props {
}
export function SyncRepository({ repository }: Props) {
const [syncResource, syncQuery] = useCreateRepositorySyncMutation();
const [createJob, jobQuery] = useCreateRepositoryJobsMutation();
const [isModalOpen, setIsModalOpen] = useState(false);
const navigate = useNavigate();
const name = repository.metadata?.name;
useEffect(() => {
const appEvents = getAppEvents();
if (syncQuery.isSuccess) {
if (jobQuery.isSuccess) {
appEvents.publish({
type: AppEvents.alertSuccess.name,
payload: [t('provisioning.sync-repository.success-pull-started', 'Pull started')],
});
} else if (syncQuery.isError) {
} else if (jobQuery.isError) {
appEvents.publish({
type: AppEvents.alertError.name,
payload: [
t('provisioning.sync-repository.error-pulling-resources', 'Error pulling resources'),
syncQuery.error,
],
payload: [t('provisioning.sync-repository.error-pulling-resources', 'Error pulling resources'), jobQuery.error],
});
}
}, [syncQuery.error, syncQuery.isError, syncQuery.isSuccess]);
}, [jobQuery.error, jobQuery.isError, jobQuery.isSuccess]);
const onClick = () => {
if (!name) {
return;
}
syncResource({ name, body: { incremental: false } }); // will queue a full resync job
createJob({
name,
jobSpec: {
pull: {
incremental: false, // will queue a full resync job
},
},
});
setIsModalOpen(false);
};
@@ -57,7 +61,7 @@ export function SyncRepository({ repository }: Props) {
? undefined
: t('provisioning.sync-repository.tooltip-unhealthy-repository', 'Unable to pull an unhealthy repository')
}
disabled={syncQuery.isLoading || !name || !isHealthy}
disabled={jobQuery.isLoading || !name || !isHealthy}
onClick={onClick}
>
<Trans i18nKey="provisioning.sync-repository.pull">Pull</Trans>
@@ -3,6 +3,7 @@ import { useFormContext } from 'react-hook-form';
import { useAsync } from 'react-use';
import { Stack, Text } from '@grafana/ui';
import { Job } from 'app/api/clients/provisioning';
import { t } from 'app/core/internationalization';
import { JobStatus } from '../Job/JobStatus';
@@ -13,7 +14,7 @@ import { WizardFormData } from './types';
interface JobStepProps {
onStepUpdate: (status: StepStatus, error?: string) => void;
description: ReactNode;
startJob: (repositoryName: string) => Promise<{ metadata?: { name?: string } }>;
startJob: (repositoryName: string) => Promise<Job>;
children?: ReactNode;
}
@@ -23,12 +24,12 @@ export function JobStep({ onStepUpdate, description, startJob, children }: JobSt
const { watch } = useFormContext<WizardFormData>();
const repositoryName = watch('repositoryName');
const stepStatus = useStepStatus({ onStepUpdate });
const [jobName, setJobName] = useState<string>();
const [job, setJob] = useState<Job>();
// Set initial running state outside the async operation
useAsync(async () => {
// Skip if we don't have a repository name or if we already started the job
if (!repositoryName || jobName) {
if (!repositoryName || job) {
return;
}
@@ -40,7 +41,7 @@ export function JobStep({ onStepUpdate, description, startJob, children }: JobSt
if (!response?.metadata?.name) {
throw new Error(t('provisioning.job-step.error-invalid-response', 'Invalid response from operation'));
}
setJobName(response.metadata.name);
setJob(response);
} catch (error) {
const errorMessage =
error instanceof Error
@@ -49,16 +50,16 @@ export function JobStep({ onStepUpdate, description, startJob, children }: JobSt
stepStatus.setError(errorMessage);
throw error; // Re-throw to mark the async operation as failed
}
}, [repositoryName, jobName]); // Only depend on values that determine if we should start the job
}, [repositoryName, job, setJob]); // Only depend on values that determine if we should start the job
return (
<Stack direction="column" gap={2}>
{description && <Text color="secondary">{description}</Text>}
{children}
{jobName && (
{job && (
<JobStatus
name={jobName}
watch={job}
onStatusChange={(success) => {
if (success) {
stepStatus.setSuccess();
@@ -1,6 +1,6 @@
import { useFormContext } from 'react-hook-form';
import { useCreateRepositoryMigrateMutation } from 'app/api/clients/provisioning';
import { useCreateRepositoryJobsMutation } from 'app/api/clients/provisioning';
import { t } from 'app/core/internationalization';
import { StepStatus } from '../hooks/useStepStatus';
@@ -13,15 +13,20 @@ export interface MigrateStepProps {
}
export function MigrateStep({ onStepUpdate }: MigrateStepProps) {
const [migrateRepo] = useCreateRepositoryMigrateMutation();
const [createJob] = useCreateRepositoryJobsMutation();
const { watch } = useFormContext<WizardFormData>();
const identifier = watch('migrate.identifier');
const history = watch('migrate.history');
const startMigration = async (repositoryName: string) => {
const response = await migrateRepo({
const response = await createJob({
name: repositoryName,
body: { identifier, history },
jobSpec: {
migrate: {
identifier,
history,
},
},
}).unwrap();
return response;
@@ -1,4 +1,4 @@
import { useCreateRepositorySyncMutation } from 'app/api/clients/provisioning';
import { useCreateRepositoryJobsMutation } from 'app/api/clients/provisioning';
import { t } from 'app/core/internationalization';
import { StepStatus } from '../hooks/useStepStatus';
@@ -10,12 +10,16 @@ interface PullStepProps {
}
export function PullStep({ onStepUpdate }: PullStepProps) {
const [syncRepo] = useCreateRepositorySyncMutation();
const [createJob] = useCreateRepositoryJobsMutation();
const startSync = async (repositoryName: string) => {
const response = await syncRepo({
const response = await createJob({
name: repositoryName,
body: { incremental: false },
jobSpec: {
pull: {
incremental: false, // will queue a full resync job
},
},
}).unwrap();
return response;
};
@@ -1,68 +1,39 @@
import { HistoricJob, Job, useListHistoricJobQuery, useListJobQuery } from 'app/api/clients/provisioning';
import { useDebounce } from 'react-use';
import { Job, useGetRepositoryJobsQuery, useListJobQuery } from 'app/api/clients/provisioning';
interface RepositoryHistoricalJobsArgs {
/** Limits the returned jobs to those which have this job name (max 1 active, unlimited historic). */
jobName?: string;
/** Limits the returned jobs to those which apply to this repository. */
repositoryName?: string;
/** Whether to continue receiving more updates of the current jobs. */
watch?: boolean;
/**
* How to sort the resulting jobs.
*
* - `created-first`: All jobs are treated equally. The newest jobs are shown first.
* - `active-first`: Active jobs are shown first, then historic jobs. Within each group, the newest jobs are shown first.
*/
sort?: 'created-first' | 'active-first';
repositoryName: string;
}
function labelSelectorActive(repositoryName?: string): string | undefined {
return repositoryName ? `repository=${repositoryName}` : undefined;
}
function labelSelectorHistoric(repositoryName?: string, jobName?: string): string | undefined {
const repoName = repositoryName ? `provisioning.grafana.app/repository=${repositoryName}` : '';
const name = jobName ? `provisioning.grafana.app/original-name=${jobName}` : '';
const selector = [repoName, name].filter(Boolean).join(', ');
return !!selector ? selector : undefined;
}
function fieldSelectorActive(jobName?: string): string | undefined {
return jobName ? `metadata.name=${jobName}` : undefined;
return repositoryName ? `provisioning.grafana.app/repository=${repositoryName}` : undefined;
}
export function useRepositoryAllJobs({
jobName,
repositoryName,
watch = true,
sort = 'created-first',
}: RepositoryHistoricalJobsArgs = {}): [
Array<Job | HistoricJob> | undefined,
}: RepositoryHistoricalJobsArgs): [
Job[] | undefined,
ReturnType<typeof useListJobQuery>,
ReturnType<typeof useListHistoricJobQuery>,
ReturnType<typeof useGetRepositoryJobsQuery>,
] {
const activeQuery = useListJobQuery({
labelSelector: labelSelectorActive(repositoryName),
fieldSelector: fieldSelectorActive(jobName),
watch,
watch: true,
});
const historicQuery = useListHistoricJobQuery({ labelSelector: labelSelectorHistoric(), watch });
const historicQuery = useGetRepositoryJobsQuery({ name: repositoryName! });
useDebounce(
() => {
historicQuery.refetch(); // fetch again when the watch value changes
},
250,
[activeQuery.data]
);
const concatedItems = [...(activeQuery.data?.items ?? []), ...(historicQuery.data?.items ?? [])];
const collator = new Intl.Collator(undefined, { numeric: true });
const sortedItems = concatedItems.slice().sort((a, b) => {
if (sort === 'active-first') {
const aActive = a.kind === 'Job';
const bActive = b.kind === 'Job';
if (aActive && !bActive) {
return -1;
} else if (!aActive && bActive) {
return 1;
}
// otherwise, both are active or both are historic. Sort by creation timestamp.
}
const aTime = a.metadata?.creationTimestamp ?? '';
const bTime = b.metadata?.creationTimestamp ?? '';
@@ -1,36 +0,0 @@
import { HistoricJob, useListHistoricJobQuery } from 'app/api/clients/provisioning';
interface RepositoryHistoricalJobsArgs {
/** Limits the returned jobs to those which had this name before archival. */
originalJobName?: string;
/** Limits the returned jobs to those which apply to this repository. */
repositoryName?: string;
/** Whether to continue receiving more updates of the current historic jobs (i.e. if more come in; existing ones are immutable but may be deleted). */
watch?: boolean;
}
function labelSelectors({
originalJobName,
repositoryName,
}: Pick<RepositoryHistoricalJobsArgs, 'originalJobName' | 'repositoryName'>): string | undefined {
const repoName = repositoryName ? `provisioning.grafana.app/repository=${repositoryName}` : '';
const originalName = originalJobName ? `provisioning.grafana.app/original-name=${originalJobName}` : '';
const selector = [repoName, originalName].filter(Boolean).join(', ');
return !!selector ? selector : undefined;
}
export function useRepositoryHistoricalJobs(
args: RepositoryHistoricalJobsArgs = {}
): [HistoricJob[] | undefined, ReturnType<typeof useListHistoricJobQuery>] {
const query = useListHistoricJobQuery({ labelSelector: labelSelectors(args), watch: args.watch });
const collator = new Intl.Collator(undefined, { numeric: true });
const sortedItems = query.data?.items?.slice().sort((a, b) => {
const aTime = a.metadata?.creationTimestamp ?? '';
const bTime = b.metadata?.creationTimestamp ?? '';
return collator.compare(bTime, aTime); // Reverse order for newest first
});
return [sortedItems, query];
}