Skip to content

Commit

Permalink
Merge pull request #33 from Kong/simplify-cfg-again
Browse files Browse the repository at this point in the history
Feat: Improvements to config and processing
  • Loading branch information
rspurgeon authored Mar 7, 2025
2 parents 6427edb + ed2017a commit 6cb2f88
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 44 deletions.
128 changes: 84 additions & 44 deletions cmd/koctl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var resourceFiles embed.FS

var (
loopInterval int
wholeFileArg string
platformFileArg string
teamsFileArg string
organizationsFileArg string
Expand Down Expand Up @@ -73,15 +74,14 @@ var versionCmd = &cobra.Command{
}

func init() {
applyCmd.Flags().StringVar(&platformFileArg, "platform", "", "Path to the platform configuration file")
applyCmd.Flags().StringVar(&teamsFileArg, "teams", "", "Path to the teams configuration file")
applyCmd.Flags().StringVar(&organizationsFileArg, "orgs", "", "Path to the organizations configuration file")
applyCmd.Flags().StringVar(&wholeFileArg, "file", "", "Path to the configuration file. This is a convenience flag to apply the whole configuration in one file")
applyCmd.Flags().StringVar(&platformFileArg, "platform", "", "Path to the platform configuration file. Superseded by --file")
applyCmd.Flags().StringVar(&teamsFileArg, "teams", "", "Path to the teams configuration file. Superseded by --file")
applyCmd.Flags().StringVar(&organizationsFileArg, "orgs", "", "Path to the organizations configuration file. Superseded by --file")
applyCmd.Flags().IntVarP(&loopInterval,
"loop", "l", 0, "Run in a loop with specified interval in seconds (0 = run once)")

_ = applyCmd.MarkFlagRequired("platform")
_ = applyCmd.MarkFlagRequired("teams")
_ = applyCmd.MarkFlagRequired("orgs")
applyCmd.MarkFlagsOneRequired("file", "platform")

cobra.OnInitialize(initConfig)

Expand Down Expand Up @@ -136,7 +136,11 @@ func applyService(
svcGitCfg := serviceConfig.Git
if svcGitCfg.Auth == nil {
// If the user doesn't provide a service level git auth config, we use the platform level git auth
svcGitCfg.Auth = platformGit.Auth
if platformGit.Auth == nil {
svcGitCfg.GitHub = platformGit.GitHub
} else {
svcGitCfg.Auth = platformGit.Auth
}
}

// This loads the Service Spec from the teams Git Repository
Expand Down Expand Up @@ -232,9 +236,8 @@ func applyService(
if len(services) == 1 {
serviceID = *services[0].GetID()
} else {
fmt.Printf("!!!Found %d serivces for API %s. Cannot create API implementation relation, "+
"requires exactly 1 service with `ko-api-name` tag.\n", len(services), *apiName)
return nil
fmt.Printf("Warn: Found %d serivces for API %s. Cannot create API implementation relation, "+
"requires exactly 1 service with `ko-api-name` tag. APIOps workflows may need to be ran.\n", len(services), *apiName)
}

_, err = portal.ApplyAPIConfig(
Expand Down Expand Up @@ -751,53 +754,90 @@ func applyPlatformRepo(gitCfg *manifest.GitConfig) error {

func runApply(_ *cobra.Command, _ []string) error {
applyOnce := func() error {
platformFilePath, err := filepath.Abs(platformFileArg)
if err != nil {
return fmt.Errorf("failed to resolve platform file path: %w", err)
}
if _, err := os.Stat(platformFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", platformFilePath, err)
}
teamsFilePath, err := filepath.Abs(teamsFileArg)
if err != nil {
return fmt.Errorf("failed to resolve teams file path: %w", err)
}
if _, err := os.Stat(teamsFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", teamsFilePath, err)
}
organizationsFilePath, err := filepath.Abs(organizationsFileArg)
if err != nil {
return fmt.Errorf("failed to resolve organizations file path: %w", err)
}
if _, err := os.Stat(organizationsFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", organizationsFilePath, err)
}
var wholeFilePath, platformFilePath, teamsFilePath, organizationsFilePath string
if wholeFileArg != "" {
var err error
wholeFilePath, err = filepath.Abs(wholeFileArg)
if err != nil {
return fmt.Errorf("failed to resolve whole file path: %w", err)
}
if _, err := os.Stat(wholeFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", wholeFilePath, err)
}
} else {
var err error
platformFilePath, err := filepath.Abs(platformFileArg)
if err != nil {
return fmt.Errorf("failed to resolve platform file path: %w", err)
}
if _, err := os.Stat(platformFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", platformFilePath, err)
}

var manifest manifest.Orchestrator
if err := readConfigSection(platformFilePath, &manifest.Platform); err != nil {
return fmt.Errorf("failed to read platform configuration: %w", err)
}
if err := readConfigSection(teamsFilePath, &manifest.Teams); err != nil {
return fmt.Errorf("failed to read teams configuration: %w", err)
var teamsFilePath string
if teamsFileArg != "" {
teamsFilePath, err = filepath.Abs(teamsFileArg)
if err != nil {
return fmt.Errorf("failed to resolve teams file path: %w", err)
}
if _, err := os.Stat(teamsFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", teamsFilePath, err)
}
}

var organizationsFilePath string
if organizationsFileArg != "" {
organizationsFilePath, err = filepath.Abs(organizationsFileArg)
if err != nil {
return fmt.Errorf("failed to resolve organizations file path: %w", err)
}
if _, err := os.Stat(organizationsFilePath); err != nil {
return fmt.Errorf("failed to access file %s: %w", organizationsFilePath, err)
}
}
}
if err := readConfigSection(organizationsFilePath, &manifest.Organizations); err != nil {
return fmt.Errorf("failed to read organizations configuration: %w", err)

var man manifest.Orchestrator

if wholeFilePath != "" {
if err := readConfigSection(wholeFilePath, &man); err != nil {
return fmt.Errorf("failed to read whole configuration: %w", err)
}
} else {
if err := readConfigSection(platformFilePath, &man); err != nil {
return fmt.Errorf("failed to read platform configuration: %w", err)
}

if teamsFilePath != "" {
if err := readConfigSection(teamsFilePath, &man); err != nil {
return fmt.Errorf("failed to read teams configuration: %w", err)
}
} else {
man.Teams = make(map[string]*manifest.Team)
}

if organizationsFilePath != "" {
if err := readConfigSection(organizationsFilePath, &man); err != nil {
return fmt.Errorf("failed to read organizations configuration: %w", err)
}
} else {
man.Organizations = make(map[string]*manifest.Organization)
}
}

err = applyPlatformRepo(manifest.Platform.Git)
err := applyPlatformRepo(man.Platform.Git)
if err != nil {
return fmt.Errorf("failed to apply platform repository changes: %w", err)
}

// Process each organization
for orgName, orgConfig := range manifest.Organizations {
if err := applyOrganization(orgName, *manifest.Platform.Git, *orgConfig, manifest.Teams); err != nil {
for orgName, orgConfig := range man.Organizations {
if err := applyOrganization(orgName, *man.Platform.Git, *orgConfig, man.Teams); err != nil {
return err
}
}

fmt.Printf("Successfully applied configuration from:\n - %s\n - %s\n - %s\n",
platformFileArg, teamsFileArg, organizationsFileArg)
fmt.Println("Configuration Applied")

return nil
}
Expand Down
16 changes: 16 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ import (

// getAuthMethod returns an ssh.AuthMethod based on the git config, or nil if no auth is specified
func getAuthMethod(gitConfig manifest.GitConfig) (transport.AuthMethod, error) {
if gitConfig.Auth == nil {
// by default, we can use GitHub auth for git auth which can simplify the configuration required
if gitConfig.GitHub == nil || gitConfig.GitHub.Token == nil {
return nil, errors.New("no auth configured. Must specify either auth or github with token value")
}
key, err := util.ResolveSecretValue(*gitConfig.GitHub.Token)
if err != nil {
return nil, err
}
bashAuth := &http.BasicAuth{
Username: "x-access-token",
Password: key,
}
return bashAuth, nil
}

// Return nil if no auth configured
if gitConfig.Auth.Type == nil {
return nil, nil
Expand Down

0 comments on commit 6cb2f88

Please sign in to comment.