github.com/polarismesh/polaris@v1.17.8/apiserver/xdsserverv3/resource/dump.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package resource
    19  
    20  import (
    21  	"encoding/json"
    22  	"sort"
    23  
    24  	"github.com/envoyproxy/go-control-plane/pkg/cache/types"
    25  	"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
    26  	res "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
    27  	"google.golang.org/protobuf/encoding/protojson"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  func DumpSnapShot(snapshot cache.ResourceSnapshot) []byte {
    32  	return YamlEncode(map[string]interface{}{
    33  		"endpoints": ToJSONArray(snapshot.GetResources(res.EndpointType)),
    34  		"clusters":  ToJSONArray(snapshot.GetResources(res.ClusterType)),
    35  		"routers":   ToJSONArray(snapshot.GetResources(res.RouteType)),
    36  		"listeners": ToJSONArray(snapshot.GetResources(res.ListenerType)),
    37  	})
    38  }
    39  
    40  func DumpSnapShotJSON(snapshot cache.ResourceSnapshot) []byte {
    41  	data, err := json.Marshal(map[string]interface{}{
    42  		"endpoints": ToJSONArray(snapshot.GetResources(res.EndpointType)),
    43  		"clusters":  ToJSONArray(snapshot.GetResources(res.ClusterType)),
    44  		"routers":   ToJSONArray(snapshot.GetResources(res.RouteType)),
    45  		"listeners": ToJSONArray(snapshot.GetResources(res.ListenerType)),
    46  	})
    47  	if err != nil {
    48  		return nil
    49  	}
    50  	return data
    51  }
    52  
    53  func YamlEncode(any interface{}) []byte {
    54  	data, err := json.Marshal(any)
    55  	if err != nil {
    56  		log.Errorf("yaml encode json marshal failed error %v", err)
    57  		return nil
    58  	}
    59  	o := make(map[string]interface{})
    60  	if err = json.Unmarshal(data, &o); err != nil {
    61  		log.Errorf("yaml encode json unmarshal failed error %v", err)
    62  		return nil
    63  	}
    64  	if data, err = yaml.Marshal(o); err != nil {
    65  		log.Errorf("yaml encode yaml marshal failed error %v", err)
    66  		return nil
    67  	}
    68  	return data
    69  }
    70  
    71  func ToJSONArray(resources map[string]types.Resource) []json.RawMessage {
    72  	list := make([]resouceWithName, 0, len(resources))
    73  	for name, x := range resources {
    74  		list = append(list, resouceWithName{resource: x, name: name})
    75  	}
    76  
    77  	sort.Slice(list, func(i, j int) bool {
    78  		return list[i].name < list[j].name
    79  	})
    80  
    81  	messages := make([]json.RawMessage, 0, len(resources))
    82  	for _, x := range list {
    83  		data, _ := protojson.Marshal(x.resource)
    84  		messages = append(messages, data)
    85  	}
    86  	return messages
    87  }
    88  
    89  type resouceWithName struct {
    90  	resource types.Resource
    91  	name     string
    92  }