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

Add image lookup #281

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
15 changes: 14 additions & 1 deletion api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,22 @@ type InstanceSourceViaImageConfig struct {
// * `30`-`120`: Represents the Ultra High Performance option.
// For volumes with the auto-tuned performance feature enabled, this is set to the default (minimum) VPUs/GB.
BootVolumeVpusPerGB *int64 `json:"bootVolumeVpusPerGB,omitempty"`

// ImageLookup defines the image lookup parameters for the node image.
ImageLookup *ImageLookup `json:"imageLookup,omitempty"`
}

// LaunchInstanceAvailabilityConfigDetailsRecoveryActionEnum Enum with underlying type: string
// ImageLookup defines the image lookup parameters for the node image.
type ImageLookup struct {
// OperatingSystem defines operating system of the image.
// +optional
OperatingSystem *string `json:"operatingSystem,omitempty"`

// OperatingSystemVersion defines operating system version of the image.
OperatingSystemVersion *string `json:"operatingSystemVersion,omitempty"`
}

// PlatformConfigTypeEnum Enum with underlying type: string
type PlatformConfigTypeEnum string

// Set of constants representing the allowable values for LaunchInstanceAvailabilityConfigDetailsRecoveryActionEnum
Expand Down
34 changes: 34 additions & 0 deletions api/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,22 @@ type InstanceSourceViaImageConfig struct {
// * `30`-`120`: Represents the Ultra High Performance option.
// For volumes with the auto-tuned performance feature enabled, this is set to the default (minimum) VPUs/GB.
BootVolumeVpusPerGB *int64 `json:"bootVolumeVpusPerGB,omitempty"`

// ImageLookup defines the image lookup parameters for the node image.
ImageLookup *ImageLookup `json:"imageLookup,omitempty"`
}

// LaunchInstanceAvailabilityConfigDetailsRecoveryActionEnum Enum with underlying type: string
// ImageLookup defines the image lookup parameters for the node image.
type ImageLookup struct {
// OperatingSystem defines operating system of the image.
// +optional
OperatingSystem *string `json:"operatingSystem,omitempty"`

// OperatingSystemVersion defines operating system version of the image.
OperatingSystemVersion *string `json:"operatingSystemVersion,omitempty"`
}

// PlatformConfigTypeEnum Enum with underlying type: string
type PlatformConfigTypeEnum string

// Set of constants representing the allowable values for LaunchInstanceAvailabilityConfigDetailsRecoveryActionEnum
Expand Down
30 changes: 30 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,32 @@ func (m *MachineScope) GetOrCreateMachine(ctx context.Context) (*core.Instance,
shapeConfig.BaselineOcpuUtilization = value
}
}
imageId := m.OCIMachine.Spec.ImageId
if imageId == "" {
if m.OCIMachine.Spec.InstanceSourceViaImageDetails != nil &&
m.OCIMachine.Spec.InstanceSourceViaImageDetails.ImageLookup != nil {
lookupSpec := m.OCIMachine.Spec.InstanceSourceViaImageDetails.ImageLookup
response, err := m.ComputeClient.ListImages(ctx, core.ListImagesRequest{
CompartmentId: common.String(m.getCompartmentId()),
OperatingSystem: lookupSpec.OperatingSystem,
OperatingSystemVersion: lookupSpec.OperatingSystemVersion,
Shape: common.String(m.OCIMachine.Spec.Shape),
Limit: common.Int(1),
SortBy: core.ListImagesSortByTimecreated,
})
if err != nil {
return nil, err
}
if len(response.Items) == 0 {
return nil, errors.New(fmt.Sprintf("could not lookup image from lookup parameters"))
}
imageId = *response.Items[0].Id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a quick todo maybe for "pagination"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have limit in the request to one as we really need the latest one, hence we sort by time created. I hope this makes sense.

} else {
return nil, errors.New(fmt.Sprintf("image id and image lookup not provided"))
}
}
sourceDetails := core.InstanceSourceViaImageDetails{
ImageId: common.String(m.OCIMachine.Spec.ImageId),
ImageId: common.String(imageId),
}
if m.OCIMachine.Spec.BootVolumeSizeInGBs != "" {
bootVolumeSizeInGBsString := m.OCIMachine.Spec.BootVolumeSizeInGBs
Expand Down
96 changes: 93 additions & 3 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ func TestInstanceReconciliation(t *testing.T) {
matchError: errors.New(fmt.Sprintf("bootVolumeSizeInGBs provided %s is not a valid floating point",
"invalid")),
testSpecificSetup: func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient) {
setupAllParams(ms)
ms.OCIMachine.Spec.BootVolumeSizeInGBs = "invalid"
computeClient.EXPECT().ListInstances(gomock.Any(), gomock.Eq(core.ListInstancesRequest{
DisplayName: common.String("test"),
DisplayName: common.String("name"),
CompartmentId: common.String("test"),
})).Return(core.ListInstancesResponse{}, nil)
},
Expand All @@ -231,6 +232,7 @@ func TestInstanceReconciliation(t *testing.T) {
matchError: errors.New(fmt.Sprintf("bootVolumeSizeInGBs provided %s is not a valid floating point",
"invalid")),
testSpecificSetup: func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient) {
setupAllParams(ms)
ms.OCIMachine.Spec.InstanceId = nil
ms.OCIMachine.Name = "test"
ms.OCIMachine.Spec.BootVolumeSizeInGBs = "invalid"
Expand All @@ -246,9 +248,10 @@ func TestInstanceReconciliation(t *testing.T) {
errorSubStringMatch: true,
matchError: errors.New("invalid failure domain parameter, must be a valid integer"),
testSpecificSetup: func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient) {
setupAllParams(ms)
ms.Machine.Spec.FailureDomain = common.String("invalid")
computeClient.EXPECT().ListInstances(gomock.Any(), gomock.Eq(core.ListInstancesRequest{
DisplayName: common.String("test"),
DisplayName: common.String("name"),
CompartmentId: common.String("test"),
})).Return(core.ListInstancesResponse{}, nil)
},
Expand All @@ -258,9 +261,10 @@ func TestInstanceReconciliation(t *testing.T) {
errorExpected: true,
matchError: errors.New("failure domain should be a value between 1 and 3"),
testSpecificSetup: func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient) {
setupAllParams(ms)
ms.Machine.Spec.FailureDomain = common.String("4")
computeClient.EXPECT().ListInstances(gomock.Any(), gomock.Eq(core.ListInstancesRequest{
DisplayName: common.String("test"),
DisplayName: common.String("name"),
CompartmentId: common.String("test"),
})).Return(core.ListInstancesResponse{}, nil)
},
Expand Down Expand Up @@ -381,6 +385,92 @@ func TestInstanceReconciliation(t *testing.T) {
OpcRetryToken: ociutil.GetOPCRetryToken("machineuid")})).Return(core.LaunchInstanceResponse{}, nil)
},
},
{
name: "image lookup",
errorExpected: false,
testSpecificSetup: func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient) {
setupAllParams(ms)
ms.OCIMachine.Spec.ImageId = ""
ms.OCIMachine.Spec.CapacityReservationId = common.String("cap-id")
ms.OCIMachine.Spec.DedicatedVmHostId = common.String("dedicated-host-id")
ms.OCIMachine.Spec.NetworkDetails.HostnameLabel = common.String("hostname-label")
ms.OCIMachine.Spec.NetworkDetails.SkipSourceDestCheck = common.Bool(true)
ms.OCIMachine.Spec.NetworkDetails.AssignPrivateDnsRecord = common.Bool(true)
ms.OCIMachine.Spec.NetworkDetails.DisplayName = common.String("display-name")
ms.OCIMachine.Spec.InstanceSourceViaImageDetails = &infrastructurev1beta2.InstanceSourceViaImageConfig{
KmsKeyId: common.String("kms-key-id"),
BootVolumeVpusPerGB: common.Int64(32),
ImageLookup: &infrastructurev1beta2.ImageLookup{
OperatingSystem: common.String("Oracle Linux"),
OperatingSystemVersion: common.String("8"),
},
}
computeClient.EXPECT().ListInstances(gomock.Any(), gomock.Eq(core.ListInstancesRequest{
DisplayName: common.String("name"),
CompartmentId: common.String("test"),
})).Return(core.ListInstancesResponse{}, nil)

computeClient.EXPECT().ListImages(gomock.Any(), gomock.Eq(core.ListImagesRequest{
CompartmentId: common.String("test"),
OperatingSystem: common.String("Oracle Linux"),
OperatingSystemVersion: common.String("8"),
Limit: common.Int(1),
Shape: common.String("shape"),
SortBy: core.ListImagesSortByTimecreated,
})).Return(core.ListImagesResponse{
Items: []core.Image{
{
Id: common.String("test-lookup-image"),
},
},
}, nil)

launchDetails := core.LaunchInstanceDetails{DisplayName: common.String("name"),
CapacityReservationId: common.String("cap-id"),
DedicatedVmHostId: common.String("dedicated-host-id"),
SourceDetails: core.InstanceSourceViaImageDetails{
ImageId: common.String("test-lookup-image"),
BootVolumeSizeInGBs: common.Int64(120),
KmsKeyId: common.String("kms-key-id"),
BootVolumeVpusPerGB: common.Int64(32),
},
CreateVnicDetails: &core.CreateVnicDetails{
SubnetId: common.String("nodesubnet"),
AssignPublicIp: common.Bool(false),
DefinedTags: map[string]map[string]interface{}{},
FreeformTags: map[string]string{
ociutil.CreatedBy: ociutil.OCIClusterAPIProvider,
ociutil.ClusterResourceIdentifier: "resource_uid",
},
NsgIds: make([]string, 0),
HostnameLabel: common.String("hostname-label"),
SkipSourceDestCheck: common.Bool(true),
AssignPrivateDnsRecord: common.Bool(true),
DisplayName: common.String("display-name"),
},
Metadata: map[string]string{
"user_data": base64.StdEncoding.EncodeToString([]byte("test")),
},
Shape: common.String("shape"),
ShapeConfig: &core.LaunchInstanceShapeConfigDetails{
Ocpus: common.Float32(2),
MemoryInGBs: common.Float32(100),
BaselineOcpuUtilization: core.LaunchInstanceShapeConfigDetailsBaselineOcpuUtilization8,
},
AvailabilityDomain: common.String("ad2"),
CompartmentId: common.String("test"),
IsPvEncryptionInTransitEnabled: common.Bool(true),
DefinedTags: map[string]map[string]interface{}{},
FreeformTags: map[string]string{
ociutil.CreatedBy: ociutil.OCIClusterAPIProvider,
ociutil.ClusterResourceIdentifier: "resource_uid",
},
}
computeClient.EXPECT().LaunchInstance(gomock.Any(), gomock.Eq(core.LaunchInstanceRequest{
LaunchInstanceDetails: launchDetails,
OpcRetryToken: ociutil.GetOPCRetryToken("machineuid")})).Return(core.LaunchInstanceResponse{}, nil)
},
},
{
name: "shape config is empty",
errorExpected: false,
Expand Down
1 change: 1 addition & 0 deletions cloud/scope/subnet_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *ClusterScope) ReconcileSubnet(ctx context.Context) error {
s.Logger.Info("Created the security list", "ocid", seclistId)
desiredSubnet.SecurityList.ID = seclistId
} else {
desiredSubnet.SecurityList.ID = securityList.Id
if s.IsSecurityListEqual(*securityList, *desiredSubnet.SecurityList) {
s.Logger.Info("No Reconciliation Required for Security List", "securitylist", securityList.Id)
} else {
Expand Down
1 change: 1 addition & 0 deletions cloud/services/compute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type ComputeClient interface {
ListInstances(ctx context.Context, request core.ListInstancesRequest) (response core.ListInstancesResponse, err error)
AttachVnic(ctx context.Context, request core.AttachVnicRequest) (response core.AttachVnicResponse, err error)
ListVnicAttachments(ctx context.Context, request core.ListVnicAttachmentsRequest) (response core.ListVnicAttachmentsResponse, err error)
ListImages(ctx context.Context, request core.ListImagesRequest) (response core.ListImagesResponse, err error)
}
Loading