Skip to content

Commit

Permalink
Reconciliation: Update
Browse files Browse the repository at this point in the history
Signed-off-by: Karl Dasan <[email protected]>
  • Loading branch information
ikarldasan committed Jun 5, 2021
1 parent 48c5b73 commit 97d38c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
7 changes: 2 additions & 5 deletions examples/network/ddosprotectionplan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ kind: DdosProtectionPlan
metadata:
name: example-ddos-protection-plan
spec:
resourceGroupNameRef:
name: example-rg
ddosProtectionPlanName: test-ddos-protection-plan-name
resourceGroupName: example-rg
location: eastus
tags:
a: b
properties:
providerConfigRef:
name: example
name: default
23 changes: 17 additions & 6 deletions pkg/clients/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ func NewDdosProtectionPlanParameters(d *v1alpha3.DdosProtectionPlan) networkmgmt
}
}

// DdosProtectionPlanNeedsUpdate determines if a ddos protection plan needs to be updated
func DdosProtectionPlanNeedsUpdate(kube *v1alpha3.DdosProtectionPlan, az networkmgmt.DdosProtectionPlan) bool {
// IsDdosProtectionPlanUpToDate determines if a ddos protection plan is up to date or not.
func IsDdosProtectionPlanUpToDate(kube *v1alpha3.DdosProtectionPlan, az networkmgmt.DdosProtectionPlan) bool {
up := NewDdosProtectionPlanParameters(kube)
switch {
case !reflect.DeepEqual(up.Name, az.Name):
return true
return false
case !reflect.DeepEqual(up.Location, az.Location):
return true
return false
case !reflect.DeepEqual(up.Tags, az.Tags):
return true
return false
}
return false
return true
}

// UpdateDdosProtectionPlanStatusFromAzure updates the status related to the external
Expand All @@ -144,3 +144,14 @@ func UpdateDdosProtectionPlanStatusFromAzure(d *v1alpha3.DdosProtectionPlan, az
d.Status.State = azure.ToString(az.DdosProtectionPlanPropertiesFormat.ProvisioningState)
d.Status.Location = azure.ToString(az.Location)
}

// LateInitializeDdos updates the Tags from the external
// Azure DdosProtectionPlan in the DdosProtectionPlan
func LateInitializeDdos(d *v1alpha3.DdosProtectionPlan, az networkmgmt.DdosProtectionPlan) {
if len(d.Spec.Tags) == 0 && len(az.Tags) != 0 {
d.Spec.Tags = make(map[string]string)
for k, v := range az.Tags {
d.Spec.Tags[k] = *v
}
}
}
28 changes: 16 additions & 12 deletions pkg/controller/network/ddosprotectionplan/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

azurenetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network/networkapi"
"github.com/google/go-cmp/cmp"

"github.com/pkg/errors"

Expand Down Expand Up @@ -95,23 +96,28 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotDdosProtectionPlan)
}

// az, err := e.client.Get(ctx, d.Spec.ResourceGroupName, meta.GetExternalName(d))
az, err := e.client.Get(ctx, d.Spec.ResourceGroupName, meta.GetExternalName(d))
if azureclients.IsNotFound(err) {
fmt.Println("=====================OBSERVE END TWO=====================")
return managed.ExternalObservation{ResourceExists: false}, nil
}
if err != nil {
fmt.Println("=====================OBSERVE END THREE=====================")
return managed.ExternalObservation{}, errors.Wrap(err, errGetDdosProtectionPlan)
return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(azureclients.IsNotFound, err), errGetDdosProtectionPlan)
}

currentDdpp := d.DeepCopy()
network.LateInitializeDdos(currentDdpp, az)
if !cmp.Equal(currentDdpp.Spec, d.Spec) {
if _, err := e.Update(ctx, mg); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errUpdateDdosProtectionPlan)
}
}

network.UpdateDdosProtectionPlanStatusFromAzure(d, az)
d.SetConditions(xpv1.Available())

o := managed.ExternalObservation{
ResourceExists: true,
ConnectionDetails: managed.ConnectionDetails{},
ResourceExists: true,
ResourceUpToDate: network.IsDdosProtectionPlanUpToDate(d, az),
ConnectionDetails: managed.ConnectionDetails{
"ddpp": []byte(meta.GetExternalName(d)),
},
}

fmt.Println("=====================OBSERVE END LAST=====================")
Expand All @@ -129,8 +135,6 @@ func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

ddos := network.NewDdosProtectionPlanParameters(d)

// if _, err := e.client.CreateOrUpdate(ctx, d.Spec.ResourceGroupName, *ddos.Name, ddos); err != nil {
// if _, err := e.client.CreateOrUpdate(ctx, d.Spec.ResourceGroupNameRef.Name, meta.GetExternalName(d), ddos); err != nil {
if _, err := e.client.CreateOrUpdate(ctx, d.Spec.ResourceGroupName, meta.GetExternalName(d), ddos); err != nil {
fmt.Println("***********************CREATE END ONE***********************")
return managed.ExternalCreation{}, errors.Wrap(err, errCreateDdosProtectionPlan)
Expand All @@ -153,7 +157,7 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, errors.Wrap(err, errGetDdosProtectionPlan)
}

if network.DdosProtectionPlanNeedsUpdate(d, az) {
if !network.IsDdosProtectionPlanUpToDate(d, az) {
ddos := network.NewDdosProtectionPlanParameters(d)
if _, err := e.client.CreateOrUpdate(ctx, d.Spec.ResourceGroupName, meta.GetExternalName(d), ddos); err != nil {
fmt.Println("^^^^^^^^^^^^^^^^^^^^UPDATE END THREE^^^^^^^^^^^^^^^^^^^^")
Expand Down

0 comments on commit 97d38c9

Please sign in to comment.