Skip to content

Commit

Permalink
Switch to equinix sdk and remove facility usage
Browse files Browse the repository at this point in the history
  • Loading branch information
schrodit committed Feb 18, 2024
1 parent ad4baa8 commit 1af517c
Show file tree
Hide file tree
Showing 405 changed files with 125,522 additions and 5,504 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/gardener/machine-controller-manager-provider-equinix-metal
go 1.20

require (
github.com/equinix/equinix-sdk-go v0.33.0
github.com/gardener/machine-controller-manager v0.49.1
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.23.0
github.com/packethost/packngo v0.9.0
github.com/spf13/pflag v1.0.5
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
k8s.io/api v0.26.2
Expand Down
10 changes: 3 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinix/equinix-sdk-go v0.33.0 h1:AmRUWJ1bdZwaNm7/yOcD4TLOWGhbHLSk6SB2fE7lchM=
github.com/equinix/equinix-sdk-go v0.33.0/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -230,8 +230,6 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.23.0 h1:/oxKu9c2HVap+F3PfKort2Hw5DEU+HGlW8n+tguWsys=
github.com/onsi/gomega v1.23.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
github.com/packethost/packngo v0.9.0 h1:dcrJFDNdbW+xCCNzWCmtol1x+hanEZmff5Myv/ku82U=
github.com/packethost/packngo v0.9.0/go.mod h1:YrtUNN9IRjjqN6zK+cy2IYoi3EjHfoWTWxJkI1I1Vk0=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -281,7 +279,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -296,9 +294,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down
139 changes: 65 additions & 74 deletions pkg/mock/equinixmetal.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package mock

import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/gardener/machine-controller-manager-provider-equinix-metal/pkg/spi"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status"
"github.com/packethost/packngo"
corev1 "k8s.io/api/core/v1"
)

// PluginSPIImpl is the plugin SPI implementation to mock the provider
type PluginSPIImpl struct {
Devices []packngo.Device
Devices []metalv1.Device
index int
mu sync.Mutex // so that we can increment index without conflicts
}

// NewSession creates a mock session for provider
func (p *PluginSPIImpl) NewSession(secret *corev1.Secret) packngo.DeviceService {
func (p *PluginSPIImpl) NewSession(secret *corev1.Secret) (spi.MetalDeviceService, error) {
apiKey := spi.GetAPIKey(secret)
token := strings.TrimSpace(apiKey)

if token != "" {
return &deviceService{
spi: p,
name: "gardener",
token: token,
}
if token == "" {
return nil, errors.New("Equinix Metal api token required")
}
return nil

return &deviceService{
spi: p,
name: "gardener",
token: token,
}, nil
}

func (p *PluginSPIImpl) increment() {
Expand All @@ -42,7 +43,7 @@ func (p *PluginSPIImpl) increment() {
p.index++
}

func (p *PluginSPIImpl) addDevice(dev packngo.Device) {
func (p *PluginSPIImpl) addDevice(dev metalv1.Device) {
p.mu.Lock()
defer p.mu.Unlock()
p.Devices = append(p.Devices, dev)
Expand All @@ -54,86 +55,76 @@ type deviceService struct {
token string
}

func (d *deviceService) List(projectID string, opts *packngo.ListOptions) ([]packngo.Device, *packngo.Response, error) {
return d.spi.Devices, &packngo.Response{}, nil
func (d *deviceService) FindProjectDevices(
ctx context.Context,
projectID string,
) (*metalv1.DeviceList, *http.Response, error) {
return &metalv1.DeviceList{
Devices: d.spi.Devices,
}, &http.Response{}, nil
}
func (d *deviceService) Get(deviceID string, opts *packngo.GetOptions) (*packngo.Device, *packngo.Response, error) {

func (d *deviceService) FindDeviceByID(
ctx context.Context,
deviceID string,
) (*metalv1.Device, *http.Response, error) {
for _, dev := range d.spi.Devices {
if dev.ID == deviceID {
return &dev, &packngo.Response{}, nil
if *dev.Id == deviceID {
return &dev, &http.Response{}, nil
}
}
return nil, &packngo.Response{
Response: &http.Response{
StatusCode: 404,
Status: "404 NOT FOUND",
},
return nil, &http.Response{
StatusCode: 404,
Status: "404 NOT FOUND",
}, fmt.Errorf("404 NOT FOUND")
}
func (d *deviceService) Create(req *packngo.DeviceCreateRequest) (*packngo.Device, *packngo.Response, error) {

func (d *deviceService) CreateDevice(
ctx context.Context,
projectID string,
createDeviceRequest metalv1.CreateDeviceRequest,
) (*metalv1.Device, *http.Response, error) {
now := time.Now()
d.spi.increment()
dev := packngo.Device{
ID: fmt.Sprintf("%06d", d.spi.index),
req := createDeviceRequest.DeviceCreateInMetroInput
var (
name = fmt.Sprintf("%06d", d.spi.index)
billingCycle = string(*req.BillingCycle)
)
dev := metalv1.Device{
Id: &name,
Hostname: req.Hostname,
Description: &req.Description,
Created: now.String(),
Updated: now.String(),
BillingCycle: req.BillingCycle,
Description: req.Description,
CreatedAt: &now,
UpdatedAt: &now,
BillingCycle: &billingCycle,
Tags: req.Tags,
OS: &packngo.OS{
Name: req.OS,
OperatingSystem: &metalv1.OperatingSystem{
Name: &req.OperatingSystem,
},
Plan: &packngo.Plan{},
Facility: &packngo.Facility{
Code: req.Facility[0],
Plan: &metalv1.Plan{},
Metro: &metalv1.DeviceMetro{
Code: &req.Metro,
},
Project: &packngo.Project{
ID: req.ProjectID,
Project: &metalv1.Project{
Id: &projectID,
},
UserData: req.UserData,
Userdata: req.Userdata,
}
d.spi.addDevice(dev)
return &dev, &packngo.Response{}, nil
return &dev, &http.Response{}, nil
}
func (d *deviceService) Delete(deviceID string, force bool) (*packngo.Response, error) {
var devs []packngo.Device

func (d *deviceService) DeleteDevice(
ctx context.Context,
deviceID string,
) (*http.Response, error) {
var devs []metalv1.Device
for _, dev := range d.spi.Devices {
if dev.ID != deviceID {
if *dev.Id != deviceID {
devs = append(devs, dev)
}
}
d.spi.Devices = devs
return &packngo.Response{}, nil
}

/*
Below are not implemenetd as unnecessary for MCM
*/
func (d *deviceService) Update(string, *packngo.DeviceUpdateRequest) (*packngo.Device, *packngo.Response, error) {
return nil, nil, status.Error(codes.Unimplemented, "Update unsupported in mock")
}
func (d *deviceService) Reboot(string) (*packngo.Response, error) {
return nil, status.Error(codes.Unimplemented, "Reboot unsupported in mock")
}
func (d *deviceService) PowerOff(string) (*packngo.Response, error) {
return nil, status.Error(codes.Unimplemented, "PowerOff unsupported in mock")
}
func (d *deviceService) PowerOn(string) (*packngo.Response, error) {
return nil, status.Error(codes.Unimplemented, "PowerOn unsupported in mock")
}
func (d *deviceService) Lock(string) (*packngo.Response, error) {
return nil, status.Error(codes.Unimplemented, "Lock unsupported in mock")
}
func (d *deviceService) Unlock(string) (*packngo.Response, error) {
return nil, status.Error(codes.Unimplemented, "Unlock unsupported in mock")
}
func (d *deviceService) ListBGPSessions(deviceID string, opts *packngo.ListOptions) ([]packngo.BGPSession, *packngo.Response, error) {
return nil, nil, status.Error(codes.Unimplemented, "ListBGPSessions unsupported in mock")
}
func (d *deviceService) ListBGPNeighbors(deviceID string, opts *packngo.ListOptions) ([]packngo.BGPNeighbor, *packngo.Response, error) {
return nil, nil, status.Error(codes.Unimplemented, "ListBGPNeighbors unsupported in mock")
}
func (d *deviceService) ListEvents(deviceID string, opts *packngo.ListOptions) ([]packngo.Event, *packngo.Response, error) {
return nil, nil, status.Error(codes.Unimplemented, "ListEvents unsupported in mock")
return &http.Response{}, nil
}
3 changes: 1 addition & 2 deletions pkg/provider/apis/provider_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ const (
type EquinixMetalProviderSpec struct {
APIVersion string `json:"apiVersion,omitempty"`
Metro string `json:"metro,omitempty"`
Facilities []string `json:"facilities"`
MachineType string `json:"machineType"`
BillingCycle string `json:"billingCycle"`
OS string `json:"OS,omitempty"`
IPXEScriptURL string `json:"ipxeScriptUrl,omitempty"`
IPXEScriptURL *string `json:"ipxeScriptUrl,omitempty"`
ProjectID string `json:"projectID"`
Tags []string `json:"tags,omitempty"`
SSHKeys []string `json:"sshKeys,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/apis/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ValidateProviderSpec(spec *api.EquinixMetalProviderSpec, fldPath *field.Pat
allErrs = field.ErrorList{}
)

if "" == spec.OS && "" == spec.IPXEScriptURL {
if "" == spec.OS && nil == spec.IPXEScriptURL {
allErrs = append(allErrs, field.Required(fldPath.Child("os"), "OS or IPXEScriptURL is required"))
}
if "" == spec.MachineType {
Expand Down
Loading

0 comments on commit 1af517c

Please sign in to comment.