Skip to content

Commit

Permalink
Merge pull request #715 from kon-angelo/flow-shared-network
Browse files Browse the repository at this point in the history
Add support for shared network in the flow reconciler
  • Loading branch information
MartinWeindel authored Feb 14, 2024
2 parents 8caf21c + f7c3faf commit c590c4b
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/gardener/machine-controller-manager v0.50.0
github.com/go-logr/logr v1.2.4
github.com/google/uuid v1.3.0
github.com/gophercloud/gophercloud v1.1.1
github.com/gophercloud/gophercloud v1.8.0
github.com/gophercloud/utils v0.0.0-20221207145018-e8fba78967ca
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.29.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ github.com/googleapis/gnostic v0.1.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTV
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gophercloud/gophercloud v1.1.1 h1:MuGyqbSxiuVBqkPZ3+Nhbytk1xZxhmfCB2Rg1cJWFWM=
github.com/gophercloud/gophercloud v1.1.1/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
github.com/gophercloud/gophercloud v1.8.0 h1:TM3Jawprb2NrdOnvcHhWJalmKmAmOGgfZElM/3oBYCk=
github.com/gophercloud/gophercloud v1.8.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
github.com/gophercloud/utils v0.0.0-20221207145018-e8fba78967ca h1:HZWyhvVXgS2rx5StixMKhrTjpfUg5vLSNhF/xHkcRNg=
github.com/gophercloud/utils v0.0.0-20221207145018-e8fba78967ca/go.mod h1:z4Dey7xsTUXgcB1C8elMvGRKTjV1ez0eoYQlMrduG1g=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/infrastructure/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func computeProviderStatusFromFlowState(state *infraflow.PersistentState) (*open
status.Networks.Router.IP = shared.ValidValue(state.Data[infraflow.RouterIP])
status.Networks.FloatingPool.ID = shared.ValidValue(state.Data[infraflow.IdentifierFloatingNetwork])
status.Networks.FloatingPool.Name = shared.ValidValue(state.Data[infraflow.NameFloatingNetwork])
if v := shared.ValidValue(state.Data[infraflow.IdentifierShareNetwork]); v != "" {
status.Networks.ShareNetwork = &openstackv1alpha1.ShareNetworkStatus{
ID: v,
Name: shared.ValidValue(state.Data[infraflow.NameShareNetwork]),
}
}

subnetID := shared.ValidValue(state.Data[infraflow.IdentifierSubnet])
if subnetID != "" {
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/infrastructure/infraflow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
IdentifierFloatingNetwork = "FloatingNetwork"
// IdentifierSecGroup is the key for the security group id
IdentifierSecGroup = "SecurityGroup"
// IdentifierShareNetwork is the key for the share network id
IdentifierShareNetwork = "ShareNetwork"

// NameFloatingNetwork is the key for the floating network name
NameFloatingNetwork = "FloatingNetworkName"
Expand All @@ -49,6 +51,8 @@ const (
NameKeyPair = "KeyPair"
// NameSecGroup is the name of the security group
NameSecGroup = "SecurityGroupName"
// NameShareNetwork is the name of the shared network
NameShareNetwork = "ShareNetworkName"

// RouterIP is the key for the router IP address
RouterIP = "RouterIP"
Expand All @@ -72,6 +76,7 @@ type FlowContext struct {
cloudProfileConfig *openstackapi.CloudProfileConfig
networking osclient.Networking
loadbalancing osclient.Loadbalancing
sharedFilesystem osclient.SharedFilesystem
access access.NetworkingAccess
compute osclient.Compute
}
Expand Down Expand Up @@ -103,6 +108,10 @@ func NewFlowContext(log logr.Logger, clientFactory osclient.Factory,
if err != nil {
return nil, err
}
sharedFilesytem, err := clientFactory.SharedFilesystem(osclient.WithRegion(infra.Spec.Region))
if err != nil {
return nil, err
}

flowContext := &FlowContext{
BasicFlowContext: *shared.NewBasicFlowContext(log, whiteboard, persistor),
Expand All @@ -115,6 +124,7 @@ func NewFlowContext(log logr.Logger, clientFactory osclient.Factory,
loadbalancing: loadbalancing,
access: access,
compute: compute,
sharedFilesystem: sharedFilesytem,
}
return flowContext, nil
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/controller/infrastructure/infraflow/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"

"github.com/gardener/gardener/pkg/utils/flow"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharenetworks"
"k8s.io/utils/pointer"

. "github.com/gardener/gardener-extension-provider-openstack/pkg/controller/infrastructure/infraflow/shared"
"github.com/gardener/gardener-extension-provider-openstack/pkg/internal/infrastructure"
Expand Down Expand Up @@ -76,6 +78,9 @@ func (c *FlowContext) buildDeleteGraph() *flow.Graph {
Timeout(defaultTimeout),
)

_ = c.AddTask(g, "delete share network",
c.deleteShareNetwork,
Timeout(defaultTimeout), Dependencies(recoverSubnetID))
deleteRouterInterface := c.AddTask(g, "delete router interface",
c.deleteRouterInterface,
Timeout(defaultTimeout), Dependencies(recoverRouterID, recoverSubnetID, k8sRoutes))
Expand Down Expand Up @@ -231,3 +236,35 @@ func (c *FlowContext) deleteSSHKeyPair(ctx context.Context) error {
}
return nil
}

func (c *FlowContext) deleteShareNetwork(ctx context.Context) error {
log := c.LogFromContext(ctx)
networkID := pointer.StringDeref(c.state.Get(IdentifierNetwork), "")
subnetID := pointer.StringDeref(c.state.Get(IdentifierSubnet), "")
current, err := findExisting(c.state.Get(IdentifierShareNetwork),
c.namespace,
c.sharedFilesystem.GetShareNetwork,
func(name string) ([]*sharenetworks.ShareNetwork, error) {
list, err := c.sharedFilesystem.ListShareNetworks(sharenetworks.ListOpts{
AllTenants: false,
NeutronNetID: networkID,
NeutronSubnetID: subnetID,
})
if err != nil {
return nil, err
}
return sliceToPtr(list), nil
})
if err != nil {
return err
}
if current != nil {
log.Info("deleting...", "shareNetwork", current.ID)
if err := c.sharedFilesystem.DeleteShareNetwork(current.ID); err != nil {
return err
}
}
c.state.Set(IdentifierShareNetwork, "")
c.state.Set(NameShareNetwork, "")
return nil
}
54 changes: 54 additions & 0 deletions pkg/controller/infrastructure/infraflow/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharenetworks"
"k8s.io/utils/pointer"

"github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack/helper"
"github.com/gardener/gardener-extension-provider-openstack/pkg/controller/infrastructure/infraflow/access"
Expand Down Expand Up @@ -79,6 +81,11 @@ func (c *FlowContext) buildReconcileGraph() *flow.Graph {
c.ensureSSHKeyPair,
Timeout(defaultTimeout), Dependencies(ensureRouter))

_ = c.AddTask(g, "ensure share network",
c.ensureShareNetwork,
Timeout(defaultTimeout), Dependencies(ensureSubnet),
)

return g
}

Expand Down Expand Up @@ -455,3 +462,50 @@ func (c *FlowContext) ensureSSHKeyPair(ctx context.Context) error {
c.state.Set(NameKeyPair, keyPair.Name)
return nil
}

func (c *FlowContext) ensureShareNetwork(ctx context.Context) error {
if c.config.Networks.ShareNetwork == nil || !c.config.Networks.ShareNetwork.Enabled {
return c.deleteShareNetwork(ctx)
}

log := c.LogFromContext(ctx)
networkID := pointer.StringDeref(c.state.Get(IdentifierNetwork), "")
subnetID := pointer.StringDeref(c.state.Get(IdentifierSubnet), "")
current, err := findExisting(c.state.Get(IdentifierShareNetwork),
c.namespace,
c.sharedFilesystem.GetShareNetwork,
func(name string) ([]*sharenetworks.ShareNetwork, error) {
list, err := c.sharedFilesystem.ListShareNetworks(sharenetworks.ListOpts{
Name: name,
NeutronNetID: networkID,
NeutronSubnetID: subnetID,
})
if err != nil {
return nil, err
}
return sliceToPtr(list), nil
})

if err != nil {
return err
}

if current != nil {
c.state.Set(IdentifierShareNetwork, current.ID)
c.state.Set(NameShareNetwork, current.Name)
return nil
}

log.Info("creating...")
created, err := c.sharedFilesystem.CreateShareNetwork(sharenetworks.CreateOpts{
NeutronNetID: networkID,
NeutronSubnetID: subnetID,
Name: c.namespace,
})
if err != nil {
return err
}
c.state.Set(IdentifierShareNetwork, created.ID)
c.state.Set(NameShareNetwork, created.Name)
return nil
}
8 changes: 8 additions & 0 deletions pkg/controller/infrastructure/infraflow/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ func copyMap(src map[string]string) map[string]string {
}
return dst
}

func sliceToPtr[T any](slice []T) []*T {
res := make([]*T, len(slice))
for _, t := range slice {
res = append(res, &t)
}
return res
}
16 changes: 16 additions & 0 deletions pkg/openstack/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,23 @@ func (oc *OpenstackClientFactory) Loadbalancing(options ...Option) (Loadbalancin
return &LoadbalancingClient{
client: client,
}, nil
}

// SharedFilesystem creates a new Manila client.
func (oc *OpenstackClientFactory) SharedFilesystem(options ...Option) (SharedFilesystem, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}

client, err := openstack.NewSharedFileSystemV2(oc.providerClient, eo)
if err != nil {
return nil, err
}

return &SharedFilesystemClient{
client: client,
}, nil
}

// IsNotFoundError checks if an error returned by OpenStack is caused by HTTP 404 status code.
Expand Down
104 changes: 103 additions & 1 deletion pkg/openstack/client/mocks/client_mocks.go

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

Loading

0 comments on commit c590c4b

Please sign in to comment.