Skip to content

Commit eba98e3

Browse files
kubermatic-botMara Sophie Grosch
andauthored
[release/v1.59] Anexia: various patches (request/response logging, CPU 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]>
1 parent 676adb4 commit eba98e3

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

examples/anexia-machinedeployment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ spec:
3939
cpus: 2
4040
memory: 2048
4141

42+
# this defaults to "performance", but you can set anything
43+
# supported by the Anexia Engine here - or not set this attribute
44+
# at all
45+
cpuPerformanceType: standard
46+
4247
disks:
4348
- size: 60
4449
performanceType: ENT6

pkg/cloudprovider/provider/anexia/provider.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/kubermatic/machine-controller/pkg/providerconfig"
4949
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
5050

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

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

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

165+
if config.CPUPerformanceType != "" {
166+
vm.CPUPerformanceType = config.CPUPerformanceType
167+
}
168+
164169
for _, disk := range config.Disks[1:] {
165170
vm.AdditionalDisks = append(vm.AdditionalDisks, anxvm.AdditionalDisk{
166171
SizeGBs: disk.Size,
@@ -334,7 +339,7 @@ func (p *provider) resolveConfig(ctx context.Context, log *zap.SugaredLogger, co
334339

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

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

556-
_, cli, err := getClient(config.Token)
561+
_, cli, err := getClient(config.Token, &machine.Name)
557562
if err != nil {
558563
return false, newError(common.InvalidConfigurationMachineError, "failed to create Anexia client: %v", err)
559564
}
@@ -568,10 +573,20 @@ func (p *provider) Cleanup(ctx context.Context, log *zap.SugaredLogger, machine
568573
response, err := vsphereAPI.Provisioning().VM().Deprovision(deleteCtx, status.InstanceID, false)
569574
if err != nil {
570575
var respErr *anxclient.ResponseError
576+
571577
// Only error if the error was not "not found"
572578
if !(errors.As(err, &respErr) && respErr.ErrorData.Code == http.StatusNotFound) {
573579
return false, newError(common.DeleteMachineError, "failed to delete machine: %v", err)
574580
}
581+
582+
// good thinking checking for a "not found" error, but go-anxcloud does only
583+
// return >= 500 && < 600 errors (:
584+
// since that's the legacy client in go-anxcloud and the new one is not yet available,
585+
// this will not be fixed there but we have a nice workaround here:
586+
587+
if response.Identifier == "" {
588+
return true, nil
589+
}
575590
}
576591
status.DeprovisioningID = response.Identifier
577592
}
@@ -609,16 +624,29 @@ func (p *provider) SetMetricsForMachines(_ clusterv1alpha1.MachineList) error {
609624
return nil
610625
}
611626

612-
func getClient(token string) (api.API, anxclient.Client, error) {
613-
tokenOpt := anxclient.TokenFromString(token)
614-
client := anxclient.HTTPClient(&http.Client{Timeout: 120 * time.Second})
627+
func getClient(token string, machineName *string) (api.API, anxclient.Client, error) {
628+
logPrefix := "[Anexia API]"
629+
630+
if machineName != nil {
631+
logPrefix = fmt.Sprintf("[Anexia API for Machine %q]", *machineName)
632+
}
633+
634+
httpClient := cloudproviderutil.HTTPClientConfig{
635+
Timeout: 120 * time.Second,
636+
LogPrefix: logPrefix,
637+
}.New()
638+
639+
legacyClientOptions := []anxclient.Option{
640+
anxclient.TokenFromString(token),
641+
anxclient.HTTPClient(&httpClient),
642+
}
615643

616-
a, err := api.NewAPI(api.WithClientOptions(client, tokenOpt))
644+
a, err := api.NewAPI(api.WithClientOptions(legacyClientOptions...))
617645
if err != nil {
618646
return nil, nil, fmt.Errorf("error creating generic API client: %w", err)
619647
}
620648

621-
legacyClient, err := anxclient.New(tokenOpt, client)
649+
legacyClient, err := anxclient.New(legacyClientOptions...)
622650
if err != nil {
623651
return nil, nil, fmt.Errorf("error creating legacy client: %w", err)
624652
}

pkg/cloudprovider/provider/anexia/types/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ type RawConfig struct {
6363
Template providerconfigtypes.ConfigVarString `json:"template"`
6464
TemplateBuild providerconfigtypes.ConfigVarString `json:"templateBuild"`
6565

66-
CPUs int `json:"cpus"`
67-
Memory int `json:"memory"`
66+
CPUs int `json:"cpus"`
67+
CPUPerformanceType string `json:"cpuPerformanceType"`
68+
Memory int `json:"memory"`
6869

6970
// Deprecated, use Disks instead.
7071
DiskSize int `json:"diskSize"`

0 commit comments

Comments
 (0)