Skip to content

Commit

Permalink
Refactor core schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Dec 4, 2023
1 parent e793d75 commit ae8af40
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 118 deletions.
63 changes: 58 additions & 5 deletions blueprints/minimal/cue.mod/pkg/timoni.sh/core/v1alpha1/image.cue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package v1alpha1

import "strings"
import (
"encoding/base64"
"strings"
)

// Image defines the schema for OCI image reference used in Kubernetes PodSpec container image.
#Image: {
Expand All @@ -22,14 +25,14 @@ import "strings"
// Spec: https://github.com/opencontainers/image-spec/blob/main/descriptor.md#digests.
digest!: string

// Reference is the image address computed from repository, tag and digest
// in the format [REPOSITORY]:[TAG]@[DIGEST].
reference: string

// PullPolicy defines the pull policy for the image.
// By default, it is set to IfNotPresent.
pullPolicy: *"IfNotPresent" | "Always" | "Never"

// Reference is the image address computed from repository, tag and digest
// in the format [REPOSITORY]:[TAG]@[DIGEST].
reference: string

if digest != "" && tag != "" {
reference: "\(repository):\(tag)@\(digest)"
}
Expand All @@ -46,3 +49,53 @@ import "strings"
reference: "\(repository):latest"
}
}

// ImagePullSecret is a generator for Kubernetes Secrets of type kubernetes.io/dockerconfigjson.
// Spec: https://kubernetes.io/docs/concepts/configuration/secret/#docker-config-secrets.
#ImagePullSecret: {
// Metadata is the Kubernetes object's metadata generated by Timoni.
meta=metadata: #Metadata

// Registry is the hostname of the container registry in the format [HOST[:PORT_NUMBER]].
registry!: string

// Username is the username used to authenticate to the container registry.
username!: string

// Password is the password used to authenticate to the container registry.
password!: string

// Optional suffix used to generate the Secret name.
suffix: *"" | string

let auth = base64.Encode(null, username+":"+password)

// The object is a read-only struct that contains the generated
// Kubernetes Secret of type kubernetes.io/dockerconfigjson.
object: {
apiVersion: "v1"
kind: "Secret"
type: "kubernetes.io/dockerconfigjson"
metadata: {
name: meta.name + suffix
namespace: meta.namespace
labels: meta.labels
if meta.annotations != _|_ {
annotations: meta.annotations
}
}
stringData: {
".dockerconfigjson": #"""
{
"auths": {
"\#(registry)": {
"username": "\#(username)",
"password": "\#(password)",
"auth": "\#(auth)"
}
}
}
"""#
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 Stefan Prodan
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import "strings"

// InstanceName defines the schema for the name of a Timoni instance.
// The instance name is used as a Kubernetes label value and must be 63 characters or less.
#InstanceName: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MinRunes(1) & strings.MaxRunes(63)

// InstanceNamespace defines the schema for the namespace of a Timoni instance.
// The instance namespace is used as a Kubernetes label value and must be 63 characters or less.
#InstanceNamespace: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MinRunes(1) & strings.MaxRunes(63)

// InstanceOwnerReference defines the schema for Kubernetes labels used to denote ownership.
#InstanceOwnerReference: {
#Name: "instance.timoni.sh/name"
#Namespace: "instance.timoni.sh/namespace"
}

// InstanceModule defines the schema for the Module of a Timoni instance.
#InstanceModule: {
url: string & =~"^((oci|file)://.*)$"
version: *"latest" | string
digest?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package v1alpha1
import "strings"

// Annotations defines the schema for Kubernetes object metadata annotations.
#Annotations: {[string & =~"^(([A-Za-z0-9][-A-Za-z0-9_./]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)]: string}
#Annotations: {[string & strings.MaxRunes(253)]: string}

// Labels defines the schema for Kubernetes object metadata labels.
#Labels: {[string & =~"^(([A-Za-z0-9][-A-Za-z0-9_./]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)]: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)}
#Labels: {[string & strings.MaxRunes(253)]: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)}

#StdLabelName: "app.kubernetes.io/name"
#StdLabelVersion: "app.kubernetes.io/version"
Expand All @@ -26,11 +26,11 @@ import "strings"
// Name must be unique within a namespace. Is required when creating resources.
// Name is primarily intended for creation idempotence and configuration definition.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names
name!: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)
name!: #InstanceName

// Namespace defines the space within which each name must be unique.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces
namespace!: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)
namespace!: #InstanceNamespace

// Annotations is an unstructured key value map stored with a resource that may be
// set to store and retrieve arbitrary metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
// MemoryQuantity is a string that is validated as a quantity of memory, such as 128Mi or 2Gi.
#MemoryQuantity: string & =~"^[1-9]\\d*(Mi|Gi)$"

// ResourceRequirement describes the CPU and Memory resource requirements.
// ResourceRequirement defines the schema for the CPU and Memory resource requirements.
#ResourceRequirement: {
cpu?: #CPUQuantity
memory?: #MemoryQuantity
}

// ResourceRequirements describes the compute resource requirements.
// ResourceRequirements defines the schema for the compute resource requirements of a container.
// More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/.
#ResourceRequirements: {
// Limits describes the maximum amount of compute resources allowed.
Expand Down
80 changes: 0 additions & 80 deletions blueprints/minimal/cue.mod/pkg/timoni.sh/core/v1alpha1/secrets.cue

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

package v1alpha1

import "strings"

// Selector defines the schema for Kubernetes Pod label selector used in Deployments, Services, Jobs, etc.
#Selector: {
// Name must be unique within a namespace. Is required when creating resources.
// Name is primarily intended for creation idempotence and configuration definition.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names
#Name!: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MinRunes(1) & strings.MaxRunes(63)
#Name!: #InstanceName

// Map of string keys and values that can be used to organize and categorize (scope and select) objects.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels
Expand Down
29 changes: 29 additions & 0 deletions blueprints/minimal/cue.mod/pkg/timoni.sh/core/v1alpha1/semver.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Stefan Prodan
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"strconv"
"strings"
)

// SemVer validates the input version string and extracts the major and minor version numbers.
// When Minimum is set, the major and minor parts must be greater or equal to the minimum
// or a validation error is returned.
#SemVer: {
// Input version string in strict semver format.
#Version!: string & =~"^\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"

// Minimum is the minimum allowed MAJOR.MINOR version.
#Minimum: *"0.0.0" | string & =~"^\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"

let minMajor = strconv.Atoi(strings.Split(#Minimum, ".")[0])
let minMinor = strconv.Atoi(strings.Split(#Minimum, ".")[1])

major: int & >=minMajor
major: strconv.Atoi(strings.Split(#Version, ".")[0])

minor: int & >=minMinor
minor: strconv.Atoi(strings.Split(#Version, ".")[1])
}
23 changes: 0 additions & 23 deletions blueprints/minimal/cue.mod/pkg/timoni.sh/core/v1alpha1/version.cue

This file was deleted.

2 changes: 1 addition & 1 deletion blueprints/minimal/templates/config.cue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
kubeVersion!: string
// Using the kubeVersion you can enforce a minimum Kubernetes minor version.
// By default, the minimum Kubernetes version is set to 1.20.
clusterVersion: timoniv1.#KubernetesVersion & {#Version: kubeVersion, #Minimum: "1.20"}
clusterVersion: timoniv1.#SemVer & {#Version: kubeVersion, #Minimum: "1.20.0"}

// The moduleVersion is set from the user-supplied module version.
// This field is used for the `app.kubernetes.io/version` label.
Expand Down

0 comments on commit ae8af40

Please sign in to comment.