37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
catalog.go 5.32 KB
一键复制 编辑 原始数据 按行查看 历史
package catalog
import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/ghodss/yaml"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/compose"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "refresh")
resource.Links["exportYaml"] = apiContext.URLBuilder.Link("exportYaml", resource)
}
func CollectionFormatter(request *types.APIContext, collection *types.GenericCollection) {
collection.AddAction(request, "refresh")
}
type ActionHandler struct {
CatalogClient v3.CatalogInterface
ProjectCatalogClient v3.ProjectCatalogInterface
ClusterCatalogClient v3.ClusterCatalogInterface
}
func (a ActionHandler) RefreshActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
if actionName != "refresh" {
return httperror.NewAPIError(httperror.NotFound, "not found")
}
catalogs := []v3.Catalog{}
if apiContext.ID != "" {
catalog, err := a.CatalogClient.Get(apiContext.ID, metav1.GetOptions{})
if err != nil {
return err
}
catalogs = append(catalogs, *catalog)
} else {
catalogList, err := a.CatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, catalog := range catalogList.Items {
catalogs = append(catalogs, catalog)
}
}
for _, catalog := range catalogs {
catalog.Status.LastRefreshTimestamp = time.Now().Format(time.RFC3339)
v3.CatalogConditionRefreshed.Unknown(&catalog)
if _, err := a.CatalogClient.Update(&catalog); err != nil {
return err
}
}
data := map[string]interface{}{
"catalogs": catalogs,
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
func (a ActionHandler) ExportYamlHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "exportyaml":
catalog, err := a.CatalogClient.Get(apiContext.ID, metav1.GetOptions{})
if err != nil {
return rpctypes.ErrGRPCStopped
}
topkey := compose.Config{}
topkey.Version = "v3"
ca := client.Catalog{}
if err := convert.ToObj(catalog.Spec, &ca); err != nil {
return err
}
topkey.Catalogs = map[string]client.Catalog{}
topkey.Catalogs[catalog.Name] = ca
m, err := convert.EncodeToMap(topkey)
if err != nil {
return err
}
delete(m["catalogs"].(map[string]interface{})[catalog.Name].(map[string]interface{}), "actions")
delete(m["catalogs"].(map[string]interface{})[catalog.Name].(map[string]interface{}), "links")
delete(m["catalogs"].(map[string]interface{})[catalog.Name].(map[string]interface{}), "password")
data, err := json.Marshal(m)
if err != nil {
return err
}
buf, err := yaml.JSONToYAML(data)
if err != nil {
return err
}
reader := bytes.NewReader(buf)
apiContext.Response.Header().Set("Content-Type", "text/yaml")
http.ServeContent(apiContext.Response, apiContext.Request, "exportYaml", time.Now(), reader)
return nil
}
return nil
}
func (a ActionHandler) RefreshProjectCatalogActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
if actionName != "refresh" {
return httperror.NewAPIError(httperror.NotFound, "not found")
}
prjCatalogs := []v3.ProjectCatalog{}
if apiContext.ID != "" {
ns, name := ref.Parse(apiContext.ID)
catalog, err := a.ProjectCatalogClient.GetNamespaced(ns, name, metav1.GetOptions{})
if err != nil {
return err
}
prjCatalogs = append(prjCatalogs, *catalog)
} else {
catalogList, err := a.ProjectCatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, catalog := range catalogList.Items {
prjCatalogs = append(prjCatalogs, catalog)
}
}
for _, catalog := range prjCatalogs {
catalog.Status.LastRefreshTimestamp = time.Now().Format(time.RFC3339)
v3.CatalogConditionRefreshed.Unknown(&catalog)
if _, err := a.ProjectCatalogClient.Update(&catalog); err != nil {
return err
}
}
data := map[string]interface{}{
"catalogs": prjCatalogs,
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
func (a ActionHandler) RefreshClusterCatalogActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
if actionName != "refresh" {
return httperror.NewAPIError(httperror.NotFound, "not found")
}
clCatalogs := []v3.ClusterCatalog{}
if apiContext.ID != "" {
ns, name := ref.Parse(apiContext.ID)
catalog, err := a.ClusterCatalogClient.GetNamespaced(ns, name, metav1.GetOptions{})
if err != nil {
return err
}
clCatalogs = append(clCatalogs, *catalog)
} else {
catalogList, err := a.ClusterCatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, catalog := range catalogList.Items {
clCatalogs = append(clCatalogs, catalog)
}
}
for _, catalog := range clCatalogs {
catalog.Status.LastRefreshTimestamp = time.Now().Format(time.RFC3339)
v3.CatalogConditionRefreshed.Unknown(&catalog)
if _, err := a.ClusterCatalogClient.Update(&catalog); err != nil {
return err
}
}
data := map[string]interface{}{
"catalogs": clCatalogs,
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.2-rc12

搜索帮助