Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anexia: various patches (request/response logging, CPU performance type, 404 fix on delete) #1797

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -550,7 +555,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 @@ -565,10 +570,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 @@ -606,16 +621,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