-
Notifications
You must be signed in to change notification settings - Fork 71
/
stats.go
106 lines (87 loc) · 2.58 KB
/
stats.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
package transformer
import (
"context"
"fmt"
"github.com/armory/spinnaker-operator/pkg/apis/spinnaker/interfaces"
"github.com/armory/spinnaker-operator/pkg/generated"
"github.com/armory/spinnaker-operator/pkg/inspect"
"github.com/armory/spinnaker-operator/pkg/util"
"github.com/armory/spinnaker-operator/pkg/version"
"github.com/ghodss/yaml"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
echoConfigFile = "echo.yml"
kubernetesOperator = "kubernetes_operator"
statsKey = "stats"
)
type statsTransformer struct {
log logr.Logger
}
type StatsTransformerGenerator struct{}
func (t *StatsTransformerGenerator) NewTransformer(svc interfaces.SpinnakerService,
client client.Client, log logr.Logger, scheme *runtime.Scheme) (Transformer, error) {
tr := statsTransformer{log: log}
return &tr, nil
}
func (t *StatsTransformerGenerator) GetName() string {
return "Stats"
}
func (t *statsTransformer) TransformConfig(ctx context.Context) error {
return nil
}
func (t *statsTransformer) TransformManifests(ctx context.Context, gen *generated.SpinnakerGeneratedConfig) error {
n := "echo"
config, ok := gen.Config[n]
if !ok {
return nil
}
sec := util.GetSecretConfigFromConfig(config, n)
if sec == nil {
return nil
}
err := mapStatsSecret(sec)
if err != nil {
t.log.Info(fmt.Sprintf("Error setting stats.DeploymentMethod: %s, ignoring", err))
return nil
}
return nil
}
// mapStatsSecret goes through echoConfigFile and set deployment method information
func mapStatsSecret(secret *v1.Secret) error {
for key := range secret.Data {
if echoConfigFile == key {
// Attempt to deserialize as YAML
m := make(map[string]interface{})
if err := yaml.Unmarshal(secret.Data[key], &m); err != nil {
return err
}
data, err := setStatsDeploymentMethod(m)
if err != nil {
return err
}
secret.Data[key] = data
}
}
return nil
}
func setStatsDeploymentMethod(row map[string]interface{}) ([]byte, error) {
// read stats property and map content
rowContent := make(map[string]interface{})
if err := inspect.Convert(row[statsKey], &rowContent); err != nil {
return nil, err
}
if len(rowContent) == 0 {
rowContent = make(map[string]interface{})
}
deploymentmethodContent := make(map[string]interface{})
deploymentmethodContent["type"] = kubernetesOperator
deploymentmethodContent["version"] = version.GetOperatorVersion()
rowContent["deploymentMethod"] = deploymentmethodContent
// override stats property
row[statsKey] = rowContent
return yaml.Marshal(row)
}