forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s_components_registration.go
133 lines (114 loc) · 4.09 KB
/
k8s_components_registration.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
package models
import (
"context"
"fmt"
"sync"
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/logger"
"github.com/layer5io/meshkit/models/events"
meshmodel "github.com/layer5io/meshkit/models/meshmodel/registry"
"github.com/spf13/viper"
)
type RegistrationStatus int
const (
RegistrationComplete RegistrationStatus = iota
NotRegistered
Registering
)
// INstead define a set of actions
func (rs RegistrationStatus) String() string {
switch rs {
case RegistrationComplete:
return "register"
case NotRegistered:
return "not_registered"
case Registering:
return "registering"
default:
return ""
}
}
type ComponentsRegistrationHelper struct {
// map that holds the registration status of each of the contexts in this runtime of the server
// it should be private
ctxRegStatusMap map[string]RegistrationStatus
log logger.Handler
mx sync.RWMutex
}
func NewComponentsRegistrationHelper(logger logger.Handler) *ComponentsRegistrationHelper {
return &ComponentsRegistrationHelper{
ctxRegStatusMap: make(map[string]RegistrationStatus),
log: logger,
mx: sync.RWMutex{},
}
}
// update the map with the given list of contexts
func (cg *ComponentsRegistrationHelper) UpdateContexts(ctxs []*K8sContext) *ComponentsRegistrationHelper {
for _, ctx := range ctxs {
ctxID := ctx.ID
cg.mx.Lock()
if _, ok := cg.ctxRegStatusMap[ctxID]; !ok {
cg.ctxRegStatusMap[ctxID] = NotRegistered
}
cg.mx.Unlock()
}
return cg
}
type K8sRegistrationFunction func(provider *Provider, ctxt context.Context, config []byte, ctxID string, connectionID string, userID string, MesheryInstanceID uuid.UUID, reg *meshmodel.RegistryManager, eb *Broadcast, ctxName string) error
// start registration of components for the contexts
func (cg *ComponentsRegistrationHelper) RegisterComponents(ctxs []*K8sContext, regFunc []K8sRegistrationFunction, reg *meshmodel.RegistryManager, eventsBrodcaster *Broadcast, provider Provider, userID string, skip bool) {
/* If flag "SKIP_COMP_GEN" is set but the registration is invoked in form of API request explicitly,
then flag should not be respected and to control this behaviour skip is introduced.
In case of API requests "skip" is set to false, otherise true and behaviour is controlled by "SKIP_COMP_GEN".
*/
if viper.GetBool("SKIP_COMP_GEN") && skip {
return
}
userUUID, _ := uuid.FromString(userID)
for _, ctx := range ctxs {
ctxID := ctx.ID
connectionID, _ := uuid.FromString(ctx.ConnectionID)
ctxName := ctx.Name
cg.mx.Lock()
// do not do anything about the contexts that are not present in the ctxRegStatusMap
// only start registering components for contexts whose status is NotRegistered
status, ok := cg.ctxRegStatusMap[ctxID]
if !ok || status != NotRegistered {
cg.mx.Unlock()
continue
}
// update the status
cg.ctxRegStatusMap[ctxID] = Registering
cg.mx.Unlock()
cg.log.Info("Registration of ", ctxName, " components started for contextID: ", ctxID)
event := events.NewEvent().ActedUpon(connectionID).FromSystem(*ctx.MesheryInstanceID).WithSeverity(events.Informational).WithCategory("connection").WithAction(Registering.String()).FromUser(userUUID).WithDescription(fmt.Sprintf("Registration for Kubernetes context %s started", ctxName)).Build()
err := provider.PersistEvent(event)
if err != nil {
// Even if event was not persisted continue with the operation and publish the event to user.
cg.log.Warn(err)
}
eventsBrodcaster.Publish(userUUID, event)
go func(ctx *K8sContext) {
// set the status to RegistrationComplete
defer func() {
cg.mx.Lock()
cg.ctxRegStatusMap[ctxID] = RegistrationComplete
cg.mx.Unlock()
cg.log.Info("components registered for context ", ctxName, " ID:", ctxID)
}()
// start registration
cfg, err := ctx.GenerateKubeConfig()
if err != nil {
cg.log.Error(err)
return
}
for _, f := range regFunc {
err = f(&provider, context.Background(), cfg, ctxID, ctx.ConnectionID, userID, *ctx.MesheryInstanceID, reg, eventsBrodcaster, ctxName)
if err != nil {
cg.log.Error(err)
return
}
}
}(ctx)
}
}