Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
joekr committed Jan 25, 2023
1 parent dbdec76 commit 7d7b874
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 66 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ generate-e2e-templates: $(KUSTOMIZE)
$(KUSTOMIZE) build $(OCI_TEMPLATES)/v1beta1/cluster-template-remote-vcn-peering --load-restrictor LoadRestrictionsNone > $(OCI_TEMPLATES)/v1beta1/cluster-template-remote-vcn-peering.yaml
$(KUSTOMIZE) build $(OCI_TEMPLATES)/v1beta1/cluster-template-externally-managed-vcn --load-restrictor LoadRestrictionsNone > $(OCI_TEMPLATES)/v1beta1/cluster-template-externally-managed-vcn.yaml
$(KUSTOMIZE) build $(OCI_TEMPLATES)/v1beta1/cluster-template-machine-pool --load-restrictor LoadRestrictionsNone > $(OCI_TEMPLATES)/v1beta1/cluster-template-machine-pool.yaml
$(KUSTOMIZE) build $(OCI_TEMPLATES)/v1beta1/cluster-template-windows-calico --load-restrictor LoadRestrictionsNone > $(OCI_TEMPLATES)/v1beta1/cluster-template-windows-calico.yaml

.PHONY: test-e2e-run
test-e2e-run: generate-e2e-templates $(GINKGO) $(ENVSUBST) ## Run e2e tests
Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/conditions_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const (
RouteTableEventReady = "RouteTableReady"
// SubnetEventReady used after reconciliation has completed successfully
SubnetEventReady = "SubnetReady"
// InstanceVnicAttachmentReady used after reconciliation has been completed successfully
InstanceVnicAttachmentReady = "VnicAttachmentReady"
// ApiServerLoadBalancerEventReady used after reconciliation has completed successfully
ApiServerLoadBalancerEventReady = "APIServerLoadBalancerReady"
// FailureDomainEventReady used after reconciliation has completed successfully
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type VnicAttachment struct {
// VnicAttachmentId defines the ID of the VnicAttachment
VnicAttachmentId *string `json:"vnicAttachmentId,omitempty"`

// AssignPublicIp defines whether the instance should have a public IP address
// AssignPublicIp defines whether the vnic should have a public IP address
// +optional
AssignPublicIp bool `json:"assignPublicIp,omitempty"`

Expand All @@ -77,7 +77,7 @@ type VnicAttachment struct {
// +optional
SubnetName string `json:"subnetName,omitempty"`

// DisplayName defines a user-friendly name. Does not have to be unique, and it's changeable.
// DisplayName defines a user-friendly name. Does not have to be unique.
// Avoid entering confidential information.
DisplayName *string `json:"displayName"`

Expand Down
16 changes: 4 additions & 12 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,7 @@ func (m *MachineScope) GetOrCreateMachine(ctx context.Context) (*core.Instance,

tags := m.getFreeFormTags(*m.OCICluster)

definedTags := make(map[string]map[string]interface{})
if m.OCIMachine.Spec.DefinedTags != nil {
for ns, mapNs := range m.OCIMachine.Spec.DefinedTags {
mapValues := make(map[string]interface{})
for k, v := range mapNs {
mapValues[k] = v
}
definedTags[ns] = mapValues
}
}
definedTags := ConvertMachineDefinedTags(m.OCIMachine.Spec.DefinedTags)

availabilityDomain := m.OCICluster.Status.FailureDomains[*failureDomain].Attributes[AvailabilityDomain]
faultDomain := m.OCICluster.Status.FailureDomains[*failureDomain].Attributes[FaultDomain]
Expand Down Expand Up @@ -474,8 +465,10 @@ func (m *MachineScope) GetInstanceIp(ctx context.Context) (*string, error) {
}
}

if page = resp.OpcNextPage; resp.OpcNextPage == nil {
if resp.OpcNextPage == nil {
break
} else {
page = resp.OpcNextPage
}
}

Expand Down Expand Up @@ -624,7 +617,6 @@ func (m *MachineScope) getGetControlPlaneMachineNSGs() []string {
// and returns the subnet ID if the name matches
func (m *MachineScope) getMachineSubnet(name string) (*string, error) {
for _, subnet := range m.OCICluster.Spec.NetworkSpec.Vcn.Subnets {
// if a subnet name is defined, use the correct subnet
if subnet.Name == name {
return subnet.ID, nil
}
Expand Down
17 changes: 17 additions & 0 deletions cloud/scope/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ func GetSubnetNamesFromId(ids []string, subnets []*infrastructurev1beta1.Subnet)
}
return names
}

// ConvertMachineDefinedTags passes in the OCIMachineSpec DefinedTags and returns a converted map of defined tags
// to be used when creating API requests.
func ConvertMachineDefinedTags(machineDefinedTags map[string]map[string]string) map[string]map[string]interface{} {
definedTags := make(map[string]map[string]interface{})
if machineDefinedTags != nil {
for ns, mapNs := range machineDefinedTags {
mapValues := make(map[string]interface{})
for k, v := range mapNs {
mapValues[k] = v
}
definedTags[ns] = mapValues
}
}

return definedTags
}
54 changes: 11 additions & 43 deletions cloud/scope/vnic_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package scope
import (
"context"
"fmt"
"github.com/oracle/cluster-api-provider-oci/cloud/ociutil"

infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
"github.com/oracle/cluster-api-provider-oci/cloud/ociutil"
"github.com/oracle/oci-go-sdk/v65/common"
"github.com/oracle/oci-go-sdk/v65/core"
"github.com/pkg/errors"
Expand All @@ -18,9 +18,7 @@ func (m *MachineScope) ReconcileVnicAttachments(ctx context.Context) error {

for index, vnicAttachment := range m.OCIMachine.Spec.VnicAttachments {
if m.vnicAttachmentExists(ctx, vnicAttachment) {
m.Logger.Info("vnicAttachment", vnicAttachment.DisplayName, " already exists")
// TODO: update vs create https://docs.oracle.com/en-us/iaas/api/#/en/iaas/20160918/Vnic/UpdateVnic
m.updateVnicAttachment(ctx, vnicAttachment)
m.Logger.Info("vnicAttachment", ociutil.DerefString(vnicAttachment.DisplayName), " already exists and is immutable")
continue
}

Expand All @@ -31,9 +29,8 @@ func (m *MachineScope) ReconcileVnicAttachments(ctx context.Context) error {
m.Logger.Error(err, msg)
return err
}
// TODO: kick off the "flush" so ids are stored as soon as they are attached

m.OCIMachine.Spec.VnicAttachments[index].VnicAttachmentId = vnicId
vnicAttachment.VnicAttachmentId = common.String("TESTING")
}

return nil
Expand All @@ -54,16 +51,7 @@ func (m *MachineScope) createVnicAttachment(ctx context.Context, spec infrastruc

tags := m.getFreeFormTags(*m.OCICluster)

definedTags := make(map[string]map[string]interface{})
if m.OCIMachine.Spec.DefinedTags != nil {
for ns, mapNs := range m.OCIMachine.Spec.DefinedTags {
mapValues := make(map[string]interface{})
for k, v := range mapNs {
mapValues[k] = v
}
definedTags[ns] = mapValues
}
}
definedTags := ConvertMachineDefinedTags(m.OCIMachine.Spec.DefinedTags)

if spec.NicIndex == nil {
spec.NicIndex = common.Int(0)
Expand All @@ -85,38 +73,16 @@ func (m *MachineScope) createVnicAttachment(ctx context.Context, spec infrastruc
},
}

retryToken := fmt.Sprintf("%s-%s", string(m.OCIMachine.UID), *vnicName)
req := core.AttachVnicRequest{AttachVnicDetails: secondVnic,
OpcRetryToken: ociutil.GetOPCRetryToken(retryToken)}
req := core.AttachVnicRequest{AttachVnicDetails: secondVnic}
resp, err := m.ComputeClient.AttachVnic(ctx, req)
m.Logger.Info("AttachVnic resp: ", resp)

if err != nil {
return nil, err
}

return resp.VnicId, nil
}

func (m *MachineScope) updateVnicAttachment(ctx context.Context, spec infrastructurev1beta1.VnicAttachment) error {

req := core.UpdateVnicRequest{
UpdateVnicDetails: core.UpdateVnicDetails{
DisplayName: spec.DisplayName,
HostnameLabel: m.OCIMachine.Spec.NetworkDetails.HostnameLabel},
VnicId: spec.VnicAttachmentId}

// Send the request using the service client
resp, err := m.VCNClient.UpdateVnic(ctx, req)
if err != nil {
m.Logger.Error(err, "failed to reconcile the vcn, failed to update")
return errors.Wrap(err, "failed to reconcile the vcn, failed to update")
}
m.Logger.Info("successfully updated the vnicAttachment", "vcn", *resp.Id)
return nil
return resp.Id, nil
}

func (m *MachineScope) vnicAttachmentExists(ctx context.Context, vnicName infrastructurev1beta1.VnicAttachment) bool {
func (m *MachineScope) vnicAttachmentExists(ctx context.Context, vnic infrastructurev1beta1.VnicAttachment) bool {

found := false
var page *string
Expand All @@ -130,14 +96,16 @@ func (m *MachineScope) vnicAttachmentExists(ctx context.Context, vnicName infras
return false
}
for _, attachment := range resp.Items {
if attachment.DisplayName == vnicName.DisplayName {
if ociutil.DerefString(attachment.DisplayName) == ociutil.DerefString(vnic.DisplayName) {
m.Logger.Info("Vnic is already attached ", attachment)
return true
}
}

if page = resp.OpcNextPage; resp.OpcNextPage == nil {
if resp.OpcNextPage == nil {
break
} else {
page = resp.OpcNextPage
}
}
return found
Expand Down
Loading

0 comments on commit 7d7b874

Please sign in to comment.