Skip to content

Commit

Permalink
[release/v1.59] Anexia: various patches (request/response logging, CP…
Browse files Browse the repository at this point in the history
…U performance type, 404 fix on delete) (#1798)

* Anexia: really handle 404 response for DELETE

There already was a check for a 404 error being returned, but the client
library does not actually return 404 as error. Workaround is needed, as
that won't be fixed in go-anxcloud, as it's the legacy client - but
nicely commented and good workaround.

Signed-off-by: Mara Sophie Grosch <[email protected]>

* Anexia: use cloudproviderutil.HttpClient

This way we get request/response logging if required - even prefixed
with the Machine name, if applicable.

Signed-off-by: Mara Sophie Grosch <[email protected]>

* Anexia: allow to specify CPU performance type

Signed-off-by: Mara Sophie Grosch <[email protected]>

---------

Signed-off-by: Mara Sophie Grosch <[email protected]>
Co-authored-by: Mara Sophie Grosch <[email protected]>
  • Loading branch information
kubermatic-bot and Mara Sophie Grosch authored May 21, 2024
1 parent 676adb4 commit eba98e3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions examples/anexia-machinedeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ spec:
cpus: 2
memory: 2048

# this defaults to "performance", but you can set anything
# supported by the Anexia Engine here - or not set this attribute
# at all
cpuPerformanceType: standard

disks:
- size: 60
performanceType: ENT6
Expand Down
46 changes: 37 additions & 9 deletions pkg/cloudprovider/provider/anexia/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/kubermatic/machine-controller/pkg/providerconfig"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"

cloudproviderutil "github.com/kubermatic/machine-controller/pkg/cloudprovider/util"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (p *provider) Create(ctx context.Context, log *zap.SugaredLogger, machine *
Machine: machine,
})

_, client, err := getClient(config.Token)
_, client, err := getClient(config.Token, &machine.Name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,6 +162,10 @@ func provisionVM(ctx context.Context, log *zap.SugaredLogger, client anxclient.C

vm.DiskType = config.Disks[0].PerformanceType

if config.CPUPerformanceType != "" {
vm.CPUPerformanceType = config.CPUPerformanceType
}

for _, disk := range config.Disks[1:] {
vm.AdditionalDisks = append(vm.AdditionalDisks, anxvm.AdditionalDisk{
SizeGBs: disk.Size,
Expand Down Expand Up @@ -334,7 +339,7 @@ func (p *provider) resolveConfig(ctx context.Context, log *zap.SugaredLogger, co

// when "templateID" is not set, we expect "template" to be
if ret.TemplateID == "" {
a, _, err := getClient(ret.Token)
a, _, err := getClient(ret.Token, nil)
if err != nil {
return nil, fmt.Errorf("failed initializing API clients: %w", err)
}
Expand Down Expand Up @@ -467,7 +472,7 @@ func (p *provider) Get(ctx context.Context, log *zap.SugaredLogger, machine *clu
return nil, newError(common.InvalidConfigurationMachineError, "failed to retrieve config: %v", err)
}

_, cli, err := getClient(config.Token)
_, cli, err := getClient(config.Token, &machine.Name)
if err != nil {
return nil, newError(common.InvalidConfigurationMachineError, "failed to create Anexia client: %v", err)
}
Expand Down Expand Up @@ -553,7 +558,7 @@ func (p *provider) Cleanup(ctx context.Context, log *zap.SugaredLogger, machine
return false, newError(common.InvalidConfigurationMachineError, "failed to parse MachineSpec: %v", err)
}

_, cli, err := getClient(config.Token)
_, cli, err := getClient(config.Token, &machine.Name)
if err != nil {
return false, newError(common.InvalidConfigurationMachineError, "failed to create Anexia client: %v", err)
}
Expand All @@ -568,10 +573,20 @@ func (p *provider) Cleanup(ctx context.Context, log *zap.SugaredLogger, machine
response, err := vsphereAPI.Provisioning().VM().Deprovision(deleteCtx, status.InstanceID, false)
if err != nil {
var respErr *anxclient.ResponseError

// Only error if the error was not "not found"
if !(errors.As(err, &respErr) && respErr.ErrorData.Code == http.StatusNotFound) {
return false, newError(common.DeleteMachineError, "failed to delete machine: %v", err)
}

// good thinking checking for a "not found" error, but go-anxcloud does only
// return >= 500 && < 600 errors (:
// since that's the legacy client in go-anxcloud and the new one is not yet available,
// this will not be fixed there but we have a nice workaround here:

if response.Identifier == "" {
return true, nil
}
}
status.DeprovisioningID = response.Identifier
}
Expand Down Expand Up @@ -609,16 +624,29 @@ func (p *provider) SetMetricsForMachines(_ clusterv1alpha1.MachineList) error {
return nil
}

func getClient(token string) (api.API, anxclient.Client, error) {
tokenOpt := anxclient.TokenFromString(token)
client := anxclient.HTTPClient(&http.Client{Timeout: 120 * time.Second})
func getClient(token string, machineName *string) (api.API, anxclient.Client, error) {
logPrefix := "[Anexia API]"

if machineName != nil {
logPrefix = fmt.Sprintf("[Anexia API for Machine %q]", *machineName)
}

httpClient := cloudproviderutil.HTTPClientConfig{
Timeout: 120 * time.Second,
LogPrefix: logPrefix,
}.New()

legacyClientOptions := []anxclient.Option{
anxclient.TokenFromString(token),
anxclient.HTTPClient(&httpClient),
}

a, err := api.NewAPI(api.WithClientOptions(client, tokenOpt))
a, err := api.NewAPI(api.WithClientOptions(legacyClientOptions...))
if err != nil {
return nil, nil, fmt.Errorf("error creating generic API client: %w", err)
}

legacyClient, err := anxclient.New(tokenOpt, client)
legacyClient, err := anxclient.New(legacyClientOptions...)
if err != nil {
return nil, nil, fmt.Errorf("error creating legacy client: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cloudprovider/provider/anexia/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type RawConfig struct {
Template providerconfigtypes.ConfigVarString `json:"template"`
TemplateBuild providerconfigtypes.ConfigVarString `json:"templateBuild"`

CPUs int `json:"cpus"`
Memory int `json:"memory"`
CPUs int `json:"cpus"`
CPUPerformanceType string `json:"cpuPerformanceType"`
Memory int `json:"memory"`

// Deprecated, use Disks instead.
DiskSize int `json:"diskSize"`
Expand Down

0 comments on commit eba98e3

Please sign in to comment.