From 3670ea19a0acf636dd20f4454164d2e2cb3b23f8 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Fri, 12 Apr 2024 12:54:00 +0200 Subject: [PATCH 1/2] Remove definitionsDir from k8s in agentdeployer --- internal/agentdeployer/factory.go | 23 ++++++-------- internal/agentdeployer/kubernetes.go | 46 ++++++++++++---------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/internal/agentdeployer/factory.go b/internal/agentdeployer/factory.go index 669ea1f54..25eb01074 100644 --- a/internal/agentdeployer/factory.go +++ b/internal/agentdeployer/factory.go @@ -57,8 +57,6 @@ func Factory(options FactoryOptions) (AgentDeployer, error) { agentDeployerName = "default" } - agentDeployerPath := filepath.Join(devDeployPath, agentDeployerName) - switch agentDeployerName { case "default": if options.Type != TypeTest { @@ -80,19 +78,16 @@ func Factory(options FactoryOptions) (AgentDeployer, error) { // FIXME: this docker-compose scenario contains both agent and service return nil, nil case "k8s": - if _, err := os.Stat(agentDeployerPath); err == nil { - opts := KubernetesAgentDeployerOptions{ - Profile: options.Profile, - DefinitionsDir: agentDeployerPath, - StackVersion: options.StackVersion, - PolicyName: options.PolicyName, - DataStream: options.DataStream, - RunSetup: options.RunSetup, - RunTestsOnly: options.RunTestsOnly, - RunTearDown: options.RunTearDown, - } - return NewKubernetesAgentDeployer(opts) + opts := KubernetesAgentDeployerOptions{ + Profile: options.Profile, + StackVersion: options.StackVersion, + PolicyName: options.PolicyName, + DataStream: options.DataStream, + RunSetup: options.RunSetup, + RunTestsOnly: options.RunTestsOnly, + RunTearDown: options.RunTearDown, } + return NewKubernetesAgentDeployer(opts) } return nil, fmt.Errorf("unsupported agent deployer (name: %s)", agentDeployerName) } diff --git a/internal/agentdeployer/kubernetes.go b/internal/agentdeployer/kubernetes.go index c34e15289..c6aeb07c8 100644 --- a/internal/agentdeployer/kubernetes.go +++ b/internal/agentdeployer/kubernetes.go @@ -25,11 +25,10 @@ import ( // KubernetesAgentDeployer is responsible for deploying resources in the Kubernetes cluster. type KubernetesAgentDeployer struct { - profile *profile.Profile - definitionsDir string - stackVersion string - policyName string - dataStream string + profile *profile.Profile + stackVersion string + policyName string + dataStream string agentRunID string @@ -39,11 +38,10 @@ type KubernetesAgentDeployer struct { } type KubernetesAgentDeployerOptions struct { - Profile *profile.Profile - DefinitionsDir string - StackVersion string - PolicyName string - DataStream string + Profile *profile.Profile + StackVersion string + PolicyName string + DataStream string RunSetup bool RunTestsOnly bool @@ -56,8 +54,6 @@ type kubernetesDeployedAgent struct { stackVersion string agentName string - - definitionsDir string } func (s kubernetesDeployedAgent) TearDown(ctx context.Context) error { @@ -67,7 +63,7 @@ func (s kubernetesDeployedAgent) TearDown(ctx context.Context) error { } err = kubectl.DeleteStdin(ctx, elasticAgentManagedYaml) if err != nil { - return fmt.Errorf("can't uninstall Kubernetes resources (path: %s): %w", s.definitionsDir, err) + return fmt.Errorf("can't uninstall Kubernetes Elastic Agent resources: %w", err) } return nil } @@ -94,14 +90,13 @@ var _ DeployedAgent = new(kubernetesDeployedAgent) // NewKubernetesAgentDeployer function creates a new instance of KubernetesAgentDeployer. func NewKubernetesAgentDeployer(opts KubernetesAgentDeployerOptions) (*KubernetesAgentDeployer, error) { return &KubernetesAgentDeployer{ - profile: opts.Profile, - definitionsDir: opts.DefinitionsDir, - stackVersion: opts.StackVersion, - policyName: opts.PolicyName, - dataStream: opts.DataStream, - runSetup: opts.RunSetup, - runTestsOnly: opts.RunTestsOnly, - runTearDown: opts.RunTearDown, + profile: opts.Profile, + stackVersion: opts.StackVersion, + policyName: opts.PolicyName, + dataStream: opts.DataStream, + runSetup: opts.RunSetup, + runTestsOnly: opts.RunTestsOnly, + runTearDown: opts.RunTearDown, }, nil } @@ -140,11 +135,10 @@ func (ksd *KubernetesAgentDeployer) SetUp(ctx context.Context, agentInfo AgentIn // to deploy Agent Pod. Because of this, hostname inside pod will be equal to the name of the k8s host. agentInfo.Agent.Host.NamePrefix = "kind-control-plane" return &kubernetesDeployedAgent{ - agentInfo: agentInfo, - definitionsDir: ksd.definitionsDir, - profile: ksd.profile, - stackVersion: ksd.stackVersion, - agentName: agentName, + agentInfo: agentInfo, + profile: ksd.profile, + stackVersion: ksd.stackVersion, + agentName: agentName, }, nil } From 4a25dea266a82d0018a5517e7d56e55874442d61 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Mon, 15 Apr 2024 12:07:13 +0200 Subject: [PATCH 2/2] Remove returned path parameter --- internal/agentdeployer/factory.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/internal/agentdeployer/factory.go b/internal/agentdeployer/factory.go index 9c53fec1b..c384e5aed 100644 --- a/internal/agentdeployer/factory.go +++ b/internal/agentdeployer/factory.go @@ -41,7 +41,7 @@ type FactoryOptions struct { // Factory chooses the appropriate service runner for the given data stream, depending // on service configuration files defined in the package or data stream. func Factory(options FactoryOptions) (AgentDeployer, error) { - agentDeployerName, agentDeployerPath, err := selectAgentDeployerType(options) + agentDeployerName, err := selectAgentDeployerType(options) if err != nil { return nil, fmt.Errorf("failed to select agent deployer type: %w", err) } @@ -81,37 +81,35 @@ func Factory(options FactoryOptions) (AgentDeployer, error) { return nil, fmt.Errorf("unsupported agent deployer (name: %s)", agentDeployerName) } -func selectAgentDeployerType(options FactoryOptions) (string, string, error) { +func selectAgentDeployerType(options FactoryOptions) (string, error) { devDeployPath, err := FindDevDeployPath(options) if errors.Is(err, os.ErrNotExist) { - return "default", "", nil + return "default", nil } if err != nil { - return "", "", fmt.Errorf("can't find \"%s\" directory: %w", options.DevDeployDir, err) + return "", fmt.Errorf("can't find \"%s\" directory: %w", options.DevDeployDir, err) } agentDeployerNames, err := findAgentDeployers(devDeployPath) if errors.Is(err, os.ErrNotExist) || len(agentDeployerNames) == 0 { logger.Debugf("Not agent deployer found, using default one") - return "default", "", nil + return "default", nil } if err != nil { - return "", "", fmt.Errorf("failed to find agent deployer: %w", err) + return "", fmt.Errorf("failed to find agent deployer: %w", err) } if len(agentDeployerNames) != 1 { - return "", "", fmt.Errorf("expected to find only one agent deployer in \"%s\"", devDeployPath) + return "", fmt.Errorf("expected to find only one agent deployer in \"%s\"", devDeployPath) } agentDeployerName := agentDeployerNames[0] - // if package defines `_dev/deploy/docker` folder to start their services, it should be - // using the default agent deployer` + // if package defines `_dev/deploy/docker` or `_dev/deploy/tf` folder to start their services, + // it should be using the default agent deployer` if agentDeployerName == "docker" || agentDeployerName == "tf" { - return "default", "", nil + return "default", nil } - // No need to check if this path exists because it comes from a directory list. - agentDeployerPath := filepath.Join(devDeployPath, agentDeployerName) - return agentDeployerName, agentDeployerPath, nil + return agentDeployerName, nil } // FindDevDeployPath function returns a path reference to the "_dev/deploy" directory.