-
Notifications
You must be signed in to change notification settings - Fork 53
/
sync.go
138 lines (120 loc) · 3.51 KB
/
sync.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
package backend
import (
"context"
"errors"
"github.com/google/go-cmp/cmp"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
opnicorev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/auth/cluster"
"github.com/rancher/opni/pkg/capabilities/wellknown"
"github.com/rancher/opni/plugins/logging/pkg/apis/node"
loggingerrors "github.com/rancher/opni/plugins/logging/pkg/errors"
"github.com/rancher/opni/plugins/logging/pkg/gateway/drivers"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/testing/protocmp"
)
func (b *LoggingBackend) Status(ctx context.Context, req *capabilityv1.StatusRequest) (*capabilityv1.NodeCapabilityStatus, error) {
b.WaitForInit()
b.nodeStatusMu.RLock()
defer b.nodeStatusMu.RUnlock()
capStatus, err := b.ClusterDriver.GetClusterStatus(ctx, req.GetCluster().GetId())
if err != nil {
if errors.Is(err, loggingerrors.ErrInvalidList) {
return nil, status.Error(codes.NotFound, "unable to list cluster status")
}
return nil, err
}
return capStatus, nil
}
func (b *LoggingBackend) Sync(ctx context.Context, req *node.SyncRequest) (*node.SyncResponse, error) {
b.WaitForInit()
id := cluster.StreamAuthorizedID(ctx)
// look up the cluster and check if the capability is installed
cluster, err := b.StorageBackend.GetCluster(ctx, &opnicorev1.Reference{
Id: id,
})
if err != nil {
return nil, err
}
var enabled bool
for _, cap := range cluster.GetCapabilities() {
if cap.Name == wellknown.CapabilityLogs {
enabled = (cap.DeletionTimestamp == nil)
}
}
var conditions []string
if enabled {
if b.shouldDisableNode(ctx) {
reason := "opensearch is not installed"
enabled = false
conditions = append(conditions, reason)
}
}
b.desiredNodeSpecMu.RLock()
defer b.desiredNodeSpecMu.RUnlock()
b.nodeStatusMu.RLock()
defer b.nodeStatusMu.RUnlock()
if enabled {
err = b.ClusterDriver.SetClusterStatus(ctx, id, enabled)
if err != nil {
return nil, err
}
} else {
b.ClusterDriver.SetSyncTime()
}
return b.buildResponse(req.GetCurrentConfig(), &node.LoggingCapabilityConfig{
Enabled: enabled,
Conditions: conditions,
}), nil
}
func (b *LoggingBackend) buildResponse(old, new *node.LoggingCapabilityConfig) *node.SyncResponse {
if cmp.Equal(old, new, protocmp.Transform()) {
return &node.SyncResponse{
ConfigStatus: node.ConfigStatus_UpToDate,
}
}
return &node.SyncResponse{
ConfigStatus: node.ConfigStatus_NeedsUpdate,
UpdatedConfig: new,
}
}
func (b *LoggingBackend) shouldDisableNode(ctx context.Context) bool {
installState := b.ClusterDriver.GetInstallStatus(ctx)
switch installState {
case drivers.Absent:
return true
case drivers.Pending, drivers.Installed:
return false
case drivers.Error:
fallthrough
default:
// if status is unknown don't uninstall from the node
}
return false
}
func (b *LoggingBackend) requestNodeSync(ctx context.Context, cluster *opnicorev1.Reference) {
_, err := b.NodeManagerClient.RequestSync(ctx, &capabilityv1.SyncRequest{
Cluster: cluster,
Filter: &capabilityv1.Filter{
CapabilityNames: []string{wellknown.CapabilityLogs},
},
})
name := cluster.GetId()
if name == "" {
name = "(all)"
}
if err != nil {
b.Logger.With(
"cluster", name,
"capability", wellknown.CapabilityLogs,
zap.Error(err),
).Warn("failed to request node sync; nodes may not be updated immediately")
return
}
b.Logger.With(
"cluster", name,
"capability", wellknown.CapabilityLogs,
).Info("node sync requested")
}