forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
130 lines (119 loc) · 3.56 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// 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 clusterfile
import (
"bytes"
"fmt"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/types/v1beta1"
fileutil "github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/logger"
)
func Cluster(filepath string) (clusters []v1beta1.Cluster, err error) {
decodeClusters, err := decodeCRD(filepath, constants.Cluster)
if err != nil {
return nil, fmt.Errorf("failed to decode cluster from %s, %v", filepath, err)
}
clusters = decodeClusters.([]v1beta1.Cluster)
return
}
func Configs(filepath string) (configs []v1beta1.Config, err error) {
decodeConfigs, err := decodeCRD(filepath, constants.Config)
if err != nil {
return nil, fmt.Errorf("failed to decode config from %s, %v", filepath, err)
}
configs = decodeConfigs.([]v1beta1.Config)
return
}
func decodeCRD(filepath string, kind string) (out interface{}, err error) {
data, err := fileutil.ReadAll(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read file %v", err)
}
return CRDForBytes(data, kind)
}
func CRDForBytes(data []byte, kind string) (out interface{}, err error) {
keyFunc := func(v interface{}) string {
if acc, ok := v.(metav1.ObjectMetaAccessor); ok {
return fmt.Sprintf("%s/%s", kind, acc.GetObjectMeta().GetName())
}
return fmt.Sprintf("%s/unknown", kind)
}
var (
clusters []v1beta1.Cluster
configs []v1beta1.Config
tmp = make(map[string]int)
)
r := bytes.NewReader(data)
d := yaml.NewYAMLOrJSONDecoder(r, 4096)
for {
ext := runtime.RawExtension{}
if err = d.Decode(&ext); err != nil {
if err == io.EOF {
break
}
return nil, err
}
// TODO: This needs to be able to handle object in other encodings and schemas.
ext.Raw = bytes.TrimSpace(ext.Raw)
if len(ext.Raw) == 0 || bytes.Equal(ext.Raw, []byte("null")) {
continue
}
typeMeta := runtime.TypeMeta{}
if err := yaml.Unmarshal(ext.Raw, &typeMeta); err != nil {
return nil, err
}
if typeMeta.Kind != kind {
continue
}
// ext.Raw
switch kind {
case constants.Cluster:
cluster := v1beta1.Cluster{}
err = yaml.Unmarshal(ext.Raw, &cluster)
if err != nil {
return nil, fmt.Errorf("decode cluster failed %v", err)
}
k := keyFunc(&cluster)
if idx, ok := tmp[k]; !ok {
tmp[k] = len(tmp)
clusters = append(clusters, cluster)
} else {
logger.Warn("duplicate resource: %s, replace with new one", k)
clusters[idx] = cluster
}
out = clusters
case constants.Config:
config := v1beta1.Config{}
err = yaml.Unmarshal(ext.Raw, &config)
if err != nil {
return nil, fmt.Errorf("decode config failed %v", err)
}
k := keyFunc(&config)
if idx, ok := tmp[k]; !ok {
tmp[k] = len(tmp)
configs = append(configs, config)
} else {
logger.Warn("duplicate resource: %s, replace with new one", k)
configs[idx] = config
}
out = configs
}
}
return out, nil
}