Skip to content

Commit be40047

Browse files
authored
Merge pull request #3554 from Eileen-Yu/feat/externalize-cfg
✨ Externalize config struct to be available for external plugin
2 parents cd1341d + 1099b02 commit be40047

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

pkg/config/v3/config.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (ss *stringSlice) UnmarshalJSON(b []byte) error {
5252
return nil
5353
}
5454

55-
type cfg struct {
55+
type Cfg struct {
5656
// Version
5757
Version config.Version `json:"version"`
5858

@@ -81,103 +81,103 @@ type pluginConfig interface{}
8181

8282
// New returns a new config.Config
8383
func New() config.Config {
84-
return &cfg{Version: Version}
84+
return &Cfg{Version: Version}
8585
}
8686

8787
func init() {
8888
config.Register(Version, New)
8989
}
9090

9191
// GetVersion implements config.Config
92-
func (c cfg) GetVersion() config.Version {
92+
func (c Cfg) GetVersion() config.Version {
9393
return c.Version
9494
}
9595

9696
// GetDomain implements config.Config
97-
func (c cfg) GetDomain() string {
97+
func (c Cfg) GetDomain() string {
9898
return c.Domain
9999
}
100100

101101
// SetDomain implements config.Config
102-
func (c *cfg) SetDomain(domain string) error {
102+
func (c *Cfg) SetDomain(domain string) error {
103103
c.Domain = domain
104104
return nil
105105
}
106106

107107
// GetRepository implements config.Config
108-
func (c cfg) GetRepository() string {
108+
func (c Cfg) GetRepository() string {
109109
return c.Repository
110110
}
111111

112112
// SetRepository implements config.Config
113-
func (c *cfg) SetRepository(repository string) error {
113+
func (c *Cfg) SetRepository(repository string) error {
114114
c.Repository = repository
115115
return nil
116116
}
117117

118118
// GetProjectName implements config.Config
119-
func (c cfg) GetProjectName() string {
119+
func (c Cfg) GetProjectName() string {
120120
return c.Name
121121
}
122122

123123
// SetProjectName implements config.Config
124-
func (c *cfg) SetProjectName(name string) error {
124+
func (c *Cfg) SetProjectName(name string) error {
125125
c.Name = name
126126
return nil
127127
}
128128

129129
// GetPluginChain implements config.Config
130-
func (c cfg) GetPluginChain() []string {
130+
func (c Cfg) GetPluginChain() []string {
131131
return c.PluginChain
132132
}
133133

134134
// SetPluginChain implements config.Config
135-
func (c *cfg) SetPluginChain(pluginChain []string) error {
135+
func (c *Cfg) SetPluginChain(pluginChain []string) error {
136136
c.PluginChain = pluginChain
137137
return nil
138138
}
139139

140140
// IsMultiGroup implements config.Config
141-
func (c cfg) IsMultiGroup() bool {
141+
func (c Cfg) IsMultiGroup() bool {
142142
return c.MultiGroup
143143
}
144144

145145
// SetMultiGroup implements config.Config
146-
func (c *cfg) SetMultiGroup() error {
146+
func (c *Cfg) SetMultiGroup() error {
147147
c.MultiGroup = true
148148
return nil
149149
}
150150

151151
// ClearMultiGroup implements config.Config
152-
func (c *cfg) ClearMultiGroup() error {
152+
func (c *Cfg) ClearMultiGroup() error {
153153
c.MultiGroup = false
154154
return nil
155155
}
156156

157157
// IsComponentConfig implements config.Config
158-
func (c cfg) IsComponentConfig() bool {
158+
func (c Cfg) IsComponentConfig() bool {
159159
return c.ComponentConfig
160160
}
161161

162162
// SetComponentConfig implements config.Config
163-
func (c *cfg) SetComponentConfig() error {
163+
func (c *Cfg) SetComponentConfig() error {
164164
c.ComponentConfig = true
165165
return nil
166166
}
167167

168168
// ClearComponentConfig implements config.Config
169-
func (c *cfg) ClearComponentConfig() error {
169+
func (c *Cfg) ClearComponentConfig() error {
170170
c.ComponentConfig = false
171171
return nil
172172
}
173173

174174
// ResourcesLength implements config.Config
175-
func (c cfg) ResourcesLength() int {
175+
func (c Cfg) ResourcesLength() int {
176176
return len(c.Resources)
177177
}
178178

179179
// HasResource implements config.Config
180-
func (c cfg) HasResource(gvk resource.GVK) bool {
180+
func (c Cfg) HasResource(gvk resource.GVK) bool {
181181
for _, res := range c.Resources {
182182
if gvk.IsEqualTo(res.GVK) {
183183
return true
@@ -188,7 +188,7 @@ func (c cfg) HasResource(gvk resource.GVK) bool {
188188
}
189189

190190
// GetResource implements config.Config
191-
func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
191+
func (c Cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
192192
for _, res := range c.Resources {
193193
if gvk.IsEqualTo(res.GVK) {
194194
r := res.Copy()
@@ -206,7 +206,7 @@ func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
206206
}
207207

208208
// GetResources implements config.Config
209-
func (c cfg) GetResources() ([]resource.Resource, error) {
209+
func (c Cfg) GetResources() ([]resource.Resource, error) {
210210
resources := make([]resource.Resource, 0, len(c.Resources))
211211
for _, res := range c.Resources {
212212
r := res.Copy()
@@ -223,7 +223,7 @@ func (c cfg) GetResources() ([]resource.Resource, error) {
223223
}
224224

225225
// AddResource implements config.Config
226-
func (c *cfg) AddResource(res resource.Resource) error {
226+
func (c *Cfg) AddResource(res resource.Resource) error {
227227
// As res is passed by value it is already a shallow copy, but we need to make a deep copy
228228
res = res.Copy()
229229

@@ -239,7 +239,7 @@ func (c *cfg) AddResource(res resource.Resource) error {
239239
}
240240

241241
// UpdateResource implements config.Config
242-
func (c *cfg) UpdateResource(res resource.Resource) error {
242+
func (c *Cfg) UpdateResource(res resource.Resource) error {
243243
// As res is passed by value it is already a shallow copy, but we need to make a deep copy
244244
res = res.Copy()
245245

@@ -259,7 +259,7 @@ func (c *cfg) UpdateResource(res resource.Resource) error {
259259
}
260260

261261
// HasGroup implements config.Config
262-
func (c cfg) HasGroup(group string) bool {
262+
func (c Cfg) HasGroup(group string) bool {
263263
// Return true if the target group is found in the tracked resources
264264
for _, r := range c.Resources {
265265
if strings.EqualFold(group, r.Group) {
@@ -272,7 +272,7 @@ func (c cfg) HasGroup(group string) bool {
272272
}
273273

274274
// ListCRDVersions implements config.Config
275-
func (c cfg) ListCRDVersions() []string {
275+
func (c Cfg) ListCRDVersions() []string {
276276
// Make a map to remove duplicates
277277
versionSet := make(map[string]struct{})
278278
for _, r := range c.Resources {
@@ -290,7 +290,7 @@ func (c cfg) ListCRDVersions() []string {
290290
}
291291

292292
// ListWebhookVersions implements config.Config
293-
func (c cfg) ListWebhookVersions() []string {
293+
func (c Cfg) ListWebhookVersions() []string {
294294
// Make a map to remove duplicates
295295
versionSet := make(map[string]struct{})
296296
for _, r := range c.Resources {
@@ -308,7 +308,7 @@ func (c cfg) ListWebhookVersions() []string {
308308
}
309309

310310
// DecodePluginConfig implements config.Config
311-
func (c cfg) DecodePluginConfig(key string, configObj interface{}) error {
311+
func (c Cfg) DecodePluginConfig(key string, configObj interface{}) error {
312312
if len(c.Plugins) == 0 {
313313
return config.PluginKeyNotFoundError{Key: key}
314314
}
@@ -329,7 +329,7 @@ func (c cfg) DecodePluginConfig(key string, configObj interface{}) error {
329329
}
330330

331331
// EncodePluginConfig will return an error if used on any project version < v3.
332-
func (c *cfg) EncodePluginConfig(key string, configObj interface{}) error {
332+
func (c *Cfg) EncodePluginConfig(key string, configObj interface{}) error {
333333
// Get object's bytes and set them under key in extra fields.
334334
b, err := yaml.Marshal(configObj)
335335
if err != nil {
@@ -347,7 +347,7 @@ func (c *cfg) EncodePluginConfig(key string, configObj interface{}) error {
347347
}
348348

349349
// Marshal implements config.Config
350-
func (c cfg) MarshalYAML() ([]byte, error) {
350+
func (c Cfg) MarshalYAML() ([]byte, error) {
351351
for i, r := range c.Resources {
352352
// If API is empty, omit it (prevents `api: {}`).
353353
if r.API != nil && r.API.IsEmpty() {
@@ -368,7 +368,7 @@ func (c cfg) MarshalYAML() ([]byte, error) {
368368
}
369369

370370
// Unmarshal implements config.Config
371-
func (c *cfg) UnmarshalYAML(b []byte) error {
371+
func (c *Cfg) UnmarshalYAML(b []byte) error {
372372
if err := yaml.UnmarshalStrict(b, c); err != nil {
373373
return config.UnmarshalError{Err: err}
374374
}

pkg/config/v3/config_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestConfigV3(t *testing.T) {
3333
RunSpecs(t, "Config V3 Suite")
3434
}
3535

36-
var _ = Describe("cfg", func() {
36+
var _ = Describe("Cfg", func() {
3737
const (
3838
domain = "my.domain"
3939
repo = "myrepo"
@@ -45,15 +45,15 @@ var _ = Describe("cfg", func() {
4545
)
4646

4747
var (
48-
c cfg
48+
c Cfg
4949

5050
pluginChain = []string{"go.kubebuilder.io/v2"}
5151

5252
otherPluginChain = []string{"go.kubebuilder.io/v3"}
5353
)
5454

5555
BeforeEach(func() {
56-
c = cfg{
56+
c = Cfg{
5757
Version: Version,
5858
Domain: domain,
5959
Repository: repo,
@@ -376,14 +376,14 @@ var _ = Describe("cfg", func() {
376376
)
377377

378378
var (
379-
c0 = cfg{
379+
c0 = Cfg{
380380
Version: Version,
381381
Domain: domain,
382382
Repository: repo,
383383
Name: name,
384384
PluginChain: pluginChain,
385385
}
386-
c1 = cfg{
386+
c1 = Cfg{
387387
Version: Version,
388388
Domain: domain,
389389
Repository: repo,
@@ -395,7 +395,7 @@ var _ = Describe("cfg", func() {
395395
},
396396
},
397397
}
398-
c2 = cfg{
398+
c2 = Cfg{
399399
Version: Version,
400400
Domain: domain,
401401
Repository: repo,
@@ -429,7 +429,7 @@ var _ = Describe("cfg", func() {
429429
})
430430

431431
DescribeTable("DecodePluginConfig should retrieve the plugin data correctly",
432-
func(inputConfig cfg, expectedPluginConfig PluginConfig) {
432+
func(inputConfig Cfg, expectedPluginConfig PluginConfig) {
433433
var pluginConfig PluginConfig
434434
Expect(inputConfig.DecodePluginConfig(key, &pluginConfig)).To(Succeed())
435435
Expect(pluginConfig).To(Equal(expectedPluginConfig))
@@ -441,7 +441,7 @@ var _ = Describe("cfg", func() {
441441
)
442442

443443
DescribeTable("EncodePluginConfig should encode the plugin data correctly",
444-
func(pluginConfig PluginConfig, expectedConfig cfg) {
444+
func(pluginConfig PluginConfig, expectedConfig Cfg) {
445445
Expect(c.EncodePluginConfig(key, pluginConfig)).To(Succeed())
446446
Expect(c).To(Equal(expectedConfig))
447447
},
@@ -455,14 +455,14 @@ var _ = Describe("cfg", func() {
455455
Context("Persistence", func() {
456456
var (
457457
// BeforeEach is called after the entries are evaluated, and therefore, c is not available
458-
c1 = cfg{
458+
c1 = Cfg{
459459
Version: Version,
460460
Domain: domain,
461461
Repository: repo,
462462
Name: name,
463463
PluginChain: pluginChain,
464464
}
465-
c2 = cfg{
465+
c2 = Cfg{
466466
Version: Version,
467467
Domain: otherDomain,
468468
Repository: otherRepo,
@@ -591,7 +591,7 @@ version: "3"
591591
)
592592

593593
DescribeTable("MarshalYAML should succeed",
594-
func(c cfg, content string) {
594+
func(c Cfg, content string) {
595595
b, err := c.MarshalYAML()
596596
Expect(err).NotTo(HaveOccurred())
597597
Expect(string(b)).To(Equal(content))
@@ -601,8 +601,8 @@ version: "3"
601601
)
602602

603603
DescribeTable("UnmarshalYAML should succeed",
604-
func(content string, c cfg) {
605-
var unmarshalled cfg
604+
func(content string, c Cfg) {
605+
var unmarshalled Cfg
606606
Expect(unmarshalled.UnmarshalYAML([]byte(content))).To(Succeed())
607607
Expect(unmarshalled.Version.Compare(c.Version)).To(Equal(0))
608608
Expect(unmarshalled.Domain).To(Equal(c.Domain))
@@ -622,7 +622,7 @@ version: "3"
622622

623623
DescribeTable("UnmarshalYAML should fail",
624624
func(content string) {
625-
var c cfg
625+
var c Cfg
626626
Expect(c.UnmarshalYAML([]byte(content))).NotTo(Succeed())
627627
},
628628
Entry("for unknown fields", `field: 1

0 commit comments

Comments
 (0)