Skip to content

Commit 837f845

Browse files
authored
update kubectl-pg plugin (#1264)
* update kubectl-pg plugin * fix remaining build issues and update go modules * some more README updates
1 parent 17da6bc commit 837f845

File tree

18 files changed

+790
-266
lines changed

18 files changed

+790
-266
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mocks:
8585
GO111MODULE=on go generate ./...
8686

8787
tools:
88-
GO111MODULE=on go get k8s.io/client-go@kubernetes-1.19.3
88+
GO111MODULE=on go get k8s.io/client-go@kubernetes-1.20.2
8989
GO111MODULE=on go get github.com/golang/mock/mockgen@v1.4.4
9090
GO111MODULE=on go mod tidy
9191

@@ -104,4 +104,4 @@ test:
104104
GO111MODULE=on go test ./...
105105

106106
e2e: docker # build operator image to be tested
107-
cd e2e; make e2etest
107+
cd e2e; make e2etest

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ require (
1414
golang.org/x/mod v0.4.0 // indirect
1515
golang.org/x/tools v0.0.0-20201207204333-a835c872fcea // indirect
1616
gopkg.in/yaml.v2 v2.4.0
17-
k8s.io/api v0.19.4
18-
k8s.io/apiextensions-apiserver v0.19.3
19-
k8s.io/apimachinery v0.19.4
20-
k8s.io/client-go v0.19.3
17+
k8s.io/api v0.20.2
18+
k8s.io/apiextensions-apiserver v0.19.4
19+
k8s.io/apimachinery v0.20.2
20+
k8s.io/client-go v0.20.2
2121
k8s.io/code-generator v0.19.4
2222
)

go.sum

Lines changed: 120 additions & 9 deletions
Large diffs are not rendered by default.

kubectl-pg/README.md

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,137 @@
11
# Kubectl Plugin for Zalando's Postgres Operator
22

3-
## Google Summer of Code 2019
4-
5-
This plugin is a prototype developed as a part of GSoC 2019 under the organisation
6-
**The Postgres Operator**
7-
8-
### GSoC Proposal
3+
This plugin is a prototype developed as a part of **Google Summer of Code 2019** under the [Postgres Operator](https://summerofcode.withgoogle.com/archive/2019/organizations/6187982082539520/) organization.
94

10-
[kubectl pg proposal](https://docs.google.com/document/d/1-WMy9HkfZ1XnnMbzplMe9rCzKrRMGaMz4owLVXXPb7w/edit)
5+
## Installation of kubectl pg plugin
116

12-
### Weekly Reports
7+
This project uses Go Modules for dependency management to build locally.
8+
Install go and enable go modules with ```export GO111MODULE=on```.
9+
From Go >=1.13 Go modules will be enabled by default.
1310

14-
https://github.com/VineethReddy02/GSoC-Kubectl-Plugin-for-Postgres-Operator-tracker
15-
16-
### Final Project Report
17-
18-
https://gist.github.com/VineethReddy02/159283bd368a710379eaf0f6bd60a40a
19-
20-
21-
### Installtion of kubectl pg plugin
22-
23-
This project uses Go Modules for dependency management to build locally
24-
Install go and enable go modules ```export GO111MODULE=on```
25-
From Go >=1.13 Go modules will be enabled by default
26-
```
11+
```bash
2712
# Assumes you have a working KUBECONFIG
28-
$ GO111MODULE="on"
29-
# As of now go by default doesn't support Go mods. So explicit enabling is required.
30-
$ GOPATH/src/github.com/zalando/postgres-operator/kubectl-pg go mod vendor
13+
$ GO111MODULE="on"
14+
$ GOPATH/src/github.com/zalando/postgres-operator/kubectl-pg go mod vendor
3115
# This generate a vendor directory with all dependencies needed by the plugin.
3216
$ $GOPATH/src/github.com/zalando/postgres-operator/kubectl-pg go install
3317
# This will place the kubectl-pg binary in your $GOPATH/bin
3418
```
3519

36-
### Before using the kubectl pg plugin make sure to set KUBECONFIG env varibale
20+
## Before using the kubectl pg plugin make sure to set KUBECONFIG env variable
3721

3822
Ideally KUBECONFIG is found in $HOME/.kube/config else specify the KUBECONFIG path here.
23+
```export KUBECONFIG=$HOME/.kube/config```
3924

40-
```export KUBECONFIG=$HOME/.kube/config```
41-
42-
### To list all commands available in kubectl pg
25+
## List all commands available in kubectl pg
4326

4427
```kubectl pg --help``` (or) ```kubectl pg```
4528

46-
### This basically means the operator pod managed to start, so our operator is installed.
29+
## Check if Postgres Operator is installed and running
4730

4831
```kubectl pg check```
4932

50-
### To create postgresql cluster using manifest file
33+
## Create a new cluster using manifest file
5134

5235
```kubectl pg create -f acid-minimal-cluster.yaml```
5336

54-
### To update existing cluster using manifest file
37+
## List postgres resources
38+
39+
```kubectl pg list```
40+
41+
List clusters across namespaces
42+
```kubectl pg list all```
43+
44+
## Update existing cluster using manifest file
5545

5646
```kubectl pg update -f acid-minimal-cluster.yaml```
5747

58-
### To delete existing cluster using manifest file
48+
## Delete existing cluster
5949

50+
Using the manifest file:
6051
```kubectl pg delete -f acid-minimal-cluster.yaml```
6152

62-
### To delete existing cluster using cluster name
63-
53+
Or by specifying the cluster name:
6454
```kubectl pg delete acid-minimal-cluster```
6555

66-
--namespace or -n flag to specify namespace if cluster is in other namespace.
67-
56+
Use `--namespace` or `-n` flag if your cluster is in a different namespace to where your current context is pointing to:
6857
```kubectl pg delete acid-minimal-cluster -n namespace01```
6958

70-
### To list postgres resources for the current namespace
59+
## Adding manifest roles to an existing cluster
7160

72-
```kubectl pg list```
61+
```kubectl pg add-user USER01 -p CREATEDB,LOGIN -c acid-minimal-cluster```
7362

74-
### To list postgres resources across namespaces
63+
Privileges can only be [SUPERUSER, REPLICATION, INHERIT, LOGIN, NOLOGIN, CREATEROLE, CREATEDB, BYPASSURL]
64+
Note: By default, a LOGIN user is created (unless NOLOGIN is specified).
7565

76-
```kubectl pg list all```
66+
## Adding databases to an existing cluster
7767

78-
### To add-user and it's roles to an existing pg cluster
68+
You have to specify an owner of the new database and this role must already exist in the cluster:
69+
```kubectl pg add-db DB01 -o OWNER01 -c acid-minimal-cluster```
7970

80-
```kubectl pg add-user USER01 -p CREATEDB,LOGIN -c acid-minimal-cluster```
71+
## Extend the volume of an existing pg cluster
8172

82-
Privileges can only be [SUPERUSER, REPLICATION, INHERIT, LOGIN, NOLOGIN, CREATEROLE, CREATEDB, BYPASSURL]
73+
```kubectl pg ext-volume 2Gi -c acid-minimal-cluster```
8374

84-
Note: A login user is created by default unless NOLOGIN is specified, in which case the operator creates a role.
75+
## Print the version of Postgres Operator and kubectl pg plugin
8576

86-
### To add-db and it's owner to an existing pg cluster
77+
```kubectl pg version```
8778

88-
```kubectl pg add-db DB01 -o OWNER01 -c acid-minimal-cluster```
79+
## Connect to the shell of a postgres pod
8980

90-
### To extend volume for an existing pg cluster
81+
Connect to the master pod:
82+
```kubectl pg connect -c CLUSTER -m```
9183

92-
```kubectl pg ext-volume 2Gi -c acid-minimal-cluster```
84+
Connect to a random replica pod:
85+
```kubectl pg connect -c CLUSTER```
9386

94-
### To find the version of postgres operator and kubectl plugin
87+
Connect to a certain replica pod:
88+
```kubectl pg connect -c CLUSTER -r 0```
9589

96-
```kubectl pg version (optional -n NAMESPACE allows to know specific to a namespace)```
90+
## Connect to a database via psql
9791

98-
### To connect to the shell of a postgres pod
92+
Adding the `-p` flag allows you to directly connect to a given database with the psql client.
93+
With `-u` you specify the user. If left out the name of the current OS user is taken.
94+
`-d` lets you specify the database. If no database is specified, it will be the same as the user name.
9995

100-
```kubectl pg connect -c CLUSTER``` #This connects to a random pod
101-
```kubectl pg connect -c CLUSTER -m``` #This connects the master
102-
```kubectl pg connect -c CLUSTER -r 0``` #This connects to the desired replica
96+
Connect to `app_db` database on the master with role `app_user`:
97+
```kubectl pg connect -c CLUSTER -m -p -u app_user -d app_db```
10398

104-
### To connect to the psql prompt
99+
Connect to the `postgres` database on a random replica with role `postgres`:
100+
```kubectl pg connect -c CLUSTER -p -u postgres```
105101

106-
```kubectl pg connect -c CLUSTER -p -u username``` #This connects to a random pod. Not master
107-
```kubectl pg connect -c CLUSTER -m -p -u username``` #This connects the master
108-
```kubectl pg connect -c CLUSTER -r 0 -p -u username``` #This connects to the desired replica
102+
Connect to a certain replica assuming name of OS user, database role and name are all the same:
103+
```kubectl pg connect -c CLUSTER -r 0 -p```
109104

110-
Note: -p represents psql prompt
111105

112-
### To get the logs of postgres operator
106+
## Access Postgres Operator logs
113107

114108
```kubectl pg logs -o```
115109

116-
### To get the logs of the postgres cluster
110+
## Access Patroni logs of different database pods
111+
112+
Fetch logs of master:
113+
```kubectl pg logs -c CLUSTER -m```
114+
115+
Fetch logs of a random replica pod:
116+
```kubectl pg logs -c CLUSTER```
117117

118-
```kubectl pg logs -c CLUSTER``` #Fetches the logs of a random pod. Not master
119-
```kubectl pg logs -c CLUSTER -m``` #Fetches the logs of master
120-
```kubectl pg logs -c CLUSTER -r 2``` #Fecthes the logs of specified replica
118+
Fetch logs of specified replica
119+
```kubectl pg logs -c CLUSTER -r 2```
121120

122121
## Development
123122

124-
- When making changes to plugin make sure to change the major or patch version
125-
of plugin in ```build.sh``` and run ```./build.sh```
123+
When making changes to the plugin make sure to change the (major/patch) version of plugin in `build.sh` script and run `./build.sh`.
124+
125+
## Google Summer of Code 2019
126+
127+
### GSoC Proposal
128+
129+
[kubectl pg proposal](https://docs.google.com/document/d/1-WMy9HkfZ1XnnMbzplMe9rCzKrRMGaMz4owLVXXPb7w/edit)
130+
131+
### Weekly Reports
132+
133+
https://github.com/VineethReddy02/GSoC-Kubectl-Plugin-for-Postgres-Operator-tracker
134+
135+
### Final Project Report
136+
137+
https://gist.github.com/VineethReddy02/159283bd368a710379eaf0f6bd60a40a

kubectl-pg/cmd/addDb.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ THE SOFTWARE.
2323
package cmd
2424

2525
import (
26+
"context"
2627
"encoding/json"
2728
"fmt"
29+
"log"
30+
2831
"github.com/spf13/cobra"
2932
PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1"
3033
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3134
"k8s.io/apimachinery/pkg/types"
32-
"log"
3335
)
3436

3537
// addDbCmd represents the addDb command
@@ -62,7 +64,7 @@ func addDb(dbName string, dbOwner string, clusterName string) {
6264
}
6365

6466
namespace := getCurrentNamespace()
65-
postgresql, err := postgresConfig.Postgresqls(namespace).Get(clusterName, metav1.GetOptions{})
67+
postgresql, err := postgresConfig.Postgresqls(namespace).Get(context.TODO(), clusterName, metav1.GetOptions{})
6668
if err != nil {
6769
log.Fatal(err)
6870
}
@@ -84,7 +86,7 @@ func addDb(dbName string, dbOwner string, clusterName string) {
8486
log.Fatal("The provided db-name is reserved by postgres")
8587
}
8688

87-
updatedPostgres, err := postgresConfig.Postgresqls(namespace).Patch(postgresql.Name, types.MergePatchType, patch, "")
89+
updatedPostgres, err := postgresConfig.Postgresqls(namespace).Patch(context.TODO(), postgresql.Name, types.MergePatchType, patch, metav1.PatchOptions{})
8890
if err != nil {
8991
log.Fatal(err)
9092
}

kubectl-pg/cmd/addUser.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ THE SOFTWARE.
2323
package cmd
2424

2525
import (
26+
"context"
2627
"encoding/json"
2728
"fmt"
29+
"log"
30+
"strings"
31+
2832
"github.com/spf13/cobra"
2933
PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1"
3034
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3135
"k8s.io/apimachinery/pkg/types"
32-
"log"
33-
"strings"
3436
)
3537

3638
var allowedPrivileges = []string{"SUPERUSER", "REPLICATION", "INHERIT", "LOGIN", "NOLOGIN", "CREATEROLE", "CREATEDB", "BYPASSURL"}
@@ -90,7 +92,7 @@ func addUser(user string, clusterName string, permissions []string) {
9092
}
9193

9294
namespace := getCurrentNamespace()
93-
postgresql, err := postgresConfig.Postgresqls(namespace).Get(clusterName, metav1.GetOptions{})
95+
postgresql, err := postgresConfig.Postgresqls(namespace).Get(context.TODO(), clusterName, metav1.GetOptions{})
9496
if err != nil {
9597
log.Fatal(err)
9698
}
@@ -114,7 +116,7 @@ func addUser(user string, clusterName string, permissions []string) {
114116
}
115117

116118
patch := applyUserPatch(user, Privileges)
117-
updatedPostgresql, err := postgresConfig.Postgresqls(namespace).Patch(postgresql.Name, types.MergePatchType, patch, "")
119+
updatedPostgresql, err := postgresConfig.Postgresqls(namespace).Patch(context.TODO(), postgresql.Name, types.MergePatchType, patch, metav1.PatchOptions{})
118120
if err != nil {
119121
log.Fatal(err)
120122
}

kubectl-pg/cmd/check.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ THE SOFTWARE.
2323
package cmd
2424

2525
import (
26+
"context"
2627
"fmt"
2728
"log"
2829

@@ -55,7 +56,7 @@ func check() *v1.CustomResourceDefinition {
5556
log.Fatal(err)
5657
}
5758

58-
crdInfo, err := apiExtClient.CustomResourceDefinitions().Get(postgresConstants.PostgresCRDResouceName, metav1.GetOptions{})
59+
crdInfo, err := apiExtClient.CustomResourceDefinitions().Get(context.TODO(), postgresConstants.PostgresCRDResouceName, metav1.GetOptions{})
5960
if err != nil {
6061
log.Fatal(err)
6162
}

kubectl-pg/cmd/create.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ THE SOFTWARE.
2323
package cmd
2424

2525
import (
26+
"context"
2627
"fmt"
28+
"io/ioutil"
29+
"log"
30+
2731
"github.com/spf13/cobra"
2832
v1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
2933
PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1"
30-
"io/ioutil"
3134
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
32-
"log"
35+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3336
)
3437

3538
// createCmd kubectl pg create.
@@ -62,7 +65,7 @@ func create(fileName string) {
6265
}
6366

6467
postgresSql := obj.(*v1.Postgresql)
65-
_, err = postgresConfig.Postgresqls(postgresSql.Namespace).Create(postgresSql)
68+
_, err = postgresConfig.Postgresqls(postgresSql.Namespace).Create(context.TODO(), postgresSql, metav1.CreateOptions{})
6669
if err != nil {
6770
log.Fatal(err)
6871
}

0 commit comments

Comments
 (0)