/
gcp_actuator.go
176 lines (159 loc) · 6.16 KB
/
gcp_actuator.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package hibernation
import (
"context"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
compute "google.golang.org/api/compute/v1"
corev1 "k8s.io/api/core/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
hivev1 "github.com/openshift/hive/apis/hive/v1"
controllerutils "github.com/openshift/hive/pkg/controller/utils"
"github.com/openshift/hive/pkg/gcpclient"
)
const (
instanceFields = "items/*/instances(name,zone,status),nextPageToken"
)
var (
gcpRunningStatuses = sets.NewString("RUNNING")
gcpStoppedStatuses = sets.NewString("STOPPED", "TERMINATED")
gcpPendingStatuses = sets.NewString("PROVISIONING", "STAGING")
gcpStoppingStatuses = sets.NewString("STOPPING")
gcpRunningOrPendingStatuses = gcpRunningStatuses.Union(gcpPendingStatuses)
gcpStoppedOrStoppingStatuses = gcpStoppedStatuses.Union(gcpStoppingStatuses)
gcpNotRunningStatuses = gcpStoppedOrStoppingStatuses.Union(gcpPendingStatuses)
gcpNotStoppedStatuses = gcpRunningOrPendingStatuses.Union(gcpStoppingStatuses)
)
func init() {
RegisterActuator(&gcpActuator{getGCPClientFn: getGCPClient})
}
type gcpActuator struct {
getGCPClientFn func(*hivev1.ClusterDeployment, client.Client, log.FieldLogger) (gcpclient.Client, error)
}
// CanHandle returns true if the actuator can handle a particular ClusterDeployment
func (a *gcpActuator) CanHandle(cd *hivev1.ClusterDeployment) bool {
return cd.Spec.Platform.GCP != nil
}
// StopMachines will start machines belonging to the given ClusterDeployment
func (a *gcpActuator) StopMachines(cd *hivev1.ClusterDeployment, hiveClient client.Client, logger log.FieldLogger) error {
logger = logger.WithField("cloud", "GCP")
gcpClient, err := a.getGCPClientFn(cd, hiveClient, logger)
if err != nil {
return err
}
instances, err := gcpListComputeInstances(gcpClient, cd, gcpRunningOrPendingStatuses, logger)
if err != nil {
return err
}
var errs []error
for _, instance := range instances {
logger.WithField("instance", instance.Name).Info("Stopping instance")
err = gcpClient.StopInstance(instance)
if err != nil {
errs = append(errs, err)
}
}
return utilerrors.NewAggregate(errs)
}
// StartMachines will select machines belonging to the given ClusterDeployment
func (a *gcpActuator) StartMachines(cd *hivev1.ClusterDeployment, hiveClient client.Client, logger log.FieldLogger) error {
logger = logger.WithField("cloud", "GCP")
gcpClient, err := a.getGCPClientFn(cd, hiveClient, logger)
if err != nil {
return err
}
instances, err := gcpListComputeInstances(gcpClient, cd, gcpStoppedOrStoppingStatuses, logger)
if err != nil {
return err
}
if len(instances) == 0 {
logger.Info("No instances were found to start")
return nil
}
var errs []error
for _, instance := range instances {
logger.WithField("instance", instance.Name).Info("Starting instance")
err = gcpClient.StartInstance(instance)
if err != nil {
errs = append(errs, err)
}
}
return utilerrors.NewAggregate(errs)
}
// MachinesRunning will return true if the machines associated with the given
// ClusterDeployment are in a running state. It also returns a list of machines that
// are not running.
func (a *gcpActuator) MachinesRunning(cd *hivev1.ClusterDeployment, hiveClient client.Client, logger log.FieldLogger) (bool, []string, error) {
logger = logger.WithField("cloud", "GCP")
gcpClient, err := a.getGCPClientFn(cd, hiveClient, logger)
if err != nil {
return false, nil, err
}
instances, err := gcpListComputeInstances(gcpClient, cd, gcpNotRunningStatuses, logger)
if err != nil {
return false, nil, err
}
return len(instances) == 0, instanceNames(instances), nil
}
// MachinesStopped will return true if the machines associated with the given
// ClusterDeployment are in a stopped state. It also returns a list of machines
// that have not stopped.
func (a *gcpActuator) MachinesStopped(cd *hivev1.ClusterDeployment, hiveClient client.Client, logger log.FieldLogger) (bool, []string, error) {
logger = logger.WithField("cloud", "GCP")
gcpClient, err := a.getGCPClientFn(cd, hiveClient, logger)
if err != nil {
return false, nil, err
}
instances, err := gcpListComputeInstances(gcpClient, cd, gcpNotStoppedStatuses, logger)
if err != nil {
return false, nil, err
}
return len(instances) == 0, instanceNames(instances), nil
}
func getGCPClient(cd *hivev1.ClusterDeployment, c client.Client, logger log.FieldLogger) (gcpclient.Client, error) {
if cd.Spec.Platform.GCP == nil {
return nil, errors.New("GCP platform is not set in ClusterDeployment")
}
secret := &corev1.Secret{}
err := c.Get(context.TODO(), client.ObjectKey{Name: cd.Spec.Platform.GCP.CredentialsSecretRef.Name, Namespace: cd.Namespace}, secret)
if err != nil {
logger.WithError(err).Log(controllerutils.LogLevel(err), "Failed to fetch GCP credentials secret")
return nil, errors.Wrap(err, "failed to fetch GCP credentials secret")
}
return gcpclient.NewClientFromSecret(secret)
}
func instanceFilter(cd *hivev1.ClusterDeployment) string {
return fmt.Sprintf("name eq \"%s-.*\"", cd.Spec.ClusterMetadata.InfraID)
}
func gcpListComputeInstances(gcpClient gcpclient.Client, cd *hivev1.ClusterDeployment, statuses sets.String, logger log.FieldLogger) ([]*compute.Instance, error) {
var instances []*compute.Instance
logger.Debug("listing client instances")
err := gcpClient.ListComputeInstances(gcpclient.ListComputeInstancesOptions{
Filter: instanceFilter(cd),
Fields: instanceFields,
}, func(list *compute.InstanceAggregatedList) error {
for _, scopedList := range list.Items {
for _, instance := range scopedList.Instances {
if statuses.Has(instance.Status) {
instances = append(instances, instance)
}
}
}
return nil
})
if err != nil {
logger.WithError(err).Log(controllerutils.LogLevel(err), "Failed to fetch compute instances")
} else {
logger.WithField("count", len(instances)).WithField("statuses", statuses.List()).Debug("found instances")
}
return instances, err
}
func instanceNames(instances []*compute.Instance) []string {
ret := make([]string, len(instances))
for i, instance := range instances {
ret[i] = instance.Name
}
return ret
}