Skip to content

Commit

Permalink
GCP: Add ignition GCP stage
Browse files Browse the repository at this point in the history
** 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.
  • Loading branch information
barbacbd committed Feb 6, 2024
1 parent 0c5daee commit 0f0965c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gcp
package clusterapi

import (
"context"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
36 changes: 35 additions & 1 deletion pkg/infrastructure/gcp/clusterapi/clusterapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down

0 comments on commit 0f0965c

Please sign in to comment.