mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-04 07:15:05 -04:00
[node operator] Add GCP client
Signed-off-by: Malte Poll <mp@edgeless.systems>
This commit is contained in:
parent
0618a000a7
commit
717570d00a
23 changed files with 2102 additions and 21 deletions
|
@ -0,0 +1,59 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1"
|
||||
updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1"
|
||||
"google.golang.org/api/googleapi"
|
||||
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
|
||||
)
|
||||
|
||||
// GetNodeState returns the state of the node.
|
||||
func (c *Client) GetNodeState(ctx context.Context, providerID string) (updatev1alpha1.CSPNodeState, error) {
|
||||
project, zone, instanceName, err := splitProviderID(providerID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
instance, err := c.instanceAPI.Get(ctx, &computepb.GetInstanceRequest{
|
||||
Instance: instanceName,
|
||||
Project: project,
|
||||
Zone: zone,
|
||||
})
|
||||
if err != nil {
|
||||
var apiErr *googleapi.Error
|
||||
if errors.As(err, &apiErr) {
|
||||
if apiErr.Code == http.StatusNotFound {
|
||||
return v1alpha1.NodeStateTerminated, nil
|
||||
}
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
if instance.Status == nil {
|
||||
return v1alpha1.NodeStateUnknown, nil
|
||||
}
|
||||
|
||||
// reference: https://cloud.google.com/compute/docs/instances/instance-life-cycle
|
||||
switch *instance.Status {
|
||||
case computepb.Instance_PROVISIONING.String():
|
||||
fallthrough
|
||||
case computepb.Instance_STAGING.String():
|
||||
return v1alpha1.NodeStateCreating, nil
|
||||
case computepb.Instance_RUNNING.String():
|
||||
return v1alpha1.NodeStateReady, nil
|
||||
case computepb.Instance_STOPPING.String():
|
||||
fallthrough
|
||||
case computepb.Instance_SUSPENDING.String():
|
||||
fallthrough
|
||||
case computepb.Instance_SUSPENDED.String():
|
||||
fallthrough
|
||||
case computepb.Instance_REPAIRING.String():
|
||||
fallthrough
|
||||
case computepb.Instance_TERMINATED.String(): // this is stopped in GCP terms
|
||||
return v1alpha1.NodeStateStopped, nil
|
||||
}
|
||||
return v1alpha1.NodeStateUnknown, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue