-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
224 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright © 2023 zc2638 <zc2638@qq.com>. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/99nil/gopkg/sets" | ||
|
||
storageV1 "github.com/zc2638/ink/pkg/api/storage/v1" | ||
"github.com/zc2638/ink/pkg/database" | ||
) | ||
|
||
func SelectNamesByLabels(ctx context.Context, kind, namespace string, labels map[string]string) ([]string, error) { | ||
if len(labels) == 0 { | ||
return nil, nil | ||
} | ||
|
||
db := database.FromContext(ctx) | ||
db = db.Where(&storageV1.Label{Namespace: namespace, Kind: kind}) | ||
|
||
var start bool | ||
nameSet := sets.New[string]() | ||
for k, v := range labels { | ||
var selectNames []string | ||
if err := db.Where("key = ?", k).Where("value = ?", v).Pluck("name", &selectNames).Error; err != nil { | ||
return nil, fmt.Errorf("select label(%s=%s) failed: %v", k, v, err) | ||
} | ||
|
||
if !start { | ||
start = true | ||
nameSet.Add(selectNames...) | ||
continue | ||
} | ||
for _, sn := range selectNames { | ||
if !nameSet.Has(sn) { | ||
nameSet.Remove(sn) | ||
} | ||
} | ||
if nameSet.Len() == 0 { | ||
return nil, nil | ||
} | ||
} | ||
return nameSet.List(), nil | ||
} |
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,67 @@ | ||
// Copyright © 2023 zc2638 <zc2638@qq.com>. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v1 | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type ListOption struct { | ||
Pagination Pagination | ||
LabelSelector string | ||
} | ||
|
||
func (o *ListOption) ToValues() url.Values { | ||
result := url.Values{} | ||
for k, v := range o.Pagination.ToValues() { | ||
result[k] = v | ||
} | ||
if len(o.LabelSelector) > 0 { | ||
result.Set("labelSelector", o.LabelSelector) | ||
} | ||
return result | ||
} | ||
|
||
func (o *ListOption) SetLabels(labels map[string]string) { | ||
var sb strings.Builder | ||
for k, v := range labels { | ||
sb.WriteString(k) | ||
sb.WriteString("=") | ||
sb.WriteString(v) | ||
sb.WriteString(",") | ||
} | ||
s := sb.String() | ||
s = strings.TrimSuffix(s, ",") | ||
o.LabelSelector = s | ||
} | ||
|
||
func (o *ListOption) Labels() map[string]string { | ||
selector := strings.TrimSpace(o.LabelSelector) | ||
if len(selector) == 0 { | ||
return nil | ||
} | ||
|
||
labels := make(map[string]string) | ||
parts := strings.Split(o.LabelSelector, ",") | ||
for _, part := range parts { | ||
kv := strings.Split(part, "=") | ||
if len(kv) != 2 { | ||
continue | ||
} | ||
labels[kv[0]] = kv[1] | ||
} | ||
return labels | ||
} |
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
1 change: 1 addition & 0 deletions
1
resource/database/migrations/mysql/000008_create_labels_table.down.sql
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 @@ | ||
DROP TABLE IF EXISTS `labels`; |
11 changes: 11 additions & 0 deletions
11
resource/database/migrations/mysql/000008_create_labels_table.up.sql
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,11 @@ | ||
CREATE TABLE IF NOT EXISTS `labels` | ||
( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`namespace` VARCHAR(255) NOT NULL, | ||
`name` VARCHAR(255) NOT NULL, | ||
`kind` VARCHAR(255) NOT NULL, | ||
`key` VARCHAR(255) NOT NULL, | ||
`value` VARCHAR(255), | ||
|
||
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP | ||
); |
1 change: 1 addition & 0 deletions
1
resource/database/migrations/sqlite/000008_create_labels_table.down.sql
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 @@ | ||
DROP TABLE IF EXISTS `labels`; |
Oops, something went wrong.