代码拉取完成,页面将自动刷新
package node
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/encryptedstore"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
configKey = "extractedConfig"
)
type DriverHandlers struct {
NodeDriverClient v3.NodeDriverInterface
}
func (h *DriverHandlers) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
m, err := h.NodeDriverClient.GetNamespaced("", apiContext.ID, metav1.GetOptions{})
if err != nil {
return err
}
switch actionName {
case "activate":
m.Spec.Active = true
v3.NodeDriverConditionActive.Unknown(m)
case "deactivate":
m.Spec.Active = false
v3.NodeDriverConditionInactive.Unknown(m)
}
_, err = h.NodeDriverClient.Update(m)
if err != nil {
return err
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, &data); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
// Formatter for NodeDriver
func (h *DriverHandlers) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "deactivate")
}
type DriverHandler struct {
SecretStore *encryptedstore.GenericEncryptedStore
}
func (h DriverHandler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
var node client.Node
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, &node); err != nil {
return err
}
nodeID := strings.Split(node.ID, ":")[1]
secret, err := h.SecretStore.Get(nodeID)
if err != nil {
return err
}
data, err := base64.StdEncoding.DecodeString(secret[configKey])
if err != nil {
return err
}
gzipReader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return err
}
tarReader := tar.NewReader(gzipReader)
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for {
header, err := tarReader.Next()
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("error reinitializing config (tarRead.Next): %v", err)
}
parts := strings.Split(header.Name, "/")
if len(parts) != 4 {
continue
}
if parts[3] == "config.json" {
continue
}
fh := &zip.FileHeader{}
fh.Name = fmt.Sprintf("%s/%s", parts[2], parts[3])
fh.SetMode(0400)
file, err := w.CreateHeader(fh)
if err != nil {
return err
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, tarReader)
if err != nil {
return err
}
_, err = file.Write(buf.Bytes())
if err != nil {
return err
}
}
if err := w.Close(); err != nil {
return err
}
apiContext.Response.Header().Set("Content-Length", strconv.Itoa(len(buf.Bytes())))
apiContext.Response.Header().Set("Content-Type", "application/octet-stream")
apiContext.Response.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", node.RequestedHostname))
apiContext.Response.Header().Set("Cache-Control", "private")
apiContext.Response.Header().Set("Pragma", "private")
apiContext.Response.Header().Set("Expires", "Wed 24 Feb 1982 18:42:00 GMT")
_, err = apiContext.Response.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。