<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #44046 # Details Updates GitOps to ensure that team labels are created before any consumers (e.g. profiles, policies, software) that might use the labels are applied. It does this by adding a new `afterTeamApply` callback option to `ApplyGroup` that is called after team config is applied -- this is when team labels are now created, instead of after `ApplyGroup` runs. # Checklist for submitter If some of the following don't apply, delete the relevant line. - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [X] Added/updated automated tests - [ ] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed GitOps deployments failing when a label and a profile that references it are created in the same run. * Ensures the correct apply ordering so dry-run/log output shows label application before profile application. * **Tests** * Added a regression test validating label → profile apply ordering to prevent future regressions. [](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/45194) <!-- end of auto-generated comment: release notes by coderabbit.ai -->
119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package fleetctl
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/fleetdm/fleet/v4/pkg/spec"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func applyCommand() *cli.Command {
|
|
var (
|
|
flFilename string
|
|
flForce bool
|
|
flDryRun bool
|
|
)
|
|
return &cli.Command{
|
|
Name: "apply",
|
|
Usage: "Use for one-off imports and backwards compatibility GitOps",
|
|
UsageText: `fleetctl apply [options]`,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "f",
|
|
EnvVars: []string{"FILENAME"},
|
|
Value: "",
|
|
Destination: &flFilename,
|
|
Usage: "A file to apply",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "force",
|
|
EnvVars: []string{"FORCE"},
|
|
Destination: &flForce,
|
|
Usage: "Force applying the file even if it raises validation errors (only supported for 'config' and 'fleet' specs)",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "dry-run",
|
|
EnvVars: []string{"DRY_RUN"},
|
|
Destination: &flDryRun,
|
|
Usage: "Do not apply the file, just validate it (only supported for 'config' and 'fleet' specs)",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "policies-fleet",
|
|
Aliases: []string{"policies-team"},
|
|
Usage: "A fleet's name, this flag is only used on policies specs (overrides 'fleet' key in the policies file). This allows to easily import a group of policies to a fleet.",
|
|
},
|
|
configFlag(),
|
|
contextFlag(),
|
|
debugFlag(),
|
|
enableLogTopicsFlag(),
|
|
disableLogTopicsFlag(),
|
|
},
|
|
Before: func(c *cli.Context) error {
|
|
logDeprecatedFlagName(c, "policies-team", "policies-fleet")
|
|
return nil
|
|
},
|
|
Action: func(c *cli.Context) error {
|
|
// Apply log topic overrides from flags/env vars.
|
|
applyLogTopicFlags(c)
|
|
|
|
if flFilename == "" {
|
|
return errors.New("-f must be specified")
|
|
}
|
|
b, err := os.ReadFile(flFilename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fleetClient, err := clientFromCLI(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if the file has a .yml or .yaml extension
|
|
ext := strings.ToLower(filepath.Ext(flFilename))
|
|
if ext == "" {
|
|
return errors.New("Missing file extension: only .yml or .yaml files can be applied")
|
|
}
|
|
if ext != ".yml" && ext != ".yaml" {
|
|
return fmt.Errorf("Invalid file extension %s: only .yml or .yaml files can be applied", ext)
|
|
}
|
|
|
|
logf := func(format string, a ...interface{}) {
|
|
fmt.Fprintf(c.App.Writer, format, a...)
|
|
}
|
|
|
|
specs, err := spec.GroupFromBytes(b, spec.GroupFromBytesOpts{
|
|
LogFn: logf,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := fleet.ApplyClientSpecOptions{
|
|
ApplySpecOptions: fleet.ApplySpecOptions{
|
|
Force: flForce,
|
|
DryRun: flDryRun,
|
|
},
|
|
}
|
|
if policiesTeamName := c.String("policies-fleet"); policiesTeamName != "" {
|
|
opts.TeamForPolicies = policiesTeamName
|
|
}
|
|
baseDir := filepath.Dir(flFilename)
|
|
|
|
teamsSoftwareInstallers := make(map[string][]fleet.SoftwarePackageResponse)
|
|
teamsVPPApps := make(map[string][]fleet.VPPAppResponse)
|
|
teamsScripts := make(map[string][]fleet.ScriptResponse)
|
|
|
|
_, _, _, _, err = fleetClient.ApplyGroup(c.Context, false, specs, baseDir, logf, nil, opts, teamsSoftwareInstallers, teamsVPPApps, teamsScripts, nil, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
}
|