forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanza.go
87 lines (71 loc) · 2.66 KB
/
stanza.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
package backrest
/*
Copyright 2019 - 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 (
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
"github.com/crunchydata/postgres-operator/config"
"github.com/crunchydata/postgres-operator/kubeapi"
log "github.com/sirupsen/logrus"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func StanzaCreate(namespace, clusterName string, clientset *kubernetes.Clientset, RESTClient *rest.RESTClient) {
taskName := clusterName + "-" + crv1.PgtaskBackrestStanzaCreate
//look up the backrest-repo pod name
selector := config.LABEL_PG_CLUSTER + "=" + clusterName + "," + config.LABEL_PGO_BACKREST_REPO + "=true"
pods, err := kubeapi.GetPods(clientset, selector, namespace)
if len(pods.Items) != 1 {
log.Errorf("pods len != 1 for cluster %s", clusterName)
return
}
if err != nil {
log.Error(err)
return
}
podName := pods.Items[0].Name
// get the cluster to determine the proper storage type
cluster := crv1.Pgcluster{}
_, err = kubeapi.Getpgcluster(RESTClient, &cluster,
clusterName, namespace)
if err != nil {
return
}
//create the stanza-create task
spec := crv1.PgtaskSpec{}
spec.Name = taskName
jobName := clusterName + "-" + crv1.PgtaskBackrestStanzaCreate
spec.TaskType = crv1.PgtaskBackrest
spec.Parameters = make(map[string]string)
spec.Parameters[config.LABEL_JOB_NAME] = jobName
spec.Parameters[config.LABEL_PG_CLUSTER] = clusterName
spec.Parameters[config.LABEL_POD_NAME] = podName
spec.Parameters[config.LABEL_CONTAINER_NAME] = "pgo-backrest-repo"
spec.Parameters[config.LABEL_BACKREST_COMMAND] = crv1.PgtaskBackrestStanzaCreate
spec.Parameters[config.LABEL_BACKREST_OPTS] = ""
spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE] = cluster.Spec.UserLabels[config.LABEL_BACKREST_STORAGE_TYPE]
newInstance := &crv1.Pgtask{
ObjectMeta: meta_v1.ObjectMeta{
Name: taskName,
},
Spec: spec,
}
newInstance.ObjectMeta.Labels = make(map[string]string)
newInstance.ObjectMeta.Labels[config.LABEL_PG_CLUSTER] = clusterName
err = kubeapi.Createpgtask(RESTClient,
newInstance,
namespace)
if err != nil {
log.Error(err)
}
}