Skip to content

Commit a80acf0

Browse files
committed
OCPBUGS-12890: Create GCP Bucket and signed url
** Create an ignition shim and use this for metadata. ** Add proxy information to the shim.
1 parent a9da892 commit a80acf0

File tree

5 files changed

+50
-90
lines changed

5 files changed

+50
-90
lines changed

data/data/gcp/bootstrap/main.tf

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,6 @@ provider "google" {
1111
region = var.gcp_region
1212
}
1313

14-
resource "google_storage_bucket" "ignition" {
15-
name = "${var.cluster_id}-bootstrap-ignition"
16-
location = var.gcp_region
17-
uniform_bucket_level_access = true
18-
labels = var.gcp_extra_labels
19-
}
20-
21-
resource "google_tags_location_tag_binding" "user_tag_binding_bucket" {
22-
for_each = var.gcp_extra_tags
23-
24-
parent = format("//storage.googleapis.com/projects/_/buckets/%s",
25-
google_storage_bucket.ignition.name,
26-
)
27-
tag_value = each.value
28-
location = var.gcp_region
29-
30-
depends_on = [google_storage_bucket.ignition]
31-
}
32-
33-
resource "google_storage_bucket_object" "ignition" {
34-
bucket = google_storage_bucket.ignition.name
35-
name = "bootstrap.ign"
36-
content = var.ignition_bootstrap
37-
}
38-
39-
resource "google_service_account" "bootstrap-node-sa" {
40-
count = var.gcp_create_bootstrap_sa ? 1 : 0
41-
account_id = "${var.cluster_id}-b"
42-
display_name = "${var.cluster_id}-bootstrap-node"
43-
description = local.description
44-
}
45-
46-
resource "google_service_account_key" "bootstrap" {
47-
count = var.gcp_create_bootstrap_sa ? 1 : 0
48-
service_account_id = google_service_account.bootstrap-node-sa[0].name
49-
}
50-
51-
resource "google_project_iam_member" "bootstrap-storage-admin" {
52-
count = var.gcp_create_bootstrap_sa ? 1 : 0
53-
project = var.gcp_project_id
54-
role = "roles/storage.admin"
55-
member = "serviceAccount:${google_service_account.bootstrap-node-sa[0].email}"
56-
}
57-
58-
data "google_storage_object_signed_url" "ignition_url" {
59-
bucket = google_storage_bucket.ignition.name
60-
path = "bootstrap.ign"
61-
duration = "1h"
62-
credentials = var.gcp_create_bootstrap_sa ? base64decode(google_service_account_key.bootstrap[0].private_key) : null
63-
}
64-
65-
data "ignition_config" "redirect" {
66-
replace {
67-
source = data.google_storage_object_signed_url.ignition_url.signed_url
68-
}
69-
}
70-
7114
resource "google_compute_address" "bootstrap" {
7215
name = "${var.cluster_id}-bootstrap-ip"
7316
description = local.description
@@ -144,7 +87,7 @@ resource "google_compute_instance" "bootstrap" {
14487
}
14588

14689
metadata = {
147-
user-data = data.ignition_config.redirect.rendered
90+
user-data = var.gcp_ignition_shim
14891
}
14992

15093
tags = ["${var.cluster_id}-master", "${var.cluster_id}-bootstrap"]

data/data/gcp/variables-gcp.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ variable "gcp_master_on_host_maintenance" {
153153
default = ""
154154
}
155155

156-
variable "gcp_create_bootstrap_sa" {
157-
type = bool
158-
default = false
159-
description = "Whether a service account should be created to sign the ignition URL."
160-
}
161-
162156
variable "gcp_user_provisioned_dns" {
163157
type = bool
164158
default = false
@@ -175,3 +169,9 @@ Example: `{ "tagKeys/123" = "tagValues/456", "tagKeys/456" = "tagValues/789" }`
175169
EOF
176170
default = {}
177171
}
172+
173+
variable "gcp_ignition_shim" {
174+
type = string
175+
description = "Ignition stub containing the signed url that points to the bucket containing the ignition data."
176+
default = ""
177+
}

pkg/asset/cluster/tfvars/tfvars.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/openshift/installer/pkg/asset/ignition"
3030
"github.com/openshift/installer/pkg/asset/ignition/bootstrap"
3131
baremetalbootstrap "github.com/openshift/installer/pkg/asset/ignition/bootstrap/baremetal"
32+
gcpbootstrap "github.com/openshift/installer/pkg/asset/ignition/bootstrap/gcp"
3233
"github.com/openshift/installer/pkg/asset/ignition/machine"
3334
"github.com/openshift/installer/pkg/asset/installconfig"
3435
awsconfig "github.com/openshift/installer/pkg/asset/installconfig/aws"
@@ -515,6 +516,29 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
515516
}
516517
}
517518

519+
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
520+
defer cancel()
521+
522+
bucketName := gcpbootstrap.GetBootstrapStorageName(clusterID.InfraID)
523+
bucketHandle, err := gcpbootstrap.CreateBucketHandle(ctx, bucketName)
524+
if err != nil {
525+
return fmt.Errorf("failed to create bucket handle %s: %w", bucketName, err)
526+
}
527+
528+
bootstrapIgnURL, err := gcpbootstrap.ProvisionBootstrapStorage(ctx, installConfig, bucketHandle, clusterID.InfraID)
529+
if err != nil {
530+
return fmt.Errorf("failed to provision gcp bootstrap storage resources: %w", err)
531+
}
532+
533+
if err := gcpbootstrap.FillBucket(ctx, bucketHandle, bootstrapIgn); err != nil {
534+
return fmt.Errorf("failed to fill bootstrap ignition bucket: %w", err)
535+
}
536+
537+
shim, err := bootstrap.GenerateIgnitionShimWithCertBundleAndProxy(bootstrapIgnURL, installConfig.Config.AdditionalTrustBundle, installConfig.Config.Proxy)
538+
if err != nil {
539+
return fmt.Errorf("failed to create gcp ignition shim: %w", err)
540+
}
541+
518542
archName := coreosarch.RpmArch(string(installConfig.Config.ControlPlane.Architecture))
519543
st, err := rhcospkg.FetchCoreOSBuild(ctx)
520544
if err != nil {
@@ -551,6 +575,7 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
551575
InfrastructureName: clusterID.InfraID,
552576
UserProvisionedDNS: installConfig.Config.GCP.UserProvisionedDNS == gcp.UserProvisionedDNSEnabled,
553577
UserTags: tags,
578+
IgnitionShim: string(shim),
554579
},
555580
)
556581
if err != nil {

pkg/terraform/stages/gcp/stages.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package gcp
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
6-
"os"
7+
"time"
78

89
igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
910
"github.com/hashicorp/terraform-exec/tfexec"
1011
"github.com/pkg/errors"
1112

1213
"github.com/openshift/installer/pkg/asset"
1314
"github.com/openshift/installer/pkg/asset/ignition"
15+
gcpbootstrap "github.com/openshift/installer/pkg/asset/ignition/bootstrap/gcp"
1416
"github.com/openshift/installer/pkg/asset/lbconfig"
1517
"github.com/openshift/installer/pkg/terraform"
1618
"github.com/openshift/installer/pkg/terraform/providers"
@@ -118,20 +120,21 @@ func extractGCPLBConfig(s stages.SplitStage, directory string, terraformDir stri
118120
return "", err
119121
}
120122

121-
// Update the ignition bootstrap variable to include the lbconfig.
122-
tfvarData["ignition_bootstrap"] = string(ignitionOutput)
123+
clusterID, ok := tfvarData["cluster_id"]
124+
if !ok {
125+
return "", fmt.Errorf("failed to read cluster id from tfvars")
126+
}
127+
128+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
129+
defer cancel()
123130

124-
// Convert the bootstrap data and write the data back to a file. This will overwrite the original tfvars file.
125-
jsonBootstrap, err := json.Marshal(tfvarData)
131+
bucketName := gcpbootstrap.GetBootstrapStorageName(clusterID.(string))
132+
bucketHandle, err := gcpbootstrap.CreateBucketHandle(ctx, bucketName)
126133
if err != nil {
127-
return "", fmt.Errorf("failed to convert bootstrap ignition to bytes: %w", err)
134+
return "", fmt.Errorf("failed to create bucket handle %s: %w", bucketName, err)
128135
}
129-
tfvarsFile.Data = jsonBootstrap
130-
131-
// update the value on disk to match
132-
if err := os.WriteFile(fmt.Sprintf("%s/%s", directory, tfvarsFile.Filename), jsonBootstrap, 0o600); err != nil {
133-
return "", fmt.Errorf("failed to rewrite %s: %w", tfvarsFile.Filename, err)
136+
if err := gcpbootstrap.FillBucket(context.Background(), bucketHandle, string(ignitionOutput)); err != nil {
137+
return "", fmt.Errorf("failed to fill gcp bucket with updated boostrap ignition contents: %w", err)
134138
}
135-
136139
return "", nil
137140
}

pkg/tfvars/gcp/gcp.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/pkg/errors"
8-
97
machineapi "github.com/openshift/api/machine/v1beta1"
108
"github.com/openshift/installer/pkg/types"
119
)
@@ -29,7 +27,6 @@ type config struct {
2927
Auth `json:",inline"`
3028
Region string `json:"gcp_region,omitempty"`
3129
BootstrapInstanceType string `json:"gcp_bootstrap_instance_type,omitempty"`
32-
CreateBootstrapSA bool `json:"gcp_create_bootstrap_sa"`
3330
CreateFirewallRules bool `json:"gcp_create_firewall_rules"`
3431
MasterInstanceType string `json:"gcp_master_instance_type,omitempty"`
3532
MasterAvailabilityZones []string `json:"gcp_master_availability_zones"`
@@ -52,6 +49,7 @@ type config struct {
5249
ExtraLabels map[string]string `json:"gcp_extra_labels,omitempty"`
5350
UserProvisionedDNS bool `json:"gcp_user_provisioned_dns,omitempty"`
5451
ExtraTags map[string]string `json:"gcp_extra_tags,omitempty"`
52+
IgnitionShim string `json:"gcp_ignition_shim,omitempty"`
5553
}
5654

5755
// TFVarsSources contains the parameters to be converted into Terraform variables
@@ -67,6 +65,7 @@ type TFVarsSources struct {
6765
InfrastructureName string
6866
UserProvisionedDNS bool
6967
UserTags map[string]string
68+
IgnitionShim string
7069
}
7170

7271
// TFVars generates gcp-specific Terraform variables launching the cluster.
@@ -109,6 +108,7 @@ func TFVars(sources TFVarsSources) ([]byte, error) {
109108
ExtraLabels: labels,
110109
UserProvisionedDNS: sources.UserProvisionedDNS,
111110
ExtraTags: sources.UserTags,
111+
IgnitionShim: sources.IgnitionShim,
112112
}
113113

114114
if masterConfig.Disks[0].EncryptionKey != nil {
@@ -124,17 +124,6 @@ func TFVars(sources TFVarsSources) ([]byte, error) {
124124
}
125125
cfg.InstanceServiceAccount = instanceServiceAccount
126126

127-
serviceAccount := make(map[string]interface{})
128-
129-
if err := json.Unmarshal([]byte(cfg.Auth.ServiceAccount), &serviceAccount); len(cfg.Auth.ServiceAccount) > 0 && err != nil {
130-
return nil, errors.Wrapf(err, "unmarshaling service account")
131-
}
132-
133-
// A private key is needed to sign the URL for bootstrap ignition.
134-
// If there is no key in the credentials, we need to generate a new SA.
135-
_, foundKey := serviceAccount["private_key"]
136-
cfg.CreateBootstrapSA = !foundKey
137-
138127
return json.MarshalIndent(cfg, "", " ")
139128
}
140129

0 commit comments

Comments
 (0)