Skip to content

Commit

Permalink
refactor entitystore CRD (#298)
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Ramachandra Sekar <vsekar@nvidia.com>
Co-authored-by: Varun Ramachandra Sekar <vsekar@nvidia.com>
  • Loading branch information
varunrsekar and Varun Ramachandra Sekar authored Jan 22, 2025
1 parent 850b403 commit 549bab6
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 144 deletions.
139 changes: 61 additions & 78 deletions api/apps/v1alpha1/nemo_entitystore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"maps"
"os"
"strconv"

rendertypes "github.com/NVIDIA/k8s-nim-operator/internal/render/types"
utils "github.com/NVIDIA/k8s-nim-operator/internal/utils"
Expand Down Expand Up @@ -51,18 +52,6 @@ const (
NemoEntitystoreStatusFailed = "Failed"
)

// Default values for NEMO entitystore.
const (
defaultDBSecretPasswordKey = "password"
defaultDatabasePort = "5432"
)

// Various error messages for NEMO entitystore.
var (
nilErrorFmtStr = "`%s` cannot be nil"
emptyErrorFmtStr = "`%s` cannot be empty"
)

// NemoEntitystoreSpec defines the desired state of NemoEntitystore
type NemoEntitystoreSpec struct {
Image Image `json:"image,omitempty"`
Expand All @@ -89,19 +78,59 @@ type NemoEntitystoreSpec struct {
UserID *int64 `json:"userID,omitempty"`
GroupID *int64 `json:"groupID,omitempty"`
RuntimeClass string `json:"runtimeClass,omitempty"`
// Database stores the database config for the entity-store service
EntityStoreParams *EntityStoreParams `json:"entityStoreParams,omitempty"`
}

type EntityStoreParams struct {
AppVersion string `json:"appVersion,omitempty"`
DatabaseHost string `json:"databaseHost,omitempty"`
DatabasePort string `json:"databasePort,omitempty"`
DatabaseUser string `json:"databaseUser,omitempty"`
DatabaseName string `json:"databaseName,omitempty"`
DBSecret string `json:"dbSecret,omitempty"`
DBSecretPasswordKey string `json:"dbSecretPasswordKey,omitempty"`
EnvConfigMap string `json:"envConfigMap,omitempty"`
// DatabaseConfig stores the database configuration for NEMO entitystore.
// Required, must not be nil.
//
// +kubebuilder:validation:Required
DatabaseConfig *DatabaseConfig `json:"databaseConfig,omitempty"`
}

type DatabaseConfig struct {
// Host is the hostname of the database.
// Required, must not be empty.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
Host string `json:"host,omitempty"`
// Port is the port where the database is reachable at.
// If specified, this must be a valid port number, 0 < databasePort < 65536.
// Defaults to 5432.
//
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=65535
// +kubebuilder:default:=5432
Port int32 `json:"port,omitempty"`
// DatabaseName is the database name for NEMO EntityStore.
// Required, must not be empty.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
DatabaseName string `json:"databaseName,omitempty"`
// DatabaseCredentials stores the configuration to retrieve the database credentials.
// Required, must not be nil.
//
// +kubebuilder:validation:Required
Credentials *DatabaseCredentials `json:"credentials,omitempty"`
}

type DatabaseCredentials struct {
// User is the non-root username for NEMO EntityStore in the database.
// Required, must not be empty.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
User string `json:"user,omitempty"`
// SecretName is the name of the secret which has the database credentials for the NEMO entitystore user.
// Required, must not be empty.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
SecretName string `json:"secretName,omitempty"`
// PasswordKey is the name of the key in the `CredentialsSecret` secret for the database credentials.
// Defaults to "password".
//
// +kubebuilder:default:="password"
PasswordKey string `json:"passwordKey,omitempty"`
}

// NemoEntitystoreStatus defines the observed state of NemoEntitystore
Expand Down Expand Up @@ -135,52 +164,6 @@ type NemoEntitystoreList struct {
Items []NemoEntitystore `json:"items"`
}

func (n *EntityStoreParams) Normalize() {
if n.DatabasePort == "" {
n.DatabasePort = defaultDatabasePort
}

if n.DBSecretPasswordKey == "" {
n.DBSecretPasswordKey = defaultDBSecretPasswordKey
}
}

func (n *EntityStoreParams) Validate() error {
if n.DatabaseHost == "" {
return fmt.Errorf(emptyErrorFmtStr, "spec.entityStoreParams.databaseHost")
}

if n.DatabaseUser == "" {
return fmt.Errorf(emptyErrorFmtStr, "spec.entityStoreParams.databaseUser")
}

if n.DatabaseName == "" {
return fmt.Errorf(emptyErrorFmtStr, "spec.entityStoreParams.databaseName")
}

if n.DBSecret == "" {
return fmt.Errorf(emptyErrorFmtStr, "spec.entityStoreParams.dbSecret")
}

return nil
}

func (n *NemoEntitystore) Normalize() {
if n.Spec.EntityStoreParams == nil {
return
}

n.Spec.EntityStoreParams.Normalize()
}

func (n *NemoEntitystore) Validate() error {
if n.Spec.EntityStoreParams == nil {
return fmt.Errorf(nilErrorFmtStr, "spec.entityStoreParams")
}

return n.Spec.EntityStoreParams.Validate()
}

// GetPVCName returns the name to be used for the PVC based on the custom spec
// Prefers pvc.Name if explicitly set by the user in the NemoEntitystore instance
func (n *NemoEntitystore) GetPVCName(pvc PersistentVolumeClaim) string {
Expand Down Expand Up @@ -217,34 +200,34 @@ func (n *NemoEntitystore) GetStandardEnv() []corev1.EnvVar {
envVars := []corev1.EnvVar{
{
Name: "APP_VERSION",
Value: n.Spec.EntityStoreParams.AppVersion,
Value: n.Spec.Image.Tag,
},
{
Name: "POSTGRES_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: n.Spec.EntityStoreParams.DBSecretPasswordKey,
Key: n.Spec.DatabaseConfig.Credentials.PasswordKey,
LocalObjectReference: corev1.LocalObjectReference{
Name: n.Spec.EntityStoreParams.DBSecret,
Name: n.Spec.DatabaseConfig.Credentials.SecretName,
},
},
},
},
{
Name: "POSTGRES_USER",
Value: n.Spec.EntityStoreParams.DatabaseUser,
Value: n.Spec.DatabaseConfig.Credentials.User,
},
{
Name: "POSTGRES_HOST",
Value: n.Spec.EntityStoreParams.DatabaseHost,
Value: n.Spec.DatabaseConfig.Host,
},
{
Name: "POSTGRES_PORT",
Value: n.Spec.EntityStoreParams.DatabasePort,
Value: strconv.FormatInt(int64(n.Spec.DatabaseConfig.Port), 10),
},
{
Name: "POSTGRES_DB",
Value: n.Spec.EntityStoreParams.DatabaseName,
Value: n.Spec.DatabaseConfig.DatabaseName,
},
}

Expand Down
36 changes: 28 additions & 8 deletions api/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 55 additions & 16 deletions bundle/manifests/apps.nvidia.com_nemoentitystores.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,64 @@ spec:
items:
type: string
type: array
entityStoreParams:
description: Database stores the database config for the entity-store
service
databaseConfig:
description: |-
DatabaseConfig stores the database configuration for NEMO entitystore.
Required, must not be nil.
properties:
appVersion:
type: string
databaseHost:
type: string
credentials:
description: |-
DatabaseCredentials stores the configuration to retrieve the database credentials.
Required, must not be nil.
properties:
passwordKey:
default: password
description: |-
PasswordKey is the name of the key in the `CredentialsSecret` secret for the database credentials.
Defaults to "password".
type: string
secretName:
description: |-
SecretName is the name of the secret which has the database credentials for the NEMO entitystore user.
Required, must not be empty.
minLength: 1
type: string
user:
description: |-
User is the non-root username for NEMO EntityStore in the database.
Required, must not be empty.
minLength: 1
type: string
required:
- secretName
- user
type: object
databaseName:
description: |-
DatabaseName is the database name for NEMO EntityStore.
Required, must not be empty.
minLength: 1
type: string
databasePort:
type: string
databaseUser:
type: string
dbSecret:
type: string
dbSecretPasswordKey:
type: string
envConfigMap:
host:
description: |-
Host is the hostname of the database.
Required, must not be empty.
minLength: 1
type: string
port:
default: 5432
description: |-
Port is the port where the database is reachable at.
If specified, this must be a valid port number, 0 < databasePort < 65536.
Defaults to 5432.
format: int32
maximum: 65535
minimum: 1
type: integer
required:
- credentials
- databaseName
- host
type: object
env:
items:
Expand Down Expand Up @@ -2120,6 +2158,7 @@ spec:
type: integer
required:
- authSecret
- databaseConfig
type: object
status:
description: NemoEntitystoreStatus defines the observed state of NemoEntitystore
Expand Down
Loading

0 comments on commit 549bab6

Please sign in to comment.