Skip to content

Commit df8f78b

Browse files
committed
MEDIUM: adapt kubebuilder_marker_generator to v6 models
Also adds convert function from client native v5 to v6 models
1 parent 4425075 commit df8f78b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+743
-337
lines changed

cmd/kubebuilder_marker_generator/main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func generate(fileName string) error { //nolint:gocognit
5858
// We must keep empty strings:
5959
// For example in Globals HttpclientSslVerify: // Enum: [ none required]
6060
// from swagger: enum: ["", "none", "required"]
61-
for _, enum := range strings.Split(comment, " ") {
61+
for _, enum := range strings.Split(comment, ",") {
62+
enum = strings.Trim(enum, "\"")
6263
if enum == "" {
6364
newComment += `""`
6465
}
@@ -86,6 +87,21 @@ func generate(fileName string) error { //nolint:gocognit
8687
field.Decorations().Start.Append("// +kubebuilder:validation:Optional")
8788
}
8889
}
90+
if strings.HasPrefix(comment, "// metadata") {
91+
decorations := field.Decorations().Start
92+
preserveField := "// +kubebuilder:pruning:PreserveUnknownFields"
93+
schemalessField := "// +kubebuilder:validation:Schemaless"
94+
updatedDecorations := make([]string, 0)
95+
copy(updatedDecorations, decorations)
96+
if !containsDecoration(updatedDecorations, preserveField) {
97+
updatedDecorations = append(updatedDecorations, preserveField)
98+
}
99+
if !containsDecoration(updatedDecorations, schemalessField) {
100+
updatedDecorations = append(updatedDecorations, schemalessField)
101+
}
102+
field.Decorations().Start = updatedDecorations
103+
field.Decorations().Before = dst.NewLine
104+
}
89105
}
90106
// if len(field.Names) > 0 {
91107
// log.Printf("Comments before the field %s: %v\n", field.Names[0].Name, comments)
@@ -152,3 +168,14 @@ func cleanup(comments []string, prefixToRemove string) []string {
152168
}
153169
return res
154170
}
171+
172+
// Helper function to check if a decoration exists
173+
func containsDecoration(decorations []string, decoration string) bool {
174+
for _, d := range decorations {
175+
// Normalize whitespace to prevent mismatch
176+
if strings.TrimSpace(d) == strings.TrimSpace(decoration) {
177+
return true
178+
}
179+
}
180+
return false
181+
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package v2v3
2+
3+
import (
4+
v2 "github.com/haproxytech/client-native/v5/models"
5+
v3 "github.com/haproxytech/client-native/v6/models"
6+
)
7+
8+
func GlobalV2ToV3(v2g *v2.Global) (*v3.GlobalBase, error) { //nolint:maintidx
9+
var v3g *v3.GlobalBase
10+
// Global
11+
daemon := false
12+
if v2g.Daemon == "enabled" {
13+
daemon = true
14+
}
15+
v3t, err := V2Tov3[v2.Global, v3.GlobalBase](v2g, "daemon")
16+
if err != nil {
17+
return nil, err
18+
}
19+
v3g = v3t
20+
21+
// Fields restructured
22+
// PerformanceOptions
23+
performanceOptions, err := V2Tov3[v2.Global, v3.PerformanceOptions](v2g)
24+
if err != nil {
25+
return nil, err
26+
}
27+
if !performanceOptions.Equal(v3.PerformanceOptions{}) {
28+
v3g.PerformanceOptions = performanceOptions
29+
}
30+
31+
// HTTPClientOptions
32+
httpClientOptions := &v3.HTTPClientOptions{
33+
ResolversDisabled: v2g.HttpclientResolversDisabled,
34+
ResolversID: v2g.HttpclientResolversID,
35+
ResolversPrefer: v2g.HttpclientResolversPrefer,
36+
Retries: v2g.HttpclientRetries,
37+
SslCaFile: v2g.HttpclientSslCaFile,
38+
SslVerify: v2g.HttpclientSslVerify,
39+
TimeoutConnect: v2g.HttpclientTimeoutConnect,
40+
}
41+
if !httpClientOptions.Equal(v3.HTTPClientOptions{}) {
42+
v3g.HTTPClientOptions = httpClientOptions
43+
}
44+
45+
// TuneQuicOptions
46+
tuneQuicOptions := &v3.TuneQuicOptions{
47+
FrontendConnTxBuffersLimit: v2g.TuneOptions.QuicFrontendConnTxBuffersLimit,
48+
FrontendMaxIdleTimeout: v2g.TuneOptions.QuicFrontendMaxIdleTimeout,
49+
FrontendMaxStreamsBidi: v2g.TuneOptions.QuicFrontendMaxStreamsBidi,
50+
MaxFrameLoss: v2g.TuneOptions.QuicMaxFrameLoss,
51+
RetryThreshold: v2g.TuneOptions.QuicRetryThreshold,
52+
SocketOwner: v2g.TuneOptions.QuicSocketOwner,
53+
// ReorderRatio: not present in v2
54+
// ZeroCopyFwdSend: not present in v2
55+
}
56+
if !tuneQuicOptions.Equal(v3.TuneQuicOptions{}) {
57+
v3g.TuneQuicOptions = tuneQuicOptions
58+
}
59+
60+
// TuneVarsOptions
61+
tuneVarsOptions := &v3.TuneVarsOptions{
62+
GlobalMaxSize: v2g.TuneOptions.VarsGlobalMaxSize,
63+
ProcMaxSize: v2g.TuneOptions.VarsProcMaxSize,
64+
ReqresMaxSize: v2g.TuneOptions.VarsReqresMaxSize,
65+
SessMaxSize: v2g.TuneOptions.VarsSessMaxSize,
66+
TxnMaxSize: v2g.TuneOptions.VarsTxnMaxSize,
67+
}
68+
if !tuneVarsOptions.Equal(v3.TuneVarsOptions{}) {
69+
v3g.TuneVarsOptions = tuneVarsOptions
70+
}
71+
72+
// TuneZlibOptions
73+
tuneZlibOptions := &v3.TuneZlibOptions{
74+
Memlevel: v2g.TuneOptions.ZlibMemlevel,
75+
Windowsize: v2g.TuneOptions.ZlibWindowsize,
76+
}
77+
if !tuneZlibOptions.Equal(v3.TuneZlibOptions{}) {
78+
v3g.TuneZlibOptions = tuneZlibOptions
79+
}
80+
81+
// TuneSslOptions
82+
tuneSslOptions := &v3.TuneSslOptions{
83+
Cachesize: v2g.TuneOptions.SslCachesize,
84+
CtxCacheSize: v2g.TuneOptions.SslCtxCacheSize,
85+
CaptureBufferSize: v2g.TuneOptions.SslCaptureBufferSize,
86+
DefaultDhParam: v2g.TuneOptions.SslDefaultDhParam,
87+
ForcePrivateCache: v2g.TuneOptions.SslForcePrivateCache,
88+
Keylog: v2g.TuneOptions.SslKeylog,
89+
Lifetime: v2g.TuneOptions.SslLifetime,
90+
Maxrecord: v2g.TuneOptions.SslMaxrecord,
91+
OcspUpdateMaxDelay: v2g.TuneOptions.SslOcspUpdateMaxDelay,
92+
OcspUpdateMinDelay: v2g.TuneOptions.SslOcspUpdateMinDelay,
93+
}
94+
if !tuneSslOptions.Equal(v3.TuneSslOptions{}) {
95+
v3g.TuneSslOptions = tuneSslOptions
96+
}
97+
98+
// TuneLuaOptions
99+
tuneLuaOptions := &v3.TuneLuaOptions{
100+
BurstTimeout: v2g.TuneOptions.LuaBurstTimeout,
101+
ForcedYield: v2g.TuneOptions.LuaForcedYield,
102+
LogLoggers: v2g.TuneOptions.LuaLogLoggers,
103+
LogStderr: v2g.TuneOptions.LuaLogStderr,
104+
// Maxmem: was a boolean
105+
ServiceTimeout: v2g.TuneOptions.LuaServiceTimeout,
106+
SessionTimeout: v2g.TuneOptions.LuaSessionTimeout,
107+
TaskTimeout: v2g.TuneOptions.LuaTaskTimeout,
108+
}
109+
if !tuneLuaOptions.Equal(v3.TuneLuaOptions{}) {
110+
v3g.TuneLuaOptions = tuneLuaOptions
111+
}
112+
113+
// TuneBufOptions
114+
tuneBufferOptions := &v3.TuneBufferOptions{
115+
BuffersLimit: v2g.TuneOptions.BuffersLimit,
116+
BuffersReserve: v2g.TuneOptions.BuffersReserve,
117+
Bufsize: v2g.TuneOptions.Bufsize,
118+
Pipesize: v2g.TuneOptions.Pipesize,
119+
RcvbufBackend: v2g.TuneOptions.RcvbufBackend,
120+
RcvbufClient: v2g.TuneOptions.RcvbufClient,
121+
RcvbufFrontend: v2g.TuneOptions.RcvbufFrontend,
122+
RcvbufServer: v2g.TuneOptions.RcvbufServer,
123+
RecvEnough: v2g.TuneOptions.RecvEnough,
124+
SndbufBackend: v2g.TuneOptions.SndbufBackend,
125+
SndbufClient: v2g.TuneOptions.SndbufClient,
126+
SndbufFrontend: v2g.TuneOptions.SndbufFrontend,
127+
SndbufServer: v2g.TuneOptions.SndbufServer,
128+
}
129+
if !tuneBufferOptions.Equal(v3.TuneBufferOptions{}) {
130+
v3g.TuneBufferOptions = tuneBufferOptions
131+
}
132+
133+
// SslOptions
134+
v3engines, err := ListV2ToV3[v2.SslEngine, v3.SslEngine](v2g.SslEngines)
135+
if err != nil {
136+
return nil, err
137+
}
138+
sslOptions := &v3.SslOptions{
139+
SslEngines: v3engines,
140+
CaBase: v2g.CaBase,
141+
CrtBase: v2g.CrtBase,
142+
DefaultBindCiphers: v2g.SslDefaultBindCiphers,
143+
DefaultBindCiphersuites: v2g.SslDefaultBindCiphersuites,
144+
DefaultBindClientSigalgs: v2g.SslDefaultBindClientSigalgs,
145+
DefaultBindCurves: v2g.SslDefaultBindCurves,
146+
DefaultBindOptions: v2g.SslDefaultBindOptions,
147+
DefaultBindSigalgs: v2g.SslDefaultBindSigalgs,
148+
DefaultServerCiphers: v2g.SslDefaultServerCiphers,
149+
DefaultServerCiphersuites: v2g.SslDefaultServerCiphersuites,
150+
DefaultServerClientSigalgs: v2g.SslDefaultServerClientSigalgs,
151+
DefaultServerCurves: v2g.SslDefaultServerCurves,
152+
DefaultServerOptions: v2g.SslDefaultServerOptions,
153+
DefaultServerSigalgs: v2g.SslDefaultServerSigalgs,
154+
DhParamFile: v2g.SslDhParamFile,
155+
IssuersChainPath: v2g.IssuersChainPath,
156+
LoadExtraFiles: v2g.SslLoadExtraFiles,
157+
Maxsslconn: v2g.Maxsslconn,
158+
Maxsslrate: v2g.Maxsslrate,
159+
ModeAsync: v2g.SslModeAsync,
160+
Propquery: v2g.SslPropquery,
161+
Provider: v2g.SslProvider,
162+
ProviderPath: v2g.SslProviderPath,
163+
// SecurityLevel: not present in v2
164+
ServerVerify: v2g.SslServerVerify,
165+
SkipSelfIssuedCa: v2g.SslSkipSelfIssuedCa,
166+
}
167+
if !sslOptions.Equal(v3.SslOptions{}) {
168+
v3g.SslOptions = sslOptions
169+
}
170+
171+
// EnvironmentOptions
172+
v3preset, err := ListV2ToV3[v2.PresetEnv, v3.PresetEnv](v2g.PresetEnvs)
173+
if err != nil {
174+
return nil, err
175+
}
176+
v3set, err := ListV2ToV3[v2.SetEnv, v3.SetEnv](v2g.SetEnvs)
177+
if err != nil {
178+
return nil, err
179+
}
180+
envOptions := &v3.EnvironmentOptions{
181+
PresetEnvs: v3preset,
182+
SetEnvs: v3set,
183+
Resetenv: v2g.Resetenv,
184+
Unsetenv: v2g.Unsetenv,
185+
}
186+
if !envOptions.Equal(v3.EnvironmentOptions{}) {
187+
v3g.EnvironmentOptions = envOptions
188+
}
189+
190+
// DebugOptions
191+
debugOptions := &v3.DebugOptions{
192+
Anonkey: v2g.Anonkey,
193+
Quiet: v2g.Quiet,
194+
ZeroWarning: v2g.ZeroWarning,
195+
}
196+
if !debugOptions.Equal(v3.DebugOptions{}) {
197+
v3g.DebugOptions = debugOptions
198+
}
199+
200+
// LuaOptions
201+
luaLoad, err := ListV2ToV3[v2.LuaLoad, v3.LuaLoad](v2g.LuaLoads)
202+
if err != nil {
203+
return nil, err
204+
}
205+
luaPrependPath, err := ListV2ToV3[v2.LuaPrependPath, v3.LuaPrependPath](v2g.LuaPrependPath)
206+
if err != nil {
207+
return nil, err
208+
}
209+
luaOptions := &v3.LuaOptions{
210+
LoadPerThread: v2g.LuaLoadPerThread,
211+
Loads: luaLoad,
212+
PrependPath: luaPrependPath,
213+
}
214+
if !luaOptions.Equal(v3.LuaOptions{}) {
215+
v3g.LuaOptions = luaOptions
216+
}
217+
218+
v3g.Daemon = daemon
219+
return v3g, nil
220+
}

0 commit comments

Comments
 (0)