forked from nocalhost/nocalhost
/
config_get.go
201 lines (179 loc) · 6.16 KB
/
config_get.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package cmds
import (
"fmt"
"github.com/lsutils/nocalhost/cmd/nhctl/cmds/common"
"github.com/lsutils/nocalhost/internal/nhctl/appmeta"
"github.com/lsutils/nocalhost/internal/nhctl/dev_dir"
"github.com/lsutils/nocalhost/internal/nhctl/fp"
"github.com/lsutils/nocalhost/internal/nhctl/profile"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
var notificationPrefix = `# This is the runtime configuration which stored in K8s cluster. Modifications
# to the development configuration will take effect the next time you enter
# the DevMode, and modification will share with all those who use this cluster.
#
# If you want to customized personal configuration, you can create a configuration
# file named config.yaml in the root directory of your project under the
# folder .nocalhost (/.nocalhost/config.yaml). It will become part of your
# project, you can easily share configuration with other developers, or
# develop on any other devices using this personal configuration.
#`
var svcNotificationTips = `
# Tips: You can paste the configuration follow into
# %s
#`
var notificationSuffix = `
# In addition, if you want to config multi service in same config.yaml, or use
# the Server-version of Nocalhost, you can also configure under the definition
# of the application, such as:
# https://nocalhost.dev/docs/config/config-deployment-quickstart
#`
var svcNotificationTipsLocalLoaded = `# Tips: This configuration is a in-memory replica of local file:
#
# '%s'
#
# You should modify your configuration in local file, and the modification will
# take effect immediately. (Dev modification will take effect the next time you enter the DevMode)
#`
var svcNotificationTipsCmLoaded = `# Tips: This configuration is a in-memory replica of configmap:
#
# '%s'
#
# You should modify your configuration in configmap, and the modification will
# take effect immediately. (Dev modification will take effect the next time you enter the DevMode)
#`
var svcNotificationTipsAnnotationLoaded = `# Tips: This configuration is a in-memory replica of annotation:
#
# annotations:
# %s: |
# [Your Config]
#
# You should modify your configuration in resource's annotation', and the modification will
# take effect immediately. (Dev modification will take effect the next time you enter the DevMode)
#`
func init() {
configGetCmd.Flags().StringVarP(
&commonFlags.SvcName, "deployment", "d", "",
"k8s deployment which your developing service exists",
)
configGetCmd.Flags().StringVarP(
&common.ServiceType, "controller-type", "t", "deployment",
"kind of k8s controller,such as deployment,statefulSet",
)
configGetCmd.Flags().BoolVar(
&commonFlags.AppConfig, "app-config", false,
"get application config",
)
configCmd.AddCommand(configGetCmd)
}
type ConfigForPlugin struct {
Services []*profile.ServiceConfigV2 `json:"services" yaml:"services"`
}
var configGetCmd = &cobra.Command{
Use: "get [Name]",
Short: "get application/service config",
Long: "get application/service config",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.Errorf("%q requires at least 1 argument\n", cmd.CommandPath())
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
commonFlags.AppName = args[0]
nocalhostApp, err := common.InitAppMutate(commonFlags.AppName)
if err != nil {
log.Logf("init app:%s on namespace: %s, error: %v", commonFlags.AppName, common.NameSpace, err)
return
}
// get application config
if commonFlags.AppConfig {
applicationConfig := nocalhostApp.GetApplicationConfigV2()
bys, err := yaml.Marshal(applicationConfig)
must(errors.Wrap(err, "fail to get application config"))
fmt.Println(string(bys))
return
}
if commonFlags.SvcName == "" {
appConfig := nocalhostApp.GetApplicationConfigV2()
config := &ConfigForPlugin{}
config.Services = make([]*profile.ServiceConfigV2, 0)
for _, svcPro := range appConfig.ServiceConfigs {
config.Services = append(config.Services, svcPro)
}
bys, err := yaml.Marshal(config)
must(errors.Wrap(err, "fail to get application config"))
fmt.Println(string(bys))
} else {
nocalhostSvc, err := nocalhostApp.InitAndCheckIfSvcExist(commonFlags.SvcName, common.ServiceType)
must(err)
_ = nocalhostSvc.LoadConfigFromHub()
// need to load latest config
_ = nocalhostApp.ReloadSvcCfg(commonFlags.SvcName, nocalhostSvc.Type, false, true)
nocalhostSvc.ReloadConfig()
svcProfile, err := nocalhostSvc.GetProfile()
must(err)
svcConfig := nocalhostSvc.Config()
// to avoid empty config
if svcConfig == nil {
svcConfig = &profile.ServiceConfigV2{
Name: commonFlags.SvcName,
Type: nocalhostSvc.Type.String(),
ContainerConfigs: []*profile.ContainerConfig{},
}
}
if svcProfile != nil {
bys, err := yaml.Marshal(svcConfig)
must(errors.Wrap(err, "fail to marshal svc config"))
pack := dev_dir.NewSvcPack(
nocalhostSvc.NameSpace,
nocalhostSvc.AppMeta.NamespaceId,
nocalhostSvc.AppName,
nocalhostSvc.Type,
nocalhostSvc.Name,
"",
)
path := fp.NewFilePath(string(pack.GetAssociatePath())).
RelOrAbs(".nocalhost").
RelOrAbs("config.yaml").Path
notification := ""
if svcProfile.LocalConfigLoaded {
notification += fmt.Sprintf(
svcNotificationTipsLocalLoaded,
path,
)
notification += notificationSuffix
} else if svcProfile.CmConfigLoaded {
notification += fmt.Sprintf(
svcNotificationTipsCmLoaded,
appmeta.ConfigMapName(commonFlags.AppName),
)
} else if svcProfile.AnnotationsConfigLoaded {
notification += fmt.Sprintf(
svcNotificationTipsAnnotationLoaded,
appmeta.AnnotationKey,
)
} else {
notification += notificationPrefix
notification += fmt.Sprintf(
svcNotificationTips,
path,
)
notification += notificationSuffix
}
fmt.Println(
fmt.Sprintf(
"%s \n%s", notification, string(bys),
),
)
}
}
},
}