1
0
mirror of https://github.com/rancher/types.git synced 2025-08-31 21:00:16 +00:00

Add HPA type to schema

This commit is contained in:
orangedeng
2019-03-28 21:16:41 +08:00
committed by Craig Jellick
parent 02110b7332
commit 5694548201
7 changed files with 317 additions and 1 deletions

View File

@@ -0,0 +1,76 @@
package mapper
import (
"fmt"
"strings"
"github.com/rancher/norman/types/values"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)
var (
kindMap = map[string]string{
"deployment": "Deployment",
"replicationcontroller": "ReplicationController",
"statefulset": "StatefulSet",
"daemonset": "DaemonSet",
"job": "Job",
"cronjob": "CronJob",
"replicaset": "ReplicaSet",
}
groupVersionMap = map[string]string{
"deployment": "apps/v1beta2",
"replicationcontroller": "core/v1",
"statefulset": "apps/v1beta2",
"daemonset": "apps/v1beta2",
"job": "batch/v1",
"cronjob": "batch/v1beta1",
"replicaset": "apps/v1beta2",
}
)
type CrossVersionObjectToWorkload struct {
Field string
}
func (c CrossVersionObjectToWorkload) ToInternal(data map[string]interface{}) error {
obj, ok := values.GetValue(data, strings.Split(c.Field, "/")...)
if !ok {
return nil
}
workloadID := convert.ToString(obj)
parts := strings.SplitN(workloadID, ":", 3)
newObj := map[string]interface{}{
"kind": getKind(parts[0]),
"apiVersion": groupVersionMap[parts[0]],
"name": parts[2],
}
values.PutValue(data, newObj, strings.Split(c.Field, "/")...)
return nil
}
func (c CrossVersionObjectToWorkload) FromInternal(data map[string]interface{}) {
obj, ok := values.GetValue(data, strings.Split(c.Field, "/")...)
if !ok {
return
}
cvo := convert.ToMapInterface(obj)
ns := convert.ToString(data["namespaceId"])
values.PutValue(data,
fmt.Sprintf("%s:%s:%s",
strings.ToLower(convert.ToString(cvo["kind"])),
ns,
convert.ToString(cvo["name"])),
strings.Split(c.Field, "/")...,
)
}
func (c CrossVersionObjectToWorkload) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return nil
}
func getKind(i string) string {
return kindMap[i]
}

View File

@@ -0,0 +1,111 @@
package mapper
import (
"fmt"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/definition"
"github.com/rancher/norman/types/mapper"
)
func NewMergeListByIndexMapper(From, To string, Ignores ...string) *MergeListByIndexMapper {
rtn := MergeListByIndexMapper{
From: From,
To: To,
Ignore: make(map[string]struct{}),
fromFields: []string{},
}
for _, Ignore := range Ignores {
rtn.Ignore[Ignore] = struct{}{}
}
return &rtn
}
type MergeListByIndexMapper struct {
From string
To string
Ignore map[string]struct{}
fromFields []string
}
func (m *MergeListByIndexMapper) FromInternal(data map[string]interface{}) {
fromObj, ok := data[m.From]
if !ok {
return
}
toObj, ok := data[m.To]
if !ok {
return
}
fromList := convert.ToMapSlice(fromObj)
toList := convert.ToMapSlice(toObj)
for i := 0; i < len(fromList) && i < len(toList); i++ {
fromItem := fromList[i]
toItem := toList[i]
for key, value := range fromItem {
if _, ignore := m.Ignore[key]; ignore {
continue
}
toItem[key] = value
}
}
delete(data, m.From)
}
func (m *MergeListByIndexMapper) ToInternal(data map[string]interface{}) error {
toObj, ok := data[m.To]
if !ok {
return nil
}
if _, ok = data[m.From]; ok {
return fmt.Errorf("field %s should not exist", m.From)
}
toList := convert.ToMapSlice(toObj)
var fromList []map[string]interface{}
for _, toItem := range toList {
obj := make(map[string]interface{})
for _, field := range m.fromFields {
value, ok := toItem[field]
if !ok {
continue
}
obj[field] = value
if _, ok := m.Ignore[field]; !ok {
delete(toItem, field)
}
}
fromList = append(fromList, obj)
}
data[m.From] = fromList
return nil
}
func (m *MergeListByIndexMapper) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
if err := mapper.ValidateField(m.From, schema); err != nil {
return err
}
fromType := schema.ResourceFields[m.From].Type
if !definition.IsArrayType(fromType) {
return fmt.Errorf("type of field %s in schema %s is not array", m.From, schema.CodeName)
}
fromSchema := schemas.Schema(&schema.Version, definition.SubType(fromType))
for field := range fromSchema.ResourceFields {
m.fromFields = append(m.fromFields, field)
}
if err := mapper.ValidateField(m.To, schema); err != nil {
return err
}
toType := schema.ResourceFields[m.To].Type
if !definition.IsArrayType(toType) {
return fmt.Errorf("type of field %s in schema %s is not array", m.To, schema.CodeName)
}
delete(schema.ResourceFields, m.From)
return nil
}

View File

@@ -0,0 +1,38 @@
package mapper
import (
"reflect"
"testing"
)
var (
metrics = []map[string]interface{}{
{"type": "Resource", "resource": "abc"},
{"type": "Object", "object": "def"},
}
currentMetrics = []map[string]interface{}{
{"type": "Resource", "currentResource": "tuvw"},
{"type": "Object", "currentObject": "xyz"},
}
origin = map[string]interface{}{
"metrics": metrics,
"currentMetrics": currentMetrics,
}
)
func Test_MergeList(t *testing.T) {
mapper := NewMergeListByIndexMapper("currentMetrics", "metrics", "type")
mapper.fromFields = []string{"type", "currentResource", "currentObject"}
internal := map[string]interface{}{
"metrics": metrics,
"currentMetrics": currentMetrics,
}
mapper.FromInternal(internal)
if err := mapper.ToInternal(internal); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(internal, origin) {
t.Fatal("merge list not match after parse")
}
}