package thinclient import ( "context" corev1 "k8s.io/api/core/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/config" "edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/plugins/thinclient/configobject" "edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/plugins/thinclient/selector" "edge-infra.dev/pkg/sds/lib/os/file" "edge-infra.dev/pkg/sds/lib/os/passthrough" ) type ConfigMapWatcherPlugin struct{} func (configMapWatcherPlugin ConfigMapWatcherPlugin) Reconcile(ctx context.Context, object client.Object, conf config.Config) error { configMap, ok := object.(*corev1.ConfigMap) if !ok { return nil } configObject := configobject.NewFromConfigMap(*configMap) return updateObject(ctx, configObject, conf) } type SecretWatcherPlugin struct{} func (secretWatcherPlugin SecretWatcherPlugin) Reconcile(ctx context.Context, object client.Object, conf config.Config) error { secret, ok := object.(*corev1.Secret) if !ok { return nil } configObject := configobject.NewFromSecret(*secret) return updateObject(ctx, configObject, conf) } func updateObject(ctx context.Context, configObject configobject.ConfigObject, conf config.Config) error { log := ctrl.LoggerFrom(ctx) selector, err := getSelectorForConfigObject(ctx, configObject, conf) if err != nil { log.Error(err, "failed to get selector for Secret") return err } else if selector == nil { return nil // not a target } rootPath, err := conf.GetRootPath(ctx) if err != nil { log.Error(err, "failed to get host root path") return err } if err = configObject.Update(selector.Directory, selector.Service, file.New(), passthrough.New(rootPath)); err != nil { log.Error(err, "failed to update Secret") return err } return nil } func getSelectorForConfigObject(ctx context.Context, configObject configobject.ConfigObject, conf config.Config) (*selector.Selector, error) { thinclientConfiguration, err := conf.GetThinclientConfigMap(ctx) if err != nil { return nil, err } selectors, err := getSelectorsFromThinclientConfiguration(thinclientConfiguration) if err != nil { return nil, err } selectors, err = selectors.FilterByObjectType(configObject.ObjectType()) if err != nil { return nil, err } selector, err := selectors.FindSelectorForConfigObject(configObject) if err != nil { return nil, err } return selector, nil }