mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
Merge pull request #1883 from haiyanmeng/mapping
Set the ElasticSearch index creation configuration
This commit is contained in:
@@ -13,6 +13,40 @@ import (
|
|||||||
"github.com/elastic/go-elasticsearch/v6/esapi"
|
"github.com/elastic/go-elasticsearch/v6/esapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const IndexConfig = `
|
||||||
|
{
|
||||||
|
"mappings": {
|
||||||
|
"_doc": {
|
||||||
|
"properties": {
|
||||||
|
"repositoryUrl": {
|
||||||
|
"type": "keyword"
|
||||||
|
},
|
||||||
|
"filePath": {
|
||||||
|
"type": "keyword"
|
||||||
|
},
|
||||||
|
"defaultBranch": {
|
||||||
|
"type": "keyword"
|
||||||
|
},
|
||||||
|
"document": {
|
||||||
|
"type": "text"
|
||||||
|
},
|
||||||
|
"creationTime": {
|
||||||
|
"type": "date"
|
||||||
|
},
|
||||||
|
"kinds": {
|
||||||
|
"type": "text"
|
||||||
|
},
|
||||||
|
"identifiers": {
|
||||||
|
"type": "text"
|
||||||
|
},
|
||||||
|
"values": {
|
||||||
|
"type": "text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
// TODO(damienr74) Split index into reader and writer?
|
// TODO(damienr74) Split index into reader and writer?
|
||||||
type index struct {
|
type index struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@@ -118,21 +152,19 @@ func (idx *index) UpdateSetting(settings []byte) error {
|
|||||||
res, err, ignoreResponseBody)
|
res, err, ignoreResponseBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an index providing both the mappings and the settings.
|
// Create an index providing the config for both the mappings and the settings.
|
||||||
func (idx *index) CreateIndex(mappings []byte, settings []byte) error {
|
func (idx *index) CreateIndex(config []byte) error {
|
||||||
request := byteJoin(`{ "mappings":`, mappings, `, "settings":`, settings, `}`)
|
|
||||||
op := idx.client.Indices.Create
|
op := idx.client.Indices.Create
|
||||||
res, err := op(
|
res, err := op(
|
||||||
idx.name,
|
idx.name,
|
||||||
op.WithBody(bytes.NewReader(request)),
|
op.WithBody(bytes.NewReader(config)),
|
||||||
op.WithContext(idx.ctx),
|
op.WithContext(idx.ctx),
|
||||||
op.WithHuman(),
|
op.WithHuman(),
|
||||||
op.WithPretty(),
|
op.WithPretty(),
|
||||||
op.WithIncludeTypeName(true),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return idx.responseErrorOrNil(
|
return idx.responseErrorOrNil(
|
||||||
fmt.Sprintf("could not create index with config '%s'", request),
|
fmt.Sprintf("could not create index with config '%s'", config),
|
||||||
res, err, ignoreResponseBody)
|
res, err, ignoreResponseBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,24 @@ func NewKustomizeIndex(ctx context.Context) (*KustomizeIndex, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
indicesExistsOp := idx.client.Indices.Exists
|
||||||
|
resp, err := indicesExistsOp([]string{"kustomize"},
|
||||||
|
indicesExistsOp.WithContext(idx.ctx),
|
||||||
|
indicesExistsOp.WithPretty())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == 200 {
|
||||||
|
fmt.Printf("The kustomize index already exists\n")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Creating the kustomize index\n")
|
||||||
|
if err := idx.CreateIndex([]byte(IndexConfig)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &KustomizeIndex{idx}, nil
|
return &KustomizeIndex{idx}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user