Skip to content

Commit

Permalink
Merge pull request #1355 from dtantsur/ironic-inventory
Browse files Browse the repository at this point in the history
⚠️ Use ironic inventory API instead of calling ironic-inspector
  • Loading branch information
metal3-io-bot committed Sep 28, 2023
2 parents 3f5884b + d8b09e7 commit faab936
Show file tree
Hide file tree
Showing 29 changed files with 170 additions and 512 deletions.
35 changes: 27 additions & 8 deletions cmd/get-hardware-details/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package main
import (
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"strings"

"github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection"
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes"
"k8s.io/klog/v2"

"github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/clients"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/hardwaredetails"
Expand All @@ -34,22 +37,38 @@ func main() {
InsecureSkipVerify: ironicInsecure,
}

inspector, err := clients.InspectorClient(opts.Endpoint, opts.AuthConfig, tlsConf)
endpoint := opts.Endpoint
parsedEndpoint, err := url.Parse(endpoint)
if err != nil {
fmt.Printf("could not get inspector client: %s", err)
fmt.Printf("invalid ironic endpoint: %s", err)
os.Exit(1)
}

introData := introspection.GetIntrospectionData(inspector, opts.NodeID)
// Previously, this command accepted the Inspector endpoint. But since
// we're transitioning to not using Inspector directly, it now requires
// the Ironic endpoint. Try to handle the transition by checking for
// the well-known Inspector port and replacing it with the Ironic port.
if parsedEndpoint.Port() == "5050" {
parsedEndpoint.Host = net.JoinHostPort(parsedEndpoint.Hostname(), "6385")
endpoint = parsedEndpoint.String()
}

ironic, err := clients.IronicClient(endpoint, opts.AuthConfig, tlsConf)
if err != nil {
fmt.Printf("could not get ironic client: %s", err)
os.Exit(1)
}

introData := nodes.GetInventory(ironic, opts.NodeID)
data, err := introData.Extract()
if err != nil {
fmt.Printf("could not get introspection data: %s", err)
fmt.Printf("could not get inspection data: %s", err)
os.Exit(1)
}

json, err := json.MarshalIndent(hardwaredetails.GetHardwareDetails(data), "", "\t")
json, err := json.MarshalIndent(hardwaredetails.GetHardwareDetails(data, klog.NewKlogr()), "", "\t")
if err != nil {
fmt.Printf("could not convert introspection data: %s", err)
fmt.Printf("could not convert inspection data: %s", err)
os.Exit(1)
}

Expand All @@ -58,7 +77,7 @@ func main() {

func getOptions() (o options) {
if len(os.Args) != 3 {
fmt.Println("Usage: get-hardware-details <inspector URI> <node UUID>")
fmt.Println("Usage: get-hardware-details <ironic URI> <node UUID>")
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ When an external Ironic is used, the following requirements must be met:
* Either HTTP basic or no-auth authentication must be used (Keystone is not
supported).

* API version 1.74 (Xena release cycle) or newer must be available.
* API version 1.81 (2023.1 "Antelope" release cycle) or newer must be available.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/go-logr/logr v1.2.4
github.com/google/safetext v0.0.0-20230106111101-7156a760e523
github.com/gophercloud/gophercloud v1.6.0
github.com/gophercloud/gophercloud v1.5.1-0.20230912084133-c79ed6d3b371
github.com/metal3-io/baremetal-operator/apis v0.2.0
github.com/metal3-io/baremetal-operator/pkg/hardwareutils v0.2.0
github.com/onsi/gomega v1.27.10
Expand Down Expand Up @@ -62,7 +62,7 @@ require (
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
13 changes: 4 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ github.com/google/safetext v0.0.0-20230106111101-7156a760e523 h1:i4NsbmB9pD5+Ggp
github.com/google/safetext v0.0.0-20230106111101-7156a760e523/go.mod h1:mJNEy0r5YPHC7ChQffpOszlGB4L1iqjXWpIEKcFpr9s=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gophercloud/gophercloud v1.6.0 h1:JwJN1bauRnWPba5ueWs9IluONHteXPWjjK+MvfM4krY=
github.com/gophercloud/gophercloud v1.6.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
github.com/gophercloud/gophercloud v1.5.1-0.20230912084133-c79ed6d3b371 h1:/4TDrmUM3o52/9J3Pn4mZ3VP7CYiFkDKRNuWyxLkpp4=
github.com/gophercloud/gophercloud v1.5.1-0.20230912084133-c79ed6d3b371/go.mod h1:vYRGjRkTeMAwdvQFTVuPGfRWv4dc3J2QHy9H3bHD6Pc=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down Expand Up @@ -155,7 +155,6 @@ go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
Expand All @@ -176,7 +175,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -195,19 +193,16 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
Expand Down
11 changes: 1 addition & 10 deletions pkg/provisioner/ironic/adopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes"
"github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection"
"github.com/stretchr/testify/assert"

"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
Expand Down Expand Up @@ -128,19 +127,11 @@ func TestAdopt(t *testing.T) {
defer tc.ironic.Stop()
}

inspector := testserver.NewInspector(t).Ready().WithIntrospection(nodeUUID, introspection.Introspection{
Finished: false,
})
inspector.Start()
defer inspector.Stop()

host := makeHost()
host.Status.Provisioning.ID = nodeUUID
publisher := func(reason, message string) {}
auth := clients.AuthConfig{Type: clients.NoAuth}
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, publisher,
tc.ironic.Endpoint(), auth, inspector.Endpoint(), auth,
)
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, publisher, tc.ironic.Endpoint(), auth)
if err != nil {
t.Fatalf("could not create provisioner: %s", err)
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/provisioner/ironic/bios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,13 @@ func TestGetFirmwareSettings(t *testing.T) {
tc.ironic.Start()
defer tc.ironic.Stop()

inspector := testserver.NewInspector(t).Start()
defer inspector.Stop()

host := makeHost()
host.Name = "node-1"
host.Status.Provisioning.ID = nodeUUID

auth := clients.AuthConfig{Type: clients.NoAuth}

prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, nullEventPublisher,
tc.ironic.Endpoint(), auth, inspector.Endpoint(), auth,
)
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, nullEventPublisher, tc.ironic.Endpoint(), auth)
if err != nil {
t.Fatalf("could not create provisioner: %s", err)
}
Expand Down
15 changes: 3 additions & 12 deletions pkg/provisioner/ironic/clients/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func readAuthFile(filename string) (string, error) {
return strings.TrimSpace(string(content)), err
}

func load(clientType string) (auth AuthConfig, err error) {
authPath := path.Join(authRoot(), clientType)
// LoadAuth loads the Ironic configuration from the environment
func LoadAuth() (auth AuthConfig, err error) {
authPath := path.Join(authRoot(), "ironic")

if _, err := os.Stat(authPath); err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -69,16 +70,6 @@ func load(clientType string) (auth AuthConfig, err error) {
return
}

// LoadAuth loads the Ironic and Inspector configuration from the environment
func LoadAuth() (ironicAuth, inspectorAuth AuthConfig, err error) {
ironicAuth, err = load("ironic")
if err != nil {
return
}
inspectorAuth, err = load("ironic-inspector")
return
}

// ConfigFromEndpointURL returns an endpoint and an auth config from an
// endpoint URL that may contain HTTP basic auth credentials.
func ConfigFromEndpointURL(endpointURL string) (endpoint string, auth AuthConfig, err error) {
Expand Down
29 changes: 2 additions & 27 deletions pkg/provisioner/ironic/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/baremetal/httpbasic"
"github.com/gophercloud/gophercloud/openstack/baremetal/noauth"
httpbasicintrospection "github.com/gophercloud/gophercloud/openstack/baremetalintrospection/httpbasic"
noauthintrospection "github.com/gophercloud/gophercloud/openstack/baremetalintrospection/noauth"
"go.etcd.io/etcd/client/pkg/v3/transport"
)

Expand Down Expand Up @@ -95,31 +93,8 @@ func IronicClient(ironicEndpoint string, auth AuthConfig, tls TLSConfig) (client

// Ensure we have a microversion high enough to get the features
// we need. Update docs/configuration.md when updating the version.
// Version 1.74 allows retrival of the BIOS Registry
client.Microversion = "1.74"
// Version 1.81 allows retrival of Node inventory
client.Microversion = "1.81"

return updateHTTPClient(client, tls)
}

// InspectorClient creates a client for Ironic Inspector
func InspectorClient(inspectorEndpoint string, auth AuthConfig, tls TLSConfig) (client *gophercloud.ServiceClient, err error) {
switch auth.Type {
case NoAuth:
client, err = noauthintrospection.NewBareMetalIntrospectionNoAuth(
noauthintrospection.EndpointOpts{
IronicInspectorEndpoint: inspectorEndpoint,
})
case HTTPBasicAuth:
client, err = httpbasicintrospection.NewBareMetalIntrospectionHTTPBasic(httpbasicintrospection.EndpointOpts{
IronicInspectorEndpoint: inspectorEndpoint,
IronicInspectorUser: auth.Username,
IronicInspectorUserPassword: auth.Password,
})
default:
err = fmt.Errorf("Unknown auth type %s", auth.Type)
}
if err != nil {
return
}
return updateHTTPClient(client, tls)
}
11 changes: 1 addition & 10 deletions pkg/provisioner/ironic/configdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes"
"github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection"
"github.com/stretchr/testify/assert"

"github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
Expand Down Expand Up @@ -130,19 +129,11 @@ func TestEmpty(t *testing.T) {
ironic.Start()
defer ironic.Stop()

inspector := testserver.NewInspector(t).Ready().WithIntrospection(nodeUUID, introspection.Introspection{
Finished: false,
})
inspector.Start()
defer inspector.Stop()

host := makeHost()
host.Status.Provisioning.ID = nodeUUID
publisher := func(reason, message string) {}
auth := clients.AuthConfig{Type: clients.NoAuth}
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, publisher,
ironic.Endpoint(), auth, inspector.Endpoint(), auth,
)
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, publisher, ironic.Endpoint(), auth)
if err != nil {
t.Fatalf("could not create provisioner: %s", err)
}
Expand Down
16 changes: 4 additions & 12 deletions pkg/provisioner/ironic/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func deleteTest(t *testing.T, detach bool) {
nodeUUID := "33ce8659-7400-4c68-9535-d10766f07a58"

cases := []struct {
name string
ironic *testserver.IronicMock
inspector *testserver.InspectorMock
hostName string
name string
ironic *testserver.IronicMock
hostName string

expectedDirty bool
expectedRequestAfter time.Duration
Expand Down Expand Up @@ -177,11 +176,6 @@ func deleteTest(t *testing.T, detach bool) {
defer tc.ironic.Stop()
}

if tc.inspector != nil {
tc.inspector.Start()
defer tc.inspector.Stop()
}

host := makeHost()
host.Status.Provisioning.ID = nodeUUID

Expand All @@ -190,9 +184,7 @@ func deleteTest(t *testing.T, detach bool) {
}

auth := clients.AuthConfig{Type: clients.NoAuth}
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, nullEventPublisher,
tc.ironic.Endpoint(), auth, tc.inspector.Endpoint(), auth,
)
prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, nullEventPublisher, tc.ironic.Endpoint(), auth)
if err != nil {
t.Fatalf("could not create provisioner: %s", err)
}
Expand Down
32 changes: 8 additions & 24 deletions pkg/provisioner/ironic/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ const (
)

type ironicDependenciesChecker struct {
client *gophercloud.ServiceClient
inspector *gophercloud.ServiceClient
log logr.Logger
client *gophercloud.ServiceClient
log logr.Logger
}

func newIronicDependenciesChecker(client *gophercloud.ServiceClient, inspector *gophercloud.ServiceClient, log logr.Logger) *ironicDependenciesChecker {
func newIronicDependenciesChecker(client *gophercloud.ServiceClient, log logr.Logger) *ironicDependenciesChecker {
return &ironicDependenciesChecker{
client: client,
inspector: inspector,
log: log,
client: client,
log: log,
}
}

func (i *ironicDependenciesChecker) IsReady() (result bool, err error) {

ready, err := i.checkIronic()
if ready && err == nil {
ready = i.checkIronicInspector()
ready := i.checkEndpoint(i.client)
if ready {
ready, err = i.checkIronicConductor()
}

return ready, err
}

Expand All @@ -53,14 +49,6 @@ func (i *ironicDependenciesChecker) checkEndpoint(client *gophercloud.ServiceCli
return err == nil
}

func (i *ironicDependenciesChecker) checkIronic() (ready bool, err error) {
ready = i.checkEndpoint(i.client)
if ready {
ready, err = i.checkIronicConductor()
}
return ready, err
}

func (i *ironicDependenciesChecker) checkIronicConductor() (ready bool, err error) {

pager := drivers.ListDrivers(i.client, drivers.ListDriversOpts{
Expand All @@ -86,7 +74,3 @@ func (i *ironicDependenciesChecker) checkIronicConductor() (ready bool, err erro

return ready, err
}

func (i *ironicDependenciesChecker) checkIronicInspector() (ready bool) {
return i.checkEndpoint(i.inspector)
}
Loading

0 comments on commit faab936

Please sign in to comment.