Documentation
¶
Overview ¶
Example ¶
Example demonstrates the basic usage of the service plugin system
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/opencenter-cloud/opencenter-cli/internal/services"
)
func main() {
// Create a temporary directory for manifests
tmpDir, _ := os.MkdirTemp("", "services-example")
defer os.RemoveAll(tmpDir)
// Create a simple service manifest
manifest := `name: example-service
version: 1.0.0
type: monitoring
description: Example monitoring service
`
manifestPath := filepath.Join(tmpDir, "example.yaml")
os.WriteFile(manifestPath, []byte(manifest), 0644)
// Create a service registry
registry := services.NewServiceRegistry()
// Load manifests from directory
err := registry.LoadManifestsFromDirectory(tmpDir)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// List all loaded services
serviceList := registry.ListServices()
for _, svc := range serviceList {
fmt.Printf("Loaded: %s v%s (%s)\n", svc.Name, svc.Version, svc.Type)
}
}
Output: Loaded: example-service v1.0.0 (monitoring)
Example (DependencyResolution) ¶
Example_dependencyResolution demonstrates dependency resolution
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/opencenter-cloud/opencenter-cli/internal/services"
)
func main() {
tmpDir, _ := os.MkdirTemp("", "services-deps")
defer os.RemoveAll(tmpDir)
// Create services with dependencies
manifests := map[string]string{
"core.yaml": `name: core
version: 1.0.0
type: core
`,
"storage.yaml": `name: storage
version: 1.0.0
type: storage
dependencies:
- core
`,
"monitoring.yaml": `name: monitoring
version: 1.0.0
type: monitoring
dependencies:
- core
- storage
`,
}
for filename, content := range manifests {
path := filepath.Join(tmpDir, filename)
os.WriteFile(path, []byte(content), 0644)
}
registry := services.NewServiceRegistry()
registry.LoadManifestsFromDirectory(tmpDir)
// Resolve dependencies for monitoring service
resolved, err := registry.ResolveDependencies([]string{"monitoring"})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Print services in dependency order
fmt.Println("Installation order:")
for i, svc := range resolved {
fmt.Printf("%d. %s\n", i+1, svc.Name)
}
}
Output: Installation order: 1. core 2. storage 3. monitoring
Example (Validation) ¶
Example_validation demonstrates dependency validation
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/opencenter-cloud/opencenter-cli/internal/services"
)
func main() {
tmpDir, _ := os.MkdirTemp("", "services-validation")
defer os.RemoveAll(tmpDir)
// Create a service with a missing dependency
manifest := `name: app
version: 1.0.0
type: custom
dependencies:
- missing-service
`
manifestPath := filepath.Join(tmpDir, "app.yaml")
os.WriteFile(manifestPath, []byte(manifest), 0644)
registry := services.NewServiceRegistry()
registry.LoadManifestsFromDirectory(tmpDir)
// Try to validate dependencies
err := registry.ValidateDependencies([]string{"app"})
if err != nil {
fmt.Println("Validation failed: missing dependency")
}
}
Output: Validation failed: missing dependency
Index ¶
- Constants
- func ValidateManifest(manifest *ServicePluginManifest) error
- type BaseServicePlugin
- func (p *BaseServicePlugin) Author() string
- func (p *BaseServicePlugin) Description() string
- func (p *BaseServicePlugin) License() string
- func (p *BaseServicePlugin) Name() string
- func (p *BaseServicePlugin) Render(ctx context.Context, config interface{}, workspace interface{}) error
- func (p *BaseServicePlugin) SetRenderer(renderer func(context.Context, interface{}, interface{}) error)
- func (p *BaseServicePlugin) SetStatusFunc(statusFunc func(interface{}) ServiceStatus)
- func (p *BaseServicePlugin) SetValidator(validator func(interface{}) error)
- func (p *BaseServicePlugin) Status(config interface{}) ServiceStatus
- func (p *BaseServicePlugin) Type() ServiceType
- func (p *BaseServicePlugin) Validate(config interface{}) error
- func (p *BaseServicePlugin) Version() string
- type BasicServicePlugin
- func (p *BasicServicePlugin) Name() string
- func (p *BasicServicePlugin) Render(ctx context.Context, config interface{}, workspace interface{}) error
- func (p *BasicServicePlugin) Status(config interface{}) ServiceStatus
- func (p *BasicServicePlugin) Type() ServiceType
- func (p *BasicServicePlugin) Validate(config interface{}) error
- type DefaultServiceRegistry
- func (r *DefaultServiceRegistry) ExecuteLifecycleHook(ctx context.Context, serviceName string, hook string, config interface{}) error
- func (r *DefaultServiceRegistry) ExecuteLifecycleHooks(ctx context.Context, services []string, hook string, config interface{}) error
- func (r *DefaultServiceRegistry) GetEnabledServices(config interface{}) []ServiceDefinition
- func (r *DefaultServiceRegistry) GetService(name string) (ServiceDefinition, error)
- func (r *DefaultServiceRegistry) GetValidationEngine() *validation.ValidationEngine
- func (r *DefaultServiceRegistry) ListServices() []ServiceDefinition
- func (r *DefaultServiceRegistry) LoadManifestsFromDirectory(dir string) error
- func (r *DefaultServiceRegistry) RegisterFromManifest(manifest *ServicePluginManifest, plugin ServicePlugin) error
- func (r *DefaultServiceRegistry) RegisterService(service ServiceDefinition) error
- func (r *DefaultServiceRegistry) ResolveDependencies(services []string) ([]ServiceDefinition, error)
- func (r *DefaultServiceRegistry) ValidateDependencies(services []string) error
- func (r *DefaultServiceRegistry) ValidateService(ctx context.Context, serviceName string, config interface{}) (*validation.ValidationResult, error)
- type PluginMetadata
- type ServiceConfig
- type ServiceDefinition
- type ServiceLifecycle
- type ServiceMetadata
- type ServicePlugin
- type ServicePluginManifest
- type ServiceRegistry
- type ServiceStatus
- type ServiceType
- type TemplateRef
- type ValidationRule
Examples ¶
Constants ¶
const ( LifecycleHookPreInstall = "PreInstall" LifecycleHookPostInstall = "PostInstall" LifecycleHookPreUpdate = "PreUpdate" LifecycleHookPostUpdate = "PostUpdate" LifecycleHookPreRemove = "PreRemove" LifecycleHookPostRemove = "PostRemove" )
Variables ¶
This section is empty.
Functions ¶
func ValidateManifest ¶
func ValidateManifest(manifest *ServicePluginManifest) error
ValidateManifest validates a service plugin manifest
Types ¶
type BaseServicePlugin ¶
type BaseServicePlugin struct {
// contains filtered or unexported fields
}
BaseServicePlugin provides common functionality for all service plugins using composition pattern. Plugins can embed this struct to inherit boilerplate methods and inject custom validation/rendering logic.
func NewBasePlugin ¶
func NewBasePlugin(metadata PluginMetadata) *BaseServicePlugin
NewBasePlugin creates a new base plugin with the given metadata
func (*BaseServicePlugin) Author ¶
func (p *BaseServicePlugin) Author() string
Author returns the plugin author
func (*BaseServicePlugin) Description ¶
func (p *BaseServicePlugin) Description() string
Description returns the plugin description
func (*BaseServicePlugin) License ¶
func (p *BaseServicePlugin) License() string
License returns the plugin license
func (*BaseServicePlugin) Name ¶
func (p *BaseServicePlugin) Name() string
Name returns the plugin name
func (*BaseServicePlugin) Render ¶
func (p *BaseServicePlugin) Render(ctx context.Context, config interface{}, workspace interface{}) error
Render delegates to the injected renderer function
func (*BaseServicePlugin) SetRenderer ¶
func (p *BaseServicePlugin) SetRenderer(renderer func(context.Context, interface{}, interface{}) error)
SetRenderer allows plugins to inject custom rendering logic
func (*BaseServicePlugin) SetStatusFunc ¶
func (p *BaseServicePlugin) SetStatusFunc(statusFunc func(interface{}) ServiceStatus)
SetStatusFunc allows plugins to inject custom status logic
func (*BaseServicePlugin) SetValidator ¶
func (p *BaseServicePlugin) SetValidator(validator func(interface{}) error)
SetValidator allows plugins to inject custom validation logic
func (*BaseServicePlugin) Status ¶
func (p *BaseServicePlugin) Status(config interface{}) ServiceStatus
Status delegates to the injected status function
func (*BaseServicePlugin) Type ¶
func (p *BaseServicePlugin) Type() ServiceType
Type returns the service type
func (*BaseServicePlugin) Validate ¶
func (p *BaseServicePlugin) Validate(config interface{}) error
Validate delegates to the injected validator function
func (*BaseServicePlugin) Version ¶
func (p *BaseServicePlugin) Version() string
Version returns the plugin version
type BasicServicePlugin ¶
type BasicServicePlugin struct {
// contains filtered or unexported fields
}
BasicServicePlugin is a basic implementation of ServicePlugin for testing
func (*BasicServicePlugin) Name ¶
func (p *BasicServicePlugin) Name() string
func (*BasicServicePlugin) Render ¶
func (p *BasicServicePlugin) Render(ctx context.Context, config interface{}, workspace interface{}) error
func (*BasicServicePlugin) Status ¶
func (p *BasicServicePlugin) Status(config interface{}) ServiceStatus
func (*BasicServicePlugin) Type ¶
func (p *BasicServicePlugin) Type() ServiceType
func (*BasicServicePlugin) Validate ¶
func (p *BasicServicePlugin) Validate(config interface{}) error
type DefaultServiceRegistry ¶
type DefaultServiceRegistry struct {
// contains filtered or unexported fields
}
DefaultServiceRegistry is the default implementation of ServiceRegistry
func (*DefaultServiceRegistry) ExecuteLifecycleHook ¶
func (r *DefaultServiceRegistry) ExecuteLifecycleHook(ctx context.Context, serviceName string, hook string, config interface{}) error
ExecuteLifecycleHook executes a lifecycle hook for a specific service
func (*DefaultServiceRegistry) ExecuteLifecycleHooks ¶
func (r *DefaultServiceRegistry) ExecuteLifecycleHooks(ctx context.Context, services []string, hook string, config interface{}) error
ExecuteLifecycleHooks executes a lifecycle hook for multiple services in dependency order
func (*DefaultServiceRegistry) GetEnabledServices ¶
func (r *DefaultServiceRegistry) GetEnabledServices(config interface{}) []ServiceDefinition
GetEnabledServices returns all enabled services for a configuration
func (*DefaultServiceRegistry) GetService ¶
func (r *DefaultServiceRegistry) GetService(name string) (ServiceDefinition, error)
GetService retrieves a service definition by name
func (*DefaultServiceRegistry) GetValidationEngine ¶
func (r *DefaultServiceRegistry) GetValidationEngine() *validation.ValidationEngine
GetValidationEngine returns the validation engine used by the registry
func (*DefaultServiceRegistry) ListServices ¶
func (r *DefaultServiceRegistry) ListServices() []ServiceDefinition
ListServices returns all registered services
func (*DefaultServiceRegistry) LoadManifestsFromDirectory ¶
func (r *DefaultServiceRegistry) LoadManifestsFromDirectory(dir string) error
LoadManifestsFromDirectory loads service manifests from a directory
func (*DefaultServiceRegistry) RegisterFromManifest ¶
func (r *DefaultServiceRegistry) RegisterFromManifest(manifest *ServicePluginManifest, plugin ServicePlugin) error
RegisterFromManifest registers a service from a manifest
func (*DefaultServiceRegistry) RegisterService ¶
func (r *DefaultServiceRegistry) RegisterService(service ServiceDefinition) error
RegisterService registers a service definition
func (*DefaultServiceRegistry) ResolveDependencies ¶
func (r *DefaultServiceRegistry) ResolveDependencies(services []string) ([]ServiceDefinition, error)
ResolveDependencies resolves service dependencies in correct order
func (*DefaultServiceRegistry) ValidateDependencies ¶
func (r *DefaultServiceRegistry) ValidateDependencies(services []string) error
ValidateDependencies validates that all dependencies are available
func (*DefaultServiceRegistry) ValidateService ¶
func (r *DefaultServiceRegistry) ValidateService(ctx context.Context, serviceName string, config interface{}) (*validation.ValidationResult, error)
ValidateService validates a service configuration using the ValidationEngine
type PluginMetadata ¶
type PluginMetadata struct {
Name string // Plugin identifier (e.g., "cert-manager")
Version string // Plugin version (e.g., "1.0.0")
Description string // Human-readable description
Type ServiceType // Plugin category: core, monitoring, logging, storage, networking, security, gitops, custom
Author string // Plugin author (e.g., "opencenter")
License string // License identifier (e.g., "Apache-2.0")
}
PluginMetadata contains all standard plugin metadata
type ServiceConfig ¶
type ServiceConfig struct {
Schema map[string]interface{} `yaml:"schema" json:"schema"`
Defaults map[string]interface{} `yaml:"defaults" json:"defaults"`
Required []string `yaml:"required" json:"required"`
Validation []ValidationRule `yaml:"validation" json:"validation"`
}
ServiceConfig defines the configuration schema for a service
type ServiceDefinition ¶
type ServiceDefinition struct {
Name string
Type ServiceType
Version string
Description string
Dependencies []string
Templates []TemplateRef
Plugin ServicePlugin
Lifecycle ServiceLifecycle
Metadata ServiceMetadata
}
ServiceDefinition defines a complete service with its plugin and metadata
func (*ServiceDefinition) ExecuteLifecycleHook ¶
func (s *ServiceDefinition) ExecuteLifecycleHook(ctx context.Context, hook string, config interface{}) error
ExecuteLifecycleHook executes a specific lifecycle hook if it's defined
type ServiceLifecycle ¶
type ServiceLifecycle struct {
PreInstall func(ctx context.Context, config interface{}) error
PostInstall func(ctx context.Context, config interface{}) error
PreUpdate func(ctx context.Context, config interface{}) error
PostUpdate func(ctx context.Context, config interface{}) error
PreRemove func(ctx context.Context, config interface{}) error
PostRemove func(ctx context.Context, config interface{}) error
}
ServiceLifecycle defines lifecycle hooks for a service
type ServiceMetadata ¶
type ServiceMetadata struct {
Author string `json:"author,omitempty"`
Homepage string `json:"homepage,omitempty"`
Repository string `json:"repository,omitempty"`
License string `json:"license,omitempty"`
Tags []string `json:"tags,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}
ServiceMetadata contains additional metadata about a service
type ServicePlugin ¶
type ServicePlugin interface {
// Name returns the unique name of the service
Name() string
// Type returns the service type
Type() ServiceType
// Validate validates the service configuration
Validate(config interface{}) error
// Render renders the service templates to the workspace
Render(ctx context.Context, config interface{}, workspace interface{}) error
// Status returns the current status of the service
Status(config interface{}) ServiceStatus
}
ServicePlugin defines the interface that all service plugins must implement
type ServicePluginManifest ¶
type ServicePluginManifest struct {
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
Type ServiceType `yaml:"type" json:"type"`
Description string `yaml:"description" json:"description"`
Dependencies []string `yaml:"dependencies" json:"dependencies"`
Templates []TemplateRef `yaml:"templates" json:"templates"`
Config ServiceConfig `yaml:"config" json:"config"`
Metadata map[string]string `yaml:"metadata" json:"metadata"`
}
ServicePluginManifest defines the structure of a service plugin manifest file
func LoadManifest ¶
func LoadManifest(path string) (*ServicePluginManifest, error)
LoadManifest loads a service plugin manifest from a file
func LoadManifestsFromDirectory ¶
func LoadManifestsFromDirectory(dir string) ([]*ServicePluginManifest, error)
LoadManifestsFromDirectory loads all service plugin manifests from a directory
type ServiceRegistry ¶
type ServiceRegistry interface {
// RegisterService registers a service definition
RegisterService(service ServiceDefinition) error
// RegisterFromManifest registers a service from a manifest
RegisterFromManifest(manifest *ServicePluginManifest, plugin ServicePlugin) error
// GetService retrieves a service definition by name
GetService(name string) (ServiceDefinition, error)
// GetEnabledServices returns all enabled services for a configuration
GetEnabledServices(config interface{}) []ServiceDefinition
// ResolveDependencies resolves service dependencies in correct order
ResolveDependencies(services []string) ([]ServiceDefinition, error)
// ValidateDependencies validates that all dependencies are available
ValidateDependencies(services []string) error
// ListServices returns all registered services
ListServices() []ServiceDefinition
// LoadManifestsFromDirectory loads service manifests from a directory
LoadManifestsFromDirectory(dir string) error
// ExecuteLifecycleHook executes a lifecycle hook for a specific service
ExecuteLifecycleHook(ctx context.Context, serviceName string, hook string, config interface{}) error
// ExecuteLifecycleHooks executes a lifecycle hook for multiple services in dependency order
ExecuteLifecycleHooks(ctx context.Context, services []string, hook string, config interface{}) error
// GetValidationEngine returns the validation engine used by the registry
GetValidationEngine() *validation.ValidationEngine
// ValidateService validates a service configuration using the ValidationEngine
ValidateService(ctx context.Context, serviceName string, config interface{}) (*validation.ValidationResult, error)
}
ServiceRegistry manages service definitions and plugins
func NewServiceRegistry ¶
func NewServiceRegistry() ServiceRegistry
NewServiceRegistry creates a new service registry
func NewServiceRegistryWithEngine ¶
func NewServiceRegistryWithEngine(engine *validation.ValidationEngine) ServiceRegistry
NewServiceRegistryWithEngine creates a new service registry with a custom validation engine
type ServiceStatus ¶
type ServiceStatus struct {
State string `json:"state"` // pending, running, success, failed
Message string `json:"message"` // Status message
Details map[string]interface{} `json:"details"` // Additional status details
}
ServiceStatus represents the status of a service
type ServiceType ¶
type ServiceType string
ServiceType represents the type of service
const ( ServiceTypeCore ServiceType = "core" ServiceTypeMonitoring ServiceType = "monitoring" ServiceTypeLogging ServiceType = "logging" ServiceTypeStorage ServiceType = "storage" ServiceTypeNetworking ServiceType = "networking" ServiceTypeSecurity ServiceType = "security" ServiceTypeGitOps ServiceType = "gitops" ServiceTypeCustom ServiceType = "custom" )
type TemplateRef ¶
type TemplateRef struct {
Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"`
Condition map[string]string `yaml:"condition,omitempty" json:"condition,omitempty"`
}
TemplateRef references a template used by the service
type ValidationRule ¶
type ValidationRule struct {
Field string `yaml:"field" json:"field"`
Type string `yaml:"type" json:"type"`
Message string `yaml:"message" json:"message"`
Operator string `yaml:"operator,omitempty" json:"operator,omitempty"`
Value string `yaml:"value,omitempty" json:"value,omitempty"`
}
ValidationRule defines a validation rule for service configuration