Skip to content

Commit

Permalink
Add description to resource capi created (#870)
Browse files Browse the repository at this point in the history
* Add description to resource capi created
  • Loading branch information
jichenjc authored May 25, 2021
1 parent e08cf62 commit 55ec7fa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
12 changes: 7 additions & 5 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
)

const (
Expand Down Expand Up @@ -161,7 +162,7 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
}

if i.Trunk {
trunk, err := s.getOrCreateTrunk(eventObject, i.Name, port.ID)
trunk, err := s.getOrCreateTrunk(eventObject, clusterName, i.Name, port.ID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -395,7 +396,7 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
Name: portName,
NetworkID: net.ID,
SecurityGroups: securityGroups,
Description: fmt.Sprintf("Created by cluster-api-provider-openstack cluster %s", clusterName),
Description: names.GetDescription(clusterName),
}
if net.Subnet.ID != "" {
portCreateOpts.FixedIPs = []ports.IP{{SubnetID: net.Subnet.ID}}
Expand All @@ -410,7 +411,7 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
return port, nil
}

func (s *Service) getOrCreateTrunk(eventObject runtime.Object, trunkName, portID string) (*trunks.Trunk, error) {
func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trunkName, portID string) (*trunks.Trunk, error) {
allPages, err := trunks.List(s.networkClient, trunks.ListOpts{
Name: trunkName,
PortID: portID,
Expand All @@ -428,8 +429,9 @@ func (s *Service) getOrCreateTrunk(eventObject runtime.Object, trunkName, portID
}

trunkCreateOpts := trunks.CreateOpts{
Name: trunkName,
PortID: portID,
Name: trunkName,
PortID: portID,
Description: names.GetDescription(clusterName),
}
trunk, err := trunks.Create(s.networkClient, trunkCreateOpts).Extract()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloud/services/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
)

const (
Expand All @@ -52,6 +53,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust
lbCreateOpts := loadbalancers.CreateOpts{
Name: loadBalancerName,
VipSubnetID: openStackCluster.Status.Network.Subnet.ID,
Description: names.GetDescription(clusterName),
}

lb, err = loadbalancers.Create(s.loadbalancerClient, lbCreateOpts).Extract()
Expand Down
6 changes: 4 additions & 2 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
)

type createOpts struct {
Expand Down Expand Up @@ -194,7 +195,7 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl
var subnet *subnets.Subnet
if len(subnetList) == 0 {
var err error
subnet, err = s.createSubnet(openStackCluster, subnetName)
subnet, err = s.createSubnet(openStackCluster, clusterName, subnetName)
if err != nil {
return err
}
Expand All @@ -212,13 +213,14 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl
return nil
}

func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, name string) (*subnets.Subnet, error) {
func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clusterName string, name string) (*subnets.Subnet, error) {
opts := subnets.CreateOpts{
NetworkID: openStackCluster.Status.Network.ID,
Name: name,
IPVersion: 4,
CIDR: openStackCluster.Spec.NodeCIDR,
DNSNameservers: openStackCluster.Spec.DNSNameservers,
Description: names.GetDescription(clusterName),
}
subnet, err := subnets.Create(s.client, opts).Extract()
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloud/services/networking/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
)

func (s *Service) ReconcileRouter(openStackCluster *infrav1.OpenStackCluster, clusterName string) error {
Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *Service) ReconcileRouter(openStackCluster *infrav1.OpenStackCluster, cl
var router *routers.Router
if len(routerList) == 0 {
var err error
router, err = s.createRouter(openStackCluster, routerName)
router, err = s.createRouter(openStackCluster, clusterName, routerName)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,9 +118,10 @@ INTERFACE_LOOP:
return nil
}

func (s *Service) createRouter(openStackCluster *infrav1.OpenStackCluster, name string) (*routers.Router, error) {
func (s *Service) createRouter(openStackCluster *infrav1.OpenStackCluster, clusterName, name string) (*routers.Router, error) {
opts := routers.CreateOpts{
Name: name,
Description: names.GetDescription(clusterName),
Name: name,
}
// only set the GatewayInfo right now when no externalIPs
// should be configured because at least in our environment
Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/names/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package names

import (
"fmt"
)

func GetDescription(clusterName string) string {
return fmt.Sprintf("Created by cluster-api-provider-openstack cluster %s", clusterName)
}

0 comments on commit 55ec7fa

Please sign in to comment.