Skip to content

Remove definitionsDir from k8s in agentdeployer #1769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2024
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
39 changes: 18 additions & 21 deletions internal/agentdeployer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -68,51 +68,48 @@ func Factory(options FactoryOptions) (AgentDeployer, error) {
return nil, nil
case "k8s":
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,
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)
}

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.
Expand Down
46 changes: 20 additions & 26 deletions internal/agentdeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -56,8 +54,6 @@ type kubernetesDeployedAgent struct {
stackVersion string

agentName string

definitionsDir string
}

func (s kubernetesDeployedAgent) TearDown(ctx context.Context) error {
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down