/
main.go
150 lines (134 loc) · 3.81 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"github.com/razorpay/devstack/hooks/secret_cloner/controllers"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"strings"
)
// Params defines the configuration for the application actions
type Params struct {
NameFlag string
DebugFlag bool
IsLocal bool
}
//SecretConfig defines the details of secret to be acted upon
type SecretConfig struct {
Action string
Namespace string
SecretName string
SecretSuffix string
UpdateEntries map[string]Secret
Ttl string
}
//Secret stores the secret details
type Secret struct {
Key string
Value string
}
var (
params Params
secretData SecretConfig
failure bool
logger *zap.Logger
err error
)
func main(){
params.DebugFlag = *flag.Bool("debug",false,"To log all events")
params.IsLocal = *flag.Bool("local",false,"Local debuggig")
flag.Parse()
initialize()
initializeConfig()
if strings.EqualFold(secretData.Action,"clone"){
processClone()
} else if strings.EqualFold(secretData.Action,"update"){
processUpdate()
} else {
logger.Info("Unsupported Action")
}
}
//initializeConfig initializes the config using viper
func initializeConfig() {
//load the config from env in case of cluster execution
viper.SetConfigName("app")
viper.AddConfigPath("./config")
viper.SetConfigType("yaml")
viper.AutomaticEnv()
viper.ReadInConfig()
viper.Unmarshal(&secretData)
}
//initialize the application
func initialize(){
var cfg zap.Config
if params.DebugFlag {
cfg = getConfig(zapcore.DebugLevel)
} else {
cfg = getConfig(zapcore.InfoLevel)
}
logger, err = cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync()
}
//getConfig internal method for reading zap configuration
func getConfig(level zapcore.Level) zap.Config {
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(level),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message"},
}
return cfg
}
//processClone clones the secret , deletes if the secret already exists
func processClone(){
err := controllers.New(params.IsLocal)
if err != nil{
logger.Info("Initialziation error. Error : "+ err.Error())
os.Exit(1)
}
oldSecret , err := controllers.GetSecret(secretData.Namespace,secretData.SecretName)
if err != nil {
logger.Info("The secret with the name not found exiting " + secretData.SecretName)
logger.Debug(err.Error())
os.Exit(1)
}
annotations := map[string]string{"janitor/ttl":secretData.Ttl}
metaObject := metaV1.ObjectMeta{Name: secretData.SecretName+"-"+secretData.SecretSuffix , Annotations: annotations}
newSecret := v1.Secret{Data: oldSecret.Data,ObjectMeta: metaObject}
secret ,_ := controllers.GetSecret(secretData.Namespace,newSecret.GetName())
if secret != nil {
err := controllers.DeleteSecret(secretData.Namespace, newSecret.GetName())
if err != nil {
logger.Info("The deletion of secret failed Error: "+ err.Error())
os.Exit(1)
}
}
err = controllers.CreateSecret(secretData.Namespace, &newSecret)
if err != nil {
logger.Info("The creation of secert failed Error: "+err.Error())
os.Exit(1)
}
}
//processUpdate updates the secrets with the provided values
func processUpdate(){
failure = false
for _,value := range secretData.UpdateEntries {
err := controllers.UpdateSecret(secretData.Namespace,secretData.SecretName,value.Key,value.Value)
if err != nil {
logger.Info("The creation of secert failed Error: "+err.Error())
failure=true
}
}
if failure {
logger.Info("Failing as some of the components creation failed with error , please check the above logs")
os.Exit(1)
}
}