* refactor template service to contstruct notification template in one place, get provenance before creating and calculate resource version after. * refactor get by UID and name * introduce template kind in NotificationTemplate * introduce includeImported flag and use in the k8s api * support imported templates * add kind to template uid * tests for imported templates * update API model * set kind to default templates
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
package templategroup
|
|
|
|
import (
|
|
"github.com/grafana/alerting/definition"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
model "github.com/grafana/grafana/apps/alerting/notifications/pkg/apis/alertingnotifications/v0alpha1"
|
|
gapiutil "github.com/grafana/grafana/pkg/services/apiserver/utils"
|
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
func convertToK8sResources(orgID int64, list []definitions.NotificationTemplate, namespacer request.NamespaceMapper, selector fields.Selector) (*model.TemplateGroupList, error) {
|
|
result := &model.TemplateGroupList{}
|
|
for _, t := range list {
|
|
item := convertToK8sResource(orgID, t, namespacer)
|
|
if selector != nil && !selector.Empty() && !selector.Matches(model.TemplateGroupSelectableFields(item)) {
|
|
continue
|
|
}
|
|
result.Items = append(result.Items, *item)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func convertToK8sResource(orgID int64, template definitions.NotificationTemplate, namespacer request.NamespaceMapper) *model.TemplateGroup {
|
|
result := &model.TemplateGroup{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
UID: types.UID(template.UID),
|
|
Name: template.UID,
|
|
Namespace: namespacer(orgID),
|
|
ResourceVersion: template.ResourceVersion,
|
|
},
|
|
Spec: model.TemplateGroupSpec{
|
|
Title: template.Name,
|
|
Content: template.Template,
|
|
Kind: model.TemplateGroupTemplateKind(template.Kind),
|
|
},
|
|
}
|
|
result.SetProvenanceStatus(string(template.Provenance))
|
|
result.UID = gapiutil.CalculateClusterWideUID(result)
|
|
return result
|
|
}
|
|
|
|
func convertToDomainModel(template *model.TemplateGroup) definitions.NotificationTemplate {
|
|
return definitions.NotificationTemplate{
|
|
UID: template.Name,
|
|
Name: template.Spec.Title,
|
|
Template: template.Spec.Content,
|
|
ResourceVersion: template.ResourceVersion,
|
|
Provenance: definitions.Provenance(ngmodels.ProvenanceNone),
|
|
Kind: definition.TemplateKind(template.Spec.Kind),
|
|
}
|
|
}
|