refactor code structure (#1738)
This commit is contained in:
@@ -26,7 +26,7 @@ import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/api/logging/v1alpha2"
|
||||
"kubesphere.io/kubesphere/pkg/apis/logging/v1alpha2"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -52,8 +52,8 @@ func createCRDClientSet() (*rest.RESTClient, *runtime.Scheme, error) {
|
||||
return fb.NewFluentbitCRDClient(config)
|
||||
}
|
||||
|
||||
func FluentbitOutputsQuery() *FluentbitOutputsResult {
|
||||
var result FluentbitOutputsResult
|
||||
func FluentbitOutputsQuery() *v1alpha2.FluentbitOutputsResult {
|
||||
var result v1alpha2.FluentbitOutputsResult
|
||||
|
||||
outputs, err := GetFluentbitOutputFromConfigMap()
|
||||
if err != nil {
|
||||
@@ -68,11 +68,11 @@ func FluentbitOutputsQuery() *FluentbitOutputsResult {
|
||||
return &result
|
||||
}
|
||||
|
||||
func FluentbitOutputInsert(output fb.OutputPlugin) *FluentbitOutputsResult {
|
||||
var result FluentbitOutputsResult
|
||||
func FluentbitOutputInsert(output v1alpha2.OutputPlugin) *v1alpha2.FluentbitOutputsResult {
|
||||
var result v1alpha2.FluentbitOutputsResult
|
||||
|
||||
// 1. Update ConfigMap
|
||||
var outputs []fb.OutputPlugin
|
||||
var outputs []v1alpha2.OutputPlugin
|
||||
outputs, err := GetFluentbitOutputFromConfigMap()
|
||||
if err != nil {
|
||||
// If the ConfigMap doesn't exist, a new one will be created later
|
||||
@@ -105,11 +105,11 @@ func FluentbitOutputInsert(output fb.OutputPlugin) *FluentbitOutputsResult {
|
||||
return &result
|
||||
}
|
||||
|
||||
func FluentbitOutputUpdate(output fb.OutputPlugin, id string) *FluentbitOutputsResult {
|
||||
var result FluentbitOutputsResult
|
||||
func FluentbitOutputUpdate(output v1alpha2.OutputPlugin, id string) *v1alpha2.FluentbitOutputsResult {
|
||||
var result v1alpha2.FluentbitOutputsResult
|
||||
|
||||
// 1. Update ConfigMap
|
||||
var outputs []fb.OutputPlugin
|
||||
var outputs []v1alpha2.OutputPlugin
|
||||
outputs, err := GetFluentbitOutputFromConfigMap()
|
||||
if err != nil {
|
||||
// If the ConfigMap doesn't exist, a new one will be created later
|
||||
@@ -152,8 +152,8 @@ func FluentbitOutputUpdate(output fb.OutputPlugin, id string) *FluentbitOutputsR
|
||||
return &result
|
||||
}
|
||||
|
||||
func FluentbitOutputDelete(id string) *FluentbitOutputsResult {
|
||||
var result FluentbitOutputsResult
|
||||
func FluentbitOutputDelete(id string) *v1alpha2.FluentbitOutputsResult {
|
||||
var result v1alpha2.FluentbitOutputsResult
|
||||
|
||||
// 1. Update ConfigMap
|
||||
// If the ConfigMap doesn't exist, a new one will be created
|
||||
@@ -194,7 +194,7 @@ func FluentbitOutputDelete(id string) *FluentbitOutputsResult {
|
||||
return &result
|
||||
}
|
||||
|
||||
func GetFluentbitOutputFromConfigMap() ([]fb.OutputPlugin, error) {
|
||||
func GetFluentbitOutputFromConfigMap() ([]v1alpha2.OutputPlugin, error) {
|
||||
configMap, err := informers.SharedInformerFactory().Core().V1().ConfigMaps().Lister().ConfigMaps(LoggingNamespace).Get(ConfigMapName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -265,22 +265,22 @@ func updateFluentbitOutputConfigMap(outputs []fb.OutputPlugin) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func syncFluentbitCRDOutputWithConfigMap(outputs []fb.OutputPlugin) error {
|
||||
func syncFluentbitCRDOutputWithConfigMap(outputs []v1alpha2.OutputPlugin) error {
|
||||
|
||||
var enabledOutputs []fb.Plugin
|
||||
var enabledOutputs []v1alpha2.Plugin
|
||||
for _, output := range outputs {
|
||||
if output.Enable {
|
||||
enabledOutputs = append(enabledOutputs, fb.Plugin{Type: output.Type, Name: output.Name, Parameters: output.Parameters})
|
||||
enabledOutputs = append(enabledOutputs, v1alpha2.Plugin{Type: output.Type, Name: output.Name, Parameters: output.Parameters})
|
||||
}
|
||||
}
|
||||
|
||||
// Empty output is not allowed, must specify a null-type output
|
||||
if len(enabledOutputs) == 0 {
|
||||
enabledOutputs = []fb.Plugin{
|
||||
enabledOutputs = []v1alpha2.Plugin{
|
||||
{
|
||||
Type: "fluentbit_output",
|
||||
Name: "fluentbit-output-null",
|
||||
Parameters: []fb.Parameter{
|
||||
Parameters: []v1alpha2.Parameter{
|
||||
{
|
||||
Name: "Name",
|
||||
Value: "null",
|
||||
@@ -301,7 +301,7 @@ func syncFluentbitCRDOutputWithConfigMap(outputs []fb.OutputPlugin) error {
|
||||
}
|
||||
|
||||
// Create a CRD client interface
|
||||
crdclient := fb.CrdClient(crdcs, scheme, LoggingNamespace)
|
||||
crdclient := v1alpha2.CrdClient(crdcs, scheme, LoggingNamespace)
|
||||
|
||||
fluentbit, err := crdclient.Get("fluent-bit")
|
||||
if err != nil {
|
||||
@@ -320,7 +320,7 @@ func syncFluentbitCRDOutputWithConfigMap(outputs []fb.OutputPlugin) error {
|
||||
}
|
||||
|
||||
// Parse es host, port and index
|
||||
func ParseEsOutputParams(params []fb.Parameter) *v1alpha2.Config {
|
||||
func ParseEsOutputParams(params []v1alpha2.Parameter) *v1alpha2.Config {
|
||||
|
||||
var (
|
||||
isEsFound bool
|
||||
|
||||
@@ -17,9 +17,3 @@
|
||||
*/
|
||||
|
||||
package log
|
||||
|
||||
type FluentbitOutputsResult struct {
|
||||
Status int `json:"status" description:"response status"`
|
||||
Error string `json:"error,omitempty" description:"debug information"`
|
||||
Outputs []fb.OutputPlugin `json:"outputs,omitempty" description:"array of fluent bit output plugins"`
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package openpitrix
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/go-openapi/strfmt"
|
||||
@@ -25,6 +25,8 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/utils"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
@@ -83,13 +85,13 @@ func ListApps(conditions *params.Conditions, orderBy string, reverse bool, limit
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.AppSet {
|
||||
items = append(items, convertApp(item))
|
||||
items = append(items, utils.ConvertApp(item))
|
||||
}
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
}
|
||||
|
||||
func DescribeApp(id string) (*App, error) {
|
||||
func DescribeApp(id string) (*types.App, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -104,10 +106,10 @@ func DescribeApp(id string) (*App, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var app *App
|
||||
var app *types.App
|
||||
|
||||
if len(resp.AppSet) > 0 {
|
||||
app = convertApp(resp.AppSet[0])
|
||||
app = utils.ConvertApp(resp.AppSet[0])
|
||||
return app, nil
|
||||
} else {
|
||||
err := status.New(codes.NotFound, "resource not found").Err()
|
||||
@@ -132,7 +134,7 @@ func DeleteApp(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateApp(request *CreateAppRequest) (*CreateAppResponse, error) {
|
||||
func CreateApp(request *types.CreateAppRequest) (*types.CreateAppResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -157,13 +159,13 @@ func CreateApp(request *CreateAppRequest) (*CreateAppResponse, error) {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &CreateAppResponse{
|
||||
return &types.CreateAppResponse{
|
||||
AppID: resp.GetAppId().GetValue(),
|
||||
VersionID: resp.GetVersionId().GetValue(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func PatchApp(appId string, request *ModifyAppRequest) error {
|
||||
func PatchApp(appId string, request *types.ModifyAppRequest) error {
|
||||
client, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -237,7 +239,7 @@ func PatchApp(appId string, request *ModifyAppRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateAppVersion(request *CreateAppVersionRequest) (*CreateAppVersionResponse, error) {
|
||||
func CreateAppVersion(request *types.CreateAppVersionRequest) (*types.CreateAppVersionResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -259,12 +261,12 @@ func CreateAppVersion(request *CreateAppVersionRequest) (*CreateAppVersionRespon
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &CreateAppVersionResponse{
|
||||
return &types.CreateAppVersionResponse{
|
||||
VersionId: resp.GetVersionId().GetValue(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ValidatePackage(request *ValidatePackageRequest) (*ValidatePackageResponse, error) {
|
||||
func ValidatePackage(request *types.ValidatePackageRequest) (*types.ValidatePackageResponse, error) {
|
||||
client, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -288,7 +290,7 @@ func ValidatePackage(request *ValidatePackageRequest) (*ValidatePackageResponse,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &ValidatePackageResponse{}
|
||||
result := &types.ValidatePackageResponse{}
|
||||
|
||||
if resp.Error != nil {
|
||||
result.Error = resp.Error.Value
|
||||
@@ -330,7 +332,7 @@ func DeleteAppVersion(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PatchAppVersion(id string, request *ModifyAppVersionRequest) error {
|
||||
func PatchAppVersion(id string, request *types.ModifyAppVersionRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -361,7 +363,7 @@ func PatchAppVersion(id string, request *ModifyAppVersionRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DescribeAppVersion(id string) (*AppVersion, error) {
|
||||
func DescribeAppVersion(id string) (*types.AppVersion, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -376,10 +378,10 @@ func DescribeAppVersion(id string) (*AppVersion, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var app *AppVersion
|
||||
var app *types.AppVersion
|
||||
|
||||
if len(resp.AppVersionSet) > 0 {
|
||||
app = convertAppVersion(resp.AppVersionSet[0])
|
||||
app = utils.ConvertAppVersion(resp.AppVersionSet[0])
|
||||
return app, nil
|
||||
} else {
|
||||
err := status.New(codes.NotFound, "resource not found").Err()
|
||||
@@ -388,7 +390,7 @@ func DescribeAppVersion(id string) (*AppVersion, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetAppVersionPackage(appId, versionId string) (*GetAppVersionPackageResponse, error) {
|
||||
func GetAppVersionPackage(appId, versionId string) (*types.GetAppVersionPackageResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -402,7 +404,7 @@ func GetAppVersionPackage(appId, versionId string) (*GetAppVersionPackageRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app := &GetAppVersionPackageResponse{
|
||||
app := &types.GetAppVersionPackageResponse{
|
||||
AppId: appId,
|
||||
VersionId: versionId,
|
||||
}
|
||||
@@ -414,7 +416,7 @@ func GetAppVersionPackage(appId, versionId string) (*GetAppVersionPackageRespons
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func DoAppAction(appId string, request *ActionRequest) error {
|
||||
func DoAppAction(appId string, request *types.ActionRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -479,7 +481,7 @@ func DoAppAction(appId string, request *ActionRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DoAppVersionAction(versionId string, request *ActionRequest) error {
|
||||
func DoAppVersionAction(versionId string, request *types.ActionRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -529,7 +531,7 @@ func DoAppVersionAction(versionId string, request *ActionRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAppVersionFiles(versionId string, request *GetAppVersionFilesRequest) (*GetAppVersionPackageFilesResponse, error) {
|
||||
func GetAppVersionFiles(versionId string, request *types.GetAppVersionFilesRequest) (*types.GetAppVersionPackageFilesResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -548,7 +550,7 @@ func GetAppVersionFiles(versionId string, request *GetAppVersionFilesRequest) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
version := &GetAppVersionPackageFilesResponse{
|
||||
version := &types.GetAppVersionPackageFilesResponse{
|
||||
VersionId: versionId,
|
||||
}
|
||||
|
||||
@@ -600,7 +602,7 @@ func ListAppVersionAudits(conditions *params.Conditions, orderBy string, reverse
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.AppVersionAuditSet {
|
||||
appVersion := convertAppVersionAudit(item)
|
||||
appVersion := utils.ConvertAppVersionAudit(item)
|
||||
items = append(items, appVersion)
|
||||
}
|
||||
|
||||
@@ -640,7 +642,7 @@ func ListAppVersionReviews(conditions *params.Conditions, orderBy string, revers
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.AppVersionReviewSet {
|
||||
appVersion := convertAppVersionReview(item)
|
||||
appVersion := utils.ConvertAppVersionReview(item)
|
||||
items = append(items, appVersion)
|
||||
}
|
||||
|
||||
@@ -682,7 +684,7 @@ func ListAppVersions(conditions *params.Conditions, orderBy string, reverse bool
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.AppVersionSet {
|
||||
appVersion := convertAppVersion(item)
|
||||
appVersion := utils.ConvertAppVersion(item)
|
||||
items = append(items, appVersion)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package openpitrix
|
||||
package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -32,6 +32,8 @@ import (
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/utils"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
@@ -42,8 +44,8 @@ import (
|
||||
type Interface interface {
|
||||
List(conditions *params.Conditions, limit, offset int, orderBy string, reverse bool) (*models.PageableResponse, error)
|
||||
Get(namespace, clusterID string) (*Application, error)
|
||||
Create(namespace string, request CreateClusterRequest) error
|
||||
Patch(request ModifyClusterAttributesRequest) error
|
||||
Create(namespace string, request types.CreateClusterRequest) error
|
||||
Patch(request types.ModifyClusterAttributesRequest) error
|
||||
Delete(id string) error
|
||||
}
|
||||
|
||||
@@ -61,9 +63,9 @@ func NewApplicaitonOperator(informers informers.SharedInformerFactory, client pb
|
||||
|
||||
type Application struct {
|
||||
Name string `json:"name" description:"application name"`
|
||||
Cluster *Cluster `json:"cluster,omitempty" description:"application cluster info"`
|
||||
Version *AppVersion `json:"version,omitempty" description:"application template version info"`
|
||||
App *App `json:"app,omitempty" description:"application template info"`
|
||||
Cluster *types.Cluster `json:"cluster,omitempty" description:"application cluster info"`
|
||||
Version *types.AppVersion `json:"version,omitempty" description:"application template version info"`
|
||||
App *types.App `json:"app,omitempty" description:"application template info"`
|
||||
WorkLoads *workLoads `json:"workloads,omitempty" description:"application workloads"`
|
||||
Services []v1.Service `json:"services,omitempty" description:"application services"`
|
||||
Ingresses []v1beta1.Ingress `json:"ingresses,omitempty" description:"application ingresses"`
|
||||
@@ -130,14 +132,14 @@ func (c *applicationOperator) describeApplication(cluster *pb.Cluster) (*Applica
|
||||
}
|
||||
var app Application
|
||||
app.Name = cluster.Name.Value
|
||||
app.Cluster = convertCluster(cluster)
|
||||
app.Cluster = utils.ConvertCluster(cluster)
|
||||
versionInfo, err := op.App().DescribeAppVersions(openpitrix.SystemContext(), &pb.DescribeAppVersionsRequest{VersionId: []string{cluster.GetVersionId().GetValue()}})
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
if len(versionInfo.AppVersionSet) > 0 {
|
||||
app.Version = convertAppVersion(versionInfo.AppVersionSet[0])
|
||||
app.Version = utils.ConvertAppVersion(versionInfo.AppVersionSet[0])
|
||||
}
|
||||
appInfo, err := op.App().DescribeApps(openpitrix.SystemContext(), &pb.DescribeAppsRequest{AppId: []string{cluster.GetAppId().GetValue()}, Limit: 1})
|
||||
if err != nil {
|
||||
@@ -145,7 +147,7 @@ func (c *applicationOperator) describeApplication(cluster *pb.Cluster) (*Applica
|
||||
return nil, err
|
||||
}
|
||||
if len(appInfo.AppSet) > 0 {
|
||||
app.App = convertApp(appInfo.GetAppSet()[0])
|
||||
app.App = utils.ConvertApp(appInfo.GetAppSet()[0])
|
||||
}
|
||||
return &app, nil
|
||||
}
|
||||
@@ -350,7 +352,7 @@ func (c *applicationOperator) getIng(namespace string, services []v1.Service) []
|
||||
return ings
|
||||
}
|
||||
|
||||
func (c *applicationOperator) Create(namespace string, request CreateClusterRequest) error {
|
||||
func (c *applicationOperator) Create(namespace string, request types.CreateClusterRequest) error {
|
||||
ns, err := c.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -385,7 +387,7 @@ func (c *applicationOperator) Create(namespace string, request CreateClusterRequ
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *applicationOperator) Patch(request ModifyClusterAttributesRequest) error {
|
||||
func (c *applicationOperator) Patch(request types.ModifyClusterAttributesRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -16,18 +16,20 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package openpitrix
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/utils"
|
||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
)
|
||||
|
||||
func DescribeAttachment(id string) (*Attachment, error) {
|
||||
func DescribeAttachment(id string) (*types.Attachment, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -41,7 +43,7 @@ func DescribeAttachment(id string) (*Attachment, error) {
|
||||
return nil, err
|
||||
}
|
||||
if len(resp.Attachments) > 0 {
|
||||
return convertAttachment(resp.Attachments[id]), nil
|
||||
return utils.ConvertAttachment(resp.Attachments[id]), nil
|
||||
} else {
|
||||
err := status.New(codes.NotFound, "resource not found").Err()
|
||||
klog.Error(err)
|
||||
@@ -16,7 +16,7 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package openpitrix
|
||||
package category
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/ptypes/wrappers"
|
||||
@@ -24,13 +24,15 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/utils"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
)
|
||||
|
||||
func CreateCategory(request *CreateCategoryRequest) (*CreateCategoryResponse, error) {
|
||||
func CreateCategory(request *types.CreateCategoryRequest) (*types.CreateCategoryResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -50,7 +52,7 @@ func CreateCategory(request *CreateCategoryRequest) (*CreateCategoryResponse, er
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &CreateCategoryResponse{
|
||||
return &types.CreateCategoryResponse{
|
||||
CategoryId: resp.GetCategoryId().GetValue(),
|
||||
}, nil
|
||||
}
|
||||
@@ -71,7 +73,7 @@ func DeleteCategory(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PatchCategory(id string, request *ModifyCategoryRequest) error {
|
||||
func PatchCategory(id string, request *types.ModifyCategoryRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -101,7 +103,7 @@ func PatchCategory(id string, request *ModifyCategoryRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DescribeCategory(id string) (*Category, error) {
|
||||
func DescribeCategory(id string) (*types.Category, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -116,10 +118,10 @@ func DescribeCategory(id string) (*Category, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var category *Category
|
||||
var category *types.Category
|
||||
|
||||
if len(resp.CategorySet) > 0 {
|
||||
category = convertCategory(resp.CategorySet[0])
|
||||
category = utils.ConvertCategory(resp.CategorySet[0])
|
||||
return category, nil
|
||||
} else {
|
||||
err := status.New(codes.NotFound, "resource not found").Err()
|
||||
@@ -156,7 +158,7 @@ func ListCategories(conditions *params.Conditions, orderBy string, reverse bool,
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.CategorySet {
|
||||
items = append(items, convertCategory(item))
|
||||
items = append(items, utils.ConvertCategory(item))
|
||||
}
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
@@ -16,7 +16,7 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package openpitrix
|
||||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -25,6 +25,8 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/utils"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
@@ -32,7 +34,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CreateRepo(request *CreateRepoRequest) (*CreateRepoResponse, error) {
|
||||
func CreateRepo(request *types.CreateRepoRequest) (*types.CreateRepoResponse, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -61,7 +63,7 @@ func CreateRepo(request *CreateRepoRequest) (*CreateRepoResponse, error) {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &CreateRepoResponse{
|
||||
return &types.CreateRepoResponse{
|
||||
RepoID: resp.GetRepoId().GetValue(),
|
||||
}, nil
|
||||
}
|
||||
@@ -82,7 +84,7 @@ func DeleteRepo(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PatchRepo(id string, request *ModifyRepoRequest) error {
|
||||
func PatchRepo(id string, request *types.ModifyRepoRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -132,7 +134,7 @@ func PatchRepo(id string, request *ModifyRepoRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DescribeRepo(id string) (*Repo, error) {
|
||||
func DescribeRepo(id string) (*types.Repo, error) {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -147,10 +149,10 @@ func DescribeRepo(id string) (*Repo, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var repo *Repo
|
||||
var repo *types.Repo
|
||||
|
||||
if len(resp.RepoSet) > 0 {
|
||||
repo = convertRepo(resp.RepoSet[0])
|
||||
repo = utils.ConvertRepo(resp.RepoSet[0])
|
||||
return repo, nil
|
||||
} else {
|
||||
err := status.New(codes.NotFound, "resource not found").Err()
|
||||
@@ -202,13 +204,13 @@ func ListRepos(conditions *params.Conditions, orderBy string, reverse bool, limi
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.RepoSet {
|
||||
items = append(items, convertRepo(item))
|
||||
items = append(items, utils.ConvertRepo(item))
|
||||
}
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
}
|
||||
|
||||
func ValidateRepo(request *ValidateRepoRequest) (*ValidateRepoResponse, error) {
|
||||
func ValidateRepo(request *types.ValidateRepoRequest) (*types.ValidateRepoResponse, error) {
|
||||
client, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if err != nil {
|
||||
@@ -227,13 +229,13 @@ func ValidateRepo(request *ValidateRepoRequest) (*ValidateRepoResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ValidateRepoResponse{
|
||||
return &types.ValidateRepoResponse{
|
||||
ErrorCode: int64(resp.ErrorCode),
|
||||
Ok: resp.Ok.Value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func DoRepoAction(repoId string, request *RepoActionRequest) error {
|
||||
func DoRepoAction(repoId string, request *types.RepoActionRequest) error {
|
||||
op, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
@@ -289,7 +291,7 @@ func ListRepoEvents(repoId string, conditions *params.Conditions, limit, offset
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.RepoEventSet {
|
||||
items = append(items, convertRepoEvent(item))
|
||||
items = append(items, utils.ConvertRepoEvent(item))
|
||||
}
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
@@ -1,4 +1,4 @@
|
||||
package openpitrix
|
||||
package types
|
||||
|
||||
import (
|
||||
"github.com/go-openapi/strfmt"
|
||||
@@ -16,28 +16,29 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package openpitrix
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/go-openapi/strfmt"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
"time"
|
||||
)
|
||||
|
||||
func convertApp(in *pb.App) *App {
|
||||
func ConvertApp(in *pb.App) *types.App {
|
||||
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
categorySet := make(AppCategorySet, 0)
|
||||
categorySet := make(types.AppCategorySet, 0)
|
||||
|
||||
for _, item := range in.CategorySet {
|
||||
category := convertResourceCategory(item)
|
||||
category := ConvertResourceCategory(item)
|
||||
categorySet = append(categorySet, category)
|
||||
}
|
||||
|
||||
out := App{
|
||||
out := types.App{
|
||||
CategorySet: categorySet,
|
||||
}
|
||||
|
||||
@@ -92,7 +93,7 @@ func convertApp(in *pb.App) *App {
|
||||
out.Keywords = in.Keywords.Value
|
||||
}
|
||||
if in.LatestAppVersion != nil {
|
||||
out.LatestAppVersion = convertAppVersion(in.LatestAppVersion)
|
||||
out.LatestAppVersion = ConvertAppVersion(in.LatestAppVersion)
|
||||
}
|
||||
if in.Name != nil {
|
||||
out.Name = in.Name.Value
|
||||
@@ -130,11 +131,11 @@ func convertApp(in *pb.App) *App {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertAppVersion(in *pb.AppVersion) *AppVersion {
|
||||
func ConvertAppVersion(in *pb.AppVersion) *types.AppVersion {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := AppVersion{}
|
||||
out := types.AppVersion{}
|
||||
if in.AppId != nil {
|
||||
out.AppId = in.AppId.Value
|
||||
}
|
||||
@@ -209,11 +210,11 @@ func convertAppVersion(in *pb.AppVersion) *AppVersion {
|
||||
|
||||
}
|
||||
|
||||
func convertResourceCategory(in *pb.ResourceCategory) *ResourceCategory {
|
||||
func ConvertResourceCategory(in *pb.ResourceCategory) *types.ResourceCategory {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := ResourceCategory{}
|
||||
out := types.ResourceCategory{}
|
||||
|
||||
if in.CategoryId != nil {
|
||||
out.CategoryId = in.CategoryId.Value
|
||||
@@ -239,11 +240,11 @@ func convertResourceCategory(in *pb.ResourceCategory) *ResourceCategory {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertCategory(in *pb.Category) *Category {
|
||||
func ConvertCategory(in *pb.Category) *types.Category {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := Category{}
|
||||
out := types.Category{}
|
||||
|
||||
if in.CategoryId != nil {
|
||||
out.CategoryID = in.CategoryId.Value
|
||||
@@ -275,11 +276,11 @@ func convertCategory(in *pb.Category) *Category {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertAttachment(in *pb.Attachment) *Attachment {
|
||||
func ConvertAttachment(in *pb.Attachment) *types.Attachment {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := Attachment{}
|
||||
out := types.Attachment{}
|
||||
|
||||
out.AttachmentID = in.AttachmentId
|
||||
|
||||
@@ -297,11 +298,11 @@ func convertAttachment(in *pb.Attachment) *Attachment {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertRepo(in *pb.Repo) *Repo {
|
||||
func ConvertRepo(in *pb.Repo) *types.Repo {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := Repo{}
|
||||
out := types.Repo{}
|
||||
|
||||
if in.RepoId != nil {
|
||||
out.RepoId = in.RepoId.Value
|
||||
@@ -316,10 +317,10 @@ func convertRepo(in *pb.Repo) *Repo {
|
||||
out.Credential = in.Credential.Value
|
||||
}
|
||||
|
||||
categorySet := make(RepoCategorySet, 0)
|
||||
categorySet := make(types.RepoCategorySet, 0)
|
||||
|
||||
for _, item := range in.CategorySet {
|
||||
category := convertResourceCategory(item)
|
||||
category := ConvertResourceCategory(item)
|
||||
categorySet = append(categorySet, category)
|
||||
}
|
||||
|
||||
@@ -338,10 +339,10 @@ func convertRepo(in *pb.Repo) *Repo {
|
||||
out.Description = in.Description.Value
|
||||
}
|
||||
|
||||
labelSet := make(RepoLabels, 0)
|
||||
labelSet := make(types.RepoLabels, 0)
|
||||
|
||||
for _, item := range in.Labels {
|
||||
label := convertRepoLabel(item)
|
||||
label := ConvertRepoLabel(item)
|
||||
labelSet = append(labelSet, label)
|
||||
}
|
||||
|
||||
@@ -357,10 +358,10 @@ func convertRepo(in *pb.Repo) *Repo {
|
||||
out.RepoId = in.RepoId.Value
|
||||
}
|
||||
|
||||
selectorSet := make(RepoSelectors, 0)
|
||||
selectorSet := make(types.RepoSelectors, 0)
|
||||
|
||||
for _, item := range in.Selectors {
|
||||
selector := convertRepoSelector(item)
|
||||
selector := ConvertRepoSelector(item)
|
||||
selectorSet = append(selectorSet, selector)
|
||||
}
|
||||
|
||||
@@ -384,11 +385,11 @@ func convertRepo(in *pb.Repo) *Repo {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertRepoLabel(in *pb.RepoLabel) *RepoLabel {
|
||||
func ConvertRepoLabel(in *pb.RepoLabel) *types.RepoLabel {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := RepoLabel{}
|
||||
out := types.RepoLabel{}
|
||||
if in.CreateTime != nil {
|
||||
date := strfmt.DateTime(time.Unix(in.CreateTime.Seconds, 0))
|
||||
out.CreateTime = &date
|
||||
@@ -402,11 +403,11 @@ func convertRepoLabel(in *pb.RepoLabel) *RepoLabel {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertRepoSelector(in *pb.RepoSelector) *RepoSelector {
|
||||
func ConvertRepoSelector(in *pb.RepoSelector) *types.RepoSelector {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := RepoSelector{}
|
||||
out := types.RepoSelector{}
|
||||
if in.CreateTime != nil {
|
||||
date := strfmt.DateTime(time.Unix(in.CreateTime.Seconds, 0))
|
||||
out.CreateTime = &date
|
||||
@@ -420,11 +421,11 @@ func convertRepoSelector(in *pb.RepoSelector) *RepoSelector {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertAppVersionAudit(in *pb.AppVersionAudit) *AppVersionAudit {
|
||||
func ConvertAppVersionAudit(in *pb.AppVersionAudit) *types.AppVersionAudit {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := AppVersionAudit{}
|
||||
out := types.AppVersionAudit{}
|
||||
if in.AppId != nil {
|
||||
out.AppId = in.AppId.Value
|
||||
}
|
||||
@@ -462,11 +463,11 @@ func convertAppVersionAudit(in *pb.AppVersionAudit) *AppVersionAudit {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertAppVersionReview(in *pb.AppVersionReview) *AppVersionReview {
|
||||
func ConvertAppVersionReview(in *pb.AppVersionReview) *types.AppVersionReview {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := AppVersionReview{}
|
||||
out := types.AppVersionReview{}
|
||||
if in.AppId != nil {
|
||||
out.AppId = in.AppId.Value
|
||||
}
|
||||
@@ -474,9 +475,9 @@ func convertAppVersionReview(in *pb.AppVersionReview) *AppVersionReview {
|
||||
out.AppName = in.AppName.Value
|
||||
}
|
||||
if in.Phase != nil {
|
||||
out.Phase = make(AppVersionReviewPhaseOAIGen)
|
||||
out.Phase = make(types.AppVersionReviewPhaseOAIGen)
|
||||
for k, v := range in.Phase {
|
||||
out.Phase[k] = *convertAppVersionReviewPhase(v)
|
||||
out.Phase[k] = *ConvertAppVersionReviewPhase(v)
|
||||
}
|
||||
}
|
||||
if in.ReviewId != nil {
|
||||
@@ -503,11 +504,11 @@ func convertAppVersionReview(in *pb.AppVersionReview) *AppVersionReview {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertAppVersionReviewPhase(in *pb.AppVersionReviewPhase) *AppVersionReviewPhase {
|
||||
func ConvertAppVersionReviewPhase(in *pb.AppVersionReviewPhase) *types.AppVersionReviewPhase {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := AppVersionReviewPhase{}
|
||||
out := types.AppVersionReviewPhase{}
|
||||
if in.Message != nil {
|
||||
out.Message = in.Message.Value
|
||||
}
|
||||
@@ -531,11 +532,11 @@ func convertAppVersionReviewPhase(in *pb.AppVersionReviewPhase) *AppVersionRevie
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertRepoEvent(in *pb.RepoEvent) *RepoEvent {
|
||||
func ConvertRepoEvent(in *pb.RepoEvent) *types.RepoEvent {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := RepoEvent{}
|
||||
out := types.RepoEvent{}
|
||||
if in.CreateTime != nil {
|
||||
date := strfmt.DateTime(time.Unix(in.CreateTime.Seconds, 0))
|
||||
out.CreateTime = &date
|
||||
@@ -563,11 +564,11 @@ func convertRepoEvent(in *pb.RepoEvent) *RepoEvent {
|
||||
return &out
|
||||
}
|
||||
|
||||
func convertCluster(in *pb.Cluster) *Cluster {
|
||||
func ConvertCluster(in *pb.Cluster) *types.Cluster {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := Cluster{}
|
||||
out := types.Cluster{}
|
||||
if in.AdditionalInfo != nil {
|
||||
out.AdditionalInfo = in.AdditionalInfo.Value
|
||||
}
|
||||
@@ -132,9 +132,7 @@ func (s *appSearcher) Search(namespace string, conditions *params.Conditions, or
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
if reverse {
|
||||
tmp := i
|
||||
i = j
|
||||
j = tmp
|
||||
i, j = j, i
|
||||
}
|
||||
return s.compare(result[i], result[j], orderBy)
|
||||
})
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/informers"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
"strings"
|
||||
@@ -46,26 +48,26 @@ func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
|
||||
return &deploymentsGetter{sharedInformers: sharedInformers}
|
||||
}
|
||||
|
||||
func (d *deploymentsGetter) Get(namespace, name string) (interface{}, error) {
|
||||
func (d *deploymentsGetter) Get(namespace, name string) (runtime.Object, error) {
|
||||
return d.sharedInformers.Apps().V1().Deployments().Lister().Deployments(namespace).Get(name)
|
||||
}
|
||||
|
||||
func (d *deploymentsGetter) List(namespace string) ([]interface{}, error) {
|
||||
func (d *deploymentsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
|
||||
// first retrieves all deployments within given namespace
|
||||
all, err := d.sharedInformers.Apps().V1().Deployments().Lister().Deployments(namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []interface{}
|
||||
var result []runtime.Object
|
||||
for _, deploy := range all {
|
||||
result = append(result, deploy)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
|
||||
}
|
||||
|
||||
func (d *deploymentsGetter) Compare(left interface{}, right interface{}, field query.Field) bool {
|
||||
func (d *deploymentsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
|
||||
leftDeployment, ok := left.(*v1.Deployment)
|
||||
if !ok {
|
||||
@@ -89,7 +91,7 @@ func (d *deploymentsGetter) Compare(left interface{}, right interface{}, field q
|
||||
}
|
||||
}
|
||||
|
||||
func (d *deploymentsGetter) Filter(object interface{}, filter query.Filter) bool {
|
||||
func (d *deploymentsGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
deployment, ok := object.(*v1.Deployment)
|
||||
if !ok {
|
||||
return false
|
||||
|
||||
@@ -125,7 +125,6 @@ func TestListDeployments(t *testing.T) {
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
objs := deploymentsToRuntimeObjects(test.deployments...)
|
||||
client := fake.NewSimpleClientset(objs...)
|
||||
//client := fake.NewSimpleClientset()
|
||||
|
||||
informer := informers.NewSharedInformerFactory(client, 0)
|
||||
|
||||
|
||||
@@ -1,19 +1,67 @@
|
||||
package v1alpha3
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
// Get retrieves a single object by its namespace and name
|
||||
Get(namespace, name string) (interface{}, error)
|
||||
Get(namespace, name string) (runtime.Object, error)
|
||||
|
||||
// List retrieves a collection of objects matches given query
|
||||
List(namespace string) ([]interface{}, error)
|
||||
|
||||
//
|
||||
Filter(item interface{}, filter query.Filter) bool
|
||||
|
||||
//
|
||||
Compare(left interface{}, right interface{}, field query.Field) bool
|
||||
List(namespace string, query *query.Query) (*api.ListResult, error)
|
||||
}
|
||||
|
||||
type CompareFunc func(runtime.Object, runtime.Object, query.Field) bool
|
||||
|
||||
type FilterFunc func(runtime.Object, query.Filter) bool
|
||||
|
||||
func DefaultList(objects []runtime.Object, query *query.Query, compareFunc CompareFunc, filterFunc FilterFunc) *api.ListResult {
|
||||
// selected matched ones
|
||||
var filtered []runtime.Object
|
||||
for _, object := range objects {
|
||||
selected := true
|
||||
for _, filter := range query.Filters {
|
||||
if !filterFunc(object, filter) {
|
||||
selected = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if selected {
|
||||
filtered = append(filtered, object)
|
||||
}
|
||||
}
|
||||
|
||||
start, end := query.Pagination.GetPaginationSettings(len(filtered))
|
||||
if !query.Pagination.IsPageAvailable(len(filtered), start) {
|
||||
return &api.ListResult{
|
||||
Items: nil,
|
||||
TotalItems: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// sort by sortBy field
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
if !query.Ascending {
|
||||
return !compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
}
|
||||
return compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
})
|
||||
|
||||
return &api.ListResult{
|
||||
Items: objectsToInterfaces(filtered[start:end]),
|
||||
TotalItems: len(filtered),
|
||||
}
|
||||
}
|
||||
|
||||
func objectsToInterfaces(objs []runtime.Object) []interface{} {
|
||||
var res []interface{}
|
||||
for _, obj := range objs {
|
||||
res = append(res, obj)
|
||||
}
|
||||
return res
|
||||
}
|
||||
75
pkg/models/resources/v1alpha3/namespace/namespaces.go
Normal file
75
pkg/models/resources/v1alpha3/namespace/namespaces.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package namespace
|
||||
|
||||
import (
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/informers"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type namespaceGetter struct {
|
||||
informers informers.SharedInformerFactory
|
||||
}
|
||||
|
||||
func NewNamespaceGetter(informers informers.SharedInformerFactory) v1alpha3.Interface {
|
||||
return &namespaceGetter{informers:informers}
|
||||
}
|
||||
|
||||
func (n namespaceGetter) Get(_, name string) (runtime.Object, error) {
|
||||
return n.informers.Core().V1().Namespaces().Lister().Get(name)
|
||||
}
|
||||
|
||||
func (n namespaceGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
|
||||
ns, err := n.informers.Core().V1().Namespaces().Lister().List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []runtime.Object
|
||||
for _, item := range ns {
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, n.compare, n.filter), nil
|
||||
}
|
||||
|
||||
func (n namespaceGetter) filter(item runtime.Object, filter query.Filter) bool {
|
||||
namespace, ok := item.(*v1.Namespace)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
switch filter.Field {
|
||||
case query.FieldName:
|
||||
return query.ComparableString(namespace.Name).Contains(filter.Value)
|
||||
case query.FieldStatus:
|
||||
return query.ComparableString(namespace.Status.Phase).Compare(filter.Value) == 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (n namespaceGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
leftNs, ok := left.(*v1.Namespace)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightNs, ok := right.(*v1.Namespace)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
|
||||
switch field {
|
||||
case query.FieldName:
|
||||
return strings.Compare(leftNs.Name, rightNs.Name) > 0
|
||||
case query.FieldCreationTimeStamp:
|
||||
return leftNs.CreationTimestamp.After(rightNs.CreationTimestamp.Time)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package namespace
|
||||
@@ -3,12 +3,11 @@ package resource
|
||||
import (
|
||||
"errors"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/informers"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var ErrResourceNotSupported = errors.New("resource is not supported")
|
||||
@@ -17,10 +16,10 @@ type NamespacedResourceGetter struct {
|
||||
getters map[schema.GroupVersionResource]v1alpha3.Interface
|
||||
}
|
||||
|
||||
func New(informers informers.SharedInformerFactory) *NamespacedResourceGetter {
|
||||
func New(factory informers.InformerFactory) *NamespacedResourceGetter {
|
||||
getters := make(map[schema.GroupVersionResource]v1alpha3.Interface)
|
||||
|
||||
getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(informers)
|
||||
getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(factory.KubernetesSharedInformerFactory())
|
||||
|
||||
return &NamespacedResourceGetter{
|
||||
getters: getters,
|
||||
@@ -54,45 +53,6 @@ func (r *NamespacedResourceGetter) List(resource, namespace string, query *query
|
||||
return nil, ErrResourceNotSupported
|
||||
}
|
||||
|
||||
all, err := getter.List(namespace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getter.List(namespace, query)
|
||||
|
||||
// selected matched ones
|
||||
var filtered []interface{}
|
||||
for _, deploy := range all {
|
||||
for _, filter := range query.Filters {
|
||||
if getter.Filter(deploy, filter) {
|
||||
filtered = append(filtered, deploy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sort
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
if !query.Ascending {
|
||||
return !getter.Compare(filtered[i], filtered[j], query.SortBy)
|
||||
}
|
||||
return getter.Compare(filtered[i], filtered[j], query.SortBy)
|
||||
})
|
||||
|
||||
start, end := query.Pagination.GetPaginationSettings(len(filtered))
|
||||
if query.Pagination.IsPageAvailable(len(filtered), start) {
|
||||
var result []interface{}
|
||||
|
||||
for i := start; i < end; i++ {
|
||||
result = append(result, filtered[i])
|
||||
}
|
||||
|
||||
return &api.ListResult{
|
||||
Items: result,
|
||||
TotalItems: len(filtered),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &api.ListResult{
|
||||
Items: nil,
|
||||
TotalItems: len(filtered),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -24,10 +24,11 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
"io"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/remotecommand"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -134,15 +135,23 @@ func (t TerminalSession) Close(status uint32, reason string) {
|
||||
t.conn.Close()
|
||||
}
|
||||
|
||||
type Interface interface {
|
||||
HandleSession(shell, namespace, podName, containerName string, conn *websocket.Conn)
|
||||
}
|
||||
|
||||
type terminaler struct {
|
||||
client kubernetes.Interface
|
||||
config *rest.Config
|
||||
}
|
||||
|
||||
func NewTerminaler(client kubernetes.Interface, config *rest.Config) Interface {
|
||||
return &terminaler{client:client, config:config}
|
||||
}
|
||||
|
||||
// startProcess is called by handleAttach
|
||||
// Executed cmd in the container specified in request and connects it up with the ptyHandler (a session)
|
||||
func startProcess(namespace, podName, containerName string, cmd []string, ptyHandler PtyHandler) error {
|
||||
|
||||
k8sClient := client.ClientSets().K8s().Kubernetes()
|
||||
|
||||
cfg := client.ClientSets().K8s().Config()
|
||||
|
||||
req := k8sClient.CoreV1().RESTClient().Post().
|
||||
func (t *terminaler)startProcess(namespace, podName, containerName string, cmd []string, ptyHandler PtyHandler) error {
|
||||
req := t.client.CoreV1().RESTClient().Post().
|
||||
Resource("pods").
|
||||
Name(podName).
|
||||
Namespace(namespace).
|
||||
@@ -156,7 +165,7 @@ func startProcess(namespace, podName, containerName string, cmd []string, ptyHan
|
||||
TTY: true,
|
||||
}, scheme.ParameterCodec)
|
||||
|
||||
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL())
|
||||
exec, err := remotecommand.NewSPDYExecutor(t.config, "POST", req.URL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -185,8 +194,7 @@ func isValidShell(validShells []string, shell string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HandleSession(shell, namespace, podName, containerName string, conn *websocket.Conn) {
|
||||
|
||||
func (t *terminaler)HandleSession(shell, namespace, podName, containerName string, conn *websocket.Conn) {
|
||||
var err error
|
||||
validShells := []string{"sh", "bash"}
|
||||
|
||||
@@ -194,13 +202,13 @@ func HandleSession(shell, namespace, podName, containerName string, conn *websoc
|
||||
|
||||
if isValidShell(validShells, shell) {
|
||||
cmd := []string{shell}
|
||||
err = startProcess(namespace, podName, containerName, cmd, session)
|
||||
err = t.startProcess(namespace, podName, containerName, cmd, session)
|
||||
} else {
|
||||
// No shell given or it was not valid: try some shells until one succeeds or all fail
|
||||
// FIXME: if the first shell fails then the first keyboard event is lost
|
||||
for _, testShell := range validShells {
|
||||
cmd := []string{testShell}
|
||||
if err = startProcess(namespace, podName, containerName, cmd, session); err == nil {
|
||||
if err = t.startProcess(namespace, podName, containerName, cmd, session); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user