refactor: openpitrix module
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -302,7 +302,14 @@ func (cs *ClientSet) S3() (*s2is3.S3Client, error) {
|
||||
func (cs *ClientSet) OpenPitrix() (*openpitrix.OpenPitrixClient, error) {
|
||||
var err error
|
||||
|
||||
if cs.csoptions.openPitrixOptions == nil || cs.csoptions.openPitrixOptions.APIServer == "" {
|
||||
if cs.csoptions.openPitrixOptions == nil ||
|
||||
cs.csoptions.openPitrixOptions.RepoManagerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.RuntimeManagerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.ClusterManagerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.AppManagerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.AttachmentManagerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.RepoIndexerEndpoint == "" ||
|
||||
cs.csoptions.openPitrixOptions.CategoryManagerEndpoint == "" {
|
||||
return nil, ClientSetNotEnabledError{}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,258 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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 (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"k8s.io/klog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Unknown = "-"
|
||||
DeploySuffix = "-Deployment"
|
||||
DaemonSuffix = "-DaemonSet"
|
||||
StateSuffix = "-StatefulSet"
|
||||
)
|
||||
|
||||
func (c *OpenPitrixClient) GetAppInfo(appId string) (string, string, string, error) {
|
||||
url := fmt.Sprintf("%s/v1/apps?app_id=%s", c.apiServer, appId)
|
||||
resp, err := c.makeHttpRequest("GET", url, "")
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, Unknown, Unknown, err
|
||||
}
|
||||
|
||||
var apps appList
|
||||
err = json.Unmarshal(resp, &apps)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, Unknown, Unknown, err
|
||||
}
|
||||
|
||||
if len(apps.Apps) == 0 {
|
||||
return Unknown, Unknown, Unknown, err
|
||||
}
|
||||
|
||||
return apps.Apps[0].ChartName, apps.Apps[0].RepoId, apps.Apps[0].AppId, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) GetCluster(clusterId string) (*Cluster, error) {
|
||||
url := fmt.Sprintf("%s/v1/clusters?cluster_id=%s", c.apiServer, clusterId)
|
||||
|
||||
resp, err := c.makeHttpRequest("GET", url, "")
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var clusterList ClusterList
|
||||
err = json.Unmarshal(resp, &clusterList)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(clusterList.Clusters) == 0 {
|
||||
return nil, fmt.Errorf("NotFound, clusterId:%s", clusterId)
|
||||
}
|
||||
|
||||
return &clusterList.Clusters[0], nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) ListClusters(runtimeId, searchWord, status string, limit, offset int) (*ClusterList, error) {
|
||||
|
||||
defaultStatus := "status=active&status=stopped&status=pending&status=ceased"
|
||||
|
||||
url := fmt.Sprintf("%s/v1/clusters?limit=%s&offset=%s", c.apiServer, strconv.Itoa(limit), strconv.Itoa(offset))
|
||||
|
||||
if searchWord != "" {
|
||||
url = fmt.Sprintf("%s&search_word=%s", url, searchWord)
|
||||
}
|
||||
|
||||
if status != "" {
|
||||
url = fmt.Sprintf("%s&status=%s", url, status)
|
||||
} else {
|
||||
url = fmt.Sprintf("%s&%s", url, defaultStatus)
|
||||
}
|
||||
|
||||
if len(runtimeId) > 0 {
|
||||
url = fmt.Sprintf("%s&runtime_id=%s", url, runtimeId)
|
||||
}
|
||||
|
||||
resp, err := c.makeHttpRequest("GET", url, "")
|
||||
if err != nil {
|
||||
klog.Errorf("request %s failed, reason: %s", url, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var clusterList ClusterList
|
||||
err = json.Unmarshal(resp, &clusterList)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &clusterList, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) GetRepo(repoId string) (string, error) {
|
||||
url := fmt.Sprintf("%s/v1/repos?repo_id=%s", c.apiServer, repoId)
|
||||
resp, err := c.makeHttpRequest("GET", url, "")
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
var repos repoList
|
||||
err = json.Unmarshal(resp, &repos)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
if len(repos.Repos) == 0 {
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
return repos.Repos[0].Name, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) GetVersion(versionId string) (string, error) {
|
||||
versionUrl := fmt.Sprintf("%s/v1/app_versions?version_id=%s", c.apiServer, versionId)
|
||||
resp, err := c.makeHttpRequest("GET", versionUrl, "")
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
var versions VersionList
|
||||
err = json.Unmarshal(resp, &versions)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
if len(versions.Versions) == 0 {
|
||||
return Unknown, nil
|
||||
}
|
||||
return versions.Versions[0].Name, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) GetRuntime(runtimeId string) (string, error) {
|
||||
|
||||
versionUrl := fmt.Sprintf("%s/v1/runtimes?runtime_id=%s", c.apiServer, runtimeId)
|
||||
resp, err := c.makeHttpRequest("GET", versionUrl, "")
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
var runtimes runtimeList
|
||||
err = json.Unmarshal(resp, &runtimes)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
if len(runtimes.Runtimes) == 0 {
|
||||
return Unknown, nil
|
||||
}
|
||||
|
||||
return runtimes.Runtimes[0].Zone, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) CreateCluster(request CreateClusterRequest) error {
|
||||
|
||||
versionUrl := fmt.Sprintf("%s/v1/clusters/create", c.apiServer)
|
||||
|
||||
data, err := json.Marshal(request)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
data, err = c.makeHttpRequest("POST", versionUrl, string(data))
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) DeleteCluster(request DeleteClusterRequest) error {
|
||||
|
||||
versionUrl := fmt.Sprintf("%s/v1/clusters/delete", c.apiServer)
|
||||
|
||||
data, err := json.Marshal(request)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
data, err = c.makeHttpRequest("POST", versionUrl, string(data))
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) makeHttpRequest(method, url, data string) ([]byte, error) {
|
||||
var req *http.Request
|
||||
|
||||
var err error
|
||||
if method == "GET" {
|
||||
req, err = http.NewRequest(method, url, nil)
|
||||
} else {
|
||||
req, err = http.NewRequest(method, url, strings.NewReader(data))
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", c.token)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Request to %s failed, method: %s,token: %s, reason: %s ", url, method, c.apiServer, err)
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
err = fmt.Errorf(string(body))
|
||||
}
|
||||
return body, err
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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 (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/utils/sliceutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewOpenPitrixClient(options *OpenPitrixOptions) (*OpenPitrixClient, error) {
|
||||
return &OpenPitrixClient{
|
||||
client: &http.Client{
|
||||
Timeout: time.Duration(3) * time.Second,
|
||||
},
|
||||
apiServer: options.APIServer,
|
||||
token: options.Token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) CreateRuntime(runtime *RunTime) error {
|
||||
|
||||
data, err := json.Marshal(runtime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/runtimes", c.apiServer), bytes.NewReader(data))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", c.token)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err = ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode > http.StatusOK {
|
||||
err = Error{resp.StatusCode, string(data)}
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) deleteClusters(clusters []cluster) error {
|
||||
clusterId := make([]string, 0)
|
||||
|
||||
for _, cluster := range clusters {
|
||||
if cluster.Status != "deleted" && cluster.Status != "deleting" && !sliceutil.HasString(clusterId, cluster.ClusterId) {
|
||||
clusterId = append(clusterId, cluster.ClusterId)
|
||||
}
|
||||
}
|
||||
|
||||
if len(clusterId) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
deleteRequest := struct {
|
||||
ClusterId []string `json:"cluster_id"`
|
||||
}{
|
||||
ClusterId: clusterId,
|
||||
}
|
||||
data, _ := json.Marshal(deleteRequest)
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/clusters/delete", c.apiServer), bytes.NewReader(data))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Authorization", c.token)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
data, err = ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode > http.StatusOK {
|
||||
err = Error{resp.StatusCode, string(data)}
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) listClusters(runtimeId string) ([]cluster, error) {
|
||||
limit := 200
|
||||
offset := 0
|
||||
clusters := make([]cluster, 0)
|
||||
for {
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/clusters?runtime_id=%s&limit=%d&offset=%d", c.apiServer, runtimeId, limit, offset), nil)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", c.token)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp.Body.Close()
|
||||
|
||||
if resp.StatusCode > http.StatusOK {
|
||||
err = Error{resp.StatusCode, string(data)}
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
listClusterResponse := struct {
|
||||
TotalCount int `json:"total_count"`
|
||||
ClusterSet []cluster `json:"cluster_set"`
|
||||
}{}
|
||||
err = json.Unmarshal(data, &listClusterResponse)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clusters = append(clusters, listClusterResponse.ClusterSet...)
|
||||
|
||||
if listClusterResponse.TotalCount <= limit+offset {
|
||||
break
|
||||
}
|
||||
|
||||
offset += limit
|
||||
}
|
||||
|
||||
return clusters, nil
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) DeleteRuntime(runtimeId string) error {
|
||||
clusters, err := c.listClusters(runtimeId)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.deleteClusters(clusters)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
if e, ok := err.(Error); ok {
|
||||
if e.status == http.StatusNotFound {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(e.message, "not exist") {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(e.message, "not found") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsDeleted(err error) bool {
|
||||
if e, ok := err.(Error); ok {
|
||||
if strings.Contains(e.message, "is [deleted]") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
243
pkg/simple/client/openpitrix/openpitrixclient.go
Normal file
243
pkg/simple/client/openpitrix/openpitrixclient.go
Normal file
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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 (
|
||||
"context"
|
||||
"fmt"
|
||||
"k8s.io/klog"
|
||||
"openpitrix.io/openpitrix/pkg/manager"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
"openpitrix.io/openpitrix/pkg/sender"
|
||||
"openpitrix.io/openpitrix/pkg/util/ctxutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
KubernetesProvider = "kubernetes"
|
||||
Unknown = "-"
|
||||
DeploySuffix = "-Deployment"
|
||||
DaemonSuffix = "-DaemonSet"
|
||||
StateSuffix = "-StatefulSet"
|
||||
SystemUsername = "system"
|
||||
SystemUserPath = ":system"
|
||||
)
|
||||
|
||||
type OpenPitrixClient struct {
|
||||
runtime pb.RuntimeManagerClient
|
||||
cluster pb.ClusterManagerClient
|
||||
app pb.AppManagerClient
|
||||
repo pb.RepoManagerClient
|
||||
category pb.CategoryManagerClient
|
||||
attachment pb.AttachmentManagerClient
|
||||
repoIndexer pb.RepoIndexerClient
|
||||
}
|
||||
|
||||
func parseToHostPort(endpoint string) (string, int, error) {
|
||||
args := strings.Split(endpoint, ":")
|
||||
if len(args) != 2 {
|
||||
return "", 0, fmt.Errorf("invalid server host: %s", endpoint)
|
||||
}
|
||||
host := args[0]
|
||||
port, err := strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return host, port, nil
|
||||
}
|
||||
|
||||
func newRuntimeManagerClient(endpoint string) (pb.RuntimeManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewRuntimeManagerClient(conn), nil
|
||||
}
|
||||
func newClusterManagerClient(endpoint string) (pb.ClusterManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewClusterManagerClient(conn), nil
|
||||
}
|
||||
func newCategoryManagerClient(endpoint string) (pb.CategoryManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewCategoryManagerClient(conn), nil
|
||||
}
|
||||
|
||||
func newAttachmentManagerClient(endpoint string) (pb.AttachmentManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewAttachmentManagerClient(conn), nil
|
||||
}
|
||||
|
||||
func newRepoManagerClient(endpoint string) (pb.RepoManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewRepoManagerClient(conn), nil
|
||||
}
|
||||
|
||||
func newRepoIndexer(endpoint string) (pb.RepoIndexerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewRepoIndexerClient(conn), nil
|
||||
}
|
||||
|
||||
func newAppManagerClient(endpoint string) (pb.AppManagerClient, error) {
|
||||
host, port, err := parseToHostPort(endpoint)
|
||||
conn, err := manager.NewClient(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pb.NewAppManagerClient(conn), nil
|
||||
}
|
||||
|
||||
func NewOpenPitrixClient(options *OpenPitrixOptions) (*OpenPitrixClient, error) {
|
||||
|
||||
runtimeMangerClient, err := newRuntimeManagerClient(options.RuntimeManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clusterManagerClient, err := newClusterManagerClient(options.ClusterManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repoManagerClient, err := newRepoManagerClient(options.RepoManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repoIndexerClient, err := newRepoIndexer(options.RepoIndexerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appManagerClient, err := newAppManagerClient(options.AppManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
categoryManagerClient, err := newCategoryManagerClient(options.CategoryManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attachmentManagerClient, err := newAttachmentManagerClient(options.AttachmentManagerEndpoint)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := OpenPitrixClient{
|
||||
runtime: runtimeMangerClient,
|
||||
cluster: clusterManagerClient,
|
||||
repo: repoManagerClient,
|
||||
app: appManagerClient,
|
||||
category: categoryManagerClient,
|
||||
attachment: attachmentManagerClient,
|
||||
repoIndexer: repoIndexerClient,
|
||||
}
|
||||
|
||||
return &client, nil
|
||||
}
|
||||
func (c *OpenPitrixClient) Runtime() pb.RuntimeManagerClient {
|
||||
return c.runtime
|
||||
}
|
||||
func (c *OpenPitrixClient) App() pb.AppManagerClient {
|
||||
return c.app
|
||||
}
|
||||
func (c *OpenPitrixClient) Cluster() pb.ClusterManagerClient {
|
||||
return c.cluster
|
||||
}
|
||||
func (c *OpenPitrixClient) Category() pb.CategoryManagerClient {
|
||||
return c.category
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) Repo() pb.RepoManagerClient {
|
||||
return c.repo
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) RepoIndexer() pb.RepoIndexerClient {
|
||||
return c.repoIndexer
|
||||
}
|
||||
|
||||
func (c *OpenPitrixClient) Attachment() pb.AttachmentManagerClient {
|
||||
return c.attachment
|
||||
}
|
||||
|
||||
func SystemContext() context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = ctxutil.ContextWithSender(ctx, sender.New(SystemUsername, SystemUserPath, ""))
|
||||
return ctx
|
||||
}
|
||||
func ContextWithUsername(username string) context.Context {
|
||||
ctx := context.Background()
|
||||
if username == "" {
|
||||
username = SystemUsername
|
||||
}
|
||||
ctx = ctxutil.ContextWithSender(ctx, sender.New(username, SystemUserPath, ""))
|
||||
return ctx
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
if strings.Contains(err.Error(), "not exist") {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(err.Error(), "not found") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsDeleted(err error) bool {
|
||||
if strings.Contains(err.Error(), "is [deleted]") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -7,29 +7,72 @@ import (
|
||||
)
|
||||
|
||||
type OpenPitrixOptions struct {
|
||||
APIServer string `json:"apiServer,omitempty" yaml:"apiServer"`
|
||||
Token string `json:"token,omitempty" yaml:"token"`
|
||||
RuntimeManagerEndpoint string `json:"runtimeManagerEndpoint,omitempty" yaml:"runtimeManagerEndpoint,omitempty"`
|
||||
ClusterManagerEndpoint string `json:"clusterManagerEndpoint,omitempty" yaml:"clusterManagerEndpoint,omitempty"`
|
||||
RepoManagerEndpoint string `json:"repoManagerEndpoint,omitempty" yaml:"repoManagerEndpoint,omitempty"`
|
||||
AppManagerEndpoint string `json:"appManagerEndpoint,omitempty" yaml:"appManagerEndpoint,omitempty"`
|
||||
CategoryManagerEndpoint string `json:"categoryManagerEndpoint,omitempty" yaml:"categoryManagerEndpoint,omitempty"`
|
||||
AttachmentManagerEndpoint string `json:"attachmentManagerEndpoint,omitempty" yaml:"attachmentManagerEndpoint,omitempty"`
|
||||
RepoIndexerEndpoint string `json:"repoIndexerEndpoint,omitempty" yaml:"repoIndexerEndpoint,omitempty"`
|
||||
}
|
||||
|
||||
func NewOpenPitrixOptions() *OpenPitrixOptions {
|
||||
return &OpenPitrixOptions{
|
||||
APIServer: "",
|
||||
Token: "",
|
||||
}
|
||||
return &OpenPitrixOptions{}
|
||||
}
|
||||
|
||||
func (s *OpenPitrixOptions) ApplyTo(options *OpenPitrixOptions) {
|
||||
if s.APIServer != "" {
|
||||
if options == nil {
|
||||
options = s
|
||||
return
|
||||
}
|
||||
if s.RuntimeManagerEndpoint != "" {
|
||||
reflectutils.Override(options, s)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *OpenPitrixOptions) Validate() []error {
|
||||
errs := []error{}
|
||||
var errs []error
|
||||
|
||||
if s.APIServer != "" {
|
||||
if s.Token == "" {
|
||||
errs = append(errs, fmt.Errorf("OpenPitrix access token cannot be empty"))
|
||||
if s.RuntimeManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.RuntimeManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.RuntimeManagerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.ClusterManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.ClusterManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.ClusterManagerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.RepoManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.RepoManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.RepoManagerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.RepoIndexerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.RepoIndexerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.RepoIndexerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.AppManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.AppManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.AppManagerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.CategoryManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.CategoryManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.CategoryManagerEndpoint))
|
||||
}
|
||||
}
|
||||
if s.AttachmentManagerEndpoint != "" {
|
||||
_, _, err := parseToHostPort(s.CategoryManagerEndpoint)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("invalid host port:%s", s.CategoryManagerEndpoint))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +80,24 @@ func (s *OpenPitrixOptions) Validate() []error {
|
||||
}
|
||||
|
||||
func (s *OpenPitrixOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&s.APIServer, "openpitrix-apiserver", s.APIServer, ""+
|
||||
"OpenPitrix api gateway endpoint, if left blank, following options will be ignored.")
|
||||
fs.StringVar(&s.RuntimeManagerEndpoint, "openpitrix-runtime-manager-endpoint", s.RuntimeManagerEndpoint, ""+
|
||||
"OpenPitrix runtime manager endpoint")
|
||||
|
||||
fs.StringVar(&s.Token, "openpitrix-token", s.Token, ""+
|
||||
"OpenPitrix api access token.")
|
||||
fs.StringVar(&s.AppManagerEndpoint, "openpitrix-app-manager-endpoint", s.AppManagerEndpoint, ""+
|
||||
"OpenPitrix app manager endpoint")
|
||||
|
||||
fs.StringVar(&s.ClusterManagerEndpoint, "openpitrix-cluster-manager-endpoint", s.ClusterManagerEndpoint, ""+
|
||||
"OpenPitrix cluster manager endpoint")
|
||||
|
||||
fs.StringVar(&s.CategoryManagerEndpoint, "openpitrix-category-manager-endpoint", s.CategoryManagerEndpoint, ""+
|
||||
"OpenPitrix category manager endpoint")
|
||||
|
||||
fs.StringVar(&s.RepoManagerEndpoint, "openpitrix-repo-manager-endpoint", s.RepoManagerEndpoint, ""+
|
||||
"OpenPitrix repo manager endpoint")
|
||||
|
||||
fs.StringVar(&s.RepoIndexerEndpoint, "openpitrix-repo-indexer-endpoint", s.RepoIndexerEndpoint, ""+
|
||||
"OpenPitrix repo indexer endpoint")
|
||||
|
||||
fs.StringVar(&s.AttachmentManagerEndpoint, "openpitrix-attachment-manager-endpoint", s.AttachmentManagerEndpoint, ""+
|
||||
"OpenPitrix attachment manager endpoint")
|
||||
}
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
package openpitrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
ClusterID string `json:"cluster_id"`
|
||||
Name string `json:"name"`
|
||||
AppID string `json:"app_id"`
|
||||
VersionID string `json:"version_id"`
|
||||
Status string `json:"status"`
|
||||
UpdateTime time.Time `json:"status_time"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
RunTimeId string `json:"runtime_id"`
|
||||
Description string `json:"description"`
|
||||
ClusterRoleSets []ClusterRole `json:"cluster_role_set"`
|
||||
}
|
||||
|
||||
type ClusterRole struct {
|
||||
ClusterID string `json:"cluster_id"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type ClusterList struct {
|
||||
Total int `json:"total_count"`
|
||||
Clusters []Cluster `json:"cluster_set"`
|
||||
}
|
||||
|
||||
type VersionList struct {
|
||||
Total int `json:"total_count"`
|
||||
Versions []version `json:"app_version_set"`
|
||||
}
|
||||
|
||||
type version struct {
|
||||
Name string `json:"name"`
|
||||
VersionID string `json:"version_id"`
|
||||
}
|
||||
|
||||
type runtime struct {
|
||||
RuntimeID string `json:"runtime_id"`
|
||||
Zone string `json:"zone"`
|
||||
}
|
||||
|
||||
type runtimeList struct {
|
||||
Total int `json:"total_count"`
|
||||
Runtimes []runtime `json:"runtime_set"`
|
||||
}
|
||||
|
||||
type app struct {
|
||||
AppId string `json:"app_id"`
|
||||
Name string `json:"name"`
|
||||
ChartName string `json:"chart_name"`
|
||||
RepoId string `json:"repo_id"`
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
RepoId string `json:"repo_id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type appList struct {
|
||||
Total int `json:"total_count"`
|
||||
Apps []app `json:"app_set"`
|
||||
}
|
||||
|
||||
type repoList struct {
|
||||
Total int `json:"total_count"`
|
||||
Repos []repo `json:"repo_set"`
|
||||
}
|
||||
|
||||
type CreateClusterRequest struct {
|
||||
AppId string `json:"app_id" description:"ID of app to run in cluster, e.g. app-AA3A3y3zEgEM"`
|
||||
VersionId string `json:"version_id" description:"app version, e.g. appv-154gXYx5RKRp"`
|
||||
RuntimeId string `json:"runtime_id" description:"ID of runtime, e.g. runtime-wWwXL0LzWqEr"`
|
||||
Conf string `json:"conf" description:"conf a json string, include cpu, memory info of cluster"`
|
||||
}
|
||||
|
||||
type DeleteClusterRequest struct {
|
||||
ClusterId []string `json:"cluster_id" description:"cluster ID"`
|
||||
}
|
||||
|
||||
type RunTime struct {
|
||||
RuntimeId string `json:"runtime_id"`
|
||||
RuntimeUrl string `json:"runtime_url"`
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider"`
|
||||
Zone string `json:"zone"`
|
||||
RuntimeCredential string `json:"runtime_credential"`
|
||||
}
|
||||
|
||||
type Interface interface {
|
||||
CreateRuntime(runtime *RunTime) error
|
||||
DeleteRuntime(runtimeId string) error
|
||||
}
|
||||
type cluster struct {
|
||||
Status string `json:"status"`
|
||||
ClusterId string `json:"cluster_id"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
status int
|
||||
message string
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return fmt.Sprintf("status: %d,message: %s", e.status, e.message)
|
||||
}
|
||||
|
||||
type OpenPitrixClient struct {
|
||||
client *http.Client
|
||||
apiServer string
|
||||
token string
|
||||
}
|
||||
Reference in New Issue
Block a user