/
connectors.go
245 lines (201 loc) · 8.17 KB
/
connectors.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package config
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/fs"
"os"
"sort"
"strings"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/shared/utils/files"
"time"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/connector/internal/api/public"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/environments"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/shared"
"github.com/golang/glog"
"github.com/spf13/pflag"
)
type ConnectorsConfig struct {
ConnectorEvalDuration time.Duration `json:"connector_eval_duration"`
ConnectorEvalOrganizations []string `json:"connector_eval_organizations"`
ConnectorNamespaceLifecycleAPI bool `json:"connector_namespace_lifecycle_api"`
ConnectorEnableUnassignedConnectors bool `json:"connector_enable_unassigned_connectors"`
ConnectorCatalogDirs []string `json:"connector_types"`
ConnectorMetadataDirs []string `json:"connector_metadata"`
CatalogEntries []ConnectorCatalogEntry `json:"connector_type_urls"`
CatalogChecksums map[string]string `json:"connector_catalog_checksums"`
ConnectorsSupportedChannels []string `json:"connectors_supported_channels"`
}
var _ environments.ConfigModule = &ConnectorsConfig{}
type ConnectorChannelConfig struct {
ShardMetadata map[string]interface{} `json:"shard_metadata,omitempty"`
}
type ConnectorCatalogEntry struct {
Channels map[string]ConnectorChannelConfig `json:"channels,omitempty"`
ConnectorType public.ConnectorType `json:"connector_type"`
}
type ConnectorMetadata struct {
ConnectorTypeId string `json:"id" yaml:"id"`
FeaturedRank int32 `json:"featured-rank" yaml:"featured-rank"`
Labels []string `json:"labels" yaml:"labels"`
Annotations map[string]string `json:"annotations" yaml:"annotations"`
}
func NewConnectorsConfig() *ConnectorsConfig {
return &ConnectorsConfig{
CatalogChecksums: make(map[string]string),
}
}
func (c *ConnectorsConfig) AddFlags(fs *pflag.FlagSet) {
fs.StringArrayVar(&c.ConnectorCatalogDirs, "connector-catalog", c.ConnectorCatalogDirs, "Directory containing connector catalog entries")
fs.StringArrayVar(&c.ConnectorMetadataDirs, "connector-metadata", c.ConnectorMetadataDirs, "Directory containing connector metadata configuration files")
fs.DurationVar(&c.ConnectorEvalDuration, "connector-eval-duration", c.ConnectorEvalDuration, "Connector eval duration in golang duration format")
fs.StringSliceVar(&c.ConnectorEvalOrganizations, "connector-eval-organizations", c.ConnectorEvalOrganizations, "Connector eval organization IDs")
fs.BoolVar(&c.ConnectorNamespaceLifecycleAPI, "connector-namespace-lifecycle-api", c.ConnectorNamespaceLifecycleAPI, "Enable APIs to create, update, delete non-eval Namespaces")
fs.BoolVar(&c.ConnectorEnableUnassignedConnectors, "connector-enable-unassigned-connectors", c.ConnectorEnableUnassignedConnectors, "Enable support for 'unassigned' state for Connectors")
fs.StringSliceVar(&c.ConnectorsSupportedChannels, "connectors-supported-channels", c.ConnectorsSupportedChannels, "Connector channels that are visible")
}
func (c *ConnectorsConfig) ReadFiles() error {
// read metadata first to merge with catalog next
connectorMetadata, err := c.readConnectorMetadata()
if err != nil {
return err
}
// read catalogs and merge metadata, removing entries from the map
err = c.readConnectorCatalog(connectorMetadata)
if err != nil {
return err
}
// check if there are any unused metadata entries left
remainingIds := len(connectorMetadata)
if remainingIds > 0 {
ids := make([]string, 0, remainingIds)
for id := range connectorMetadata {
ids = append(ids, id)
}
return fmt.Errorf("found %d unrecognized connector metadata with ids: %s", remainingIds, ids)
}
glog.Infof("loaded %d connector types", len(c.CatalogEntries))
return nil
}
func (c *ConnectorsConfig) readConnectorMetadata() (connectorMetadata map[string]ConnectorMetadata, err error) {
connectorMetadata = make(map[string]ConnectorMetadata)
for _, dir := range c.ConnectorMetadataDirs {
metaDir := shared.BuildFullFilePath(dir)
err = files.Walk(metaDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
// skip over hidden files and dirs..
if info.IsDir() || strings.HasPrefix(path, ".") {
return nil
}
glog.Infof("loading connector metadata from file %s", path)
// Read the file
var metas []ConnectorMetadata
err = shared.ReadYamlFile(path, &metas)
if err != nil {
return fmt.Errorf("error reading connector metadata from %s: %s", path, err)
}
for _, m := range metas {
connectorMetadata[m.ConnectorTypeId] = m
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error listing connector metadata in %s: %s", metaDir, err)
}
}
return
}
func (c *ConnectorsConfig) readConnectorCatalog(connectorMetadata map[string]ConnectorMetadata) (err error) {
typesLoaded := map[string]string{}
for _, dir := range c.ConnectorCatalogDirs {
dir = shared.BuildFullFilePath(dir)
err = files.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
// skip over hidden files and dirs..
if info.IsDir() || strings.HasPrefix(path, ".") {
return nil
}
glog.Infof("loading connectors from file %s", path)
// Read the file
buf, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading catalog file %s: %s", path, err)
}
entry := ConnectorCatalogEntry{}
err = json.Unmarshal(buf, &entry)
if err != nil {
return fmt.Errorf("error unmarshaling catalog file %s: %s", path, err)
}
// set catalog metadata from metadata config read earlier
id := entry.ConnectorType.Id
if meta, found := connectorMetadata[id]; found {
entry.ConnectorType.FeaturedRank = meta.FeaturedRank
entry.ConnectorType.Labels = meta.Labels
entry.ConnectorType.Annotations = meta.Annotations
} else {
return fmt.Errorf("missing metadata for connector %s", id)
}
// compute checksum for catalog entry to look for updates
sum, err := checksum(entry)
if err != nil {
return fmt.Errorf("error computing checksum for catalog file %s: %s", path, err)
}
// when walking directories with symlink such as what kubernetes does when mounting
// a volume from a configmap where the actual files are double-symlinked from some
// random named path so this method is invoked twice or more, but it is not actually
// always possible to reliably determine if files have been already processed, so we
// first check if:
//
// - the previous file and the new file are the same (os.SameFile)
// - the file has already been processed
// - the previous file checksum is the same
//
// if any of the above condition is true, we assume the previous file and the current
// one are actually the same, so we can safely ignore it
if prev, found := typesLoaded[id]; found {
prevInfo, prevErr := os.Lstat(prev)
if prevErr != nil {
return nil
}
if os.SameFile(info, prevInfo) {
return nil
}
if typesLoaded[id] == path {
return nil
}
if c.CatalogChecksums[id] == sum {
return nil
}
return fmt.Errorf("connector type '%s' defined in '%s' and '%s'", id, path, prev)
}
c.CatalogChecksums[id] = sum
typesLoaded[id] = path
c.CatalogEntries = append(c.CatalogEntries, entry)
glog.Infof("loaded connector %s from file %s", id, path)
return nil
})
if err != nil {
return fmt.Errorf("error listing connector catalogs in %s: %s", dir, err)
}
}
// remove all processed metadata entries
for _, entry := range c.CatalogEntries {
delete(connectorMetadata, entry.ConnectorType.Id)
}
sort.Slice(c.CatalogEntries, func(i, j int) bool {
return c.CatalogEntries[i].ConnectorType.Id < c.CatalogEntries[j].ConnectorType.Id
})
return nil
}
func checksum(spec interface{}) (string, error) {
h := sha1.New()
err := json.NewEncoder(h).Encode(spec)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}