22package aws
33
44import (
5+ "bytes"
6+ "encoding/pem"
57 "fmt"
8+ "strings"
69
10+ "github.com/vincent-petithory/dataurl"
711 v1 "k8s.io/api/core/v1"
812 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
913 "k8s.io/apimachinery/pkg/util/sets"
@@ -24,6 +28,7 @@ type MachineInput struct {
2428 Subnets map [string ]string
2529 Tags capa.Tags
2630 PublicIP bool
31+ Ignition * capa.Ignition
2732}
2833
2934// GenerateMachines returns manifests and runtime objects to provision the control plane (including bootstrap, if applicable) nodes using CAPI.
@@ -70,7 +75,7 @@ func GenerateMachines(clusterID string, in *MachineInput) ([]*asset.RuntimeFile,
7075 },
7176 },
7277 Spec : capa.AWSMachineSpec {
73- Ignition : & capa .Ignition { Version : "3.2" } ,
78+ Ignition : in .Ignition ,
7479 UncompressedUserData : ptr .To (true ),
7580 InstanceType : mpool .InstanceType ,
7681 AMI : capa.AMIReference {ID : ptr .To (mpool .AMIID )},
@@ -164,3 +169,62 @@ func CapaTagsFromUserTags(clusterID string, usertags map[string]string) (capa.Ta
164169 }
165170 return tags , nil
166171}
172+
173+ // CapaIgnitionWithCertBundleAndProxy generates CAPA ignition config with cert and proxy information.
174+ func CapaIgnitionWithCertBundleAndProxy (userCA string , proxy * types.Proxy ) (* capa.Ignition , error ) {
175+ carefs , err := parseCertificateBundle ([]byte (userCA ))
176+ if err != nil {
177+ return nil , err
178+ }
179+ return & capa.Ignition {
180+ Version : "3.2" ,
181+ TLS : & capa.IgnitionTLS {
182+ CASources : carefs ,
183+ },
184+ Proxy : capaIgnitionProxy (proxy ),
185+ }, nil
186+ }
187+
188+ // TODO: try to share this code with ignition.bootstrap package?
189+ // parseCertificateBundle loads each certificate in the bundle to the CAPA
190+ // carrier type, ignoring any invisible character before, after and in between
191+ // certificates.
192+ func parseCertificateBundle (userCA []byte ) ([]capa.IgnitionCASource , error ) {
193+ userCA = bytes .TrimSpace (userCA )
194+
195+ var carefs []capa.IgnitionCASource
196+ for len (userCA ) > 0 {
197+ var block * pem.Block
198+ block , userCA = pem .Decode (userCA )
199+ if block == nil {
200+ return nil , fmt .Errorf ("unable to parse certificate, please check the certificates" )
201+ }
202+
203+ carefs = append (carefs , capa .IgnitionCASource (dataurl .EncodeBytes (pem .EncodeToMemory (block ))))
204+
205+ userCA = bytes .TrimSpace (userCA )
206+ }
207+
208+ return carefs , nil
209+ }
210+
211+ func capaIgnitionProxy (proxy * types.Proxy ) * capa.IgnitionProxy {
212+ capaProxy := & capa.IgnitionProxy {}
213+ if proxy == nil {
214+ return capaProxy
215+ }
216+ if httpProxy := proxy .HTTPProxy ; httpProxy != "" {
217+ capaProxy .HTTPProxy = & httpProxy
218+ }
219+ if httpsProxy := proxy .HTTPSProxy ; httpsProxy != "" {
220+ capaProxy .HTTPSProxy = & httpsProxy
221+ }
222+ capaProxy .NoProxy = make ([]capa.IgnitionNoProxy , 0 , len (proxy .NoProxy ))
223+ if noProxy := proxy .NoProxy ; noProxy != "" {
224+ noProxySplit := strings .Split (noProxy , "," )
225+ for _ , p := range noProxySplit {
226+ capaProxy .NoProxy = append (capaProxy .NoProxy , capa .IgnitionNoProxy (p ))
227+ }
228+ }
229+ return capaProxy
230+ }
0 commit comments