-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
module.go
90 lines (73 loc) · 2.26 KB
/
module.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cell
import (
"fmt"
"regexp"
"github.com/sirupsen/logrus"
"go.uber.org/dig"
"github.com/cilium/cilium/pkg/logging/logfields"
)
// Module creates a scoped set of cells with a given identifier.
//
// The id and title will be included in the object dump (hive.PrintObjects).
// The id must be lower-case, at most 30 characters and only contain [a-z0-9-_].
// Title can contain [a-zA-Z0-9_- ] and must be shorter than 80 characters.
//
// Private constructors with a module (ProvidePrivate) are only accessible
// within this module and its sub-modules.
func Module(id, title string, cells ...Cell) Cell {
validateIDAndTitle(id, title)
return &module{id, title, cells}
}
var (
idRegex = regexp.MustCompile(`^[a-z][a-z0-9_\-]{1,30}$`)
titleRegex = regexp.MustCompile(`^[a-zA-Z0-9_\- ]{1,80}$`)
)
func validateIDAndTitle(id, title string) {
if !idRegex.MatchString(id) {
panic(fmt.Sprintf("Invalid hive.Module id: %q, expected to id match %s", id, idRegex))
}
if !titleRegex.MatchString(title) {
panic(fmt.Sprintf("Invalid hive.Module title: %q, expected to title match %s", title, titleRegex))
}
}
type module struct {
// id is the module identity. It is shown in object output and is used to derive
// the scoped logger.
id string
// title is a human-readable short title for the module. Shown in object output
// alongside the identifier.
title string
cells []Cell
}
func (m *module) logger(log logrus.FieldLogger) logrus.FieldLogger {
return log.WithField(logfields.LogSubsys, m.id)
}
func (m *module) moduleScopedStatusReporter(p Health) HealthReporter {
return p.forModule(m.id)
}
func (m *module) Apply(c container) error {
scope := c.Scope(m.id)
// Provide module scoped status reporter, used for reporting module level
// health status.
if err := scope.Provide(m.moduleScopedStatusReporter, dig.Export(false)); err != nil {
return err
}
if err := scope.Decorate(m.logger); err != nil {
return err
}
for _, cell := range m.cells {
if err := cell.Apply(scope); err != nil {
return err
}
}
return nil
}
func (m *module) Info(c container) Info {
n := NewInfoNode("Ⓜ️ " + m.id + " (" + m.title + ")")
for _, cell := range m.cells {
n.Add(cell.Info(c))
}
return n
}