/
proto_manager.go
149 lines (127 loc) · 4.13 KB
/
proto_manager.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
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2023 The KusionStack Authors.
Copyright 2021 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package proto
import (
"context"
"fmt"
"os"
"sync"
"google.golang.org/protobuf/proto"
"k8s.io/klog/v2"
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
"github.com/KusionStack/controller-mesh/pkg/utils"
)
var (
selfInfo = &ctrlmeshproto.SelfInfo{Namespace: os.Getenv(constants.EnvPodNamespace), Name: os.Getenv(constants.EnvPodName)}
onceInit sync.Once
)
type Client interface {
Start(ctx context.Context) error
GetSpecManager() *SpecManager
}
type SpecManager struct {
sync.RWMutex
storage *storage
reportTriggerChan chan struct{}
expectedSpec *ctrlmeshproto.InternalSpec
currentSpec *ctrlmeshproto.InternalSpec
unloadReason string
leaderElectionState *ctrlmeshproto.LeaderElectionState
}
func newSpecManager(reportTriggerChan chan struct{}) (*SpecManager, error) {
storage, err := newStorage()
if err != nil {
return nil, err
}
sm := &SpecManager{
storage: storage,
reportTriggerChan: reportTriggerChan,
}
expectedSpec, currentSpec, err := sm.storage.loadData()
if err != nil {
klog.Errorf("Failed to load currentSpec, %v", err)
}
if currentSpec != nil {
klog.Infof("Loaded currentSpec from storage: %v", utils.DumpJSON(currentSpec))
sm.currentSpec = ctrlmeshproto.ConvertProtoSpecToInternal(currentSpec)
}
if expectedSpec != nil {
klog.Infof("Loaded expectedSpec from storage: %v", utils.DumpJSON(expectedSpec))
sm.UpdateSpec(expectedSpec)
}
return sm, nil
}
func (sm *SpecManager) UpdateLeaderElection(le *ctrlmeshproto.LeaderElectionState) {
sm.Lock()
defer sm.Unlock()
oldLe := sm.leaderElectionState
sm.leaderElectionState = le
if !proto.Equal(oldLe, le) {
sm.reportTriggerChan <- struct{}{}
}
}
func (sm *SpecManager) UpdateSpec(spec *ctrlmeshproto.ProxySpec) {
sm.expectedSpec = ctrlmeshproto.ConvertProtoSpecToInternal(spec)
if err := sm.storage.writeExpectedSpec(sm.expectedSpec.ProxySpec); err != nil {
panic(fmt.Errorf("failed to write expected spec for %v: %v", utils.DumpJSON(sm.expectedSpec.ProxySpec), err))
}
sm.Lock()
defer sm.Unlock()
if err := sm.checkLoadable(); err != nil {
klog.Warningf("Check new spec is not loadable, because %v", err)
sm.unloadReason = err.Error()
return
}
sm.currentSpec = sm.expectedSpec
if err := sm.storage.writeCurrentSpec(sm.currentSpec.ProxySpec); err != nil {
panic(fmt.Errorf("failed to write current spec for %v: %v", utils.DumpJSON(sm.currentSpec.ProxySpec), err))
}
}
func (sm *SpecManager) GetStatus() *ctrlmeshproto.ProxyStatus {
sm.Lock()
defer sm.Unlock()
if sm.currentSpec == nil {
return nil
}
return &ctrlmeshproto.ProxyStatus{
MetaState: &ctrlmeshproto.MetaState{
ExpectedHash: sm.expectedSpec.Meta.Hash,
CurrentHash: sm.currentSpec.Meta.Hash,
HashUnloadReason: sm.unloadReason,
},
LeaderElectionState: sm.leaderElectionState,
}
}
func (sm *SpecManager) AcquireSpec() *ctrlmeshproto.InternalSpec {
sm.RLock()
return sm.currentSpec
}
func (sm *SpecManager) ReleaseSpec() {
defer sm.RUnlock()
}
func (sm *SpecManager) checkLoadable() (err error) {
if sm.currentSpec == nil {
return nil
}
if sm.currentSpec.Meta.ShardName != sm.expectedSpec.Meta.ShardName {
return fmt.Errorf("ShardingConfig changed from %s to %s",
sm.currentSpec.Meta.ShardName, sm.expectedSpec.Meta.ShardName)
}
// TODO: Only Webhook Server Ep Changed
if sm.currentSpec.Meta.Hash != sm.expectedSpec.Meta.Hash {
return fmt.Errorf("SpecHase changed from %s to %s", sm.currentSpec.Meta.Hash, sm.expectedSpec.Meta.Hash)
}
return
}