/
redirect.go
70 lines (62 loc) · 2.43 KB
/
redirect.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package proxy
import (
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/fqdn/restore"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/proxy/endpoint"
"github.com/cilium/cilium/pkg/revert"
)
// RedirectImplementation is the generic proxy redirect interface that each
// proxy redirect type must implement
type RedirectImplementation interface {
// UpdateRules updates the rules for the given proxy redirect.
// The implementation should .Add to the WaitGroup if the update is
// asynchronous and the update should not return until it is complete.
// The returned RevertFunc must be non-nil.
// Note: UpdateRules is not called when a redirect is created.
UpdateRules(wg *completion.WaitGroup) (revert.RevertFunc, error)
// Close closes and cleans up resources associated with the redirect
// implementation. The implementation should .Add to the WaitGroup if the
// update is asynchronous and the update should not return until it is
// complete.
Close(wg *completion.WaitGroup) (revert.FinalizeFunc, revert.RevertFunc)
}
type Redirect struct {
// The following fields are only written to during initialization, it
// is safe to read these fields without locking the mutex
name string
listener *ProxyPort
dstPortProto restore.PortProto
endpointID uint64
localEndpoint endpoint.EndpointUpdater
implementation RedirectImplementation
// The following fields are updated while the redirect is alive, the
// mutex must be held to read and write these fields
mutex lock.RWMutex
rules policy.L7DataMap
}
func newRedirect(localEndpoint endpoint.EndpointUpdater, name string, listener *ProxyPort, port uint16, proto uint8) *Redirect {
return &Redirect{
name: name,
listener: listener,
dstPortProto: restore.MakeV2PortProto(port, proto),
endpointID: localEndpoint.GetID(),
localEndpoint: localEndpoint,
}
}
// updateRules updates the rules of the redirect, Redirect.mutex must be held
// 'implementation' is not initialized when this is called the first time.
// TODO: Replace this with RedirectImplementation UpdateRules method!
func (r *Redirect) updateRules(p policy.ProxyPolicy) revert.RevertFunc {
oldRules := r.rules
r.rules = p.CopyL7RulesPerEndpoint()
return func() error {
r.mutex.Lock()
r.rules = oldRules
r.mutex.Unlock()
return nil
}
}