-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure listener pod with the secret instead of env (#2965)
Co-authored-by: Bassem Dghaidi <[email protected]>
- Loading branch information
1 parent
e1edb84
commit 2117fd1
Showing
9 changed files
with
398 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Config struct { | ||
ConfigureUrl string `json:"configureUrl"` | ||
AppID int64 `json:"appID"` | ||
AppInstallationID int64 `json:"appInstallationID"` | ||
AppPrivateKey string `json:"appPrivateKey"` | ||
Token string `json:"token"` | ||
EphemeralRunnerSetNamespace string `json:"ephemeralRunnerSetNamespace"` | ||
EphemeralRunnerSetName string `json:"ephemeralRunnerSetName"` | ||
MaxRunners int `json:"maxRunners"` | ||
MinRunners int `json:"minRunners"` | ||
RunnerScaleSetId int `json:"runnerScaleSetId"` | ||
RunnerScaleSetName string `json:"runnerScaleSetName"` | ||
ServerRootCA string `json:"serverRootCA"` | ||
LogLevel string `json:"logLevel"` | ||
LogFormat string `json:"logFormat"` | ||
MetricsAddr string `json:"metricsAddr"` | ||
MetricsEndpoint string `json:"metricsEndpoint"` | ||
} | ||
|
||
func Read(path string) (Config, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
defer f.Close() | ||
|
||
var config Config | ||
if err := json.NewDecoder(f).Decode(&config); err != nil { | ||
return Config{}, fmt.Errorf("failed to decode config: %w", err) | ||
} | ||
|
||
if err := config.validate(); err != nil { | ||
return Config{}, fmt.Errorf("failed to validate config: %w", err) | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func (c *Config) validate() error { | ||
if len(c.ConfigureUrl) == 0 { | ||
return fmt.Errorf("GitHubConfigUrl is not provided") | ||
} | ||
|
||
if len(c.EphemeralRunnerSetNamespace) == 0 || len(c.EphemeralRunnerSetName) == 0 { | ||
return fmt.Errorf("EphemeralRunnerSetNamespace '%s' or EphemeralRunnerSetName '%s' is missing", c.EphemeralRunnerSetNamespace, c.EphemeralRunnerSetName) | ||
} | ||
|
||
if c.RunnerScaleSetId == 0 { | ||
return fmt.Errorf("RunnerScaleSetId '%d' is missing", c.RunnerScaleSetId) | ||
} | ||
|
||
if c.MaxRunners < c.MinRunners { | ||
return fmt.Errorf("MinRunners '%d' cannot be greater than MaxRunners '%d'", c.MinRunners, c.MaxRunners) | ||
} | ||
|
||
hasToken := len(c.Token) > 0 | ||
hasPrivateKeyConfig := c.AppID > 0 && c.AppPrivateKey != "" | ||
|
||
if !hasToken && !hasPrivateKeyConfig { | ||
return fmt.Errorf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(c.Token), c.AppID, c.AppInstallationID, len(c.AppPrivateKey)) | ||
} | ||
|
||
if hasToken && hasPrivateKeyConfig { | ||
return fmt.Errorf("only one GitHub auth method supported at a time. Have both PAT and App auth: token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(c.Token), c.AppID, c.AppInstallationID, len(c.AppPrivateKey)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfigValidationMinMax(t *testing.T) { | ||
config := &Config{ | ||
ConfigureUrl: "github.com/some_org/some_repo", | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
MinRunners: 5, | ||
MaxRunners: 2, | ||
Token: "token", | ||
} | ||
err := config.validate() | ||
assert.ErrorContains(t, err, "MinRunners '5' cannot be greater than MaxRunners '2", "Expected error about MinRunners > MaxRunners") | ||
} | ||
|
||
func TestConfigValidationMissingToken(t *testing.T) { | ||
config := &Config{ | ||
ConfigureUrl: "github.com/some_org/some_repo", | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
} | ||
err := config.validate() | ||
expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) | ||
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") | ||
} | ||
|
||
func TestConfigValidationAppKey(t *testing.T) { | ||
config := &Config{ | ||
AppID: 1, | ||
AppInstallationID: 10, | ||
ConfigureUrl: "github.com/some_org/some_repo", | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
} | ||
err := config.validate() | ||
expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) | ||
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") | ||
} | ||
|
||
func TestConfigValidationOnlyOneTypeOfCredentials(t *testing.T) { | ||
config := &Config{ | ||
AppID: 1, | ||
AppInstallationID: 10, | ||
AppPrivateKey: "asdf", | ||
Token: "asdf", | ||
ConfigureUrl: "github.com/some_org/some_repo", | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
} | ||
err := config.validate() | ||
expectedError := fmt.Sprintf("only one GitHub auth method supported at a time. Have both PAT and App auth: token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) | ||
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") | ||
} | ||
|
||
func TestConfigValidation(t *testing.T) { | ||
config := &Config{ | ||
ConfigureUrl: "https://github.com/actions", | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
MinRunners: 1, | ||
MaxRunners: 5, | ||
Token: "asdf", | ||
} | ||
|
||
err := config.validate() | ||
|
||
assert.NoError(t, err, "Expected no error") | ||
} | ||
|
||
func TestConfigValidationConfigUrl(t *testing.T) { | ||
config := &Config{ | ||
EphemeralRunnerSetNamespace: "namespace", | ||
EphemeralRunnerSetName: "deployment", | ||
RunnerScaleSetId: 1, | ||
} | ||
|
||
err := config.validate() | ||
|
||
assert.ErrorContains(t, err, "GitHubConfigUrl is not provided", "Expected error about missing ConfigureUrl") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.