Skip to content

Commit

Permalink
Add vmID, vpcID, vmName in vm object
Browse files Browse the repository at this point in the history
- Also remove unwanted labels from VM object.
- Add and update existing VMIndexers to use labels
and new fields added in the vm status object.
- Also update vpc label to use account namespaced name instead
of using only the account name.

Signed-off-by: Anand Kumar <[email protected]>
  • Loading branch information
Anandkumar26 committed Mar 21, 2023
1 parent ece3a48 commit df93005
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 155 deletions.
7 changes: 7 additions & 0 deletions apis/runtime/v1alpha1/virtualmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ type VirtualMachineStatus struct {
Region string `json:"region,omitempty"`
// Agented specifies if VM runs in agented mode, default is false.
Agented bool `json:"agented"`
// CloudAssignedId is the ID of the VM.
CloudAssignedId string `json:"cloudAssignedId,omitempty"`
// CloudAssignedName is the name of the VM.
CloudAssignedName string `json:"cloudAssignedName,omitempty"`
// CloudAssignedVPCId is the VPC ID of the VM.
CloudAssignedVPCId string `json:"cloudAssignedVPCId,omitempty"`
}

// +kubebuilder:object:root=true
Expand All @@ -115,6 +121,7 @@ type VirtualMachine struct {
}

// +kubebuilder:object:root=true

// VirtualMachineList contains a list of VirtualMachine.
type VirtualMachineList struct {
metav1.TypeMeta `json:",inline"`
Expand Down
6 changes: 6 additions & 0 deletions apis/runtime/v1alpha1/vpc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type VpcStatus struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Vpc is the Schema for the vpc API
// A vpc object is created automatically based on
// matching criteria specification of CloudProviderAccount.
type Vpc struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -37,6 +42,7 @@ type Vpc struct {
}

// +kubebuilder:object:root=true

// VpcList is a list of Vpc objects.
type VpcList struct {
metav1.TypeMeta `json:",inline"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
runtimev1alpha1 "antrea.io/nephe/apis/runtime/v1alpha1"
"antrea.io/nephe/pkg/controllers/config"
"antrea.io/nephe/pkg/controllers/inventory"
"antrea.io/nephe/pkg/controllers/inventory/common"
"antrea.io/nephe/pkg/logging"
)

Expand All @@ -44,8 +43,7 @@ var _ = Describe("Virtual Machine", func() {
Namespace: "default",
Name: "targetId",
Labels: map[string]string{
config.LabelCloudAccountID: "default/accountid1",
config.LabelCloudAssignedID: "i-0a2fa4c17ee786ad4",
config.LabelCloudNamespacedAccountName: "default/accountid1",
},
},
Status: runtimev1alpha1.VirtualMachineStatus{
Expand All @@ -64,7 +62,7 @@ var _ = Describe("Virtual Machine", func() {
Namespace: "non-default",
Name: "targetId-nondefault",
Labels: map[string]string{
config.LabelCloudAccountID: "default/accountid",
config.LabelCloudNamespacedAccountName: "default/accountid",
},
},
Status: runtimev1alpha1.VirtualMachineStatus{
Expand All @@ -79,7 +77,7 @@ var _ = Describe("Virtual Machine", func() {
Namespace: "default",
Name: "targetId2",
Labels: map[string]string{
config.LabelCloudAccountID: "default/accountid",
config.LabelCloudNamespacedAccountName: "default/accountid",
},
},
Status: runtimev1alpha1.VirtualMachineStatus{
Expand Down Expand Up @@ -208,7 +206,7 @@ var _ = Describe("Virtual Machine", func() {
}
vmMap := make(map[string]*runtimev1alpha1.VirtualMachine)
vmMap[cacheTest4.Name] = cacheTest4
namespacedName := types.NamespacedName{Namespace: cacheTest4.Namespace, Name: cacheTest4.Labels[common.VpcLabelAccountName]}
namespacedName := types.NamespacedName{Namespace: cacheTest4.Namespace, Name: cacheTest4.Labels[config.LabelCloudNamespacedAccountName]}
cloudInventory.BuildVmCache(vmMap, &namespacedName)

rest := NewREST(cloudInventory, l)
Expand All @@ -223,7 +221,7 @@ var _ = Describe("Virtual Machine", func() {
Namespace: "default",
Name: "targetId",
Labels: map[string]string{
config.LabelCloudAccountID: "default/accountID",
config.LabelCloudNamespacedAccountName: "default/accountID",
},
},
Status: runtimev1alpha1.VirtualMachineStatus{
Expand Down
19 changes: 6 additions & 13 deletions pkg/apiserver/registry/inventory/vpc/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apiserver/pkg/registry/rest"

runtimev1alpha1 "antrea.io/nephe/apis/runtime/v1alpha1"
"antrea.io/nephe/pkg/controllers/config"
"antrea.io/nephe/pkg/controllers/inventory"
"antrea.io/nephe/pkg/controllers/inventory/common"
"antrea.io/nephe/pkg/controllers/inventory/store"
Expand Down Expand Up @@ -87,21 +88,19 @@ func (r *REST) Get(ctx context.Context, name string, _ *metav1.GetOptions) (runt
func (r *REST) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
// List only supports four types of input options
// 1. All namespace
// 2. Labelselector with only the specific namespace, the only valid labelselectors are "account-name=<accountname>" and "region=<region>"
// 2. Labelselector with only the specific namespace, the only valid labelselectors is "region=<region>"
// 3. Fieldselector with only the specific namespace, the only valid fieldselectors is "metadata.name=<metadata.name>"
// 4. Specific Namespace
accountName := ""
region := ""

if options != nil && options.LabelSelector != nil && options.LabelSelector.String() != "" {
labelSelectorStrings := strings.Split(options.LabelSelector.String(), ",")
for _, labelSelectorString := range labelSelectorStrings {
labelKeyAndValue := strings.Split(labelSelectorString, "=")
if labelKeyAndValue[0] == common.VpcLabelAccountName {
accountName = labelKeyAndValue[1]
} else if labelKeyAndValue[0] == common.VpcLabelRegion {
if labelKeyAndValue[0] == config.LabelCloudRegion {
region = strings.ToLower(labelKeyAndValue[1])
} else {
return nil, errors.NewBadRequest("unsupported label selector, supported labels are: account-name and region")
return nil, errors.NewBadRequest("unsupported label selector, only region label selector is supported")
}
}
}
Expand Down Expand Up @@ -131,18 +130,12 @@ func (r *REST) List(ctx context.Context, options *internalversion.ListOptions) (
}

var objs []interface{}
if namespace == "" && (accountName != "" || region != "" || name != "") {
if namespace == "" && (region != "" || name != "") {
return nil, errors.NewBadRequest("cannot query with all namespaces. Namespace should be specified")
}

if namespace == "" {
objs = r.cloudInventory.GetAllVpcs()
} else if accountName != "" {
accountNameSpace := types.NamespacedName{
Namespace: namespace,
Name: accountName,
}
objs, _ = r.cloudInventory.GetVpcsFromIndexer(common.VpcIndexerByNameSpacedAccountName, accountNameSpace.String())
} else if name != "" {
namespacedName := types.NamespacedName{
Namespace: namespace,
Expand Down
62 changes: 36 additions & 26 deletions pkg/apiserver/registry/inventory/vpc/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"k8s.io/apiserver/pkg/endpoints/request"

runtimev1alpha1 "antrea.io/nephe/apis/runtime/v1alpha1"
"antrea.io/nephe/pkg/controllers/config"
"antrea.io/nephe/pkg/controllers/inventory"
"antrea.io/nephe/pkg/controllers/inventory/common"
"antrea.io/nephe/pkg/logging"
)

Expand Down Expand Up @@ -74,8 +74,8 @@ var _ = Describe("VPC", func() {
Namespace: "default",
Name: "targetId2",
Labels: map[string]string{
common.VpcLabelAccountName: "accountname",
common.VpcLabelRegion: "region",
config.LabelCloudNamespacedAccountName: "default/accountname",
config.LabelCloudRegion: "region",
},
},
Status: runtimev1alpha1.VpcStatus{
Expand Down Expand Up @@ -103,10 +103,11 @@ var _ = Describe("VPC", func() {
}

Describe("Test Get function of Rest", func() {
By("Test Get function of Rest")
for i, cachedVpc := range cachedVpcs {
vpcMap := make(map[string]*runtimev1alpha1.Vpc)
vpcMap[cachedVpc.Status.Id] = cachedVpc
namespacedName := types.NamespacedName{Namespace: cachedVpc.Namespace, Name: cachedVpc.Labels[common.VpcLabelAccountName]}
namespacedName := types.NamespacedName{Namespace: cachedVpc.Namespace, Name: "accountname"}
err := cloudInventory.BuildVpcCache(vpcMap, &namespacedName)
Expect(err).Should(BeNil())
rest := NewREST(cloudInventory, l)
Expand All @@ -122,6 +123,7 @@ var _ = Describe("VPC", func() {
})

Describe("Test List function of Rest", func() {
By("Test List function of Rest")
expectedPolicyList1 := &runtimev1alpha1.VpcList{
Items: []runtimev1alpha1.Vpc{
{
Expand All @@ -144,8 +146,8 @@ var _ = Describe("VPC", func() {
Namespace: "default",
Name: "targetId2",
Labels: map[string]string{
common.VpcLabelAccountName: "accountname",
common.VpcLabelRegion: "region",
config.LabelCloudNamespacedAccountName: "default/accountname",
config.LabelCloudRegion: "region",
},
},
Status: runtimev1alpha1.VpcStatus{
Expand All @@ -168,8 +170,8 @@ var _ = Describe("VPC", func() {
Namespace: "default",
Name: "targetId2",
Labels: map[string]string{
common.VpcLabelAccountName: "accountname",
common.VpcLabelRegion: "region",
config.LabelCloudNamespacedAccountName: "default/accountname",
config.LabelCloudRegion: "region",
},
},
Status: runtimev1alpha1.VpcStatus{
Expand All @@ -188,30 +190,30 @@ var _ = Describe("VPC", func() {

expectedPolicyLists := []*runtimev1alpha1.VpcList{
expectedPolicyList1,
expectedPolicyList2,
//expectedPolicyList2,
expectedPolicyList2,
}
req1, _ := labels.NewRequirement("account-name", selection.Equals, []string{"accountname"})
labelSelector1 := labels.NewSelector()
labelSelector1 = labelSelector1.Add(*req1)
//req1, _ := labels.NewRequirement(config.LabelCloudNamespacedAccountName, selection.Equals, []string{"default/accountname"})
//labelSelector1 := labels.NewSelector()
//labelSelector1 = labelSelector1.Add(*req1)

req2, _ := labels.NewRequirement("region", selection.Equals, []string{"region"})
req2, _ := labels.NewRequirement(config.LabelCloudRegion, selection.Equals, []string{"region"})
labelSelector2 := labels.NewSelector()
labelSelector2 = labelSelector2.Add(*req2)

listLabelSelectorOption1 := &internalversion.ListOptions{}
listLabelSelectorOption2 := &internalversion.ListOptions{LabelSelector: labelSelector1}
//listLabelSelectorOption2 := &internalversion.ListOptions{LabelSelector: labelSelector1}
listLabelSelectorOption3 := &internalversion.ListOptions{LabelSelector: labelSelector2}

vpcLabelSelectorListOptions := []*internalversion.ListOptions{
listLabelSelectorOption1,
listLabelSelectorOption2,
//listLabelSelectorOption2,
listLabelSelectorOption3,
}
for _, cachedVpc := range cachedVpcs {
vpcMap := make(map[string]*runtimev1alpha1.Vpc)
vpcMap[cachedVpc.Status.Id] = cachedVpc
namespacedName := types.NamespacedName{Namespace: cachedVpc.Namespace, Name: cachedVpc.Labels[common.VpcLabelAccountName]}
namespacedName := types.NamespacedName{Namespace: cachedVpc.Namespace, Name: cachedVpc.Labels[config.LabelCloudNamespacedAccountName]}
err := cloudInventory.BuildVpcCache(vpcMap, &namespacedName)
Expect(err).Should(BeNil())
}
Expand All @@ -227,6 +229,7 @@ var _ = Describe("VPC", func() {
Expect(actualObj).To(Equal(expectedPolicyLists[i]))
}
})

listFieldSelectorOption1 := &internalversion.ListOptions{}
listFieldSelectorOption1.FieldSelector = fields.OneTermEqualSelector("metadata.name", "targetId2")
listFieldSelectorOption2 := &internalversion.ListOptions{}
Expand Down Expand Up @@ -288,9 +291,10 @@ var _ = Describe("VPC", func() {
},
}
Describe("Test Convert table function of Rest", func() {
By("Test Convert table function of Rest")
vpcMap := make(map[string]*runtimev1alpha1.Vpc)
vpcMap[cacheTest4.Status.Id] = cacheTest4
namespacedName := types.NamespacedName{Namespace: cacheTest4.Namespace, Name: cacheTest4.Labels[common.VpcLabelAccountName]}
namespacedName := types.NamespacedName{Namespace: cacheTest4.Namespace, Name: "accountname"}
err := cloudInventory.BuildVpcCache(vpcMap, &namespacedName)
Expect(err).Should(BeNil())
rest := NewREST(cloudInventory, l)
Expand All @@ -303,10 +307,14 @@ var _ = Describe("VPC", func() {
cacheTest5 := &runtimev1alpha1.Vpc{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "targetId",
Name: "targetId2",
Labels: map[string]string{
config.LabelCloudNamespacedAccountName: "default/accountname",
config.LabelCloudRegion: "region",
},
},
Status: runtimev1alpha1.VpcStatus{
Id: "targetId",
Id: "targetId2",
Name: "targetName",
Tags: map[string]string{
"no.delete": "false",
Expand All @@ -316,22 +324,24 @@ var _ = Describe("VPC", func() {
}
expectedEvents := []watch.Event{
{Type: watch.Bookmark, Object: &runtimev1alpha1.Vpc{}},
{Type: watch.Added, Object: cacheTest1},
{Type: watch.Modified, Object: cacheTest5},
{Type: watch.Added, Object: cacheTest3},
{Type: watch.Modified, Object: cacheTest5}, // Add logic to test modify event.
{Type: watch.Deleted, Object: cacheTest5},
}

Describe("Test Watch function of Rest", func() {
By("Test Watch function of Rest")

cloudInventory1 := inventory.InitInventory()
vpcMap := make(map[string]*runtimev1alpha1.Vpc)
namespacedName := types.NamespacedName{Namespace: cacheTest1.Namespace, Name: cacheTest1.Labels[common.VpcLabelAccountName]}
err := cloudInventory1.BuildVpcCache(vpcMap, &namespacedName)
Expect(err).Should(BeNil())
namespacedName := types.NamespacedName{Namespace: cacheTest3.Namespace, Name: "accountname"}
rest := NewREST(cloudInventory1, l)
watcher, err := rest.Watch(request.NewDefaultContext(), &internalversion.ListOptions{})
vpcMap[cacheTest1.Status.Id] = cacheTest1
vpcMap[cacheTest3.Status.Id] = cacheTest3
_ = cloudInventory1.BuildVpcCache(vpcMap, &namespacedName)
vpcMap[cacheTest1.Status.Id] = cacheTest5
vpcMap[cacheTest3.Status.Id] = cacheTest5
_ = cloudInventory1.BuildVpcCache(vpcMap, &namespacedName)

Expect(err).Should(BeNil())
err = cloudInventory1.DeleteVpcsFromCache(&namespacedName)
Expect(err).Should(BeNil())
Expand Down
8 changes: 4 additions & 4 deletions pkg/cloud-provider/cloudapi/aws/aws_crd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

runtimev1alpha1 "antrea.io/nephe/apis/runtime/v1alpha1"
"antrea.io/nephe/pkg/cloud-provider/utils"
"antrea.io/nephe/pkg/controllers/inventory/common"
"antrea.io/nephe/pkg/controllers/config"
)

const ResourceNameTagKey = "Name"
Expand Down Expand Up @@ -79,7 +79,7 @@ func ec2InstanceToVirtualMachineCRD(instance *ec2.Instance, namespace, accountId
}

// ec2VpcToInternalVpcObject converts ec2 vpc object to vpc runtime object.
func ec2VpcToInternalVpcObject(vpc *ec2.Vpc, namespace string, accountName string, region string, managed bool) *runtimev1alpha1.Vpc {
func ec2VpcToInternalVpcObject(vpc *ec2.Vpc, namespace, namespacedAccountName, region string, managed bool) *runtimev1alpha1.Vpc {
cloudName := ""
tags := make(map[string]string, 0)
if len(vpc.Tags) != 0 {
Expand All @@ -97,8 +97,8 @@ func ec2VpcToInternalVpcObject(vpc *ec2.Vpc, namespace string, accountName strin
}
}
labelsMap := map[string]string{
common.VpcLabelAccountName: accountName,
common.VpcLabelRegion: region,
config.LabelCloudNamespacedAccountName: namespacedAccountName,
config.LabelCloudRegion: region,
}

return utils.GenerateInternalVpcObject(*vpc.VpcId, namespace, labelsMap, strings.ToLower(cloudName),
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud-provider/cloudapi/aws/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (ec2Cfg *ec2ServiceConfig) GetVpcInventory() map[string]*runtimev1alpha1.Vp
if _, ok := vpcIDs[*vpc.VpcId]; ok {
managed = true
}
vpcObj := ec2VpcToInternalVpcObject(vpc, tokens[0], tokens[1], strings.ToLower(ec2Cfg.credentials.region), managed)
vpcObj := ec2VpcToInternalVpcObject(vpc, tokens[0], ec2Cfg.accountName, strings.ToLower(ec2Cfg.credentials.region), managed)
vpcMap[strings.ToLower(*vpc.VpcId)] = vpcObj
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud-provider/cloudapi/azure/azure_compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (computeCfg *computeServiceConfig) GetVpcInventory() map[string]*runtimev1a
if _, ok := vnetIDs[strings.ToLower(*vpc.ID)]; ok {
managed = true
}
vpcObj := ComputeVpcToInternalVpcObject(&vpc, tokens[0], tokens[1], strings.ToLower(computeCfg.credentials.region), managed)
vpcObj := ComputeVpcToInternalVpcObject(&vpc, tokens[0], computeCfg.accountName, strings.ToLower(computeCfg.credentials.region), managed)
vpcMap[strings.ToLower(*vpc.ID)] = vpcObj
}
}
Expand Down
Loading

0 comments on commit df93005

Please sign in to comment.