-
Notifications
You must be signed in to change notification settings - Fork 0
/
restartimpl.go
156 lines (128 loc) · 4.86 KB
/
restartimpl.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
package restartservice
/*
Copyright 2020 Crunchy Data Solutions, Inc.
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.
*/
import (
"fmt"
"github.com/crunchydata/postgres-operator/internal/apiserver"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/patroni"
"github.com/crunchydata/postgres-operator/internal/util"
msgs "github.com/crunchydata/postgres-operator/pkg/apiservermsgs"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Restart restarts either all PostgreSQL databases within a PostgreSQL cluster (i.e. the primary
// and all replicas) or if targets are specified, just those targets.
// pgo restart mycluster
// pgo restart mycluster --target=mycluster-abcd
func Restart(request *msgs.RestartRequest, pgouser string) msgs.RestartResponse {
log.Debugf("restart called for %s", request.ClusterName)
resp := msgs.RestartResponse{
Status: msgs.Status{
Code: msgs.Ok,
},
}
clusterName := request.ClusterName
namespace := request.Namespace
cluster, err := apiserver.PGOClientset.CrunchydataV1().Pgclusters(namespace).Get(clusterName,
metav1.GetOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
// check if the current cluster is not upgraded to the deployed
// Operator version. If not, do not allow the command to complete
if cluster.Annotations[config.ANNOTATION_IS_UPGRADED] == config.ANNOTATIONS_FALSE {
resp.Status.Code = msgs.Error
resp.Status.Msg = fmt.Sprintf("%s %s", cluster.Name, msgs.UpgradeError)
return resp
}
var restartResults []patroni.RestartResult
// restart either the whole cluster, or just any targets specified
patroniClient := patroni.NewPatroniClient(apiserver.RESTConfig, apiserver.Clientset,
cluster.GetName(), namespace)
if len(request.Targets) > 0 {
restartResults, err = patroniClient.RestartInstances(request.Targets...)
} else {
restartResults, err = patroniClient.RestartCluster()
}
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
restartDetails := msgs.RestartDetail{ClusterName: clusterName}
for _, restartResult := range restartResults {
instanceDetail := msgs.InstanceDetail{InstanceName: restartResult.Instance}
if restartResult.Error != nil {
instanceDetail.Error = true
instanceDetail.ErrorMessage = restartResult.Error.Error()
}
restartDetails.Instances = append(restartDetails.Instances, instanceDetail)
}
resp.Result = restartDetails
return resp
}
// QueryRestart queries a cluster for instances available to use as as targets for a PostgreSQL restart.
// pgo restart mycluster --query
func QueryRestart(clusterName, namespace string) msgs.QueryRestartResponse {
log.Debugf("query restart called for %s", clusterName)
resp := msgs.QueryRestartResponse{
Status: msgs.Status{
Code: msgs.Ok,
},
}
cluster, err := apiserver.PGOClientset.CrunchydataV1().Pgclusters(namespace).Get(clusterName,
metav1.GetOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
// Get information about the current status of all of all cluster instances. This is
// handled by a helper function, that will return the information in a struct with the
// key elements to help the user understand the current state of the instances in a cluster
replicationStatusRequest := util.ReplicationStatusRequest{
RESTConfig: apiserver.RESTConfig,
Clientset: apiserver.Clientset,
Namespace: namespace,
ClusterName: clusterName,
}
replicationStatusResponse, err := util.ReplicationStatus(replicationStatusRequest, true)
if err != nil {
log.Error(err.Error())
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
// if there are no results, return the response as is
if len(replicationStatusResponse.Instances) == 0 {
return resp
}
// iterate through response results to create the API response
for _, instance := range replicationStatusResponse.Instances {
// create an result for the response
resp.Results = append(resp.Results, msgs.RestartTargetSpec{
Name: instance.Name,
Node: instance.Node,
Status: instance.Status,
ReplicationLag: instance.ReplicationLag,
Timeline: instance.Timeline,
PendingRestart: instance.PendingRestart,
Role: instance.Role,
})
}
resp.Standby = cluster.Spec.Standby
return resp
}