/
converter.go
59 lines (49 loc) · 1.59 KB
/
converter.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
package integration
import (
"encoding/json"
"github.com/caicloud/nirvana/log"
core_v1 "k8s.io/api/core/v1"
"github.com/caicloud/cyclone/pkg/meta"
api "github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/common"
)
// ToSecret converts an integration to k8s secret
func ToSecret(tenant string, in *api.Integration) (*core_v1.Secret, error) {
objectMeta := in.ObjectMeta
objectMeta.Name = GetSecretName(in.Name)
if objectMeta.Labels == nil {
objectMeta.Labels = make(map[string]string)
}
objectMeta.Labels[meta.LabelIntegrationType] = string(in.Spec.Type)
if in.Spec.Type == api.Cluster && in.Spec.Cluster != nil {
if in.Spec.Cluster.IsWorkerCluster {
objectMeta.Labels = meta.AddSchedulableClusterLabel(objectMeta.Labels)
} else {
delete(objectMeta.Labels, meta.LabelIntegrationSchedulableCluster)
}
}
integration, err := json.Marshal(in.Spec)
if err != nil {
log.Errorf("Marshal integration %v for tenant %s error %v", in.Name, tenant, err)
return nil, err
}
data := make(map[string][]byte)
data[common.SecretKeyIntegration] = integration
return &core_v1.Secret{
ObjectMeta: objectMeta,
Data: data,
}, nil
}
// FromSecret converts a secret to a integration
func FromSecret(secret *core_v1.Secret) (*api.Integration, error) {
integration := &api.Integration{
ObjectMeta: secret.ObjectMeta,
}
// retrieve integration name
integration.Name = GetIntegrationName(secret.Name)
err := json.Unmarshal(secret.Data[common.SecretKeyIntegration], &integration.Spec)
if err != nil {
return nil, err
}
return integration, nil
}