Merge pull request #2195 from zheng1/refactor_op
Refactor with OpenPitrix
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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 openpitrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"github.com/golang/protobuf/ptypes/wrappers"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -27,20 +25,21 @@ import (
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
"openpitrix.io/openpitrix/pkg/util/pbutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ApplicationInterface interface {
|
||||
ListApplications(conditions *params.Conditions, limit, offset int, orderBy string, reverse bool) (*models.PageableResponse, error)
|
||||
DescribeApplication(namespace, clusterId string) (*Application, error)
|
||||
CreateApplication(namespace string, request CreateClusterRequest) error
|
||||
DescribeApplication(namespace, applicationId, clusterName string) (*Application, error)
|
||||
CreateApplication(clusterName, namespace string, request CreateClusterRequest) error
|
||||
ModifyApplication(request ModifyClusterAttributesRequest) error
|
||||
DeleteApplication(id string) error
|
||||
UpgradeApplication(request UpgradeClusterRequest) error
|
||||
}
|
||||
|
||||
type applicationOperator struct {
|
||||
@@ -68,6 +67,14 @@ type workLoads struct {
|
||||
Daemonsets []appsv1.DaemonSet `json:"daemonsets,omitempty" description:"daemonset list"`
|
||||
}
|
||||
|
||||
type resourceInfo struct {
|
||||
Deployments []appsv1.Deployment `json:"deployments,omitempty" description:"deployment list"`
|
||||
Statefulsets []appsv1.StatefulSet `json:"statefulsets,omitempty" description:"statefulset list"`
|
||||
Daemonsets []appsv1.DaemonSet `json:"daemonsets,omitempty" description:"daemonset list"`
|
||||
Services []v1.Service `json:"services,omitempty" description:"application services"`
|
||||
Ingresses []v1beta1.Ingress `json:"ingresses,omitempty" description:"application ingresses"`
|
||||
}
|
||||
|
||||
func (c *applicationOperator) ListApplications(conditions *params.Conditions, limit, offset int, orderBy string, reverse bool) (*models.PageableResponse, error) {
|
||||
describeClustersRequest := &pb.DescribeClustersRequest{
|
||||
Limit: uint32(limit),
|
||||
@@ -87,6 +94,9 @@ func (c *applicationOperator) ListApplications(conditions *params.Conditions, li
|
||||
if status := conditions.Match[Status]; status != "" {
|
||||
describeClustersRequest.Status = strings.Split(status, "|")
|
||||
}
|
||||
if zone := conditions.Match[Zone]; zone != "" {
|
||||
describeClustersRequest.Zone = []string{zone}
|
||||
}
|
||||
if orderBy != "" {
|
||||
describeClustersRequest.SortKey = &wrappers.StringValue{Value: orderBy}
|
||||
}
|
||||
@@ -133,9 +143,15 @@ func (c *applicationOperator) describeApplication(cluster *pb.Cluster) (*Applica
|
||||
return &app, nil
|
||||
}
|
||||
|
||||
func (c *applicationOperator) DescribeApplication(namespace string, clusterId string) (*Application, error) {
|
||||
|
||||
clusters, err := c.opClient.DescribeClusters(openpitrix.SystemContext(), &pb.DescribeClustersRequest{ClusterId: []string{clusterId}, Limit: 1})
|
||||
func (c *applicationOperator) DescribeApplication(namespace string, applicationId string, clusterName string) (*Application, error) {
|
||||
describeClusterRequest := &pb.DescribeClustersRequest{
|
||||
ClusterId: []string{applicationId},
|
||||
RuntimeId: []string{clusterName},
|
||||
Zone: []string{namespace},
|
||||
WithDetail: pbutil.ToProtoBool(true),
|
||||
Limit: 1,
|
||||
}
|
||||
clusters, err := c.opClient.DescribeClusters(openpitrix.SystemContext(), describeClusterRequest)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
@@ -156,16 +172,26 @@ func (c *applicationOperator) DescribeApplication(namespace string, clusterId st
|
||||
return nil, err
|
||||
}
|
||||
|
||||
workloads, err := c.getWorkLoads(namespace, cluster.ClusterRoleSet)
|
||||
|
||||
resource := new(resourceInfo)
|
||||
workloads := cluster.AdditionalInfo.GetValue()
|
||||
if workloads == "" {
|
||||
err := status.New(codes.NotFound, "cannot get workload").Err()
|
||||
klog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(workloads), resource)
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
app.WorkLoads = workloads
|
||||
workloadLabels := c.getLabels(namespace, app.WorkLoads)
|
||||
app.Services = c.getSvcs(namespace, workloadLabels)
|
||||
app.Ingresses = c.getIng(namespace, app.Services)
|
||||
|
||||
app.WorkLoads = &workLoads{
|
||||
Deployments: resource.Deployments,
|
||||
Statefulsets: resource.Statefulsets,
|
||||
Daemonsets: resource.Daemonsets,
|
||||
}
|
||||
app.Services = resource.Services
|
||||
app.Ingresses = resource.Ingresses
|
||||
return app, nil
|
||||
}
|
||||
|
||||
@@ -332,24 +358,13 @@ func (c *applicationOperator) getIng(namespace string, services []v1.Service) []
|
||||
return ings
|
||||
}
|
||||
|
||||
func (c *applicationOperator) CreateApplication(namespace string, request CreateClusterRequest) error {
|
||||
ns, err := c.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]; runtimeId != "" {
|
||||
request.RuntimeId = runtimeId
|
||||
} else {
|
||||
return fmt.Errorf("runtime not init: namespace %s", namespace)
|
||||
}
|
||||
|
||||
_, err = c.opClient.CreateCluster(openpitrix.ContextWithUsername(request.Username), &pb.CreateClusterRequest{
|
||||
func (c *applicationOperator) CreateApplication(clusterName, namespace string, request CreateClusterRequest) error {
|
||||
_, err := c.opClient.CreateCluster(openpitrix.ContextWithUsername(request.Username), &pb.CreateClusterRequest{
|
||||
AppId: &wrappers.StringValue{Value: request.AppId},
|
||||
VersionId: &wrappers.StringValue{Value: request.VersionId},
|
||||
RuntimeId: &wrappers.StringValue{Value: request.RuntimeId},
|
||||
RuntimeId: &wrappers.StringValue{Value: clusterName},
|
||||
Conf: &wrappers.StringValue{Value: request.Conf},
|
||||
Zone: &wrappers.StringValue{Value: namespace},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -380,8 +395,22 @@ func (c *applicationOperator) ModifyApplication(request ModifyClusterAttributesR
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *applicationOperator) DeleteApplication(clusterId string) error {
|
||||
_, err := c.opClient.DeleteClusters(openpitrix.SystemContext(), &pb.DeleteClustersRequest{ClusterId: []string{clusterId}, Force: &wrappers.BoolValue{Value: true}})
|
||||
func (c *applicationOperator) DeleteApplication(applicationId string) error {
|
||||
_, err := c.opClient.DeleteClusters(openpitrix.SystemContext(), &pb.DeleteClustersRequest{ClusterId: []string{applicationId}, Force: &wrappers.BoolValue{Value: true}})
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *applicationOperator) UpgradeApplication(request UpgradeClusterRequest) error {
|
||||
_, err := c.opClient.UpgradeCluster(openpitrix.ContextWithUsername(request.Username), &pb.UpgradeClusterRequest{
|
||||
ClusterId: &wrappers.StringValue{Value: request.ClusterId},
|
||||
VersionId: &wrappers.StringValue{Value: request.VersionId},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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.
|
||||
@@ -97,11 +94,12 @@ func TestApplicationOperator_CreateApplication(t *testing.T) {
|
||||
VersionId: &wrappers.StringValue{Value: test.createClusterRequest.VersionId},
|
||||
RuntimeId: &wrappers.StringValue{Value: test.createClusterRequest.RuntimeId},
|
||||
Conf: &wrappers.StringValue{Value: test.createClusterRequest.Conf},
|
||||
Zone: &wrappers.StringValue{Value: test.targetNamespace},
|
||||
}).Return(&pb.CreateClusterResponse{}, nil).AnyTimes()
|
||||
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
|
||||
err := applicationOperator.CreateApplication(test.targetNamespace, test.createClusterRequest)
|
||||
err := applicationOperator.CreateApplication(test.createClusterRequest.RuntimeId, test.targetNamespace, test.createClusterRequest)
|
||||
|
||||
if err != nil && err.Error() != test.expected.Error() {
|
||||
t.Error(err)
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
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.
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
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.
|
||||
@@ -37,6 +34,7 @@ type RepoInterface interface {
|
||||
ListRepos(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
|
||||
ValidateRepo(request *ValidateRepoRequest) (*ValidateRepoResponse, error)
|
||||
DoRepoAction(repoId string, request *RepoActionRequest) error
|
||||
ListEvents(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
|
||||
ListRepoEvents(repoId string, conditions *params.Conditions, limit, offset int) (*models.PageableResponse, error)
|
||||
}
|
||||
|
||||
@@ -267,3 +265,33 @@ func (c *repoOperator) ListRepoEvents(repoId string, conditions *params.Conditio
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
}
|
||||
|
||||
func (c *repoOperator) ListEvents(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
|
||||
describeRepoEventsRequest := &pb.DescribeRepoEventsRequest{}
|
||||
if repoId := conditions.Match["repo_id"]; repoId != "" {
|
||||
describeRepoEventsRequest.RepoId = strings.Split(repoId, "|")
|
||||
}
|
||||
if eventId := conditions.Match["repo_event_id"]; eventId != "" {
|
||||
describeRepoEventsRequest.RepoEventId = strings.Split(eventId, "|")
|
||||
}
|
||||
if status := conditions.Match["status"]; status != "" {
|
||||
describeRepoEventsRequest.Status = strings.Split(status, "|")
|
||||
}
|
||||
describeRepoEventsRequest.Limit = uint32(limit)
|
||||
describeRepoEventsRequest.Offset = uint32(offset)
|
||||
|
||||
resp, err := c.opClient.DescribeRepoEvents(openpitrix.SystemContext(), describeRepoEventsRequest)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, item := range resp.RepoEventSet {
|
||||
items = append(items, convertRepoEvent(item))
|
||||
}
|
||||
|
||||
return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
|
||||
}
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
/*
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
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 openpitrix
|
||||
|
||||
import (
|
||||
@@ -737,6 +750,25 @@ type CreateClusterRequest struct {
|
||||
Username string `json:"-"`
|
||||
}
|
||||
|
||||
type UpgradeClusterRequest struct {
|
||||
// cluster id
|
||||
ClusterId string `json:"cluster_id"`
|
||||
|
||||
// advanced param
|
||||
AdvancedParam []string `json:"advanced_param"`
|
||||
|
||||
// required, conf a json string, include cpu, memory info of cluster
|
||||
Conf string `json:"conf,omitempty"`
|
||||
|
||||
// required, id of runtime
|
||||
RuntimeId string `json:"runtime_id,omitempty"`
|
||||
|
||||
// required, id of app version
|
||||
VersionId string `json:"version_id,omitempty"`
|
||||
|
||||
Username string `json:"-"`
|
||||
}
|
||||
|
||||
type Cluster struct {
|
||||
|
||||
// additional info
|
||||
@@ -840,6 +872,7 @@ const (
|
||||
CreateTime = "create_time"
|
||||
StatusTime = "status_time"
|
||||
RuntimeId = "runtime_id"
|
||||
Zone = "zone"
|
||||
VersionId = "version_id"
|
||||
RepoId = "repo_id"
|
||||
CategoryId = "category_id"
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
/*
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user