Skip to content

Commit d19fcbc

Browse files
author
Jeff McCormick
authored
Merge pull request #370 from jasonodonnell/develop
Add scheduler integration
2 parents 0b1570f + ef8b155 commit d19fcbc

File tree

26 files changed

+1459
-177
lines changed

26 files changed

+1459
-177
lines changed

apiserver.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ package main
1818
import (
1919
"crypto/tls"
2020
"crypto/x509"
21+
"io/ioutil"
22+
"net/http"
23+
"os"
24+
"strconv"
25+
2126
log "github.com/Sirupsen/logrus"
2227
"github.com/crunchydata/postgres-operator/apiserver"
2328
"github.com/crunchydata/postgres-operator/apiserver/backrestservice"
@@ -34,15 +39,12 @@ import (
3439
"github.com/crunchydata/postgres-operator/apiserver/policyservice"
3540
"github.com/crunchydata/postgres-operator/apiserver/pvcservice"
3641
"github.com/crunchydata/postgres-operator/apiserver/reloadservice"
42+
"github.com/crunchydata/postgres-operator/apiserver/scheduleservice"
3743
"github.com/crunchydata/postgres-operator/apiserver/statusservice"
3844
"github.com/crunchydata/postgres-operator/apiserver/upgradeservice"
3945
"github.com/crunchydata/postgres-operator/apiserver/userservice"
4046
"github.com/crunchydata/postgres-operator/apiserver/versionservice"
4147
"github.com/gorilla/mux"
42-
"io/ioutil"
43-
"net/http"
44-
"os"
45-
"strconv"
4648
)
4749

4850
const serverCert = "/config/server.crt"
@@ -128,6 +130,11 @@ func main() {
128130
r.HandleFunc("/pgpool", pgpoolservice.CreatePgpoolHandler).Methods("POST")
129131
r.HandleFunc("/pgpooldelete", pgpoolservice.DeletePgpoolHandler).Methods("POST")
130132

133+
//schedule
134+
r.HandleFunc("/schedule", scheduleservice.CreateScheduleHandler).Methods("POST")
135+
r.HandleFunc("/scheduledelete", scheduleservice.DeleteScheduleHandler).Methods("POST")
136+
r.HandleFunc("/scheduleshow", scheduleservice.ShowScheduleHandler).Methods("POST")
137+
131138
caCert, err := ioutil.ReadFile(serverCert)
132139
if err != nil {
133140
log.Fatal(err)

apiserver/clusterservice/clusterimpl.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ import (
1919
"database/sql"
2020
"errors"
2121
"fmt"
22+
2223
log "github.com/Sirupsen/logrus"
2324
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
2425
"github.com/crunchydata/postgres-operator/apiserver"
2526
//"github.com/crunchydata/postgres-operator/apiserver/pvcservice"
27+
"strconv"
28+
"strings"
29+
"time"
30+
2631
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
2732
"github.com/crunchydata/postgres-operator/config"
2833
"github.com/crunchydata/postgres-operator/kubeapi"
@@ -31,13 +36,10 @@ import (
3136
"k8s.io/api/core/v1"
3237
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3338
"k8s.io/apimachinery/pkg/util/validation"
34-
"strconv"
35-
"strings"
36-
"time"
3739
)
3840

3941
// DeleteCluster ...
40-
func DeleteCluster(name, selector string, deleteData, deleteBackups bool) msgs.DeleteClusterResponse {
42+
func DeleteCluster(name, selector string, deleteData, deleteBackups, deleteConfigs bool) msgs.DeleteClusterResponse {
4143
var err error
4244

4345
response := msgs.DeleteClusterResponse{}
@@ -51,6 +53,7 @@ func DeleteCluster(name, selector string, deleteData, deleteBackups bool) msgs.D
5153
}
5254
log.Debugf("delete-data is [%v]\n", deleteData)
5355
log.Debugf("delete-backups is [%v]\n", deleteBackups)
56+
log.Debugf("delete-configs is [%v]\n", deleteConfigs)
5457

5558
clusterList := crv1.PgclusterList{}
5659

@@ -87,6 +90,14 @@ func DeleteCluster(name, selector string, deleteData, deleteBackups bool) msgs.D
8790
}
8891
}
8992

93+
if deleteConfigs {
94+
if err := deleteConfigMaps(cluster.Spec.Name); err != nil {
95+
response.Status.Code = msgs.Error
96+
response.Status.Msg = err.Error()
97+
return response
98+
}
99+
}
100+
90101
err := kubeapi.Deletepgcluster(apiserver.RESTClient,
91102
cluster.Spec.Name, apiserver.Namespace)
92103
if err != nil {
@@ -1071,11 +1082,9 @@ func validateBackrestConfig(labels map[string]string) error {
10711082
log.Debugf("%s was not found", util.GLOBAL_CUSTOM_CONFIGMAP)
10721083
return errors.New(util.GLOBAL_CUSTOM_CONFIGMAP + " global configmap or --custom-config flag not set, one of these is required for enabling pgbackrest")
10731084
}
1074-
10751085
}
10761086
}
10771087
return err
1078-
10791088
}
10801089

10811090
// deleteDatabaseSecrets delete secrets that match pg-database=somecluster
@@ -1098,3 +1107,19 @@ func deleteDatabaseSecrets(db string) error {
10981107
}
10991108
return err
11001109
}
1110+
1111+
func deleteConfigMaps(clusterName string) error {
1112+
label := fmt.Sprintf("pg-cluster=%s", clusterName)
1113+
list, ok := kubeapi.ListConfigMap(apiserver.Clientset, label, apiserver.Namespace)
1114+
if !ok {
1115+
return fmt.Errorf("No configMaps found for selector: %s", label)
1116+
}
1117+
1118+
for _, configmap := range list.Items {
1119+
err := kubeapi.DeleteConfigMap(apiserver.Clientset, configmap.Name, apiserver.Namespace)
1120+
if err != nil {
1121+
return err
1122+
}
1123+
}
1124+
return nil
1125+
}

apiserver/clusterservice/clusterservice.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ limitations under the License.
1717

1818
import (
1919
"encoding/json"
20+
"net/http"
21+
"strconv"
22+
2023
log "github.com/Sirupsen/logrus"
2124
"github.com/crunchydata/postgres-operator/apiserver"
2225
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
2326
"github.com/gorilla/mux"
24-
"net/http"
25-
"strconv"
2627
)
2728

2829
// TestResults ...
@@ -151,6 +152,13 @@ func DeleteClusterHandler(w http.ResponseWriter, r *http.Request) {
151152
deleteBackups, _ = strconv.ParseBool(deleteBackupsStr)
152153
}
153154

155+
deleteConfigs := false
156+
deleteConfigsStr := r.URL.Query().Get("delete-configs")
157+
if deleteDataStr != "" {
158+
log.Debug("delete-configs param was [" + deleteConfigsStr + "]")
159+
deleteConfigs, _ = strconv.ParseBool(deleteConfigsStr)
160+
}
161+
154162
err := apiserver.Authn(apiserver.DELETE_CLUSTER_PERM, w, r)
155163
if err != nil {
156164
return
@@ -168,7 +176,7 @@ func DeleteClusterHandler(w http.ResponseWriter, r *http.Request) {
168176
resp.Status = msgs.Status{Code: msgs.Error, Msg: apiserver.VERSION_MISMATCH_ERROR}
169177
resp.Results = make([]string, 0)
170178
} else {
171-
resp = DeleteCluster(clustername, selector, deleteData, deleteBackups)
179+
resp = DeleteCluster(clustername, selector, deleteData, deleteBackups, deleteConfigs)
172180
}
173181
json.NewEncoder(w).Encode(resp)
174182

apiserver/perms.go

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,57 @@ limitations under the License.
1818
import (
1919
"bufio"
2020
"errors"
21-
log "github.com/Sirupsen/logrus"
2221
"os"
2322
"strings"
23+
24+
log "github.com/Sirupsen/logrus"
2425
)
2526

26-
const RESTORE_PERM = "Restore"
27-
const SHOW_SECRETS_PERM = "ShowSecrets"
28-
const RELOAD_PERM = "Reload"
29-
const SHOW_CONFIG_PERM = "ShowConfig"
27+
// MISC
28+
const APPLY_POLICY_PERM = "ApplyPolicy"
3029
const DF_CLUSTER_PERM = "DfCluster"
31-
const SHOW_CLUSTER_PERM = "ShowCluster"
32-
const CREATE_CLUSTER_PERM = "CreateCluster"
33-
const TEST_CLUSTER_PERM = "TestCluster"
34-
const DELETE_CLUSTER_PERM = "DeleteCluster"
35-
const SHOW_BACKUP_PERM = "ShowBackup"
36-
const CREATE_BACKUP_PERM = "CreateBackup"
37-
const DELETE_BACKUP_PERM = "DeleteBackup"
3830
const LABEL_PERM = "Label"
3931
const LOAD_PERM = "Load"
40-
const CREATE_POLICY_PERM = "CreatePolicy"
41-
const DELETE_POLICY_PERM = "DeletePolicy"
42-
const SHOW_POLICY_PERM = "ShowPolicy"
43-
const APPLY_POLICY_PERM = "ApplyPolicy"
44-
const SHOW_PVC_PERM = "ShowPVC"
45-
const CREATE_UPGRADE_PERM = "CreateUpgrade"
46-
const SHOW_UPGRADE_PERM = "ShowUpgrade"
47-
const DELETE_UPGRADE_PERM = "DeleteUpgrade"
32+
const RELOAD_PERM = "Reload"
33+
const RESTORE_PERM = "Restore"
34+
const STATUS_PERM = "Status"
35+
const TEST_CLUSTER_PERM = "TestCluster"
4836
const USER_PERM = "User"
49-
const CREATE_USER_PERM = "CreateUser"
50-
const DELETE_USER_PERM = "DeleteUser"
5137
const VERSION_PERM = "Version"
52-
const CREATE_INGEST_PERM = "CreateIngest"
53-
const SHOW_INGEST_PERM = "ShowIngest"
54-
const DELETE_INGEST_PERM = "DeleteIngest"
38+
39+
// CREATE
40+
const CREATE_BACKUP_PERM = "CreateBackup"
41+
const CREATE_CLUSTER_PERM = "CreateCluster"
5542
const CREATE_FAILOVER_PERM = "CreateFailover"
56-
const STATUS_PERM = "Status"
43+
const CREATE_INGEST_PERM = "CreateIngest"
5744
const CREATE_PGBOUNCER_PERM = "CreatePgbouncer"
58-
const DELETE_PGBOUNCER_PERM = "DeletePgbouncer"
5945
const CREATE_PGPOOL_PERM = "CreatePgpool"
46+
const CREATE_POLICY_PERM = "CreatePolicy"
47+
const CREATE_SCHEDULE_PERM = "CreateSchedule"
48+
const CREATE_UPGRADE_PERM = "CreateUpgrade"
49+
const CREATE_USER_PERM = "CreateUser"
50+
51+
// DELETE
52+
const DELETE_BACKUP_PERM = "DeleteBackup"
53+
const DELETE_CLUSTER_PERM = "DeleteCluster"
54+
const DELETE_INGEST_PERM = "DeleteIngest"
55+
const DELETE_PGBOUNCER_PERM = "DeletePgbouncer"
6056
const DELETE_PGPOOL_PERM = "DeletePgpool"
57+
const DELETE_POLICY_PERM = "DeletePolicy"
58+
const DELETE_SCHEDULE_PERM = "DeleteSchedule"
59+
const DELETE_UPGRADE_PERM = "DeleteUpgrade"
60+
const DELETE_USER_PERM = "DeleteUser"
61+
62+
// SHOW
63+
const SHOW_BACKUP_PERM = "ShowBackup"
64+
const SHOW_CLUSTER_PERM = "ShowCluster"
65+
const SHOW_CONFIG_PERM = "ShowConfig"
66+
const SHOW_INGEST_PERM = "ShowIngest"
67+
const SHOW_POLICY_PERM = "ShowPolicy"
68+
const SHOW_PVC_PERM = "ShowPVC"
69+
const SHOW_SCHEDULE_PERM = "ShowSchedule"
70+
const SHOW_SECRETS_PERM = "ShowSecrets"
71+
const SHOW_UPGRADE_PERM = "ShowUpgrade"
6172

6273
var RoleMap map[string]map[string]string
6374
var PermMap map[string]string
@@ -68,42 +79,49 @@ func InitializePerms() {
6879
PermMap = make(map[string]string)
6980
RoleMap = make(map[string]map[string]string)
7081

71-
PermMap[SHOW_SECRETS_PERM] = "yes"
82+
// MISC
83+
PermMap[APPLY_POLICY_PERM] = "yes"
84+
PermMap[DF_CLUSTER_PERM] = "yes"
85+
PermMap[LABEL_PERM] = "yes"
86+
PermMap[LOAD_PERM] = "yes"
7287
PermMap[RELOAD_PERM] = "yes"
73-
PermMap[SHOW_CONFIG_PERM] = "yes"
88+
PermMap[RESTORE_PERM] = "yes"
7489
PermMap[STATUS_PERM] = "yes"
75-
PermMap[DF_CLUSTER_PERM] = "yes"
76-
PermMap[SHOW_CLUSTER_PERM] = "yes"
77-
PermMap[CREATE_CLUSTER_PERM] = "yes"
78-
PermMap[DELETE_CLUSTER_PERM] = "yes"
7990
PermMap[TEST_CLUSTER_PERM] = "yes"
80-
PermMap[SHOW_BACKUP_PERM] = "yes"
91+
PermMap[USER_PERM] = "yes"
92+
PermMap[VERSION_PERM] = "yes"
93+
// Create
8194
PermMap[CREATE_BACKUP_PERM] = "yes"
82-
PermMap[DELETE_BACKUP_PERM] = "yes"
83-
PermMap[LABEL_PERM] = "yes"
84-
PermMap[LOAD_PERM] = "yes"
95+
PermMap[CREATE_CLUSTER_PERM] = "yes"
96+
PermMap[CREATE_FAILOVER_PERM] = "yes"
97+
PermMap[CREATE_INGEST_PERM] = "yes"
98+
PermMap[CREATE_PGBOUNCER_PERM] = "yes"
99+
PermMap[CREATE_PGPOOL_PERM] = "yes"
85100
PermMap[CREATE_POLICY_PERM] = "yes"
86-
PermMap[DELETE_POLICY_PERM] = "yes"
87-
PermMap[SHOW_POLICY_PERM] = "yes"
88-
PermMap[APPLY_POLICY_PERM] = "yes"
89-
PermMap[SHOW_PVC_PERM] = "yes"
101+
PermMap[CREATE_SCHEDULE_PERM] = "yes"
90102
PermMap[CREATE_UPGRADE_PERM] = "yes"
91-
PermMap[SHOW_UPGRADE_PERM] = "yes"
92-
PermMap[DELETE_UPGRADE_PERM] = "yes"
93-
PermMap[USER_PERM] = "yes"
94103
PermMap[CREATE_USER_PERM] = "yes"
95-
PermMap[DELETE_USER_PERM] = "yes"
96-
PermMap[VERSION_PERM] = "yes"
97-
PermMap[CREATE_INGEST_PERM] = "yes"
98-
PermMap[SHOW_INGEST_PERM] = "yes"
104+
// Delete
105+
PermMap[DELETE_BACKUP_PERM] = "yes"
106+
PermMap[DELETE_CLUSTER_PERM] = "yes"
99107
PermMap[DELETE_INGEST_PERM] = "yes"
100-
PermMap[CREATE_FAILOVER_PERM] = "yes"
101-
PermMap[RESTORE_PERM] = "yes"
102-
PermMap[CREATE_PGBOUNCER_PERM] = "yes"
103108
PermMap[DELETE_PGBOUNCER_PERM] = "yes"
104-
PermMap[CREATE_PGPOOL_PERM] = "yes"
105109
PermMap[DELETE_PGPOOL_PERM] = "yes"
106-
log.Infof("loading PermMap with %d Permissions", len(PermMap))
110+
PermMap[DELETE_POLICY_PERM] = "yes"
111+
PermMap[DELETE_SCHEDULE_PERM] = "yes"
112+
PermMap[DELETE_UPGRADE_PERM] = "yes"
113+
PermMap[DELETE_USER_PERM] = "yes"
114+
// Show
115+
PermMap[SHOW_BACKUP_PERM] = "yes"
116+
PermMap[SHOW_CLUSTER_PERM] = "yes"
117+
PermMap[SHOW_CONFIG_PERM] = "yes"
118+
PermMap[SHOW_INGEST_PERM] = "yes"
119+
PermMap[SHOW_POLICY_PERM] = "yes"
120+
PermMap[SHOW_PVC_PERM] = "yes"
121+
PermMap[SHOW_SCHEDULE_PERM] = "yes"
122+
PermMap[SHOW_SECRETS_PERM] = "yes"
123+
PermMap[SHOW_UPGRADE_PERM] = "yes"
124+
log.Infof("loading PermMap with %d Permissions\n", len(PermMap))
107125

108126
readRoles()
109127
}

0 commit comments

Comments
 (0)