/
get_machine_status.go
64 lines (56 loc) · 2.6 KB
/
get_machine_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2022 OnMetal authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package onmetal
import (
"context"
"fmt"
"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status"
apiv1alpha1 "github.com/onmetal/machine-controller-manager-provider-onmetal/pkg/api/v1alpha1"
computev1alpha1 "github.com/onmetal/onmetal-api/api/compute/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetMachineStatus handles a machine get status request
func (d *onmetalDriver) GetMachineStatus(ctx context.Context, req *driver.GetMachineStatusRequest) (*driver.GetMachineStatusResponse, error) {
if isEmptyMachineStatusRequest(req) {
return nil, status.Error(codes.InvalidArgument, "received empty request")
}
if req.MachineClass.Provider != apiv1alpha1.ProviderName {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("requested provider '%s' is not suppored by the driver '%s'", req.MachineClass.Provider, apiv1alpha1.ProviderName))
}
klog.V(3).Infof("Machine status request has been received for %q", req.Machine.Name)
defer klog.V(3).Infof("Machine status request has been processed for %q", req.Machine.Name)
// Fetch machine key from machine request
onmetalMachineKey := client.ObjectKey{
Namespace: d.OnmetalNamespace,
Name: req.Machine.Name,
}
onmetalMachine := &computev1alpha1.Machine{}
if err := d.OnmetelClient.Get(ctx, onmetalMachineKey, onmetalMachine); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
return &driver.GetMachineStatusResponse{
ProviderID: getProviderIDForOnmetalMachine(onmetalMachine),
NodeName: onmetalMachine.Name,
}, nil
}
func isEmptyMachineStatusRequest(req *driver.GetMachineStatusRequest) bool {
return req == nil || req.MachineClass == nil || req.Machine == nil || req.Secret == nil
}