Skip to content

Commit e1ed4b8

Browse files
Use code-generation for CRD API and deepcopy methods (zalando#369)
Client-go provides a https://github.com/kubernetes/code-generator package in order to provide the API to work with CRDs similar to the one available for built-in types, i.e. Pods, Statefulsets and so on. Use this package to generate deepcopy methods (required for CRDs), instead of using an external deepcopy package; we also generate APIs used to manipulate both Postgres and OperatorConfiguration CRDs, as well as informers and listers for the Postgres CRD, instead of using generic informers and CRD REST API; by using generated code we can get rid of some custom and obscure CRD-related code and use a better API. All generated code resides in /pkg/generated, with an exception of zz_deepcopy.go in apis/acid.zalan.do/v1 Rename postgres-operator-configuration CRD to OperatorConfiguration, since the former broke naming convention in the code-generator. Moved Postgresql, PostgresqlList, OperatorConfiguration and OperatorConfigurationList and other types used by them into Change the type of the Error field in the Postgresql crd to a string, so that client-go could generate a deepcopy for it. Use generated code to set status of CRD objects as well. Right now this is done with patch, however, Kubernetes 1.11 introduces the /status subresources, allowing us to set the status with the special updateStatus call in the future. For now, we keep the code that is compatible with earlier versions of Kubernetes. Rename postgresql.go to database.go and status.go to logs_and_api.go to reflect the purpose of each of those files. Update client-go dependencies. Minor reformatting and renaming.
1 parent 6e8dcab commit e1ed4b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3283
-964
lines changed

docs/reference/operator_parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ configuration.
1212

1313
* CRD-based configuration. The configuration is stored in the custom YAML
1414
manifest, an instance of the custom resource definition (CRD) called
15-
`postgresql-operator-configuration`. This CRD is registered by the operator
15+
`OperatorConfiguration`. This CRD is registered by the operator
1616
during the start when `POSTGRES_OPERATOR_CONFIGURATION_OBJECT` variable is
1717
set to a non-empty value. The CRD-based configuration is a regular YAML
1818
document; non-scalar keys are simply represented in the usual YAML way. The

glide.lock

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import:
1111
- package: github.com/lib/pq
1212
- package: github.com/motomux/pretty
1313
- package: k8s.io/apimachinery
14-
version: kubernetes-1.11.1
14+
version: kubernetes-1.11.3-beta.0
1515
- package: k8s.io/apiextensions-apiserver
16-
version: kubernetes-1.11.1
16+
version: kubernetes-1.11.3-beta.0
1717
- package: k8s.io/client-go
18-
version: ^8.0.0
18+
version: kubernetes-1.11.3-beta.0
1919
- package: k8s.io/code-generator
20-
version: kubernetes-1.11.1
20+
version: kubernetes-1.11.3-beta.0
2121
- package: k8s.io/gengo
2222
- package: gopkg.in/yaml.v2
2323
- package: github.com/mohae/deepcopy

hack/custom-boilerplate.go.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright YEAR Compose, Zalando SE
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/

hack/update-codegen.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/..
8+
CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ${GOPATH}/src/k8s.io/code-generator)}
9+
10+
vendor/k8s.io/code-generator/generate-groups.sh all \
11+
github.com/zalando-incubator/postgres-operator/pkg/generated github.com/zalando-incubator/postgres-operator/pkg/apis \
12+
acid.zalan.do:v1 \
13+
--go-header-file ${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt

hack/verify-codegen.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
8+
DIFFROOT="${SCRIPT_ROOT}/pkg"
9+
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg"
10+
_tmp="${SCRIPT_ROOT}/_tmp"
11+
12+
cleanup() {
13+
rm -rf "${_tmp}"
14+
}
15+
trap "cleanup" EXIT SIGINT
16+
17+
cleanup
18+
19+
mkdir -p "${TMP_DIFFROOT}"
20+
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"
21+
22+
"${SCRIPT_ROOT}/hack/update-codegen.sh"
23+
echo "diffing ${DIFFROOT} against freshly generated codegen"
24+
ret=0
25+
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$?
26+
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}"
27+
if [[ $ret -eq 0 ]]
28+
then
29+
echo "${DIFFROOT} up to date."
30+
else
31+
echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh"
32+
exit 1
33+
fi

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apiVersion: "acid.zalan.do/v1"
2-
kind: postgresql-operator-configuration
2+
kind: OperatorConfiguration
33
metadata:
44
name: postgresql-operator-default-configuration
55
configuration:

pkg/apis/acid.zalan.do/register.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package acidzalando
2+
3+
const (
4+
// GroupName is the group name for the operator CRDs
5+
GroupName = "acid.zalan.do"
6+
)

pkg/apis/acid.zalan.do/v1/const.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package v1
2+
3+
const (
4+
serviceNameMaxLength = 63
5+
clusterNameMaxLength = serviceNameMaxLength - len("-repl")
6+
serviceNameRegexString = `^[a-z]([-a-z0-9]*[a-z0-9])?$`
7+
8+
ClusterStatusUnknown PostgresStatus = ""
9+
ClusterStatusCreating PostgresStatus = "Creating"
10+
ClusterStatusUpdating PostgresStatus = "Updating"
11+
ClusterStatusUpdateFailed PostgresStatus = "UpdateFailed"
12+
ClusterStatusSyncFailed PostgresStatus = "SyncFailed"
13+
ClusterStatusAddFailed PostgresStatus = "CreateFailed"
14+
ClusterStatusRunning PostgresStatus = "Running"
15+
ClusterStatusInvalid PostgresStatus = "Invalid"
16+
)

pkg/apis/acid.zalan.do/v1/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// +k8s:deepcopy-gen=package,register
2+
3+
// Package v1 is the v1 version of the API.
4+
// +groupName=acid.zalan.do
5+
6+
package v1

pkg/apis/acid.zalan.do/v1/marshal.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package v1
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
"time"
8+
)
9+
10+
type postgresqlCopy Postgresql
11+
12+
// MarshalJSON converts a maintenance window definition to JSON.
13+
func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) {
14+
if m.Everyday {
15+
return []byte(fmt.Sprintf("\"%s-%s\"",
16+
m.StartTime.Format("15:04"),
17+
m.EndTime.Format("15:04"))), nil
18+
}
19+
20+
return []byte(fmt.Sprintf("\"%s:%s-%s\"",
21+
m.Weekday.String()[:3],
22+
m.StartTime.Format("15:04"),
23+
m.EndTime.Format("15:04"))), nil
24+
}
25+
26+
// UnmarshalJSON converts a JSON to the maintenance window definition.
27+
func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error {
28+
var (
29+
got MaintenanceWindow
30+
err error
31+
)
32+
33+
parts := strings.Split(string(data[1:len(data)-1]), "-")
34+
if len(parts) != 2 {
35+
return fmt.Errorf("incorrect maintenance window format")
36+
}
37+
38+
fromParts := strings.Split(parts[0], ":")
39+
switch len(fromParts) {
40+
case 3:
41+
got.Everyday = false
42+
got.Weekday, err = parseWeekday(fromParts[0])
43+
if err != nil {
44+
return fmt.Errorf("could not parse weekday: %v", err)
45+
}
46+
47+
got.StartTime, err = parseTime(fromParts[1] + ":" + fromParts[2])
48+
case 2:
49+
got.Everyday = true
50+
got.StartTime, err = parseTime(fromParts[0] + ":" + fromParts[1])
51+
default:
52+
return fmt.Errorf("incorrect maintenance window format")
53+
}
54+
if err != nil {
55+
return fmt.Errorf("could not parse start time: %v", err)
56+
}
57+
58+
got.EndTime, err = parseTime(parts[1])
59+
if err != nil {
60+
return fmt.Errorf("could not parse end time: %v", err)
61+
}
62+
63+
if got.EndTime.Before(&got.StartTime) {
64+
return fmt.Errorf("'From' time must be prior to the 'To' time")
65+
}
66+
67+
*m = got
68+
69+
return nil
70+
}
71+
72+
// UnmarshalJSON converts a JSON into the PostgreSQL object.
73+
func (p *Postgresql) UnmarshalJSON(data []byte) error {
74+
var tmp postgresqlCopy
75+
76+
err := json.Unmarshal(data, &tmp)
77+
if err != nil {
78+
metaErr := json.Unmarshal(data, &tmp.ObjectMeta)
79+
if metaErr != nil {
80+
return err
81+
}
82+
83+
tmp.Error = err.Error()
84+
tmp.Status = ClusterStatusInvalid
85+
86+
*p = Postgresql(tmp)
87+
88+
return nil
89+
}
90+
tmp2 := Postgresql(tmp)
91+
92+
if clusterName, err := extractClusterName(tmp2.ObjectMeta.Name, tmp2.Spec.TeamID); err != nil {
93+
tmp2.Error = err.Error()
94+
tmp2.Status = ClusterStatusInvalid
95+
} else if err := validateCloneClusterDescription(&tmp2.Spec.Clone); err != nil {
96+
tmp2.Error = err.Error()
97+
tmp2.Status = ClusterStatusInvalid
98+
} else {
99+
tmp2.Spec.ClusterName = clusterName
100+
}
101+
102+
*p = tmp2
103+
104+
return nil
105+
}
106+
107+
func (d *Duration) UnmarshalJSON(b []byte) error {
108+
var (
109+
v interface{}
110+
err error
111+
)
112+
if err = json.Unmarshal(b, &v); err != nil {
113+
return err
114+
}
115+
switch val := v.(type) {
116+
case string:
117+
t, err := time.ParseDuration(val)
118+
if err != nil {
119+
return err
120+
}
121+
*d = Duration(t)
122+
return nil
123+
case float64:
124+
t := time.Duration(val)
125+
*d = Duration(t)
126+
return nil
127+
default:
128+
return fmt.Errorf("could not recognize type %T as a valid type to unmarshal to Duration", val)
129+
}
130+
}

0 commit comments

Comments
 (0)