@@ -46,23 +46,72 @@ type Server interface {
4646 GetConfig (poolRequest ) (* runtime.RawExtension , error )
4747}
4848
49- func getAppenders (currMachineConfig string , version * semver.Version , f kubeconfigFunc , certs []string , serverDir string ) []appenderFunc {
50- appenders := []appenderFunc {
51- // append machine annotations file.
49+ // appendersBuilder builds a slice of appenderFunc with guaranteed ordering.
50+ // The builder ensures that appendEncapsulated is always added last.
51+ type appendersBuilder struct {
52+ version * semver.Version
53+ kubeconfigFn kubeconfigFunc
54+ certs []string
55+ serverDir string
56+ customAppenders []appenderFunc
57+ }
58+
59+ // newAppendersBuilder creates a new appendersBuilder.
60+ // Common appenders (kubeconfig, initial machine config, certs) are added automatically in build().
61+ func newAppendersBuilder (version * semver.Version , kubeconfigFn kubeconfigFunc , certs []string , serverDir string ) * appendersBuilder {
62+ return & appendersBuilder {
63+ version : version ,
64+ kubeconfigFn : kubeconfigFn ,
65+ certs : certs ,
66+ serverDir : serverDir ,
67+ customAppenders : make ([]appenderFunc , 0 ),
68+ }
69+ }
70+
71+ // WithNodeAnnotations adds the node annotations appender with the specified config and image.
72+ func (ab * appendersBuilder ) WithNodeAnnotations (currMachineConfig , image string ) * appendersBuilder {
73+ ab .customAppenders = append (ab .customAppenders , func (cfg * ign3types.Config , _ * mcfgv1.MachineConfig ) error {
74+ return appendNodeAnnotations (cfg , currMachineConfig , image )
75+ })
76+ return ab
77+ }
78+
79+ // WithCustomAppender adds a custom appender function.
80+ // Custom appenders are inserted before the common appenders and appendEncapsulated.
81+ func (ab * appendersBuilder ) WithCustomAppender (appender appenderFunc ) * appendersBuilder {
82+ ab .customAppenders = append (ab .customAppenders , appender )
83+ return ab
84+ }
85+
86+ // build creates the final slice of appenderFunc with the correct ordering:
87+ // 1. Custom appenders (node annotations, desired image, etc.)
88+ // 2. Common appenders (kubeconfig, initial machine config, certs)
89+ // 3. appendEncapsulated (always last)
90+ func (ab * appendersBuilder ) build () []appenderFunc {
91+ result := make ([]appenderFunc , 0 , len (ab .customAppenders )+ 4 )
92+
93+ // Add custom appenders first
94+ result = append (result , ab .customAppenders ... )
95+
96+ // Add common appenders and appendEncapsulated
97+ result = append (result ,
98+ // append kubeconfig
5299 func (cfg * ign3types.Config , _ * mcfgv1.MachineConfig ) error {
53- return appendNodeAnnotations (cfg , currMachineConfig , "" )
100+ return appendKubeConfig (cfg , ab . kubeconfigFn )
54101 },
55- // append kubeconfig.
56- func (cfg * ign3types.Config , _ * mcfgv1.MachineConfig ) error { return appendKubeConfig (cfg , f ) },
57102 // append the machineconfig content
58103 appendInitialMachineConfig ,
59- func (cfg * ign3types.Config , _ * mcfgv1.MachineConfig ) error { return appendCerts (cfg , certs , serverDir ) },
60- // This has to come last!!!
104+ // append certs
105+ func (cfg * ign3types.Config , _ * mcfgv1.MachineConfig ) error {
106+ return appendCerts (cfg , ab .certs , ab .serverDir )
107+ },
108+ // appendEncapsulated must always be last
61109 func (cfg * ign3types.Config , mc * mcfgv1.MachineConfig ) error {
62- return appendEncapsulated (cfg , mc , version )
110+ return appendEncapsulated (cfg , mc , ab . version )
63111 },
64- }
65- return appenders
112+ )
113+
114+ return result
66115}
67116
68117func appendCerts (cfg * ign3types.Config , certs []string , serverDir string ) error {
0 commit comments