Skip to content

Commit 9a8b820

Browse files
Merge pull request #8170 from r4f4/capi-aws-ignition-proxy
CORS-3417: capi/aws: support ignition proxy and CA bundle
2 parents b5ed3cd + 14fa8cc commit 9a8b820

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

pkg/asset/machines/aws/awsmachines.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
package aws
33

44
import (
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+
}

pkg/asset/machines/clusterapi.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,19 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
147147
return fmt.Errorf("failed to create CAPA tags from UserTags: %w", err)
148148
}
149149

150+
ignition, err := aws.CapaIgnitionWithCertBundleAndProxy(installConfig.Config.AdditionalTrustBundle, installConfig.Config.Proxy)
151+
if err != nil {
152+
return fmt.Errorf("failed to generation CAPA ignition: %w", err)
153+
}
154+
150155
pool.Platform.AWS = &mpool
151156
awsMachines, err := aws.GenerateMachines(clusterID.InfraID, &aws.MachineInput{
152157
Role: "master",
153158
Pool: &pool,
154159
Subnets: subnets,
155160
Tags: tags,
156161
PublicIP: false,
162+
Ignition: ignition,
157163
})
158164
if err != nil {
159165
return errors.Wrap(err, "failed to create master machine objects")
@@ -170,6 +176,7 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
170176
Pool: &pool,
171177
Tags: tags,
172178
PublicIP: installConfig.Config.Publish == types.ExternalPublishingStrategy,
179+
Ignition: ignition,
173180
})
174181
if err != nil {
175182
return fmt.Errorf("failed to create bootstrap machine object: %w", err)

0 commit comments

Comments
 (0)