Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b40cd78
Initial code, and removal of reset credentials
jsoriano Dec 24, 2024
47532c3
Assume 410 status gone is ok for elasticsearch
jsoriano Dec 24, 2024
b9e112f
Refactor client tests so they don't try to use the configured client …
jsoriano Dec 24, 2024
a44469d
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Dec 26, 2024
cd980a6
Refactor shellinit
jsoriano Dec 26, 2024
5b41cd9
Use API key in stack clients
jsoriano Dec 26, 2024
12aaebe
Ignore errors when getting logs from a non-local elasticsearch
jsoriano Dec 26, 2024
cce94bd
Share logic to start local services
jsoriano Dec 26, 2024
b3b1e76
Fix spaces in logstash config
jsoriano Dec 27, 2024
3797d20
Prepare interfaces to create policies and getting enrollment tokens
jsoriano Dec 27, 2024
04e22d2
Initial enrollment works
jsoriano Dec 27, 2024
8f17940
Tear down
jsoriano Dec 27, 2024
83beb64
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Dec 30, 2024
290c6d9
Fix tear down
jsoriano Dec 30, 2024
be6dd46
Fix system tests
jsoriano Dec 30, 2024
6169e15
Get kibana host directly from the config?
jsoriano Dec 30, 2024
2e12e02
Fix stack up with logstash
jsoriano Dec 30, 2024
f8d1cee
Fix logstash with api keys
jsoriano Dec 30, 2024
9a24380
Better idempotence
jsoriano Dec 30, 2024
c4822eb
Remove unused variable
jsoriano Dec 30, 2024
7295a2e
Revert change in initialization of kibana host
jsoriano Dec 30, 2024
0ec34f2
Implement status for environment provider
jsoriano Dec 31, 2024
5f000c5
Try to support local Fleet Server for remote stacks
jsoriano Jan 2, 2025
0a188b4
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 2, 2025
184209e
Fix certifictes on agent deployer
jsoriano Jan 3, 2025
d4d32ac
Fix fleet status when fleet server is locally managed
jsoriano Jan 3, 2025
038549c
Reuse existing fleet server hosts
jsoriano Jan 3, 2025
91f2b2d
Add options for API key in clients
jsoriano Jan 3, 2025
b854ca9
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 3, 2025
0d1a1b2
Merge branch 'api-key-clients' into api-key-support
jsoriano Jan 3, 2025
74f2049
Add host.docker.internal to the local services
jsoriano Jan 3, 2025
bbbc671
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 7, 2025
0095a32
Polish status
jsoriano Jan 7, 2025
f60e15d
Add output id to stack config
jsoriano Jan 7, 2025
0c407a0
Fix error formatting value
jsoriano Jan 7, 2025
f53325d
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 8, 2025
dcc5e0b
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 13, 2025
c65452b
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 14, 2025
ffeb24c
Remove unused API keys
jsoriano Jan 15, 2025
1079df7
Fix issues after merge
jsoriano Jan 15, 2025
699623e
Fix kubernetes agent deployer
jsoriano Jan 17, 2025
699cb0f
Add tech preview warning
jsoriano Jan 17, 2025
52ec637
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 17, 2025
aa71071
Merge remote-tracking branch 'origin/main' into api-key-support
jsoriano Jan 20, 2025
d728838
Pass context to call to get enrollment tokens
jsoriano Jan 20, 2025
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
Prev Previous commit
Next Next commit
Reuse existing fleet server hosts
  • Loading branch information
jsoriano committed Jan 3, 2025
commit 038549c61833ff265e86b34dc2dfadb71fdbd02a
34 changes: 31 additions & 3 deletions internal/kibana/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ type FleetOutput struct {
}

type FleetServerHost struct {
URLs []string `json:"host_urls"`
Name string `json:"name"`
IsDefault bool `json:"is_default"`
ID string `json:"id,omitempty"`
URLs []string `json:"host_urls"`
Name string `json:"name"`

// TODO: Avoid using is_default, so a cluster can be used for multiple environments.
IsDefault bool `json:"is_default"`
}

type AgentSSL struct {
Expand Down Expand Up @@ -198,6 +201,31 @@ func (c *Client) AddFleetServerHost(ctx context.Context, host FleetServerHost) e
return nil
}

func (c *Client) UpdateFleetServerHost(ctx context.Context, host FleetServerHost) error {
if host.ID == "" {
return fmt.Errorf("host id required when updating fleet server host")
}

// Payload should not contain the ID, it is set in the URL.
id := host.ID
host.ID = ""
reqBody, err := json.Marshal(host)
if err != nil {
return fmt.Errorf("could not convert fleet server host to JSON: %w", err)
}

statusCode, respBody, err := c.put(ctx, fmt.Sprintf("%s/fleet_server_hosts/%s", FleetAPI, id), reqBody)
if err != nil {
return fmt.Errorf("could not update fleet server host: %w", err)
}

if statusCode != http.StatusOK {
return fmt.Errorf("could not update fleet server host; API status code = %d; response body = %s", statusCode, respBody)
}

return nil
}

// CreateFleetServiceToken creates a service token for Fleet, to be used when enrolling Fleet Servers.
func (c *Client) CreateFleetServiceToken(ctx context.Context) (string, error) {
statusCode, respBody, err := c.post(ctx, fmt.Sprintf("%s/service_tokens", FleetAPI), nil)
Expand Down
20 changes: 15 additions & 5 deletions internal/stack/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *environmentProvider) BootUp(ctx context.Context, options Options) error
// TODO: Migrate from serverless variables.
config.Parameters[ParamServerlessLocalStackVersion] = options.StackVersion

config, err = p.setupFleet(ctx, config, options.StackVersion)
config, err = p.setupFleet(ctx, config, options)
if err != nil {
return fmt.Errorf("failed to setup Fleet: %w", err)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (p *environmentProvider) initClients() error {
return nil
}

func (p environmentProvider) setupFleet(ctx context.Context, config Config, stackVersion string) (Config, error) {
func (p environmentProvider) setupFleet(ctx context.Context, config Config, options Options) (Config, error) {
const localFleetServerURL = "https://fleet-server:8220"

fleetServerURL, err := p.kibana.DefaultFleetServerURL(ctx)
Expand All @@ -151,17 +151,23 @@ func (p environmentProvider) setupFleet(ctx context.Context, config Config, stac
config.Parameters[paramFleetServerManaged] = "true"

host := kibana.FleetServerHost{
ID: fleetServerHostID(options.Profile.ProfileName),
URLs: []string{fleetServerURL},
IsDefault: true,
Name: "elastic-package-managed-fleet-server",
}
// TODO: Check if it is already there to avoid creating many of them.
err := p.kibana.AddFleetServerHost(ctx, host)
if err != nil && !errors.Is(err, kibana.ErrConflict) {
if errors.Is(err, kibana.ErrConflict) {
err = p.kibana.UpdateFleetServerHost(ctx, host)
if err != nil {
return config, fmt.Errorf("failed to update existing Fleet Server host (id: %s): %w", host.ID, err)
}
}
if err != nil {
return config, fmt.Errorf("failed to add Fleet Server host: %w", err)
}

_, err = createFleetServerPolicy(ctx, p.kibana, stackVersion)
_, err = createFleetServerPolicy(ctx, p.kibana, options.StackVersion, options.Profile.ProfileName)
if err != nil {
return config, fmt.Errorf("failed to create agent policy for Fleet Server: %w", err)
}
Expand All @@ -178,6 +184,10 @@ func (p environmentProvider) setupFleet(ctx context.Context, config Config, stac
return config, nil
}

func fleetServerHostID(namespace string) string {
return "elastic-package-" + namespace
}

func isFleetServerReachable(ctx context.Context, address string) bool {
status, err := fleetserver.NewClient(address).Status(ctx)
return err == nil && strings.ToLower(status.Status) == "healthy"
Expand Down
2 changes: 1 addition & 1 deletion internal/stack/fleetserverpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

// createFleetServerPolicy creates an agent policy with the initial configuration used for
// agents managed by elastic-package.
func createFleetServerPolicy(ctx context.Context, kibanaClient *kibana.Client, stackVersion string) (*kibana.Policy, error) {
func createFleetServerPolicy(ctx context.Context, kibanaClient *kibana.Client, stackVersion string, namespace string) (*kibana.Policy, error) {
policy := kibana.Policy{
Name: "Fleet Server (elastic-package)",
ID: managedFleetServerPolicyID,
Expand Down