Skip to content

Commit 45cbfe9

Browse files
committed
feat: add flags for all configurable parameters
Add command-line flags for CI configuration. This makes all configuration explicit and user-controllable at the command level. - Replace NewGithubWorkflow's 10 parameters with single CIConfig - Extract hardcoded defaults to named constants - Add branch configuration option - Remove temporary ciConfig parameter from command constructors The NewCiGithubConfigVia function now reads all configuration directly from command flags, eliminating the need to construct and pass config objects through the call chain. Issue SRVOCF-744 Signed-off-by: Stanislav Jakuschevskij <sjakusch@redhat.com>
1 parent 9d6ede6 commit 45cbfe9

File tree

8 files changed

+211
-128
lines changed

8 files changed

+211
-128
lines changed

cmd/ci/config.go

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type CIConfig struct {
1212
githubWorkflowDir,
1313
githubWorkflowFilename,
14+
branch,
1415
workflowName,
1516
kubeconfigSecret,
1617
registryLoginUrlVar,
@@ -35,6 +36,10 @@ func (cc *CIConfig) WorkflowName() string {
3536
return cc.workflowName
3637
}
3738

39+
func (cc *CIConfig) Branch() string {
40+
return cc.branch
41+
}
42+
3843
func (cc *CIConfig) UseRegistryLogin() bool {
3944
return cc.useRegistryLogin
4045
}
@@ -78,19 +83,19 @@ type ciConfigBuilder struct {
7883
func NewCIConfigBuilder() *ciConfigBuilder {
7984
return &ciConfigBuilder{
8085
result: CIConfig{
81-
// TODO(twoGiants): extract into constants
82-
githubWorkflowDir: ".github/workflows",
83-
githubWorkflowFilename: "remote-build-and-deploy.yaml",
84-
workflowName: "Remote Build and Deploy",
85-
kubeconfigSecret: "KUBECONFIG",
86-
registryLoginUrlVar: "REGISTRY_LOGIN_URL",
87-
registryUserVar: "REGISTRY_USERNAME",
88-
registryPassSecret: "REGISTRY_PASSWORD",
89-
registryUrlVar: "REGISTRY_URL",
90-
useRegistryLogin: true,
91-
useRemoteBuild: false,
92-
useSelfHostedRunner: false,
93-
debug: false,
86+
githubWorkflowDir: DefaultGithubWorkflowDir,
87+
githubWorkflowFilename: DefaultGithubWorkflowFilename,
88+
branch: DefaultBranch,
89+
workflowName: DefaultWorkflowName,
90+
kubeconfigSecret: DefaultKubeconfigSecretName,
91+
registryLoginUrlVar: DefaultRegistryLoginUrlVariableName,
92+
registryUserVar: DefaultRegistryUserVariableName,
93+
registryPassSecret: DefaultRegistryPassSecretName,
94+
registryUrlVar: DefaultRegistryUrlVariableName,
95+
useRegistryLogin: DefaultUseRegistryLogin,
96+
useRemoteBuild: DefaultUseRemoteBuild,
97+
useSelfHostedRunner: DefaultUseSelfHostedRunner,
98+
debug: DefaultUseDebug,
9499
},
95100
}
96101
}
@@ -100,6 +105,11 @@ func (b *ciConfigBuilder) WithWorkflowName(name string) *ciConfigBuilder {
100105
return b
101106
}
102107

108+
func (b *ciConfigBuilder) WithBranch(v string) *ciConfigBuilder {
109+
b.result.branch = v
110+
return b
111+
}
112+
103113
func (b *ciConfigBuilder) WithKubeconfigSecret(v string) *ciConfigBuilder {
104114
b.result.kubeconfigSecret = v
105115
return b
@@ -110,6 +120,11 @@ func (b *ciConfigBuilder) WithRegistryLoginUrlVar(v string) *ciConfigBuilder {
110120
return b
111121
}
112122

123+
func (b *ciConfigBuilder) WithRegistryUrlVar(v string) *ciConfigBuilder {
124+
b.result.registryUrlVar = v
125+
return b
126+
}
127+
113128
func (b *ciConfigBuilder) WithRegistryUserVar(v string) *ciConfigBuilder {
114129
b.result.registryUserVar = v
115130
return b
@@ -145,14 +160,38 @@ func (b *ciConfigBuilder) Build() CIConfig {
145160
}
146161

147162
const (
148-
UseRegistryLoginOption = "use-registry-login"
149-
DefaultUseRegistryLoginValue = true
163+
GithubOption = "github"
164+
DefaultGithub = false
165+
166+
DefaultGithubWorkflowDir = ".github/workflows"
167+
DefaultGithubWorkflowFilename = "func-deploy.yaml"
168+
169+
BranchOption = "branch"
170+
DefaultBranch = "main"
150171

151172
WorkflowNameOption = "workflow-name"
152-
DefaultWorkflowName = "Local Build and Remote Deploy"
173+
DefaultWorkflowName = "Func Deploy"
153174

154-
UseDebugOption = "debug"
155-
DefaultUseDebugValue = false
175+
KubeconfigSecretNameOption = "kubeconfig-secret-name"
176+
DefaultKubeconfigSecretName = "KUBECONFIG"
177+
178+
RegistryLoginUrlVariableNameOption = "registry-login-url-variable-name"
179+
DefaultRegistryLoginUrlVariableName = "REGISTRY_LOGIN_URL"
180+
181+
RegistryUserVariableNameOption = "registry-user-variable-name"
182+
DefaultRegistryUserVariableName = "REGISTRY_USERNAME"
183+
184+
RegistryPassSecretNameOption = "registry-pass-secret-name"
185+
DefaultRegistryPassSecretName = "REGISTRY_PASSWORD"
186+
187+
RegistryUrlVariableNameOption = "registry-url-variable-name"
188+
DefaultRegistryUrlVariableName = "REGISTRY_URL"
189+
190+
UseRegistryLoginOption = "use-registry-login"
191+
DefaultUseRegistryLogin = true
192+
193+
UseDebugOption = "debug"
194+
DefaultUseDebug = false
156195

157196
UseRemoteBuild = "remote"
158197
DefaultUseRemoteBuild = false
@@ -170,6 +209,42 @@ func NewCiGithubConfigVia(cmd *cobra.Command) (CIConfig, error) {
170209
}
171210
result.WithWorkflowName(workflowName)
172211

212+
branch, err := cmd.Flags().GetString(BranchOption)
213+
if err != nil {
214+
return CIConfig{}, err
215+
}
216+
result.WithBranch(branch)
217+
218+
kubeconfigSecretName, err := cmd.Flags().GetString(KubeconfigSecretNameOption)
219+
if err != nil {
220+
return CIConfig{}, err
221+
}
222+
result.WithKubeconfigSecret(kubeconfigSecretName)
223+
224+
registryLoginUrlName, err := cmd.Flags().GetString(RegistryLoginUrlVariableNameOption)
225+
if err != nil {
226+
return CIConfig{}, err
227+
}
228+
result.WithRegistryLoginUrlVar(registryLoginUrlName)
229+
230+
registryUserVarName, err := cmd.Flags().GetString(RegistryUserVariableNameOption)
231+
if err != nil {
232+
return CIConfig{}, err
233+
}
234+
result.WithRegistryUserVar(registryUserVarName)
235+
236+
registryPassSecretName, err := cmd.Flags().GetString(RegistryPassSecretNameOption)
237+
if err != nil {
238+
return CIConfig{}, err
239+
}
240+
result.WithRegistryPassSecret(registryPassSecretName)
241+
242+
registryUrlVarName, err := cmd.Flags().GetString(RegistryUrlVariableNameOption)
243+
if err != nil {
244+
return CIConfig{}, err
245+
}
246+
result.WithRegistryUrlVar(registryUrlVarName)
247+
173248
registryLogin, err := cmd.Flags().GetBool(UseRegistryLoginOption)
174249
if err != nil {
175250
return CIConfig{}, err
@@ -205,6 +280,7 @@ func NewCiGithubConfigVia(cmd *cobra.Command) (CIConfig, error) {
205280
return result.Build(), nil
206281
}
207282

283+
// TODO(twoGiants): fix broken viper cmd options propagation
208284
func NewCiGithubConfigViaViper() CIConfig {
209285
result := NewCIConfigBuilder().
210286
WithWorkflowName(viper.GetString(WorkflowNameOption))

cmd/ci/workflow.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,13 @@ func (s *Step) withActionConfig(key, value string) *Step {
9393
return s
9494
}
9595

96-
func NewGithubWorkflow(
97-
name,
98-
kubeconfigSecret,
99-
registryLoginUrlVar,
100-
registryUserEnvVar,
101-
registryPassSecret,
102-
registryUrlVar string,
103-
useRegistryLogin,
104-
useRemoteBuild,
105-
useSelfHosted,
106-
useDebug bool,
107-
) *GithubWorkflow {
96+
func NewGithubWorkflow(conf CIConfig) *GithubWorkflow {
10897
runsOn := "ubuntu-latest"
109-
if useSelfHosted {
98+
if conf.UseSelfHostedRunner() {
11099
runsOn = "self-hosted"
111100
}
112101

113-
pushTrigger := newPushTrigger("main", useDebug)
102+
pushTrigger := newPushTrigger(conf.Branch(), conf.UseDebug())
114103

115104
var steps []Step
116105
checkoutCode := newStep("Checkout code").
@@ -120,19 +109,19 @@ func NewGithubWorkflow(
120109
setupK8Context := newStep("Setup Kubernetes context").
121110
withUses("azure/k8s-set-context@v4").
122111
withActionConfig("method", "kubeconfig").
123-
withActionConfig("kubeconfig", newSecret(kubeconfigSecret))
112+
withActionConfig("kubeconfig", newSecret(conf.KubeconfigSecret()))
124113
steps = append(steps, *setupK8Context)
125114

126-
if useRegistryLogin {
115+
if conf.UseRegistryLogin() {
127116
loginToContainerRegistry := newStep("Login to container registry").
128117
withUses("docker/login-action@v3").
129-
withActionConfig("registry", newVariable(registryLoginUrlVar)).
130-
withActionConfig("username", newVariable(registryUserEnvVar)).
131-
withActionConfig("password", newSecret(registryPassSecret))
118+
withActionConfig("registry", newVariable(conf.RegistryLoginUrlVar())).
119+
withActionConfig("username", newVariable(conf.RegistryUserVar())).
120+
withActionConfig("password", newSecret(conf.RegistryPassSecret()))
132121
steps = append(steps, *loginToContainerRegistry)
133122
}
134123

135-
if useDebug {
124+
if conf.UseDebug() {
136125
funcCliCache := newStep("Restore func cli from cache").
137126
withID("func-cli-cache").
138127
withUses("actions/cache@v4").
@@ -145,19 +134,26 @@ func NewGithubWorkflow(
145134
withUses("gauron99/knative-func-action@main").
146135
withActionConfig("version", "knative-v1.19.1").
147136
withActionConfig("name", "func")
148-
if useDebug {
137+
if conf.UseDebug() {
149138
installFuncCli.withIf("${{ steps.func-cli-cache.outputs.cache-hit != 'true' }}")
150139
}
151140
steps = append(steps, *installFuncCli)
152141

142+
if conf.UseDebug() {
143+
addFuncToPATH := newStep("Add func to GITHUB_PATH").
144+
withRun(`echo "$GITHUB_WORKSPACE" >> $GITHUB_PATH`)
145+
steps = append(steps, *addFuncToPATH)
146+
}
147+
148+
name := conf.WorkflowName()
153149
runFuncDeploy := "func deploy"
154-
if useRemoteBuild {
150+
if conf.UseRemoteBuild() {
155151
runFuncDeploy += " --remote"
156-
name = "Remote Build and Deploy"
152+
name = "Remote Func Deploy"
157153
}
158-
registryUrl := newVariable(registryUrlVar)
159-
if useRegistryLogin {
160-
registryUrl = newVariable(registryLoginUrlVar) + "/" + newVariable(registryUserEnvVar)
154+
registryUrl := newVariable(conf.RegistryUrlVar())
155+
if conf.UseRegistryLogin() {
156+
registryUrl = newVariable(conf.RegistryLoginUrlVar()) + "/" + newVariable(conf.RegistryUserVar())
161157
}
162158
deployFunc := newStep("Deploy function").
163159
withRun(runFuncDeploy + " --registry=" + registryUrl + " -v")

cmd/ci/workflow_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,8 @@ import (
99

1010
func TestGithubWorkflow_PersistAndLoad(t *testing.T) {
1111
// GIVEN
12-
gw := ci.NewGithubWorkflow(
13-
"gw-test",
14-
"KUBECONFIG",
15-
"REGISTRY_LOGIN_URL",
16-
"REGISTRY_USERNAME",
17-
"REGISTRY_PASSWORD",
18-
"REGISTRY_URL",
19-
false,
20-
false,
21-
false,
22-
false)
23-
tempDir := t.TempDir()
24-
targetPath := tempDir + "/" + gw.Name + ".yaml"
12+
gw := ci.NewGithubWorkflow(ci.NewCIConfigBuilder().Build())
13+
targetPath := t.TempDir() + "/" + gw.Name + ".yaml"
2514

2615
// WHEN
2716
persistErr := gw.Persist(targetPath)

cmd/config.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import (
77
"github.com/ory/viper"
88
"github.com/spf13/cobra"
99

10-
"knative.dev/func/cmd/ci"
1110
"knative.dev/func/cmd/common"
1211
"knative.dev/func/pkg/config"
1312
fn "knative.dev/func/pkg/functions"
1413
)
1514

16-
// TODO(twoGiants): decide if you want to remove TEMP_ciConfig and init somewhere else
17-
func NewConfigCmd(loadSaver common.FunctionLoaderSaver, newClient ClientFactory, TEMP_ciConfig ci.CIConfig) *cobra.Command {
15+
func NewConfigCmd(loadSaver common.FunctionLoaderSaver, newClient ClientFactory) *cobra.Command {
1816
cmd := &cobra.Command{
1917
Use: "config",
2018
Short: "Configure a function",
@@ -40,7 +38,7 @@ or from the directory specified with --path.
4038
cmd.AddCommand(NewConfigLabelsCmd(loadSaver))
4139
cmd.AddCommand(NewConfigEnvsCmd(loadSaver))
4240
cmd.AddCommand(NewConfigVolumesCmd())
43-
cmd.AddCommand(NewConfigCICmd(loadSaver, TEMP_ciConfig))
41+
cmd.AddCommand(NewConfigCICmd(loadSaver))
4442

4543
return cmd
4644
}

0 commit comments

Comments
 (0)