Skip to content

Commit

Permalink
Added OpenStack clients for Octavia
Browse files Browse the repository at this point in the history
  • Loading branch information
gthiemonge committed Oct 27, 2023
1 parent e3ff0e9 commit 9db0a8f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ go 1.19

require (
github.com/go-logr/logr v1.2.4
github.com/gophercloud/gophercloud v1.7.0
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/onsi/ginkgo/v2 v2.12.1
github.com/onsi/gomega v1.28.0
github.com/openstack-k8s-operators/infra-operator/apis v0.3.0
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20231005132119-e75019792469
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231006072650-7fe7fe16bcd1
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20231006072650-7fe7fe16bcd1
github.com/openstack-k8s-operators/mariadb-operator/api v0.3.0
github.com/openstack-k8s-operators/octavia-operator/api v0.0.0-00010101000000-000000000000
github.com/openstack-k8s-operators/ovn-operator/api v0.3.1-0.20231005121428-58b0e24477c0
Expand Down Expand Up @@ -40,7 +42,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gophercloud/gophercloud v1.7.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -50,7 +51,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20231006072650-7fe7fe16bcd1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.15.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
Expand Down
103 changes: 103 additions & 0 deletions pkg/octavia/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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 octavia

import (
"context"
"fmt"
"time"

"github.com/gophercloud/gophercloud"
gophercloudopenstack "github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common/endpoint"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"github.com/openstack-k8s-operators/lib-common/modules/openstack"
ctrl "sigs.k8s.io/controller-runtime"
)

// GetAdminServiceClient -
func GetAdminServiceClient(
ctx context.Context,
h *helper.Helper,
keystoneAPI *keystonev1.KeystoneAPI,
) (*openstack.OpenStack, ctrl.Result, error) {
// get public endpoint as authurl from keystone instance
authURL, err := keystoneAPI.GetEndpoint(endpoint.EndpointPublic)
if err != nil {
return nil, ctrl.Result{}, err
}

// get the password of the admin user from Spec.Secret
// using PasswordSelectors.Admin
authPassword, ctrlResult, err := secret.GetDataFromSecret(
ctx,
h,
keystoneAPI.Spec.Secret,
time.Duration(10)*time.Second,
keystoneAPI.Spec.PasswordSelectors.Admin)
if err != nil {
return nil, ctrl.Result{}, err
}
if (ctrlResult != ctrl.Result{}) {
return nil, ctrlResult, nil
}

os, err := openstack.NewOpenStack(
h.GetLogger(),
openstack.AuthOpts{
AuthURL: authURL,
Username: keystoneAPI.Spec.AdminUser,
Password: authPassword,
TenantName: keystoneAPI.Spec.AdminProject,
DomainName: "Default",
Region: keystoneAPI.Spec.Region,
Scope: &gophercloud.AuthScope{
System: true,
},
})
if err != nil {
return nil, ctrl.Result{}, err
}

return os, ctrl.Result{}, nil
}

// GetProject -
func GetProject(openstack *openstack.OpenStack, projectName string) (*projects.Project, error) {
allPages, err := projects.List(openstack.GetOSClient(), projects.ListOpts{Name: projectName}).AllPages()
if err != nil {
return nil, err
}
allProjects, err := projects.ExtractProjects(allPages)
if err != nil {
return nil, err
}
if len(allProjects) == 0 {
return nil, fmt.Errorf("Cannot find project \"%s\"", projectName)
}
return &allProjects[0], nil
}

// GetNetworkClient -
func GetNetworkClient(o *openstack.OpenStack) (*gophercloud.ServiceClient, error) {
endpointOpts := gophercloud.EndpointOpts{
Region: o.GetRegion(),
Availability: gophercloud.AvailabilityInternal,
}
return gophercloudopenstack.NewNetworkV2(o.GetOSClient().ProviderClient, endpointOpts)
}

0 comments on commit 9db0a8f

Please sign in to comment.