Skip to content

Commit

Permalink
Implement vpc poller on account add.
Browse files Browse the repository at this point in the history
- Perform cloud inventory poll for vpcs on CPA add, vm instanes are skipped
  as configured filters are nil.
- Move account poller(fetches cloud data from internal snapshot) to CPA.
- Account poller is applicable for both vpc and vm instance purpose.
- Maintain vpc list globally using cache indexers.
- In CES, use existing account poller(created during CPA add).
- On CES delete, reset vmSelector filters and skip inventory poll for vms,
  do not distub inventory poll for vpcs.
- On CPA delete, stop cloud inventory poll and remove account poller.
- Add unit test for the changes introduced.
- Add documentation for vpc poller.

Signed-off-by: Archana Holla <[email protected]>
  • Loading branch information
archanapholla authored and reachjainrahul committed Jan 9, 2023
1 parent e6ca0c5 commit 1278b02
Show file tree
Hide file tree
Showing 32 changed files with 2,011 additions and 378 deletions.
46 changes: 46 additions & 0 deletions apis/runtime/v1alpha1/vpc_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 Antrea Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"antrea.io/nephe/apis/crd/v1alpha1"
)

type VpcInfo struct {
Name string
Id string
CloudProvider v1alpha1.CloudProvider
Region string
Tags map[string]string
Cidrs []string
}

// +kubebuilder:object:root=true
type Vpc struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Info VpcInfo `json:"spec,omitempty"`
}

// +kubebuilder:object:root=true
// VpcList is a list of Vpc objects.
type VpcList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Vpc `json:"items"`
}
85 changes: 85 additions & 0 deletions apis/runtime/v1alpha1/zz_generated.deepcopy.go

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

16 changes: 13 additions & 3 deletions cmd/nephe-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"antrea.io/nephe/pkg/apiserver"
nephewebhook "antrea.io/nephe/pkg/apiserver/webhook"
controllers "antrea.io/nephe/pkg/controllers/cloud"
"antrea.io/nephe/pkg/controllers/inventory"
"antrea.io/nephe/pkg/logging"
// +kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -79,19 +80,28 @@ func main() {
os.Exit(1)
}

// Initialize vpc inventory cache.
cloudInventory := inventory.InitInventory()

// Initialize Account poller map.
poller := controllers.InitPollers()

if err = (&controllers.CloudEntitySelectorReconciler{
Client: mgr.GetClient(),
Log: logging.GetLogger("controllers").WithName("CloudEntitySelector"),
Scheme: mgr.GetScheme(),
Poller: poller,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudEntitySelector")
os.Exit(1)
}

if err = (&controllers.CloudProviderAccountReconciler{
Client: mgr.GetClient(),
Log: logging.GetLogger("controllers").WithName("CloudProviderAccount"),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Log: logging.GetLogger("controllers").WithName("CloudProviderAccount"),
Scheme: mgr.GetScheme(),
Inventory: cloudInventory,
Poller: poller,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudProviderAccount")
os.Exit(1)
Expand Down
47 changes: 47 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,53 @@ spec:
EOF
```

After a `CloudProviderAccount` CR is added, VPCs are polled from cloud for the
configured region. From this VPC inventory, VPC ID or VPC Name can be used in
`CloudEntitySelector` configuration to onboard vms belonging to the VPC of interest.

```bash
kubectl get vpc -A
```

```text
# Output
NAMESPACE NAME CLOUD PROVIDER REGION
sample-ns vpc-0f54c9f1b395038ab AWS us-west-1
sample-ns vpc-04269a331ab6cd649 AWS us-west-1
sample-ns vpc-047156bebab1083c9 AWS us-west-1
```

Use describe on VPC object to get `Id` or `Name` field and use it in vpcMatch
section of `CloudEntitySelector` configuration.

```bash
kubectl describe vpc vpc-0f54c9f1b395038ab -n sample-ns
```

```text
# Output
Name: vpc-0f54c9f1b395038ab
Namespace: sample-ns
Labels: account-name=cloudprovideraccount-aws-sample
region=us-west-1
Annotations: <none>
API Version: runtime.cloud.antrea.io/v1alpha1
Kind: Vpc
Metadata:
Creation Timestamp: <nil>
Spec:
Cidrs:
10.0.0.0/16
Cloud Provider: AWS
Id: vpc-0f54c9f1b395038ab
Name: test-us-west1-vpc
Region: us-west-1
Tags:
Name: test-us-west1-vpc
Terraform: true
Events: <none>
```

If there are any virtual machines in VPC `VPC_ID`, those virtual machines will
be imported. Invoke kubectl commands to get the details of imported VMs.

Expand Down
Binary file added docs/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions pkg/cloud-provider/cloudapi/aws/aws_cloudinterface_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"antrea.io/nephe/apis/crd/v1alpha1"
cloudv1alpha1 "antrea.io/nephe/apis/crd/v1alpha1"
runtimev1alpha1 "antrea.io/nephe/apis/runtime/v1alpha1"
cloudcommon "antrea.io/nephe/pkg/cloud-provider/cloudapi/common"
"antrea.io/nephe/pkg/cloud-provider/cloudapi/internal"
"antrea.io/nephe/pkg/logging"
Expand Down Expand Up @@ -103,3 +104,18 @@ func (c *awsCloud) RemoveAccountResourcesSelector(accNamespacedName *types.Names
func (c *awsCloud) GetAccountStatus(accNamespacedName *types.NamespacedName) (*cloudv1alpha1.CloudProviderAccountStatus, error) {
return c.cloudCommon.GetStatus(accNamespacedName)
}

// AddInventoryPoller adds poller for polling cloud inventory.
func (c *awsCloud) AddInventoryPoller(accountNamespacedName *types.NamespacedName) error {
return c.cloudCommon.AddInventoryPoller(accountNamespacedName)
}

// DeleteInventoryPoller deletes an existing poller created for polling cloud inventory.
func (c *awsCloud) DeleteInventoryPoller(accountNamespacedName *types.NamespacedName) error {
return c.cloudCommon.DeleteInventoryPoller(accountNamespacedName)
}

// GetVpcInventory pulls cloud vpc inventory from internal snapshot.
func (c *awsCloud) GetVpcInventory(accountNamespacedName *types.NamespacedName) (map[string]*runtimev1alpha1.Vpc, error) {
return c.cloudCommon.GetVpcInventory(accountNamespacedName)
}
29 changes: 29 additions & 0 deletions pkg/cloud-provider/cloudapi/aws/aws_crd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"

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

const ResourceNameTagKey = "Name"
Expand Down Expand Up @@ -76,3 +78,30 @@ func ec2InstanceToVirtualMachineCRD(instance *ec2.Instance, namespace string, ac
strings.ToLower(cloudNetwork), cloudNetwork, v1alpha1.VMState(*instance.State.Name), tags, networkInterfaces,
providerType, accountId)
}

// ec2VpcToInternalVpcObject converts ec2 vpc object to vpc runtime object.
func ec2VpcToInternalVpcObject(vpc *ec2.Vpc, namespace string, accountName string, region string) *runtimev1alpha1.Vpc {
cloudName := ""
tags := make(map[string]string, 0)
if len(vpc.Tags) != 0 {
for _, tag := range vpc.Tags {
tags[*(tag.Key)] = *(tag.Value)
}
if value, found := tags["Name"]; found {
cloudName = value
}
}
cidrs := make([]string, 0)
if len(vpc.CidrBlockAssociationSet) != 0 {
for _, cidr := range vpc.CidrBlockAssociationSet {
cidrs = append(cidrs, *cidr.CidrBlock)
}
}
labelsMap := map[string]string{
inventory.VpcLabelAccountName: accountName,
inventory.VpcLabelRegion: region,
}

return utils.GenerateInternalVpcObject(*vpc.VpcId, namespace, labelsMap, cloudName, *vpc.VpcId, tags,
v1alpha1.AWSCloudProvider, region, cidrs)
}
Loading

0 comments on commit 1278b02

Please sign in to comment.