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 http source for kubevirt VM Disk Image #1470

Merged

Conversation

sankalp-r
Copy link
Contributor

@sankalp-r sankalp-r commented Oct 31, 2022

What this PR does / why we need it:
This PR introduces PrimarDiskImageSource for the KubeVirt provider. The source can be HTTP or PVC-Clone.
If the source is HTTP with preAllocatedDataVolume as false, the VM disk-image will be downloaded from the HTTP source.
If the source is HTTP with preAllocatedDataVolume as true, the VM disk-image will be downloaded just once to create PreAllocatedDataVolume and VM Disks will be created by cloning over PreAllocatedDataVolume.
If the source is PVC-Clone, VM Disks will be created by cloning over custom-local-disks.

Which issue(s) this PR fixes:

Fixes #kubermatic/kubermatic#11234

What type of PR is this?

/kind feature

Special notes for your reviewer:

Does this PR introduce a user-facing change? Then add your Release Note here:

Add HTTP source for  KubeVirt VM Disk images which enables caching to avoid downloading images.

Documentation:

TBD

Signed-off-by: Sankalp Rangare sankalprangare786@gmail.com

@kubermatic-bot kubermatic-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. docs/tbd Denotes a PR that needs documentation (change) that will be done later. kind/feature Categorizes issue or PR as related to a new feature. dco-signoff: yes Denotes that all commits in the pull request have the valid DCO signoff message. sig/osm Denotes a PR or issue as being assigned to SIG OSM. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 31, 2022
@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch from d7a7115 to 4d5ebfb Compare October 31, 2022 11:47
@sankalp-r sankalp-r changed the title add http source for kubevirt diskImage add http source for kubevirt VM Disk Image Oct 31, 2022
@sankalp-r sankalp-r added the sig/virtualization Denotes a PR or issue as being assigned to SIG Virtualization. label Oct 31, 2022
@sankalp-r
Copy link
Contributor Author

/retest

@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch from 4d5ebfb to 8db73b3 Compare October 31, 2022 13:20
@sankalp-r sankalp-r changed the title add http source for kubevirt VM Disk Image [WIP] add http source for kubevirt VM Disk Image Nov 7, 2022
@kubermatic-bot kubermatic-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 7, 2022
@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch from 8db73b3 to c8f3062 Compare November 7, 2022 14:35
@sankalp-r sankalp-r changed the title [WIP] add http source for kubevirt VM Disk Image add http source for kubevirt VM Disk Image Nov 7, 2022
@kubermatic-bot kubermatic-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 7, 2022
@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch 2 times, most recently from e40b7d3 to b61138f Compare November 7, 2022 16:07
@sankalp-r
Copy link
Contributor Author

/retest

Copy link
Contributor

@mfranczy mfranczy left a comment

Choose a reason for hiding this comment

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

I did a first review round. I will repeat after new changes.

@@ -93,7 +98,7 @@ type Config struct {
CPUs string
Memory string
Namespace string
OsImage OSImage
OsImage *OSImage
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be OSImage on the left side as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw why it has to be a pointer now?

@@ -149,8 +154,18 @@ type SecondaryDisks struct {
}

type OSImage struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

please add comments to each structure with explanation of the structutre itself and fields


type HTTPSource struct {
URL string
PreAllocatedDataVolume bool
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe let's rename to CacheImage

DataVolumeName string
NameSpace string
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be named Namespace

}

var parsedOSImage *OSImage
// If PrimaryDiskOSSource is specified
Copy link
Contributor

Choose a reason for hiding this comment

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

I would extend this comment and say that if source is specified then that means we deal with new API

parsedOSImage = &OSImage{PVCClone: &PVCSource{DataVolumeName: osImage, NameSpace: nameSpace}}
}
}
// If PrimaryDiskOSSource is not specified
Copy link
Contributor

Choose a reason for hiding this comment

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

We have to extend that comment and say that we are dealing here with older API and it's a matter of backward compatibility

if !exist {
return sigClient.Create(ctx, newDataVolume(c, dvName, machine.Name))
}
// annotate existing DataVolume with VM-name, if already exist.
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is not needed, the code is quite clear

Copy link
Contributor

Choose a reason for hiding this comment

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

the same with the below comment

return sigClient.Create(ctx, newDataVolume(c, dvName, machine.Name))
}
// annotate existing DataVolume with VM-name, if already exist.
updatedDV := existingDV.DeepCopy()
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for doing a DeepCopy in here? Controller runtime get function will return already a deep copy.

func (p *provider) Cleanup(ctx context.Context, machine *clusterv1alpha1.Machine, _ *cloudprovidertypes.ProviderData) (bool, error) {
func (p *provider) createPreAllocatedDataVolume(ctx context.Context, c *Config, machine *clusterv1alpha1.Machine, sigClient client.Client, dvName string) error {
existingDV := &cdiv1beta1.DataVolume{}
exist := true
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can get rid of this flag, it's not required. You can check if the object is empty.

Copy link
Contributor

@mfranczy mfranczy Nov 9, 2022

Choose a reason for hiding this comment

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

What I mean here is that you can return an error from Get and implement a check that if err is not found then create a new data volume.

}

// Source.
type Source struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

We need better comments with explanation about the fields.

Copy link
Contributor

@mfranczy mfranczy left a comment

Choose a reason for hiding this comment

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

Another review round.

@@ -66,6 +69,8 @@ const (
// machineDeploymentLabelKey defines the label key used to contains as value the MachineDeployment name
// which machine comes from.
machineDeploymentLabelKey = "md"
// preAllocatedDVAnnotation defines the annotation for preAllocated DataVolume for cloning.
preAllocatedDVAnnotation = "datavolume.clone.kubevirt.io/machine"
Copy link
Contributor

Choose a reason for hiding this comment

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

let's change to disk-clone.machines.cluster.k8s.io/machine-%s

@@ -679,9 +759,39 @@ func (p *provider) Cleanup(ctx context.Context, machine *clusterv1alpha1.Machine
return true, nil
}

// cleanup preAllocated DataVolume
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed comment

annotationKey := fmt.Sprintf("%s-%s", preAllocatedDVAnnotation, machine.Name)
if dv.Annotations[annotationKey] == "true" {
// If the Machine-Deployment is scaled to zero or deleted or preAllocateDataVolume is disabled, remove preAllocated DataVolume.
if len(dv.Annotations) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's keep it safe and change to <= 1

return sigClient.Delete(ctx, dv)
}
// Remove annotation of corresponding VM from preAllocated DataVolume.
updatedDv := dv.DeepCopy()
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need deep copy

pkg/cloudprovider/provider/kubevirt/provider.go Outdated Show resolved Hide resolved
@@ -93,7 +98,7 @@ type Config struct {
CPUs string
Memory string
Namespace string
OsImage OSImage
OSImage *OSImage
Copy link
Contributor

Choose a reason for hiding this comment

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

Why it has to be a pointer now?

Copy link
Contributor Author

@sankalp-r sankalp-r Nov 9, 2022

Choose a reason for hiding this comment

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

Notting specific, just parseOSImage() return pointer so I can directly assign it to config.OSImage
It does not affect the old API.

Comment on lines 601 to 605
var machineDeploymentName string
if mdName, err := machineDeploymentNameAndRevisionForMachineGetter(ctx, machine, data.Client)(); err == nil {
machineDeploymentName = mdName
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if I follow this logic.
Why it cannot be:

mdName, err := machineDeploymentNameAndRevisionForMachineGetter(ctx, machine, data.Client)()
if err != nil {
    return nil, err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before this change, we used to create VMs even if getting the MD-name returned error.
So continue with earlier behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

so if MD-name returns an error then we have a problem. Based on that you create a data volume.

if !kerrors.IsNotFound(err) {
return err
}
// create DV if it doesn't exist
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't need comments like that, code is self explanatory

@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch 2 times, most recently from ed2fb43 to 3721aa8 Compare November 9, 2022 19:15
@sankalp-r
Copy link
Contributor Author

/retest

PVCClone *PVCCloneSource `json:"pvcClone,omitempty"`
}

// HTTPSource.
Copy link
Contributor

@mfranczy mfranczy Nov 10, 2022

Choose a reason for hiding this comment

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

We need better comments with explanation about the fields.

The same for all sources (HTTP and PVCClone)

@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch from 3721aa8 to 842b0cb Compare November 10, 2022 12:13
@mfranczy
Copy link
Contributor

I am sorry @sankalp-r I have to block this PR, we have to do another brainstorming session

/hold

@kubermatic-bot kubermatic-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 10, 2022

// PVCSource represents pvc source for cloning VMImage.
type PVCSource struct {
// DataVolumeName represent name of source pvc for cloning.
DataVolumeName string
Copy link
Contributor

Choose a reason for hiding this comment

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

let's change DataVolumeName to Name

@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch 2 times, most recently from ea253d9 to 5293085 Compare November 15, 2022 17:11
@sankalp-r
Copy link
Contributor Author

/retest

2 similar comments
@sankalp-r
Copy link
Contributor Author

/retest

@sankalp-r
Copy link
Contributor Author

/retest

@@ -66,6 +66,10 @@ const (
// machineDeploymentLabelKey defines the label key used to contains as value the MachineDeployment name
// which machine comes from.
machineDeploymentLabelKey = "md"
// httpSource defines the http source type for VM Disk Image.
httpSource = "http"
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to introduce imageSource type.

type imageSource string

const (
   ...
   httpSource imageSource = "http"
   pvcSource imageSource = "pvc"
)

and later in the switch statement you would have

switch imageSource(osImageSource) ...

case httpSource:
return &cdiv1beta1.DataVolumeSource{HTTP: &cdiv1beta1.DataVolumeSourceHTTP{URL: osImage}}, nil
case pvcSource:
if nameSpaceAndName := strings.Split(osImage, "/"); len(nameSpaceAndName) >= 2 {
Copy link
Contributor

Choose a reason for hiding this comment

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

please name it namespaceAndName (namespace is one word)

@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch 3 times, most recently from 419ff5d to 970c3b0 Compare November 17, 2022 13:37
Signed-off-by: Sankalp Rangare <sankalprangare786@gmail.com>
@sankalp-r sankalp-r force-pushed the update-kubevirt-diskimagesource branch from 970c3b0 to 9dc2b69 Compare November 17, 2022 13:47
Copy link
Contributor

@mfranczy mfranczy left a comment

Choose a reason for hiding this comment

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

/unhold
/approve
/lgtm

@kubermatic-bot kubermatic-bot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Nov 17, 2022
@kubermatic-bot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 908bfa286cec24942d7608b937967988b36079f9

@sankalp-r
Copy link
Contributor Author

/retest

Copy link
Member

@ahmedwaleedmalik ahmedwaleedmalik left a comment

Choose a reason for hiding this comment

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

/approve ⚡

@kubermatic-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahmedwaleedmalik, mfranczy, sankalp-r

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubermatic-bot kubermatic-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 21, 2022
@kubermatic-bot kubermatic-bot merged commit 099b8e3 into kubermatic:main Nov 21, 2022
@@ -167,7 +168,12 @@ func (k kubevirtProviderSpecConf) rawProviderSpec(t *testing.T) []byte {
"osImage": "http://x.y.z.t/ubuntu.img",
{{- end }}
"size": "10Gi",
{{- if .OsImageSource }}
"storageClassName": "longhorn",
Copy link
Contributor

Choose a reason for hiding this comment

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

As storageClassName is set to "longhorn" in the if and the else, it could go out of the if/else condition.

Copy link
Contributor

Choose a reason for hiding this comment

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

there is https://github.com/kubermatic/machine-controller/pull/1470/files#diff-2e539081e4b1bd387e1af416faf12022e219faba4f706b478b0f25523315a7a4R175

it could be just one if to add OsImageSource true, but the current state doesn't break the behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Denotes that all commits in the pull request have the valid DCO signoff message. docs/tbd Denotes a PR that needs documentation (change) that will be done later. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/osm Denotes a PR or issue as being assigned to SIG OSM. sig/virtualization Denotes a PR or issue as being assigned to SIG Virtualization. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants