Skip to content

Commit

Permalink
remove gophercloud/utils dependency
Browse files Browse the repository at this point in the history
The main features of gophercloud/utils are support for reading the
entire range of OS_* environment variables, and reading clouds.yaml
configuration files. You need neither here.

While I was at it, I refactored the two occurences of Keystone
authentication into a method.
  • Loading branch information
majewsky committed Nov 11, 2024
1 parent c90f8ef commit 38f2995
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
28 changes: 28 additions & 0 deletions common/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
package common

import (
"context"
"fmt"

"gopkg.in/ini.v1"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/sapcc/maintenance-controller/constants"
)

Expand Down Expand Up @@ -60,3 +63,28 @@ func LoadOpenStackConfig() (OpenStackConfig, error) {
ProjectID: osConf.Global.TenantID,
}, nil
}

func (osConf OpenStackConfig) Connect(ctx context.Context) (*gophercloud.ProviderClient, gophercloud.EndpointOpts, error) {
ao := gophercloud.AuthOptions{
IdentityEndpoint: osConf.AuthURL,
Username: osConf.Username,
Password: osConf.Password,
DomainName: osConf.Domainname, // domain name of user, not of project
AllowReauth: true,
Scope: &gophercloud.AuthScope{
ProjectID: osConf.ProjectID,
},
}
provider, err := openstack.NewClient(ao.IdentityEndpoint)
if err != nil {
return nil, gophercloud.EndpointOpts{}, err
}
err = openstack.Authenticate(ctx, provider, ao)
if err != nil {
return nil, gophercloud.EndpointOpts{}, err
}
eo := gophercloud.EndpointOpts{
Region: osConf.Region,
}
return provider, eo, nil
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/elastic/go-ucfg v0.8.8
github.com/go-logr/logr v1.4.2
github.com/gophercloud/gophercloud/v2 v2.2.0
github.com/gophercloud/utils/v2 v2.0.0-20241107125844-da754bc75cd6
github.com/onsi/ginkgo/v2 v2.21.0
github.com/onsi/gomega v1.35.1
github.com/prometheus/client_golang v1.20.5
Expand Down Expand Up @@ -41,7 +40,6 @@ require (
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gofrs/uuid/v5 v5.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand All @@ -56,7 +54,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand All @@ -63,8 +61,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gophercloud/gophercloud/v2 v2.2.0 h1:STqqnSXuhcg1OPBOZ14z6JDm8fKIN13H2bJg6bBuHp8=
github.com/gophercloud/gophercloud/v2 v2.2.0/go.mod h1:f2hMRC7Kakbv5vM7wSGHrIPZh6JZR60GVHryJlF/K44=
github.com/gophercloud/utils/v2 v2.0.0-20241107125844-da754bc75cd6 h1:Vikb1i71pW5yb/ayp+Cw6Z3Wc+3NtT+ui/t4LyJl7dY=
github.com/gophercloud/utils/v2 v2.0.0-20241107125844-da754bc75cd6/go.mod h1:9KHhEdDkA/4hTdwxS0sALJIp2hFSjrODlKMQcFU2GFw=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand All @@ -91,8 +87,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
17 changes: 2 additions & 15 deletions kubernikus/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import (
semver "github.com/blang/semver/v4"
"github.com/elastic/go-ucfg"
"github.com/go-logr/logr"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/utils/v2/openstack/clientconfig"
"github.com/sapcc/ucfgwrap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -205,22 +203,11 @@ func deleteVM(ctx context.Context, nodeName string) error {
if err != nil {
return fmt.Errorf("failed to parse cloudprovider.conf: %w", err)
}
opts := &clientconfig.ClientOpts{
AuthInfo: &clientconfig.AuthInfo{
AuthURL: osConf.AuthURL,
Username: osConf.Username,
Password: osConf.Password,
UserDomainName: osConf.Domainname,
ProjectID: osConf.ProjectID,
},
}
provider, err := clientconfig.AuthenticatedClient(ctx, opts)
provider, endpointOpts, err := osConf.Connect(ctx)
if err != nil {
return fmt.Errorf("failed OpenStack authentification: %w", err)
}
compute, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: osConf.Region,
})
compute, err := openstack.NewComputeV2(provider, endpointOpts)
if err != nil {
return fmt.Errorf("failed to create OS compute endpoint: %w", err)
}
Expand Down
12 changes: 1 addition & 11 deletions plugin/impl/kubernikus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"strings"

"github.com/gophercloud/utils/v2/openstack/clientconfig"
"github.com/sapcc/ucfgwrap"
corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -87,16 +86,7 @@ func (kc *KubernikusCount) fetchKluster(params *plugin.Parameters) (kluster, err
if err != nil {
return kluster{}, err
}
opts := &clientconfig.ClientOpts{
AuthInfo: &clientconfig.AuthInfo{
AuthURL: osConf.AuthURL,
Username: osConf.Username,
Password: osConf.Password,
UserDomainName: osConf.Domainname,
ProjectID: osConf.ProjectID,
},
}
provider, err := clientconfig.AuthenticatedClient(params.Ctx, opts)
provider, _, err := osConf.Connect(params.Ctx)
if err != nil {
return kluster{}, fmt.Errorf("failed OpenStack authentification: %w", err)
}
Expand Down

0 comments on commit 38f2995

Please sign in to comment.