-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb60608
commit cd68f03
Showing
15 changed files
with
247 additions
and
71 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
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package service defines Service object definitions. | ||
package service |
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,11 @@ | ||
apiVersion: n9/v1alpha | ||
kind: Service | ||
metadata: | ||
name: my-service | ||
displayName: My Service | ||
project: default | ||
labels: | ||
team: [ green, orange ] | ||
region: [ eu-central-1 ] | ||
spec: | ||
description: Example Service |
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,53 @@ | ||
package service_test | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/nobl9/nobl9-go/internal/examples" | ||
"github.com/nobl9/nobl9-go/manifest" | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha" | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha/service" | ||
) | ||
|
||
func ExampleService() { | ||
// Create the object: | ||
myService := service.New( | ||
service.Metadata{ | ||
Name: "my-service", | ||
DisplayName: "My Service", | ||
Project: "default", | ||
Labels: v1alpha.Labels{ | ||
"team": []string{"green", "orange"}, | ||
"region": []string{"eu-central-1"}, | ||
}, | ||
}, | ||
service.Spec{ | ||
Description: "Example service", | ||
}, | ||
) | ||
// Verify the object: | ||
if err := myService.Validate(); err != nil { | ||
log.Fatal("service validation failed, err: %w", err) | ||
} | ||
// Apply the object: | ||
client := examples.GetOfflineEchoClient() | ||
if err := client.ApplyObjects(context.Background(), []manifest.Object{myService}, false); err != nil { | ||
log.Fatal("failed to apply service, err: %w", err) | ||
} | ||
// Output: | ||
// apiVersion: n9/v1alpha | ||
// kind: Service | ||
// metadata: | ||
// name: my-service | ||
// displayName: My Service | ||
// project: default | ||
// labels: | ||
// region: | ||
// - eu-central-1 | ||
// team: | ||
// - green | ||
// - orange | ||
// spec: | ||
// description: Example service | ||
} |
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,49 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/nobl9/nobl9-go/manifest" | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha" | ||
) | ||
|
||
//go:generate go run ../../../scripts/generate-object-impl.go Service | ||
|
||
// New creates a new Service based on provided Metadata nad Spec. | ||
func New(metadata Metadata, spec Spec) Service { | ||
return Service{ | ||
APIVersion: manifest.VersionV1alpha.String(), | ||
Kind: manifest.KindService, | ||
Metadata: metadata, | ||
Spec: spec, | ||
} | ||
} | ||
|
||
// Service struct which mapped one to one with kind: service yaml definition | ||
type Service struct { | ||
APIVersion string `json:"apiVersion"` | ||
Kind manifest.Kind `json:"kind"` | ||
Metadata Metadata `json:"metadata"` | ||
Spec Spec `json:"spec"` | ||
Status *Status `json:"status,omitempty"` | ||
|
||
Organization string `json:"organization,omitempty"` | ||
ManifestSource string `json:"manifestSrc,omitempty"` | ||
} | ||
|
||
// Metadata provides identity information for Service. | ||
type Metadata struct { | ||
Name string `json:"name" validate:"required,objectName"` | ||
DisplayName string `json:"displayName,omitempty" validate:"omitempty,min=0,max=63"` | ||
Project string `json:"project,omitempty" validate:"objectName"` | ||
Labels v1alpha.Labels `json:"labels,omitempty" validate:"omitempty,labels"` | ||
} | ||
|
||
// Status holds dynamic fields returned when the Service is fetched from Nobl9 platform. | ||
// Status is not part of the static object definition. | ||
type Status struct { | ||
SloCount int `json:"sloCount"` | ||
} | ||
|
||
// Spec holds detailed information specific to Service. | ||
type Spec struct { | ||
Description string `json:"description" validate:"description" example:"Bleeding edge web app"` | ||
} |
5 changes: 2 additions & 3 deletions
5
manifest/v1alpha/service_object.go → manifest/v1alpha/service/service_object.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Validation for Service 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE' in project 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT' has failed for the following fields: | ||
- 'metadata.name' with value 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE...': | ||
- length must be between 1 and 63 | ||
- a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$') | ||
- 'metadata.displayName' with value 'my-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-service': | ||
- length must be between 0 and 63 | ||
- 'metadata.project' with value 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT...': | ||
- length must be between 1 and 63 | ||
- a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$') | ||
- 'metadata.labels' with value '{"L O L":["dip","dip"]}': | ||
- label key 'L O L' does not match the regex: ^\p{L}([_\-0-9\p{L}]*[0-9\p{L}])?$ | ||
- 'spec.description' with value 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...': | ||
- length must be between 0 and 1050 | ||
Manifest source: /home/me/service.yaml |
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,44 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha" | ||
"github.com/nobl9/nobl9-go/validation" | ||
) | ||
|
||
func validate(s Service) error { | ||
v := validation.RulesForStruct( | ||
validation.RulesForField[string]( | ||
"metadata.name", | ||
func() string { return s.Metadata.Name }, | ||
). | ||
With( | ||
validation.StringRequired(), | ||
validation.StringIsDNSSubdomain()), | ||
validation.RulesForField[string]( | ||
"metadata.displayName", | ||
func() string { return s.Metadata.DisplayName }, | ||
). | ||
With(validation.StringLength(0, 63)), | ||
validation.RulesForField[string]( | ||
"metadata.project", | ||
func() string { return s.Metadata.Project }, | ||
). | ||
With( | ||
validation.StringRequired(), | ||
validation.StringIsDNSSubdomain()), | ||
validation.RulesForField[v1alpha.Labels]( | ||
"metadata.labels", | ||
func() v1alpha.Labels { return s.Metadata.Labels }, | ||
). | ||
With(v1alpha.ValidationRule()), | ||
validation.RulesForField[string]( | ||
"spec.description", | ||
func() string { return s.Spec.Description }, | ||
). | ||
With(validation.StringDescription()), | ||
) | ||
if errs := v.Validate(); len(errs) > 0 { | ||
return v1alpha.NewObjectError(s, errs) | ||
} | ||
return nil | ||
} |
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,34 @@ | ||
package service | ||
|
||
import ( | ||
_ "embed" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/nobl9/nobl9-go/manifest" | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha" | ||
) | ||
|
||
//go:embed test_data/expected_error.txt | ||
var expectedError string | ||
|
||
func TestValidate_AllErrors(t *testing.T) { | ||
err := validate(Service{ | ||
Kind: manifest.KindService, | ||
Metadata: Metadata{ | ||
Name: strings.Repeat("MY SERVICE", 20), | ||
DisplayName: strings.Repeat("my-service", 10), | ||
Project: strings.Repeat("MY PROJECT", 20), | ||
Labels: v1alpha.Labels{ | ||
"L O L": []string{"dip", "dip"}, | ||
}, | ||
}, | ||
Spec: Spec{ | ||
Description: strings.Repeat("l", 2000), | ||
}, | ||
ManifestSource: "/home/me/service.yaml", | ||
}) | ||
assert.Equal(t, expectedError, err.Error()) | ||
} |
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
Oops, something went wrong.