Skip to content

Commit

Permalink
Merge pull request #1 from cloudnatix/junm/apiextensions-v1
Browse files Browse the repository at this point in the history
Switch to apiextensions/v1.
  • Loading branch information
junm-cloudnatix authored May 23, 2023
2 parents d2b43a4 + c0ea9a3 commit 57162a7
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 289 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17.0-alpine AS builder
FROM golang:1.20.0-alpine AS builder

WORKDIR /app
RUN apk --no-cache add git make
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ all: mod test build
mod:
$(GO) mod download
tools:
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
test:
$(GO) test ./... -v
lint:
Expand Down
200 changes: 103 additions & 97 deletions crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

v1 "k8s.io/api/core/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -16,7 +16,7 @@ import (

const (
CRDPlural string = "databases"
CRDGroup string = "k8s.io"
CRDGroup string = "cloudnatix.com" // TODO(junm): fix this later.
CRDVersion string = "v1"
FullCRDName string = "databases." + CRDGroup
StorageTypePattern string = `gp2|io1`
Expand All @@ -32,108 +32,114 @@ func floatptr(x float64) *float64 {
return &x
}

func NewDatabaseCRD() *apiextv1beta1.CustomResourceDefinition {
return &apiextv1beta1.CustomResourceDefinition{
func NewDatabaseCRD() *apiextv1.CustomResourceDefinition {
return &apiextv1.CustomResourceDefinition{
ObjectMeta: meta_v1.ObjectMeta{Name: FullCRDName},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Group: CRDGroup,
Version: CRDVersion,
Scope: apiextv1beta1.NamespaceScoped,
Names: apiextv1beta1.CustomResourceDefinitionNames{
Plural: "databases",
Kind: "Database",
},
Validation: &apiextv1beta1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1beta1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextv1beta1.JSONSchemaProps{
"spec": {
Spec: apiextv1.CustomResourceDefinitionSpec{
Group: CRDGroup,
Versions: []apiextv1.CustomResourceDefinitionVersion{
{
Name: CRDVersion,
Served: true,
Storage: true,
Schema: &apiextv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextv1beta1.JSONSchemaProps{
"username": {
Type: "string",
Description: "User Name to access the database",
MinLength: intptr(1),
MaxLength: intptr(16),
Pattern: DBUsernamePattern,
},
"dbname": {
Type: "string",
Description: "Database name",
MinLength: intptr(1),
MaxLength: intptr(63),
Pattern: DBNamePattern,
},
"engine": {
Type: "string",
Description: "database engine. Ex: postgres, mysql, aurora-postgresql, etc",
},
"version": {
Type: "string",
Description: "database engine version. ex 5.1.49",
},
"class": {
Type: "string",
Description: "instance class name. Ex: db.m5.24xlarge or db.m3.medium",
},
"size": {
Type: "integer",
Description: "Database size in Gb",
Minimum: floatptr(20),
Maximum: floatptr(64000),
},
"MaxAllocatedSize": {
Type: "integer",
Description: "Database size in Gb",
Minimum: floatptr(20),
Maximum: floatptr(64000),
},
"multiaz": {
Type: "boolean",
Description: "should it be available in multiple regions?",
},
"publiclyaccessible": {
Type: "boolean",
Description: "is the database publicly accessible?",
},
"storageencrypted": {
Type: "boolean",
Description: "should the storage be encrypted?",
},
"storagetype": {
Type: "string",
Description: "gp2 (General Purpose SSD) or io1 (Provisioned IOPS SSD)",
Pattern: StorageTypePattern,
},
"iops": {
Type: "integer",
Description: "I/O operations per second",
Minimum: floatptr(1000),
Maximum: floatptr(80000),
},
"backupretentionperiod": {
Type: "integer",
Description: "Retention period in days. 0 means disabled, 7 is the default and 35 is the maximum",
Minimum: floatptr(0),
Maximum: floatptr(35),
},
"deleteprotection": {
Type: "boolean",
Description: "Enable or disable deletion protection",
},
"tags": {
Type: "string",
Description: "Tags to create on the database instance format key=value,key1=value1",
},
"skipfinalsnapshot": {
Type: "boolean",
Description: "Indicates whether to skip the creation of a final DB snapshot before deleting the instance. By default, skipfinalsnapshot isn't enabled, and the DB snapshot is created.",
Properties: map[string]apiextv1.JSONSchemaProps{
"spec": {
Type: "object",
Properties: map[string]apiextv1.JSONSchemaProps{
"username": {
Type: "string",
Description: "User Name to access the database",
MinLength: intptr(1),
MaxLength: intptr(16),
Pattern: DBUsernamePattern,
},
"dbname": {
Type: "string",
Description: "Database name",
MinLength: intptr(1),
MaxLength: intptr(63),
Pattern: DBNamePattern,
},
"engine": {
Type: "string",
Description: "database engine. Ex: postgres, mysql, aurora-postgresql, etc",
},
"version": {
Type: "string",
Description: "database engine version. ex 5.1.49",
},
"class": {
Type: "string",
Description: "instance class name. Ex: db.m5.24xlarge or db.m3.medium",
},
"size": {
Type: "integer",
Description: "Database size in Gb",
Minimum: floatptr(20),
Maximum: floatptr(64000),
},
"MaxAllocatedSize": {
Type: "integer",
Description: "Database size in Gb",
Minimum: floatptr(20),
Maximum: floatptr(64000),
},
"multiaz": {
Type: "boolean",
Description: "should it be available in multiple regions?",
},
"publiclyaccessible": {
Type: "boolean",
Description: "is the database publicly accessible?",
},
"storageencrypted": {
Type: "boolean",
Description: "should the storage be encrypted?",
},
"storagetype": {
Type: "string",
Description: "gp2 (General Purpose SSD) or io1 (Provisioned IOPS SSD)",
Pattern: StorageTypePattern,
},
"iops": {
Type: "integer",
Description: "I/O operations per second",
Minimum: floatptr(1000),
Maximum: floatptr(80000),
},
"backupretentionperiod": {
Type: "integer",
Description: "Retention period in days. 0 means disabled, 7 is the default and 35 is the maximum",
Minimum: floatptr(0),
Maximum: floatptr(35),
},
"deleteprotection": {
Type: "boolean",
Description: "Enable or disable deletion protection",
},
"tags": {
Type: "string",
Description: "Tags to create on the database instance format key=value,key1=value1",
},
"skipfinalsnapshot": {
Type: "boolean",
Description: "Indicates whether to skip the creation of a final DB snapshot before deleting the instance. By default, skipfinalsnapshot isn't enabled, and the DB snapshot is created.",
},
},
},
},
},
},
},
},
Scope: apiextv1.NamespaceScoped,
Names: apiextv1.CustomResourceDefinitionNames{
Plural: "databases",
Kind: "Database",
},
},
}
}
Expand All @@ -142,7 +148,7 @@ func NewDatabaseCRD() *apiextv1beta1.CustomResourceDefinition {
func CreateCRD(clientset apiextcs.Interface) error {
ctx := context.Background()
crd := NewDatabaseCRD()
_, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(ctx, crd, meta_v1.CreateOptions{})
_, err := clientset.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, meta_v1.CreateOptions{})
if err != nil && apierrors.IsAlreadyExists(err) {
return nil
}
Expand Down
25 changes: 12 additions & 13 deletions crd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestCRDValidationWithValidInput(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand All @@ -71,18 +71,17 @@ func TestCaseInsensitiveInput(t *testing.T) {
yamlFile, err := ioutil.ReadFile("test.yaml")
assert.NoError(t, err)
db := Database{}
err = yaml.Unmarshal(yamlFile,&db)
err = yaml.Unmarshal(yamlFile, &db)
assert.NoError(t, err)
assert.Equal(t, int(db.Spec.MaxAllocatedSize), 200, "they should be equal")
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(db)

result, err := gojsonschema.Validate(loader, documentLoader)
assert.NoError(t, err)
assert.True(t, result.Valid(), result.Errors())
}


func TestDatabaseSizeIsTooSmall(t *testing.T) {
d := Database{
ObjectMeta: meta_v1.ObjectMeta{Name: "my_db", Namespace: "default"},
Expand All @@ -104,7 +103,7 @@ func TestDatabaseSizeIsTooSmall(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -133,7 +132,7 @@ func TestDatabaseSizeIsTooBig(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -162,7 +161,7 @@ func TestBackupRetentionPeriodIsTooLong(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -191,7 +190,7 @@ func TestInvalidDatabaseNameWithDashSeparator(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -220,7 +219,7 @@ func TestInvalidDatabaseNameStartingWithANumber(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -249,7 +248,7 @@ func TestInvalidUsername(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -278,7 +277,7 @@ func TestInvalidStorageType(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -307,7 +306,7 @@ func TestIopsTooSmall(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down Expand Up @@ -336,7 +335,7 @@ func TestIopsTooBig(t *testing.T) {
},
}

loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Validation.OpenAPIV3Schema)
loader := gojsonschema.NewGoLoader(NewDatabaseCRD().Spec.Versions[0].Schema.OpenAPIV3Schema)
documentLoader := gojsonschema.NewGoLoader(d)

result, err := gojsonschema.Validate(loader, documentLoader)
Expand Down
Loading

0 comments on commit 57162a7

Please sign in to comment.