forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkerd.go
110 lines (92 loc) · 3.13 KB
/
linkerd.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
package linkerd
import (
"context"
"fmt"
"strings"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
"github.com/solo-io/supergloo/pkg/meshdiscovery/utils"
"go.uber.org/zap"
)
const (
linkerd = "linkerd"
controller = "controller"
// Hardcoded name from helm chart for linkerd controller
linkerdController = linkerd + "-" + controller
linkerdSelector = "linkerd-mesh-discovery"
)
var (
DiscoverySelector = map[string]string{
utils.SelectorDiscoveredByPrefix: linkerdSelector,
utils.SelectorCreatedByPrefix: utils.SelectorCreatedByValue,
}
)
type linkerdDiscoverySyncer struct{}
func NewLinkerdDiscoverySyncer() *linkerdDiscoverySyncer {
return &linkerdDiscoverySyncer{}
}
func (s *linkerdDiscoverySyncer) DiscoverMeshes(ctx context.Context, snap *v1.DiscoverySnapshot) (v1.MeshList, error) {
ctx = contextutils.WithLogger(ctx, fmt.Sprintf("linkerd-mesh-discovery-sync-%v", snap.Hash()))
logger := contextutils.LoggerFrom(ctx)
pods := snap.Pods
installs := snap.Installs
fields := []interface{}{
zap.Int("pods", len(pods)),
zap.Int("installs", len(installs)),
}
logger.Infow("begin sync", fields...)
defer logger.Infow("end sync", fields...)
logger.Debugf("full snapshot: %v", snap)
existingInstalls := utils.GetActiveInstalls(installs, utils.LinkerdInstallFilterFunc)
var discoveredMeshes v1.MeshList
for _, linkerdPod := range utils.SelectRunningPods(pods) {
if strings.Contains(linkerdPod.Name, linkerdController) {
mesh, err := constructDiscoveredMesh(ctx, linkerdPod, existingInstalls)
if err != nil {
return nil, err
}
logger.Debugf("successfully discovered mesh data for %v", mesh)
discoveredMeshes = append(discoveredMeshes, mesh)
}
}
return discoveredMeshes, nil
}
func constructDiscoveredMesh(ctx context.Context, mainPod *kubernetes.Pod, existingInstalls v1.InstallList) (*v1.Mesh, error) {
logger := contextutils.LoggerFrom(ctx)
linkerdVersion, err := utils.GetVersionFromPodWithMatchers(mainPod, []string{linkerd, controller})
if err != nil {
logger.Debugf("unable to find version from pod %v", mainPod)
return nil, err
}
mesh := utils.BasicMeshInfo(mainPod.Namespace, DiscoverySelector, linkerd)
mesh.MeshType = &v1.Mesh_Linkerd{
Linkerd: &v1.LinkerdMesh{
Version: linkerdVersion,
InstallationNamespace: mainPod.Namespace,
},
}
// If install crd exists, overwrite discovery data
for _, install := range existingInstalls {
meshInstall := install.GetMesh()
if meshInstall == nil {
continue
}
linkerdMeshInstall := meshInstall.GetLinkerd()
if linkerdMeshInstall == nil {
continue
}
// This install refers to the current mesh
if install.InstallationNamespace == mainPod.Namespace {
mesh.MtlsConfig = &v1.MtlsConfig{
MtlsEnabled: linkerdMeshInstall.EnableMtls,
}
mesh.Metadata = install.Metadata
// Set label to be aware of discovered nature
mesh.Metadata.Labels = DiscoverySelector
// Need to explicitly set this to "" so it doesn't attempt to overwrite it.
mesh.Metadata.ResourceVersion = ""
}
}
return mesh, nil
}