Skip to content

Commit 7e5ab58

Browse files
authored
fix: merge duplicate ssl object when translate gateway (#139)
Signed-off-by: ashing <[email protected]>
1 parent 7f80427 commit 7e5ab58

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

internal/provider/adc/translator/gateway.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"encoding/json"
1818
"encoding/pem"
1919
"fmt"
20+
"slices"
2021

2122
"github.com/api7/gopkg/pkg/log"
2223
"github.com/pkg/errors"
@@ -44,6 +45,8 @@ func (t *Translator) TranslateGateway(tctx *provider.TranslateContext, obj *gate
4445
result.SSL = append(result.SSL, ssl...)
4546
}
4647
}
48+
result.SSL = mergeSSLWithSameID(result.SSL)
49+
4750
rk := provider.ResourceKind{
4851
Kind: obj.Kind,
4952
Namespace: obj.Namespace,
@@ -117,6 +120,7 @@ func (t *Translator) translateSecret(tctx *provider.TranslateContext, listener g
117120
sslObj.Snis = append(sslObj.Snis, hosts...)
118121
// Note: Dashboard doesn't allow duplicate certificate across ssl objects
119122
sslObj.ID = id.GenID(string(cert))
123+
log.Debugw("generated ssl id", zap.String("ssl id", sslObj.ID), zap.String("secret", secret.Namespace+"/"+secret.Name))
120124
sslObj.Labels = label.GenLabel(obj)
121125
sslObjs = append(sslObjs, sslObj)
122126
}
@@ -232,3 +236,47 @@ func (t *Translator) fillPluginMetadataFromGatewayProxy(pluginMetadata adctypes.
232236
pluginMetadata[pluginName] = pluginConfig
233237
}
234238
}
239+
240+
// mergeSSLWithSameID merge ssl with same id
241+
func mergeSSLWithSameID(sslList []*adctypes.SSL) []*adctypes.SSL {
242+
if len(sslList) <= 1 {
243+
return sslList
244+
}
245+
246+
// create a map to store ssl with same id
247+
sslMap := make(map[string]*adctypes.SSL)
248+
for _, ssl := range sslList {
249+
if existing, exists := sslMap[ssl.ID]; exists {
250+
// if ssl with same id exists, merge their snis
251+
// use map to deduplicate
252+
sniMap := make(map[string]struct{})
253+
// add existing snis
254+
for _, sni := range existing.Snis {
255+
sniMap[sni] = struct{}{}
256+
}
257+
// add new snis
258+
for _, sni := range ssl.Snis {
259+
sniMap[sni] = struct{}{}
260+
}
261+
// rebuild deduplicated snis list
262+
newSnis := make([]string, 0, len(sniMap))
263+
for sni := range sniMap {
264+
newSnis = append(newSnis, sni)
265+
}
266+
267+
slices.Sort(newSnis)
268+
// update existing ssl object
269+
existing.Snis = newSnis
270+
} else {
271+
slices.Sort(ssl.Snis)
272+
// if new ssl id, add to map
273+
sslMap[ssl.ID] = ssl
274+
}
275+
}
276+
277+
mergedSSL := make([]*adctypes.SSL, 0, len(sslMap))
278+
for _, ssl := range sslMap {
279+
mergedSSL = append(mergedSSL, ssl)
280+
}
281+
return mergedSSL
282+
}

test/conformance/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestMain(m *testing.M) {
160160
Namespace: namespace,
161161
AdminEnpoint: framework.DashboardTLSEndpoint,
162162
StatusAddress: address,
163-
InitSyncDelay: 30 * time.Minute,
163+
InitSyncDelay: 1 * time.Minute,
164164
})
165165

166166
defaultGatewayProxyOpts = GatewayProxyOpts{

0 commit comments

Comments
 (0)