2021-11-08 17:47:10 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
2024-12-30 08:56:41 +00:00
|
|
|
"errors"
|
2021-11-08 17:47:10 +00:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
2025-01-10 21:14:00 +00:00
|
|
|
"github.com/argoproj/argo-cd/v3/common"
|
|
|
|
|
"github.com/argoproj/argo-cd/v3/reposerver/apiclient"
|
|
|
|
|
configUtil "github.com/argoproj/argo-cd/v3/util/config"
|
2021-11-08 17:47:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ConfigManagementPluginKind string = "ConfigManagementPlugin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PluginConfig struct {
|
|
|
|
|
metav1.TypeMeta `json:",inline"`
|
|
|
|
|
Metadata metav1.ObjectMeta `json:"metadata"`
|
|
|
|
|
Spec PluginConfigSpec `json:"spec"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PluginConfigSpec struct {
|
2023-03-22 16:15:19 +00:00
|
|
|
Version string `json:"version"`
|
|
|
|
|
Init Command `json:"init,omitempty"`
|
|
|
|
|
Generate Command `json:"generate"`
|
|
|
|
|
Discover Discover `json:"discover"`
|
|
|
|
|
Parameters Parameters `yaml:"parameters"`
|
|
|
|
|
PreserveFileMode bool `json:"preserveFileMode,omitempty"`
|
2024-10-31 17:35:08 +00:00
|
|
|
ProvideGitCreds bool `json:"provideGitCreds,omitempty"`
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-14 02:51:02 +00:00
|
|
|
// Discover holds find and fileName
|
2021-11-08 17:47:10 +00:00
|
|
|
type Discover struct {
|
2021-11-10 22:57:14 +00:00
|
|
|
Find Find `json:"find"`
|
|
|
|
|
FileName string `json:"fileName"`
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-14 02:51:02 +00:00
|
|
|
func (d Discover) IsDefined() bool {
|
2023-04-24 17:58:48 +00:00
|
|
|
return d.FileName != "" || d.Find.Glob != "" || len(d.Find.Command.Command) > 0
|
2023-03-14 02:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 17:47:10 +00:00
|
|
|
// Command holds binary path and arguments list
|
|
|
|
|
type Command struct {
|
|
|
|
|
Command []string `json:"command,omitempty"`
|
|
|
|
|
Args []string `json:"args,omitempty"`
|
2021-11-10 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find holds find command or glob pattern
|
|
|
|
|
type Find struct {
|
|
|
|
|
Command
|
|
|
|
|
Glob string `json:"glob"`
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:08:32 +00:00
|
|
|
// Parameters holds static and dynamic configurations
|
|
|
|
|
type Parameters struct {
|
|
|
|
|
Static []*apiclient.ParameterAnnouncement `yaml:"static"`
|
|
|
|
|
Dynamic Command `yaml:"dynamic"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dynamic hold the dynamic announcements for CMP's
|
|
|
|
|
type Dynamic struct {
|
|
|
|
|
Command
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 17:47:10 +00:00
|
|
|
func ReadPluginConfig(filePath string) (*PluginConfig, error) {
|
|
|
|
|
path := fmt.Sprintf("%s/%s", strings.TrimRight(filePath, "/"), common.PluginConfigFileName)
|
|
|
|
|
|
|
|
|
|
var config PluginConfig
|
|
|
|
|
err := configUtil.UnmarshalLocalFile(path, &config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 15:46:50 +00:00
|
|
|
err = ValidatePluginConfig(config)
|
|
|
|
|
if err != nil {
|
2021-11-08 17:47:10 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ValidatePluginConfig(config PluginConfig) error {
|
|
|
|
|
if config.Metadata.Name == "" {
|
2025-03-27 16:37:52 +00:00
|
|
|
return errors.New("invalid plugin configuration file. metadata.name should be non-empty")
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
2025-03-27 16:37:52 +00:00
|
|
|
if config.Kind != ConfigManagementPluginKind {
|
|
|
|
|
return fmt.Errorf("invalid plugin configuration file. kind should be %s, found %s", ConfigManagementPluginKind, config.Kind)
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
if len(config.Spec.Generate.Command) == 0 {
|
2024-12-30 08:56:41 +00:00
|
|
|
return errors.New("invalid plugin configuration file. spec.generate command should be non-empty")
|
2021-11-08 17:47:10 +00:00
|
|
|
}
|
2023-02-02 17:07:58 +00:00
|
|
|
// discovery field is optional as apps can now specify plugin names directly
|
2021-11-08 17:47:10 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cfg *PluginConfig) Address() string {
|
|
|
|
|
var address string
|
|
|
|
|
pluginSockFilePath := common.GetPluginSockFilePath()
|
|
|
|
|
if cfg.Spec.Version != "" {
|
|
|
|
|
address = fmt.Sprintf("%s/%s-%s.sock", pluginSockFilePath, cfg.Metadata.Name, cfg.Spec.Version)
|
|
|
|
|
} else {
|
|
|
|
|
address = fmt.Sprintf("%s/%s.sock", pluginSockFilePath, cfg.Metadata.Name)
|
|
|
|
|
}
|
|
|
|
|
return address
|
|
|
|
|
}
|