-
Notifications
You must be signed in to change notification settings - Fork 592
/
failover.go
138 lines (113 loc) · 4.06 KB
/
failover.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
// Package cluster holds the cluster CRD logic and definitions
// A cluster is comprised of a primary service, replica service,
// primary deployment, and replica deployment
package cluster
/*
Copyright 2018 - 2022 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 (
"encoding/json"
"time"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/kubeapi"
crv1 "github.com/crunchydata/postgres-operator/pkg/apis/crunchydata.com/v1"
"github.com/crunchydata/postgres-operator/pkg/events"
pgo "github.com/crunchydata/postgres-operator/pkg/generated/clientset/versioned"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
)
// FailoverBase ...
// gets called first on a failover
func FailoverBase(namespace string, clientset kubeapi.Interface, task *crv1.Pgtask, restconfig *rest.Config) {
var err error
//look up the pgcluster for this task
//in the case, the clustername is passed as a key in the
//parameters map
var clusterName string
for k := range task.Spec.Parameters {
clusterName = k
}
cluster, err := clientset.CrunchydataV1().Pgclusters(namespace).Get(clusterName, metav1.GetOptions{})
if err != nil {
return
}
//create marker (clustername, namespace)
err = PatchpgtaskFailoverStatus(clientset, task, namespace)
if err != nil {
log.Errorf("could not set failover started marker for task %s cluster %s", task.Spec.Name, clusterName)
return
}
//get initial count of replicas --selector=pg-cluster=clusterName
selector := config.LABEL_PG_CLUSTER + "=" + clusterName
replicaList, err := clientset.CrunchydataV1().Pgreplicas(namespace).List(metav1.ListOptions{LabelSelector: selector})
if err != nil {
log.Error(err)
return
}
log.Debugf("replica count before failover is %d", len(replicaList.Items))
//publish event for failover
topics := make([]string, 1)
topics[0] = events.EventTopicCluster
f := events.EventFailoverClusterFormat{
EventHeader: events.EventHeader{
Namespace: namespace,
Username: task.ObjectMeta.Labels[config.LABEL_PGOUSER],
Topic: topics,
Timestamp: time.Now(),
EventType: events.EventFailoverCluster,
},
Clustername: clusterName,
Target: task.ObjectMeta.Labels[config.LABEL_TARGET],
}
err = events.Publish(f)
if err != nil {
log.Error(err)
}
Failover(cluster.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER], clientset, clusterName, task, namespace, restconfig)
//publish event for failover completed
topics = make([]string, 1)
topics[0] = events.EventTopicCluster
g := events.EventFailoverClusterCompletedFormat{
EventHeader: events.EventHeader{
Namespace: namespace,
Username: task.ObjectMeta.Labels[config.LABEL_PGOUSER],
Topic: topics,
Timestamp: time.Now(),
EventType: events.EventFailoverClusterCompleted,
},
Clustername: clusterName,
Target: task.ObjectMeta.Labels[config.LABEL_TARGET],
}
err = events.Publish(g)
if err != nil {
log.Error(err)
}
//remove marker
}
func PatchpgtaskFailoverStatus(clientset pgo.Interface, oldCrd *crv1.Pgtask, namespace string) error {
//change it
oldCrd.Spec.Parameters[config.LABEL_FAILOVER_STARTED] = time.Now().Format(time.RFC3339)
//create the patch
patchBytes, err := json.Marshal(map[string]interface{}{
"spec": map[string]interface{}{
"parameters": oldCrd.Spec.Parameters,
},
})
if err != nil {
return err
}
//apply patch
_, err6 := clientset.CrunchydataV1().Pgtasks(namespace).Patch(oldCrd.Name, types.MergePatchType, patchBytes)
return err6
}