Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 37 additions & 39 deletions controllers/artifact/plugin/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func NewPluginReconciler(cl client.Client, scheme *runtime.Scheme, nodeName, nam
artifactManager: artifact.NewManager(cl, namespace),
PluginsConfig: &PluginsConfig{},
nodeName: nodeName,
crToConfigName: make(map[string]string),
}
}

Expand All @@ -63,6 +64,7 @@ type PluginReconciler struct {
artifactManager *artifact.Manager
PluginsConfig *PluginsConfig
nodeName string
crToConfigName map[string]string
}

// Reconcile is part of the main kubernetes reconciliation loop which aims to
Expand Down Expand Up @@ -165,6 +167,7 @@ func (r *PluginReconciler) handleDeletion(ctx context.Context, plugin *artifactv

// Remove the plugin configuration.
r.PluginsConfig.removeConfig(plugin)
delete(r.crToConfigName, plugin.Name)

// Write the updated configuration to the file.
if err := r.removePluginConfig(ctx, plugin); err != nil {
Expand All @@ -191,6 +194,13 @@ func (r *PluginReconciler) handleDeletion(ctx context.Context, plugin *artifactv
func (r *PluginReconciler) ensurePluginConfig(ctx context.Context, plugin *artifactv1alpha1.Plugin) error {
logger := log.FromContext(ctx)
logger.Info("Ensuring plugin configuration")

configName := resolveConfigName(plugin)
if oldName, ok := r.crToConfigName[plugin.Name]; ok && oldName != configName {
r.PluginsConfig.removeByName(oldName)
}
r.crToConfigName[plugin.Name] = configName

r.PluginsConfig.addConfig(plugin)
// Convert the struct to string.
pluginConfigString, err := r.PluginsConfig.toString()
Expand Down Expand Up @@ -248,25 +258,20 @@ type PluginConfig struct {
}

func (p *PluginConfig) isSame(other *PluginConfig) bool {
if p.Name != other.Name {
if p.LibraryPath != other.LibraryPath {
return false
}
if p.OpenParams != other.OpenParams {
return false
}
// Check if the maps are equal.
if len(p.InitConfig) != len(other.InitConfig) {
return false
}
// Check if the keys and values are equal.
for key, value := range p.InitConfig {
if otherValue, ok := other.InitConfig[key]; !ok || value != otherValue {
return false
}
}
if p.LibraryPath != other.LibraryPath {
return false
}
if p.OpenParams != other.OpenParams {
return false
}
return true
}

Expand All @@ -276,13 +281,19 @@ type PluginsConfig struct {
LoadPlugins []string `yaml:"load_plugins,omitempty"`
}

func resolveConfigName(plugin *artifactv1alpha1.Plugin) string {
if plugin.Spec.Config != nil && plugin.Spec.Config.Name != "" {
return plugin.Spec.Config.Name
}
return plugin.Name
}

func (pc *PluginsConfig) addConfig(plugin *artifactv1alpha1.Plugin) {
config := PluginConfig{
LibraryPath: artifact.Path(plugin.Name, priority.DefaultPriority, artifact.MediumOCI, artifact.TypePlugin),
Name: plugin.Name,
}

// If not nil, set the values that are not empty.
if plugin.Spec.Config != nil {
if plugin.Spec.Config.InitConfig != nil {
config.InitConfig = plugin.Spec.Config.InitConfig
Expand All @@ -298,62 +309,49 @@ func (pc *PluginsConfig) addConfig(plugin *artifactv1alpha1.Plugin) {
}
}

// Check if the pluginConfig already exists in the list.
// If an entry with the same name already exists and is identical, skip the update
// to avoid unnecessary writes to the config file mounted in the pod.
for i, c := range pc.Configs {
if c.isSame(&config) {
// Remove the plugin from the list and add the current plugin.
if c.Name == config.Name {
if c.isSame(&config) {
return
}
pc.Configs = append(pc.Configs[:i], pc.Configs[i+1:]...)
break
}
}
pc.Configs = append(pc.Configs, config)

// Add the plugin to the list if it doesn't exist.
if len(pc.Configs) == 0 {
pc.Configs = append(pc.Configs, config)
} else {
found := false
for _, c := range pc.Configs {
if c.Name == plugin.Name {
found = true
break
}
}
if !found {
pc.Configs = append(pc.Configs, config)
}
}

// Check if the plugin is already in the list.
// Add to LoadPlugins if not already present (use config.Name for consistency).
for _, c := range pc.LoadPlugins {
if c == plugin.Name {
if c == config.Name {
return
}
}
pc.LoadPlugins = append(pc.LoadPlugins, plugin.Name)
pc.LoadPlugins = append(pc.LoadPlugins, config.Name)
}

func (pc *PluginsConfig) removeConfig(plugin *artifactv1alpha1.Plugin) {
// Check if the pluginConfig already exists in the list.
pc.removeByName(resolveConfigName(plugin))
}

func (pc *PluginsConfig) removeByName(name string) {
for i, c := range pc.Configs {
if c.Name == plugin.Name {
// Remove the plugin from the list.
if c.Name == name {
pc.Configs = append(pc.Configs[:i], pc.Configs[i+1:]...)
break
}
}

// Check if the plugin is already in the list.
for i, c := range pc.LoadPlugins {
if c == plugin.Name {
// Remove the plugin from the list.
if c == name {
pc.LoadPlugins = append(pc.LoadPlugins[:i], pc.LoadPlugins[i+1:]...)
break
}
}
}

func (pc *PluginsConfig) toString() (string, error) {
// Convert the struct to YAML.
data, err := yaml.Marshal(pc)
if err != nil {
return "", err
Expand Down
Loading