forked from zalando/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
6e8dcab
commit e1ed4b8
Showing
66 changed files
with
3,283 additions
and
964 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
Copyright YEAR Compose, Zalando SE | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/.. | ||
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)} | ||
|
||
vendor/k8s.io/code-generator/generate-groups.sh all \ | ||
github.com/zalando-incubator/postgres-operator/pkg/generated github.com/zalando-incubator/postgres-operator/pkg/apis \ | ||
acid.zalan.do:v1 \ | ||
--go-header-file ${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/.. | ||
DIFFROOT="${SCRIPT_ROOT}/pkg" | ||
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg" | ||
_tmp="${SCRIPT_ROOT}/_tmp" | ||
|
||
cleanup() { | ||
rm -rf "${_tmp}" | ||
} | ||
trap "cleanup" EXIT SIGINT | ||
|
||
cleanup | ||
|
||
mkdir -p "${TMP_DIFFROOT}" | ||
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}" | ||
|
||
"${SCRIPT_ROOT}/hack/update-codegen.sh" | ||
echo "diffing ${DIFFROOT} against freshly generated codegen" | ||
ret=0 | ||
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$? | ||
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}" | ||
if [[ $ret -eq 0 ]] | ||
then | ||
echo "${DIFFROOT} up to date." | ||
else | ||
echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package acidzalando | ||
|
||
const ( | ||
// GroupName is the group name for the operator CRDs | ||
GroupName = "acid.zalan.do" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package v1 | ||
|
||
const ( | ||
serviceNameMaxLength = 63 | ||
clusterNameMaxLength = serviceNameMaxLength - len("-repl") | ||
serviceNameRegexString = `^[a-z]([-a-z0-9]*[a-z0-9])?$` | ||
|
||
ClusterStatusUnknown PostgresStatus = "" | ||
ClusterStatusCreating PostgresStatus = "Creating" | ||
ClusterStatusUpdating PostgresStatus = "Updating" | ||
ClusterStatusUpdateFailed PostgresStatus = "UpdateFailed" | ||
ClusterStatusSyncFailed PostgresStatus = "SyncFailed" | ||
ClusterStatusAddFailed PostgresStatus = "CreateFailed" | ||
ClusterStatusRunning PostgresStatus = "Running" | ||
ClusterStatusInvalid PostgresStatus = "Invalid" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// +k8s:deepcopy-gen=package,register | ||
|
||
// Package v1 is the v1 version of the API. | ||
// +groupName=acid.zalan.do | ||
|
||
package v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type postgresqlCopy Postgresql | ||
|
||
// MarshalJSON converts a maintenance window definition to JSON. | ||
func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) { | ||
if m.Everyday { | ||
return []byte(fmt.Sprintf("\"%s-%s\"", | ||
m.StartTime.Format("15:04"), | ||
m.EndTime.Format("15:04"))), nil | ||
} | ||
|
||
return []byte(fmt.Sprintf("\"%s:%s-%s\"", | ||
m.Weekday.String()[:3], | ||
m.StartTime.Format("15:04"), | ||
m.EndTime.Format("15:04"))), nil | ||
} | ||
|
||
// UnmarshalJSON converts a JSON to the maintenance window definition. | ||
func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error { | ||
var ( | ||
got MaintenanceWindow | ||
err error | ||
) | ||
|
||
parts := strings.Split(string(data[1:len(data)-1]), "-") | ||
if len(parts) != 2 { | ||
return fmt.Errorf("incorrect maintenance window format") | ||
} | ||
|
||
fromParts := strings.Split(parts[0], ":") | ||
switch len(fromParts) { | ||
case 3: | ||
got.Everyday = false | ||
got.Weekday, err = parseWeekday(fromParts[0]) | ||
if err != nil { | ||
return fmt.Errorf("could not parse weekday: %v", err) | ||
} | ||
|
||
got.StartTime, err = parseTime(fromParts[1] + ":" + fromParts[2]) | ||
case 2: | ||
got.Everyday = true | ||
got.StartTime, err = parseTime(fromParts[0] + ":" + fromParts[1]) | ||
default: | ||
return fmt.Errorf("incorrect maintenance window format") | ||
} | ||
if err != nil { | ||
return fmt.Errorf("could not parse start time: %v", err) | ||
} | ||
|
||
got.EndTime, err = parseTime(parts[1]) | ||
if err != nil { | ||
return fmt.Errorf("could not parse end time: %v", err) | ||
} | ||
|
||
if got.EndTime.Before(&got.StartTime) { | ||
return fmt.Errorf("'From' time must be prior to the 'To' time") | ||
} | ||
|
||
*m = got | ||
|
||
return nil | ||
} | ||
|
||
// UnmarshalJSON converts a JSON into the PostgreSQL object. | ||
func (p *Postgresql) UnmarshalJSON(data []byte) error { | ||
var tmp postgresqlCopy | ||
|
||
err := json.Unmarshal(data, &tmp) | ||
if err != nil { | ||
metaErr := json.Unmarshal(data, &tmp.ObjectMeta) | ||
if metaErr != nil { | ||
return err | ||
} | ||
|
||
tmp.Error = err.Error() | ||
tmp.Status = ClusterStatusInvalid | ||
|
||
*p = Postgresql(tmp) | ||
|
||
return nil | ||
} | ||
tmp2 := Postgresql(tmp) | ||
|
||
if clusterName, err := extractClusterName(tmp2.ObjectMeta.Name, tmp2.Spec.TeamID); err != nil { | ||
tmp2.Error = err.Error() | ||
tmp2.Status = ClusterStatusInvalid | ||
} else if err := validateCloneClusterDescription(&tmp2.Spec.Clone); err != nil { | ||
tmp2.Error = err.Error() | ||
tmp2.Status = ClusterStatusInvalid | ||
} else { | ||
tmp2.Spec.ClusterName = clusterName | ||
} | ||
|
||
*p = tmp2 | ||
|
||
return nil | ||
} | ||
|
||
func (d *Duration) UnmarshalJSON(b []byte) error { | ||
var ( | ||
v interface{} | ||
err error | ||
) | ||
if err = json.Unmarshal(b, &v); err != nil { | ||
return err | ||
} | ||
switch val := v.(type) { | ||
case string: | ||
t, err := time.ParseDuration(val) | ||
if err != nil { | ||
return err | ||
} | ||
*d = Duration(t) | ||
return nil | ||
case float64: | ||
t := time.Duration(val) | ||
*d = Duration(t) | ||
return nil | ||
default: | ||
return fmt.Errorf("could not recognize type %T as a valid type to unmarshal to Duration", val) | ||
} | ||
} |
Oops, something went wrong.