Skip to content
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 #314

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions controllers/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const (
ScopePrincipal = "principal"
)

const (
OAMNetworkType = "oam"
)

const (
StrategyNotRequired = "not_required"
StrategyLockRequired = "lock_required"
Expand Down
115 changes: 115 additions & 0 deletions controllers/platformnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -84,6 +85,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.
Expand All @@ -108,6 +165,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
Expand Down Expand Up @@ -200,6 +263,45 @@ 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) 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.
if _, present := instance.Annotations[cloudManager.ReconcileAfterInSync]; !present {
msg := common.NoChangesAfterReconciled
r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated, msg)
return common.NewChangeAfterInSync(msg)
} else {
logPlatformNetwork.Info(common.ChangedAllowedAfterReconciled)
}
}

if instance.Status.DeploymentScope != cloudManager.ScopePrincipal {
r.ReconcilerEventLogger.WarningEvent(instance, common.ResourceDependency,
"unable to update OAM Network with deploymentScope = bootstrap")
return nil
}

// Update existing oam network
logPlatformNetwork.Info("updating oam network", "uuid", oam.UUID, "opts", opts)

result, err := oamNetworks.Update(client, oam.UUID, opts).Extract()
if err != nil {
err = perrors.Wrapf(err, "failed to update oam network: %+v", opts)
return err
}

*oam = *result

r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated,
"oam network has been updated")

}

return nil
}

func (r *PlatformNetworkReconciler) ReconcileUpdatedAddressPool(client *gophercloud.ServiceClient, instance *starlingxv1.PlatformNetwork, pool *addresspools.AddressPool) error {
if opts, ok := poolUpdateRequired(instance, pool, r); ok {
if instance.Status.Reconciled && r.StopAfterInSync() {
Expand Down Expand Up @@ -227,6 +329,7 @@ func (r *PlatformNetworkReconciler) ReconcileUpdatedAddressPool(client *gophercl

r.ReconcilerEventLogger.NormalEvent(instance, common.ResourceUpdated,
"address pool has been updated")

}

return nil
Expand Down Expand Up @@ -317,6 +420,18 @@ func (r *PlatformNetworkReconciler) ReconcileAddressPool(client *gophercloud.Ser
} else {
if pool == nil {
pool, err = r.ReconcileNewAddressPool(client, instance)
} else if instance.Spec.Type == cloudManager.OAMNetworkType {
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)
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/fsnotify/fsnotify v1.5.1
github.com/ghodss/yaml v1.0.0
github.com/go-logr/logr v1.2.0
github.com/google/go-cmp v0.5.9
github.com/gophercloud/gophercloud v0.0.0-20230605171524-742ad279b1e1
github.com/google/go-cmp v0.6.0
github.com/gophercloud/gophercloud v1.8.0
github.com/imdario/mergo v0.3.12
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.16.5
Expand Down Expand Up @@ -92,4 +92,4 @@ require (
)

// replace github.com/gophercloud/gophercloud => ./external/gophercloud
replace github.com/gophercloud/gophercloud => github.com/Wind-River/gophercloud v0.0.0-20231201194816-936d2409d43d
replace github.com/gophercloud/gophercloud => github.com/Wind-River/gophercloud v0.0.0-20231207152242-d06bd3ca4f52
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMo
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Wind-River/gophercloud v0.0.0-20231201194816-936d2409d43d h1:RR0Kb4wIXRqMGGO2KulSlkQynEKzz7hOlvpUDbxC3CY=
github.com/Wind-River/gophercloud v0.0.0-20231201194816-936d2409d43d/go.mod h1:KIxeLfVGmfRky0V1QAj7pJqsM3/UI7xLQEpDgXZWh4I=
github.com/Wind-River/gophercloud v0.0.0-20231207152242-d06bd3ca4f52 h1:2iH3Zf7+cIvhkDtHqenBaM44YHtmoaHwpItcPNtF790=
github.com/Wind-River/gophercloud v0.0.0-20231207152242-d06bd3ca4f52/go.mod h1:vVDhU9TrWNFfORJV7Tvx7UXsu/ImDlVeHhQCdrbzLEA=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -229,8 +229,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down