Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator: drop REST API workaround for SEV-SNP GCP instances #3544

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ go_library(
"@com_github_spf13_afero//:afero",
"@com_google_cloud_go_compute//apiv1",
"@com_google_cloud_go_compute//apiv1/computepb",
"@org_golang_google_api//compute/v1:compute",
"@org_golang_google_api//googleapi",
"@org_golang_google_api//iterator",
"@org_golang_google_protobuf//proto",
Expand Down Expand Up @@ -62,7 +61,6 @@ go_test(
"@com_github_stretchr_testify//require",
"@com_google_cloud_go_compute//apiv1",
"@com_google_cloud_go_compute//apiv1/computepb",
"@org_golang_google_api//compute/v1:compute",
"@org_golang_google_api//googleapi",
"@org_golang_google_api//iterator",
"@org_golang_google_protobuf//proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/googleapis/gax-go/v2"
computeREST "google.golang.org/api/compute/v1"
)

type projectAPI interface {
Expand All @@ -28,9 +27,13 @@ type instanceAPI interface {
}

type instanceTemplateAPI interface {
Get(projectID, template string) (*computeREST.InstanceTemplate, error)
Delete(projectID, template string) (*computeREST.Operation, error)
Insert(projectID string, template *computeREST.InstanceTemplate) (*computeREST.Operation, error)
Close() error
Get(ctx context.Context, req *computepb.GetInstanceTemplateRequest,
opts ...gax.CallOption) (*computepb.InstanceTemplate, error)
Delete(ctx context.Context, req *computepb.DeleteInstanceTemplateRequest,
opts ...gax.CallOption) (Operation, error)
Insert(ctx context.Context, req *computepb.InsertInstanceTemplateRequest,
opts ...gax.CallOption) (Operation, error)
}

type instanceGroupManagersAPI interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

compute "cloud.google.com/go/compute/apiv1"
"github.com/spf13/afero"
computeREST "google.golang.org/api/compute/v1"
)

// Client is a client for the Google Compute Engine.
Expand Down Expand Up @@ -49,17 +48,12 @@ func New(ctx context.Context, configPath string) (*Client, error) {
return nil, err
}
closers = append(closers, insAPI)

// TODO(msanft): Go back to protobuf-based API when it supports setting
// a confidential instance type.
// See https://github.com/googleapis/google-cloud-go/issues/10873 for the current status.
restClient, err := computeREST.NewService(ctx)
templAPI, err := compute.NewInstanceTemplatesRESTClient(ctx)
if err != nil {
_ = closeAll(closers)
return nil, err
}
templAPI := computeREST.NewInstanceTemplatesService(restClient)

closers = append(closers, templAPI)
groupAPI, err := compute.NewInstanceGroupManagersRESTClient(ctx)
if err != nil {
_ = closeAll(closers)
Expand Down Expand Up @@ -87,6 +81,7 @@ func (c *Client) Close() error {
closers := []closer{
c.projectAPI,
c.instanceAPI,
c.instanceTemplateAPI,
c.instanceGroupManagersAPI,
c.diskAPI,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/googleapis/gax-go/v2"
computeREST "google.golang.org/api/compute/v1"
"google.golang.org/api/iterator"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func (a stubInstanceAPI) Get(_ context.Context, _ *computepb.GetInstanceRequest,
}

type stubInstanceTemplateAPI struct {
template *computeREST.InstanceTemplate
template *computepb.InstanceTemplate
getErr error
deleteErr error
insertErr error
Expand All @@ -58,16 +57,30 @@ func (a stubInstanceTemplateAPI) Close() error {
return nil
}

func (a stubInstanceTemplateAPI) Get(_, _ string) (*computeREST.InstanceTemplate, error) {
func (a stubInstanceTemplateAPI) Get(_ context.Context, _ *computepb.GetInstanceTemplateRequest,
_ ...gax.CallOption,
) (*computepb.InstanceTemplate, error) {
return a.template, a.getErr
}

func (a stubInstanceTemplateAPI) Delete(_, _ string) (*computeREST.Operation, error) {
return &computeREST.Operation{}, a.deleteErr
func (a stubInstanceTemplateAPI) Delete(_ context.Context, _ *computepb.DeleteInstanceTemplateRequest,
_ ...gax.CallOption,
) (Operation, error) {
return &stubOperation{
&computepb.Operation{
Name: proto.String("name"),
},
}, a.deleteErr
}

func (a stubInstanceTemplateAPI) Insert(_ string, _ *computeREST.InstanceTemplate) (*computeREST.Operation, error) {
return &computeREST.Operation{}, a.insertErr
func (a stubInstanceTemplateAPI) Insert(_ context.Context, _ *computepb.InsertInstanceTemplateRequest,
_ ...gax.CallOption,
) (Operation, error) {
return &stubOperation{
&computepb.Operation{
Name: proto.String("name"),
},
}, a.insertErr
}

type stubInstanceGroupManagersAPI struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ import (
compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/googleapis/gax-go/v2"
computeREST "google.golang.org/api/compute/v1"
)

type instanceTemplateClient struct {
*computeREST.InstanceTemplatesService
*compute.InstanceTemplatesClient
}

func (c *instanceTemplateClient) Close() error {
return nil // no-op
return c.InstanceTemplatesClient.Close()
}

func (c *instanceTemplateClient) Get(project, template string) (*computeREST.InstanceTemplate, error) {
return c.InstanceTemplatesService.Get(project, template).Do()
}

func (c *instanceTemplateClient) Delete(project, template string) (*computeREST.Operation, error) {
return c.InstanceTemplatesService.Delete(project, template).Do()
func (c *instanceTemplateClient) Delete(ctx context.Context, req *computepb.DeleteInstanceTemplateRequest,
opts ...gax.CallOption,
) (Operation, error) {
return c.InstanceTemplatesClient.Delete(ctx, req, opts...)
}

func (c *instanceTemplateClient) Insert(projectID string, template *computeREST.InstanceTemplate) (*computeREST.Operation, error) {
return c.InstanceTemplatesService.Insert(projectID, template).Do()
func (c *instanceTemplateClient) Insert(ctx context.Context, req *computepb.InsertInstanceTemplateRequest,
opts ...gax.CallOption,
) (Operation, error) {
return c.InstanceTemplatesClient.Insert(ctx, req, opts...)
}

type instanceGroupManagersClient struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/edgelesssys/constellation/v2/internal/constants"
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/api/v1alpha1"
cspapi "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/internal/cloud/api"
computeREST "google.golang.org/api/compute/v1"
"google.golang.org/api/iterator"
)

Expand Down Expand Up @@ -50,22 +49,29 @@ func (c *Client) SetScalingGroupImage(ctx context.Context, scalingGroupID, image
}

// clone template with desired image
if instanceTemplate.Name == "" {
if instanceTemplate.Name == nil {
return fmt.Errorf("instance template of scaling group %q has no name", scalingGroupID)
}
instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage = imageURI
newTemplateName, err := generateInstanceTemplateName(instanceTemplate.Name)
instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage = &imageURI
newTemplateName, err := generateInstanceTemplateName(*instanceTemplate.Name)
if err != nil {
return err
}
instanceTemplate.Name = newTemplateName
if _, err := c.instanceTemplateAPI.Insert(project, instanceTemplate); err != nil {
instanceTemplate.Name = &newTemplateName
op, err := c.instanceTemplateAPI.Insert(ctx, &computepb.InsertInstanceTemplateRequest{
Project: project,
InstanceTemplateResource: instanceTemplate,
})
if err != nil {
return fmt.Errorf("cloning instance template: %w", err)
}
if err := op.Wait(ctx); err != nil {
return fmt.Errorf("waiting for cloned instance template: %w", err)
}

newTemplateURI := joinInstanceTemplateURI(project, newTemplateName)
// update instance group manager to use new template
op, err := c.instanceGroupManagersAPI.SetInstanceTemplate(ctx, &computepb.SetInstanceTemplateInstanceGroupManagerRequest{
op, err = c.instanceGroupManagersAPI.SetInstanceTemplate(ctx, &computepb.SetInstanceTemplateInstanceGroupManagerRequest{
InstanceGroupManager: instanceGroupName,
Project: project,
Zone: zone,
Expand Down Expand Up @@ -129,7 +135,10 @@ func (c *Client) ListScalingGroups(ctx context.Context, uid string) ([]cspapi.Sc
if len(templateURI) < 1 {
continue // invalid template URI
}
template, err := c.instanceTemplateAPI.Get(c.projectID, templateURI[len(templateURI)-1])
template, err := c.instanceTemplateAPI.Get(ctx, &computepb.GetInstanceTemplateRequest{
Project: c.projectID,
InstanceTemplate: templateURI[len(templateURI)-1],
})
if err != nil {
retErr = errors.Join(retErr, fmt.Errorf("getting instance template %q: %w", templateURI[len(templateURI)-1], err))
continue
Expand Down Expand Up @@ -190,7 +199,7 @@ func (c *Client) ListScalingGroups(ctx context.Context, uid string) ([]cspapi.Sc
return results, nil
}

func (c *Client) getScalingGroupTemplate(ctx context.Context, scalingGroupID string) (*computeREST.InstanceTemplate, error) {
func (c *Client) getScalingGroupTemplate(ctx context.Context, scalingGroupID string) (*computepb.InstanceTemplate, error) {
project, zone, instanceGroupName, err := splitInstanceGroupID(scalingGroupID)
if err != nil {
return nil, err
Expand All @@ -210,19 +219,22 @@ func (c *Client) getScalingGroupTemplate(ctx context.Context, scalingGroupID str
if err != nil {
return nil, fmt.Errorf("splitting instance template name: %w", err)
}
instanceTemplate, err := c.instanceTemplateAPI.Get(instanceTemplateProject, instanceTemplateName)
instanceTemplate, err := c.instanceTemplateAPI.Get(ctx, &computepb.GetInstanceTemplateRequest{
InstanceTemplate: instanceTemplateName,
Project: instanceTemplateProject,
})
if err != nil {
return nil, fmt.Errorf("getting instance template %q: %w", instanceTemplateName, err)
}
return instanceTemplate, nil
}

func instanceTemplateSourceImage(instanceTemplate *computeREST.InstanceTemplate) (string, error) {
func instanceTemplateSourceImage(instanceTemplate *computepb.InstanceTemplate) (string, error) {
if instanceTemplate.Properties == nil ||
len(instanceTemplate.Properties.Disks) == 0 ||
instanceTemplate.Properties.Disks[0].InitializeParams == nil ||
instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage == "" {
instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage == nil {
return "", errors.New("instance template has no source image")
}
return uriNormalize(instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage), nil
return uriNormalize(*instanceTemplate.Properties.Disks[0].InitializeParams.SourceImage), nil
}
Loading