From 0f0965c05067c9dd5e885c4a6ecdef689fc00ab5 Mon Sep 17 00:00:00 2001 From: Brent Barbachem Date: Tue, 6 Feb 2024 09:26:21 -0500 Subject: [PATCH] GCP: Add ignition GCP stage ** Add the ignition stage. The ignition stage will create a GCP storage bucket and a signed url. The Bucket is filled with the original data intended for ignition, and the shim will contain the url. --- .../gcp/{ => clusterapi}/bootstrap.go | 23 +++++------- .../gcp/clusterapi/clusterapi.go | 36 ++++++++++++++++++- 2 files changed, 44 insertions(+), 15 deletions(-) rename pkg/infrastructure/gcp/{ => clusterapi}/bootstrap.go (89%) diff --git a/pkg/infrastructure/gcp/bootstrap.go b/pkg/infrastructure/gcp/clusterapi/bootstrap.go similarity index 89% rename from pkg/infrastructure/gcp/bootstrap.go rename to pkg/infrastructure/gcp/clusterapi/bootstrap.go index 57a4037fa22..24d8d7f7968 100644 --- a/pkg/infrastructure/gcp/bootstrap.go +++ b/pkg/infrastructure/gcp/clusterapi/bootstrap.go @@ -1,4 +1,4 @@ -package gcp +package clusterapi import ( "context" @@ -52,18 +52,12 @@ func CreateStorage(ctx context.Context, ic *installconfig.InstallConfig, cluster return fmt.Errorf("failed to create bucket handle: %w", err) } - labels := map[string]string{} - labels[fmt.Sprintf("kubernetes-io-cluster-%s", clusterID)] = "owned" - for _, label := range ic.Config.GCP.UserLabels { - labels[label.Key] = label.Value - } - bucketAttrs := storage.BucketAttrs{ UniformBucketLevelAccess: storage.UniformBucketLevelAccess{ Enabled: true, }, Location: ic.Config.GCP.Region, - Labels: labels, + Labels: mergeLabels(ic, clusterID), } ctx, cancel := context.WithTimeout(ctx, time.Second*60) @@ -85,7 +79,7 @@ func CreateSignedURL(handle *storage.BucketHandle, objectName string) (string, e ctx := context.Background() session, err := gcpic.GetSession(ctx) if err != nil { - return "", err + return "", fmt.Errorf("failed to create gcp session: %w", err) } // TODO: make sure all cases are handled including the cases required by https://github.com/openshift/installer/pull/7697 @@ -109,21 +103,22 @@ func CreateSignedURL(handle *storage.BucketHandle, objectName string) (string, e } // ProvisionBootstrapStorage will provision the required storage bucket and signed url for the bootstrap process. -func ProvisionBootstrapStorage(ic *installconfig.InstallConfig, clusterID string) (string, error) { - ctx := context.Background() +func ProvisionBootstrapStorage(ctx context.Context, ic *installconfig.InstallConfig, clusterID string) (string, error) { + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) + defer cancel() if err := CreateStorage(ctx, ic, clusterID, BootstrapIgnitionBucket); err != nil { - return "", nil + return "", fmt.Errorf("failed to provision bootstrap %w", err) } bucketHandle, err := CreateBucketHandle(ctx, GetBootstrapStorageName(clusterID)) if err != nil { - return "", err + return "", fmt.Errorf("failed to provision bootstrap: %w", err) } url, err := CreateSignedURL(bucketHandle, BootstrapIgnitionBucket) if err != nil { - return "", err + return "", fmt.Errorf("failed to provision bootstrap: %w", err) } return url, nil diff --git a/pkg/infrastructure/gcp/clusterapi/clusterapi.go b/pkg/infrastructure/gcp/clusterapi/clusterapi.go index 1811e28b49b..756bb1db808 100644 --- a/pkg/infrastructure/gcp/clusterapi/clusterapi.go +++ b/pkg/infrastructure/gcp/clusterapi/clusterapi.go @@ -2,7 +2,12 @@ package clusterapi import ( "context" + "encoding/json" + "fmt" + ignutil "github.com/coreos/ignition/v2/config/util" + igntypes "github.com/coreos/ignition/v2/config/v3_2/types" gcptypes "github.com/openshift/installer/pkg/types/gcp" + "time" "github.com/openshift/installer/pkg/infrastructure/clusterapi" ) @@ -22,7 +27,36 @@ func (p Provider) Ignition(ctx context.Context, in clusterapi.IgnitionInput) ([] // Create the bucket and presigned url. The url is generated using a known/expected name so that the // url can be retrieved from the api by this name. - return nil, nil + ctx, cancel := context.WithTimeout(ctx, time.Minute*2) + defer cancel() + + url, err := ProvisionBootstrapStorage(ctx, in.InstallConfig, in.InfraID) + if err != nil { + return nil, fmt.Errorf("ignition failed to provision storage: %w", err) + } + + if err := FillBucket(ctx, BootstrapIgnitionBucket, BootstrapIgnitionBucket, string(in.BootstrapIgnData)); err != nil { + return nil, fmt.Errorf("ignition failed to fill bucket: %w", err) + } + + // Generate an ignition stub where the URL is stored + ign := igntypes.Config{ + Ignition: igntypes.Ignition{ + Version: igntypes.MaxVersion.String(), + Config: igntypes.IgnitionConfig{ + Replace: igntypes.Resource{ + Source: ignutil.StrToPtr(url), + }, + }, + }, + } + + ignShimBytes, err := json.Marshal(ign) + if err != nil { + return nil, fmt.Errorf("failed to marshal ignition shim: %w", err) + } + + return ignShimBytes, nil } func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput) error {