Documentation
¶
Overview ¶
Package provider manages external target providers for cluster discovery and configuration. It supports provider registration, configuration updates, and resource cleanup.
Index ¶
- Constants
- func CleanupOldResources(ctx context.Context, c client.Client, emptyList client.ObjectList, ...) (int, error)
- func CreateProviderConfigRequest(ctx context.Context, c client.Client, namespace string, name string) (string, error)
- func UpdateProviderConfig(ctx context.Context, c client.Client, ...) error
- type Config
- type CreatedTimeExtractor
- type ProviderRegistration
Examples ¶
Constants ¶
const (
// UUIDLabel is the label key for the UUID
UUIDLabel = "krkn.krkn-chaos.dev/uuid"
)
Variables ¶
This section is empty.
Functions ¶
func CleanupOldResources ¶
func CleanupOldResources( ctx context.Context, c client.Client, emptyList client.ObjectList, namespace string, olderThanSeconds int64, getCreatedTime CreatedTimeExtractor, ) (int, error)
CleanupOldResources deletes all instances of a specific CRD type in a namespace whose Created field is older than the specified number of seconds.
This function is idempotent and safe for concurrent execution by multiple operators. It handles conflicts gracefully (logs warnings but doesn't fail) and never panics.
Parameters:
- ctx: Context for the operation
- c: Kubernetes client
- emptyList: An empty instance of the list type (e.g., &krknv1alpha1.KrknOperatorTargetProviderConfigList{})
- namespace: Namespace to search in
- olderThanSeconds: Age threshold in seconds - resources older than this will be deleted
- getCreatedTime: Function to extract the Created timestamp from an object
Returns:
- deletedCount: Number of resources successfully deleted
- error: Non-nil only for critical errors (listing failures); deletion conflicts are logged but don't cause errors
Example usage:
deletedCount, err := provider.CleanupOldResources(
ctx,
client,
&krknv1alpha1.KrknOperatorTargetProviderConfigList{},
"krkn-operator-system",
3600, // Delete resources older than 1 hour
func(obj client.Object) *metav1.Time {
config := obj.(*krknv1alpha1.KrknOperatorTargetProviderConfig)
return &config.ObjectMeta.CreationTimestamp
},
)
Example (CronJob) ¶
ExampleCleanupOldResources_cronJob demonstrates how to use this in a CronJob controller
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
krknv1alpha1 "github.com/krkn-chaos/krkn-operator/api/v1alpha1"
"github.com/krkn-chaos/krkn-operator/pkg/provider"
)
func main() {
var c client.Client
ctx := context.Background()
// This could be run periodically (e.g., every hour) by a CronJob or controller
cleanupConfigs := func() error {
deletedCount, err := provider.CleanupOldResources(
ctx,
c,
&krknv1alpha1.KrknOperatorTargetProviderConfigList{},
"krkn-operator-system",
7200, // 2 hours
func(obj client.Object) *metav1.Time {
config := obj.(*krknv1alpha1.KrknOperatorTargetProviderConfig)
return &config.ObjectMeta.CreationTimestamp
},
)
if err != nil {
return fmt.Errorf("failed to cleanup configs: %w", err)
}
fmt.Printf("Cleaned up %d old configs\n", deletedCount)
return nil
}
// Run cleanup
if err := cleanupConfigs(); err != nil {
fmt.Printf("Cleanup failed: %v\n", err)
}
}
Output:
Example (ProviderConfig) ¶
ExampleCleanupOldResources demonstrates how to clean up old KrknOperatorTargetProviderConfig resources
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
krknv1alpha1 "github.com/krkn-chaos/krkn-operator/api/v1alpha1"
"github.com/krkn-chaos/krkn-operator/pkg/provider"
)
func main() {
// Assume we have a Kubernetes client
var c client.Client
ctx := context.Background()
namespace := "krkn-operator-system"
// Clean up config requests older than 1 hour (3600 seconds)
deletedCount, err := provider.CleanupOldResources(
ctx,
c,
&krknv1alpha1.KrknOperatorTargetProviderConfigList{},
namespace,
3600, // Delete resources older than 1 hour
func(obj client.Object) *metav1.Time {
config := obj.(*krknv1alpha1.KrknOperatorTargetProviderConfig)
return &config.ObjectMeta.CreationTimestamp
},
)
if err != nil {
fmt.Printf("Error during cleanup: %v\n", err)
return
}
fmt.Printf("Cleaned up %d old config requests\n", deletedCount)
}
Output:
Example (TargetRequest) ¶
ExampleCleanupOldResources_targetRequest demonstrates cleanup for KrknTargetRequest resources
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
krknv1alpha1 "github.com/krkn-chaos/krkn-operator/api/v1alpha1"
"github.com/krkn-chaos/krkn-operator/pkg/provider"
)
func main() {
var c client.Client
ctx := context.Background()
namespace := "krkn-operator-system"
// Clean up target requests older than 24 hours (86400 seconds)
deletedCount, err := provider.CleanupOldResources(
ctx,
c,
&krknv1alpha1.KrknTargetRequestList{},
namespace,
86400, // 24 hours
func(obj client.Object) *metav1.Time {
request := obj.(*krknv1alpha1.KrknTargetRequest)
return &request.ObjectMeta.CreationTimestamp
},
)
if err != nil {
fmt.Printf("Error during cleanup: %v\n", err)
return
}
fmt.Printf("Cleaned up %d old target requests\n", deletedCount)
}
Output:
func CreateProviderConfigRequest ¶
func CreateProviderConfigRequest( ctx context.Context, c client.Client, namespace string, name string, ) (string, error)
CreateProviderConfigRequest creates a new KrknOperatorTargetProviderConfig CR and generates a unique UUID for tracking. The UUID is set in both spec.uuid and as a label for easy selection.
Parameters:
- ctx: Context
- c: Kubernetes client
- namespace: Namespace where the CR will be created
- name: Optional name for the CR (if empty, generates "config-" + UUID prefix)
Returns:
- uuid: The generated UUID for this config request
- error: Error if creation fails
func UpdateProviderConfig ¶
func UpdateProviderConfig( ctx context.Context, c client.Client, config *krknv1alpha1.KrknOperatorTargetProviderConfig, operatorName string, configMapName string, namespace string, jsonSchema string, ) error
UpdateProviderConfig updates a KrknOperatorTargetProviderConfig CR with provider data. This function should be called by each operator's reconcile loop to contribute their configuration schema.
The caller (provider controller) has already fetched the CR in the reconcile loop, so it simply needs to pass the CR object directly.
Parameters:
- ctx: Context
- c: Kubernetes client
- config: The KrknOperatorTargetProviderConfig CR object (already fetched by the reconcile loop)
- operatorName: Name of the provider contributing the data (e.g., "krkn-operator-acm")
- configMapName: Name of the ConfigMap containing the provider's configuration
- namespace: Namespace where the ConfigMap is located
- jsonSchema: JSON schema string for the provider's configuration (must be valid JSON, not base64)
Returns:
- error: Error if update fails or validation fails
Types ¶
type Config ¶
type Config struct {
// ProviderName is the name to register as (e.g., "krkn-operator", "my-custom-operator")
ProviderName string
// HeartbeatInterval is the interval at which the provider heartbeat is updated
// If not set, defaults to 30 seconds
HeartbeatInterval time.Duration
// Namespace is the namespace where the provider CR will be created
Namespace string
}
Config holds the configuration for provider registration
type CreatedTimeExtractor ¶
CreatedTimeExtractor is a function that extracts the Created timestamp from a Kubernetes object
type ProviderRegistration ¶
type ProviderRegistration struct {
// contains filtered or unexported fields
}
ProviderRegistration manages the KrknOperatorTargetProvider CR registration and heartbeat
func NewProviderRegistration ¶
func NewProviderRegistration(c client.Client, namespace string) *ProviderRegistration
NewProviderRegistration creates a new provider registration manager Deprecated: Use NewProviderRegistrationWithConfig instead
func NewProviderRegistrationWithConfig ¶
func NewProviderRegistrationWithConfig(c client.Client, cfg Config) *ProviderRegistration
NewProviderRegistrationWithConfig creates a new provider registration manager with custom configuration
func (*ProviderRegistration) NeedLeaderElection ¶
func (p *ProviderRegistration) NeedLeaderElection() bool
NeedLeaderElection implements manager.LeaderElectionRunnable Provider registration should only run on the leader