-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete redundant resource from ES when syncer starts (#255)
## What type of PR is this? /kind feature ## What this PR does / why we need it: delete redundant resource from ES when syncer starts ## Which issue(s) this PR fixes: Fixes #256
- Loading branch information
1 parent
91da8e3
commit d8eba17
Showing
11 changed files
with
145 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/KusionStack/karbour/pkg/infra/search/storage/elasticsearch" | ||
"github.com/aquasecurity/esquery" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
k8scache "k8s.io/client-go/tools/cache" | ||
) | ||
|
||
var _ k8scache.KeyListerGetter = &ESListerGetter{} | ||
|
||
type ESListerGetter struct { | ||
cluster string | ||
esClient *elasticsearch.ESClient | ||
gvr schema.GroupVersionResource | ||
} | ||
|
||
func NewESListerGetter(cluster string, esClient *elasticsearch.ESClient, gvr schema.GroupVersionResource) *ESListerGetter { | ||
return &ESListerGetter{ | ||
cluster: cluster, | ||
esClient: esClient, | ||
gvr: gvr, | ||
} | ||
} | ||
|
||
func (e *ESListerGetter) ListKeys() []string { | ||
resource := e.gvr.Resource | ||
kind := resource[0 : len(resource)-1] | ||
query := make(map[string]interface{}) | ||
query["query"] = esquery.Bool().Must( | ||
esquery.Term("cluster", e.cluster), | ||
esquery.Term("apiVersion", e.gvr.GroupVersion().String()), | ||
esquery.Term("kind", kind), | ||
).Map() | ||
sr, err := e.esClient.SearchByQuery(context.Background(), query, nil) | ||
if err != nil { | ||
return nil | ||
} | ||
rt := []string{} | ||
for _, r := range sr.GetResources() { | ||
name, _, _ := unstructured.NestedString(r.Object, "metadata", "name") | ||
ns, _, _ := unstructured.NestedString(r.Object, "metadata", "namespace") | ||
var key string | ||
if ns != "" && name != "" { | ||
key = ns + "/" + name | ||
} else if name != "" { | ||
key = name | ||
} | ||
if key != "" { | ||
rt = append(rt, key) | ||
} | ||
} | ||
return rt | ||
} | ||
|
||
func (e *ESListerGetter) GetByKey(key string) (value interface{}, exists bool, err error) { | ||
s := strings.Split(key, "/") | ||
var name, ns string | ||
switch len(s) { | ||
case 1: | ||
name = s[0] | ||
case 2: | ||
ns = s[0] | ||
name = s[1] | ||
default: | ||
return nil, false, fmt.Errorf("invalid key:%s", key) | ||
} | ||
|
||
resource := e.gvr.Resource | ||
kind := resource[0 : len(resource)-1] | ||
query := make(map[string]interface{}) | ||
query["query"] = esquery.Bool().Must( | ||
esquery.Term("cluster", e.cluster), | ||
esquery.Term("apiVersion", e.gvr.GroupVersion().String()), | ||
esquery.Term("kind", kind), | ||
esquery.Term("namespace", ns), | ||
esquery.Term("name", name)).Map() | ||
sr, err := e.esClient.SearchByQuery(context.Background(), query, nil) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
resources := sr.GetResources() | ||
if len(resources) != 1 { | ||
return nil, false, fmt.Errorf("query result expected 1, got %d", len(resources)) | ||
} | ||
|
||
unObj := &unstructured.Unstructured{} | ||
unObj.SetUnstructuredContent(resources[0].Object) | ||
return unObj, true, nil | ||
} |