-
Notifications
You must be signed in to change notification settings - Fork 50
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
Update OAM Network using oam API #305
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/starlingx/inventory/v1/addresspools" | ||
"github.com/gophercloud/gophercloud/starlingx/inventory/v1/networks" | ||
"github.com/gophercloud/gophercloud/starlingx/inventory/v1/oamNetworks" | ||
perrors "github.com/pkg/errors" | ||
starlingxv1 "github.com/wind-river/cloud-platform-deployment-manager/api/v1" | ||
utils "github.com/wind-river/cloud-platform-deployment-manager/common" | ||
|
@@ -30,6 +31,8 @@ import ( | |
|
||
var logPlatformNetwork = log.Log.WithName("controller").WithName("platformnetwork") | ||
|
||
var SUBCLOUD_WRITABLE_NETWORK_TYPES = []string{"admin"} | ||
|
||
const PlatformNetworkControllerName = "platformnetwork-controller" | ||
|
||
const PlatformNetworkFinalizerName = "platformnetwork.finalizers.windriver.com" | ||
|
@@ -84,6 +87,62 @@ func compareRangeArrays(x, y [][]string) bool { | |
return len(x) == count | ||
} | ||
|
||
// oamUpdateRequired determines whether a oam network resource must | ||
// be updated to align with the stored value. Only the updated fields are | ||
// include in the request options to minimum churn and to ease debugging. | ||
func oamUpdateRequired(instance *starlingxv1.PlatformNetwork, p *oamNetworks.OAMNetwork, r *PlatformNetworkReconciler) (opts oamNetworks.OAMNetworkOpts, result bool) { | ||
var delta strings.Builder | ||
|
||
spec := instance.Spec | ||
instance_subnet := fmt.Sprintf("%s/%d", spec.Subnet, spec.Prefix) | ||
if instance_subnet != p.OAMSubnet { | ||
opts.OAMSubnet = &instance_subnet | ||
delta.WriteString(fmt.Sprintf("\t+Subnet: %s\n", *opts.OAMSubnet)) | ||
result = true | ||
} | ||
|
||
if instance.Spec.Type != networks.NetworkTypeOther { | ||
// TODO(alegacy): There is a sysinv bug in how the gateway address | ||
// gets registered in the database. It doesn't have a "name" and | ||
// so causes an exception when a related route is added. | ||
if spec.Gateway != nil && (p.OAMGatewayIP == nil || !strings.EqualFold(*spec.Gateway, *p.OAMGatewayIP)) { | ||
opts.OAMGatewayIP = spec.Gateway | ||
delta.WriteString(fmt.Sprintf("\t+Gateway: %s\n", *opts.OAMGatewayIP)) | ||
result = true | ||
} | ||
} | ||
|
||
if spec.FloatingAddress != "" && spec.FloatingAddress != p.OAMFloatingIP { | ||
opts.OAMFloatingIP = &spec.FloatingAddress | ||
delta.WriteString(fmt.Sprintf("\t+Floating Address: %s\n", *opts.OAMFloatingIP)) | ||
result = true | ||
} | ||
|
||
tempRange := [][]string{{p.OAMStartIP, p.OAMEndIP}} | ||
if len(spec.Allocation.Ranges) > 0 { | ||
ranges := makeRangeArray(spec.Allocation.Ranges) | ||
if !compareRangeArrays(ranges, tempRange) { | ||
opts.OAMStartIP = &ranges[0][0] | ||
opts.OAMEndIP = &ranges[0][1] | ||
delta.WriteString(fmt.Sprintf("\t+Start IP: %s\n", *opts.OAMStartIP)) | ||
delta.WriteString(fmt.Sprintf("\t+End IP: %s\n", *opts.OAMEndIP)) | ||
result = true | ||
} | ||
} | ||
deltaString := delta.String() | ||
if deltaString != "" { | ||
deltaString = "\n" + strings.TrimSuffix(deltaString, "\n") | ||
logPlatformNetwork.Info(fmt.Sprintf("delta configuration:%s\n", deltaString)) | ||
} | ||
|
||
instance.Status.Delta = deltaString | ||
err := r.Client.Status().Update(context.TODO(), instance) | ||
if err != nil { | ||
logPlatformNetwork.Info(fmt.Sprintf("failed to update oam status: %s\n", err)) | ||
} | ||
return opts, result | ||
} | ||
|
||
// poolUpdateRequired determines whether a system address pool resource must | ||
// be updated to align with the stored value. Only the updated fields are | ||
// include in the request options to minimum churn and to ease debugging. | ||
|
@@ -108,6 +167,12 @@ func poolUpdateRequired(instance *starlingxv1.PlatformNetwork, p *addresspools.A | |
result = true | ||
} | ||
|
||
if spec.FloatingAddress != "" && spec.FloatingAddress != p.FloatingAddress { | ||
opts.FloatingAddress = &spec.FloatingAddress | ||
delta.WriteString(fmt.Sprintf("\t+Floating Address: %s\n", *opts.FloatingAddress)) | ||
result = true | ||
} | ||
|
||
if instance.Spec.Type != networks.NetworkTypeOther { | ||
// TODO(alegacy): There is a sysinv bug in how the gateway address | ||
// gets registered in the database. It doesn't have a "name" and | ||
|
@@ -148,6 +213,26 @@ func poolUpdateRequired(instance *starlingxv1.PlatformNetwork, p *addresspools.A | |
return opts, result | ||
} | ||
|
||
func poolOptsCreation(instance *starlingxv1.PlatformNetwork) (opts addresspools.AddressPoolOpts) { | ||
opts.Name = &instance.Name | ||
|
||
spec := instance.Spec | ||
opts.Network = &spec.Subnet | ||
opts.Prefix = &spec.Prefix | ||
opts.FloatingAddress = &spec.FloatingAddress | ||
opts.Gateway = spec.Gateway | ||
if spec.Allocation.Order != nil { | ||
opts.Order = spec.Allocation.Order | ||
} | ||
|
||
if len(spec.Allocation.Ranges) > 0 { | ||
ranges := makeRangeArray(spec.Allocation.Ranges) | ||
opts.Ranges = &ranges | ||
} | ||
|
||
return opts | ||
} | ||
|
||
// ReconcileNew is a method which handles reconciling a new data resource and | ||
// creates the corresponding system resource thru the system API. | ||
func (r *PlatformNetworkReconciler) ReconcileNewAddressPool(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork) (*addresspools.AddressPool, error) { | ||
|
@@ -200,8 +285,8 @@ func (r *PlatformNetworkReconciler) ReconcileNewAddressPool(client *gophercloud. | |
// ReconcileUpdated is a method which handles reconciling an existing data | ||
// resource and updates the corresponding system resource thru the system API to | ||
// match the desired state of the resource. | ||
func (r *PlatformNetworkReconciler) ReconcileUpdatedAddressPool(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork, pool *addresspools.AddressPool) error { | ||
if opts, ok := poolUpdateRequired(instance, pool, r); ok { | ||
func (r *PlatformNetworkReconciler) ReconcileUpdatedOAMNetwork(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork, oam *oamNetworks.OAMNetwork) error { | ||
if opts, ok := oamUpdateRequired(instance, oam, r); ok { | ||
if instance.Status.Reconciled && r.StopAfterInSync() { | ||
// Do not process any further changes once we have reached a | ||
// synchronized state unless there is an annotation on the resource. | ||
|
@@ -214,24 +299,88 @@ func (r *PlatformNetworkReconciler) ReconcileUpdatedAddressPool(client *gophercl | |
} | ||
} | ||
|
||
// Update existing pool | ||
logPlatformNetwork.Info("updating address pool", "uuid", pool.ID, "opts", opts) | ||
// Update existing oam network | ||
logPlatformNetwork.Info("updating oam network", "uuid", oam.UUID, "opts", opts) | ||
|
||
result, err := addresspools.Update(client, pool.ID, opts).Extract() | ||
result, err := oamNetworks.Update(client, oam.UUID, opts).Extract() | ||
if err != nil { | ||
err = perrors.Wrapf(err, "failed to update pool: %+v", opts) | ||
err = perrors.Wrapf(err, "failed to update oam network: %+v", opts) | ||
return err | ||
} | ||
|
||
*pool = *result | ||
*oam = *result | ||
|
||
r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated, | ||
"address pool has been updated") | ||
"oam network has been updated") | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *PlatformNetworkReconciler) ReconcileUpdatedAddressPool(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork, pool *addresspools.AddressPool) (*addresspools.AddressPool, error) { | ||
if opts, ok := poolUpdateRequired(instance, pool, r); ok { | ||
if instance.Status.Reconciled && r.StopAfterInSync() { | ||
// Do not process any further changes once we have reached a | ||
// synchronized state unless there is an annotation on the resource. | ||
if _, present := instance.Annotations[cloudManager.ReconcileAfterInSync]; !present { | ||
msg := common.NoChangesAfterReconciled | ||
r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated, msg) | ||
return pool, common.NewChangeAfterInSync(msg) | ||
} else { | ||
logPlatformNetwork.Info(common.ChangedAllowedAfterReconciled) | ||
} | ||
} | ||
|
||
createAddressPool := false | ||
for _, t := range SUBCLOUD_WRITABLE_NETWORK_TYPES { | ||
if t == instance.Spec.Type { | ||
createAddressPool = true | ||
break | ||
} | ||
} | ||
|
||
if createAddressPool { | ||
newOpts := poolOptsCreation(instance) | ||
|
||
// Update existing pool | ||
logPlatformNetwork.Info("Deleting address pool", "uuid", pool.ID, "opts", opts) | ||
err := addresspools.Delete(client, pool.ID).ExtractErr() | ||
|
||
if err != nil { | ||
err = perrors.Wrapf(err, "failed to delete pool: %+v", opts) | ||
return pool, err | ||
} | ||
|
||
logPlatformNetwork.Info("Creating address pool", "opts", opts) | ||
|
||
result, err := addresspools.Create(client, newOpts).Extract() | ||
if err != nil { | ||
err = perrors.Wrapf(err, "failed to create pool: %+v", opts) | ||
return pool, err | ||
} | ||
*pool = *result | ||
r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated, | ||
"address pool has been created") | ||
} else { | ||
// Update existing pool | ||
logPlatformNetwork.Info("updating address pool", "uuid", pool.ID, "opts", opts) | ||
|
||
result, err := addresspools.Update(client, pool.ID, opts).Extract() | ||
if err != nil { | ||
err = perrors.Wrapf(err, "failed to update pool: %+v", opts) | ||
return pool, err | ||
} | ||
*pool = *result | ||
r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated, | ||
"address pool has been updated") | ||
} | ||
|
||
} | ||
|
||
return pool, nil | ||
} | ||
|
||
// ReconcileNew is a method which handles reconciling a new data resource and | ||
// creates the corresponding system resource thru the system API. | ||
func (r *PlatformNetworkReconciler) ReconciledDeletedAddressPool(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork, pool *addresspools.AddressPool) error { | ||
|
@@ -276,7 +425,7 @@ func (r *PlatformNetworkReconciler) FindExistingAddressPool(client *gophercloud. | |
|
||
// The resource may have been deleted by the system or operator | ||
// therefore continue and attempt to recreate it. | ||
logPlatformNetwork.Info("resource no longer exists", "id", *id) | ||
logPlatformNetwork.Info("address pool no longer exists", "id", *id) | ||
return nil, nil | ||
} | ||
|
||
|
@@ -317,8 +466,20 @@ func (r *PlatformNetworkReconciler) ReconcileAddressPool(client *gophercloud.Ser | |
} else { | ||
if pool == nil { | ||
pool, err = r.ReconcileNewAddressPool(client, instance) | ||
} else if instance.Spec.Type == "oam" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I prefer to use Constant for "oam" but not mandatory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll handle this in a follow-up commit when we create and manage the other networks (admin, mgmt). Is that okay? |
||
oamNetworkList, err := oamNetworks.ListNetworks(client) | ||
if err != nil { | ||
err = perrors.Wrapf(err, "failed to get oam network") | ||
return err | ||
} | ||
oamNetwork := &oamNetworkList[0] | ||
err = r.ReconcileUpdatedOAMNetwork(client, instance, oamNetwork) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err = r.ReconcileUpdatedAddressPool(client, instance, pool) | ||
pool, err = r.ReconcileUpdatedAddressPool(client, instance, pool) | ||
} | ||
|
||
if err == nil && pool != nil { | ||
|
@@ -484,7 +645,7 @@ func (r *PlatformNetworkReconciler) FindExistingNetwork(client *gophercloud.Serv | |
|
||
// The resource may have been deleted by the system or operator | ||
// therefore continue and attempt to recreate it. | ||
logPlatformNetwork.Info("resource no longer exists", "id", *id) | ||
logPlatformNetwork.Info("network no longer exists", "id", *id) | ||
return nil, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description need to be allocated at the right function, expecting description to the new function as well