Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add image pull policy #1462

Merged
merged 10 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 1 addition & 1 deletion pkg/kubernetes/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func createDeploymentConfig(client versioned.Interface, app runfileconfig.App) d
Name: app.AppID,
Image: app.ContainerImage,
Env: getEnv(app),
ImagePullPolicy: corev1.PullAlways,
ImagePullPolicy: corev1.PullPolicy(app.ContainerImagePullPolicy),
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/runfileconfig/run_file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type RunFileConfig struct {

// ContainerConfiguration represents the application container configuration parameters.
type ContainerConfiguration struct {
ContainerImage string `yaml:"containerImage"`
CreateService bool `yaml:"createService"`
ContainerImage string `yaml:"containerImage"`
ContainerImagePullPolicy string `yaml:"containerImagePullPolicy"`
CreateService bool `yaml:"createService"`
}

// App represents the configuration options for the apps in the run file.
Expand Down
11 changes: 11 additions & 0 deletions pkg/runfileconfig/run_file_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"gopkg.in/yaml.v2"
)

var imagePullPolicyValuesAllowed = []string{"Always", "Never", "IfNotPresent"}

// Parse the provided run file into a RunFileConfig struct.
func (a *RunFileConfig) parseAppsConfig(runFilePath string) error {
var err error
Expand Down Expand Up @@ -97,6 +99,15 @@
if len(strings.TrimSpace(a.Apps[i].ResourcesPath)) > 0 {
a.Apps[i].ResourcesPaths = append(a.Apps[i].ResourcesPaths, a.Apps[i].ResourcesPath)
}

// Check containerImagePullPolicy is valid

Check failure on line 103 in pkg/runfileconfig/run_file_config_parser.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Comment should end in a period (godot)
if a.Apps[i].ContainerImagePullPolicy != "" {
if !utils.Contains(imagePullPolicyValuesAllowed, a.Apps[i].ContainerImagePullPolicy) {
return fmt.Errorf("invalid containerImagePullPolicy: %s, allowed values: %s", a.Apps[i].ContainerImagePullPolicy, strings.Join(imagePullPolicyValuesAllowed, ", "))
}
} else {
a.Apps[i].ContainerImagePullPolicy = "Always"
}
}
return nil
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/runfileconfig/run_file_config_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
runFileForPrecedenceRuleDaprDir = filepath.Join(".", "testdata", "test_run_config_precedence_rule_dapr_dir.yaml")
runFileForLogDestination = filepath.Join(".", "testdata", "test_run_config_log_destination.yaml")
runFileForMultiResourcePaths = filepath.Join(".", "testdata", "test_run_config_multiple_resources_paths.yaml")

runFileForContainerImagePullPolicy = filepath.Join(".", "testdata", "test_run_config_container_image_pull_policy.yaml")
runFileForContainerImagePullPolicyInvalid = filepath.Join(".", "testdata", "test_run_config_container_image_pull_policy_invalid.yaml")
)

func TestRunConfigFile(t *testing.T) {

Check failure on line 40 in pkg/runfileconfig/run_file_config_parser_test.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Function name: TestRunConfigFile, Cyclomatic Complexity: 4, Halstead Volume: 8579.99, Maintainability Index: 19 (maintidx)
t.Parallel()
t.Run("test parse valid run template", func(t *testing.T) {
appsRunConfig := RunFileConfig{}
Expand Down Expand Up @@ -144,6 +147,34 @@
}
})

t.Run("test containerImagePullPolicy", func(t *testing.T) {
t.Run("default value is Always", func(t *testing.T) {
config := RunFileConfig{}
config.parseAppsConfig(validRunFilePath)
err := config.validateRunConfig(runFileForPrecedenceRuleDaprDir)
assert.NoError(t, err)
assert.Equal(t, "Always", config.Apps[0].ContainerImagePullPolicy)
assert.Equal(t, "Always", config.Apps[1].ContainerImagePullPolicy)
})

t.Run("custom value is respected", func(t *testing.T) {
config := RunFileConfig{}
config.parseAppsConfig(runFileForContainerImagePullPolicy)
err := config.validateRunConfig(runFileForContainerImagePullPolicy)
assert.NoError(t, err)
assert.Equal(t, "IfNotPresent", config.Apps[0].ContainerImagePullPolicy)
assert.Equal(t, "Always", config.Apps[1].ContainerImagePullPolicy)
})

t.Run("invalid value is rejected", func(t *testing.T) {
config := RunFileConfig{}
config.parseAppsConfig(runFileForContainerImagePullPolicyInvalid)
err := config.validateRunConfig(runFileForContainerImagePullPolicyInvalid)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid containerImagePullPolicy: Invalid, allowed values: Always, Never, IfNotPresent")
})
})

t.Run("test precedence logic with daprInstallDir for resources-path and dapr config file", func(t *testing.T) {
config := RunFileConfig{}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 1
common:
resourcesPath: ./app/resources
appProtocol: HTTP
appHealthProbeTimeout: 10
env:
DEBUG: false
tty: sts
apps:
- appDirPath: ./webapp/
resourcesPath: ./resources
configFilePath: ./config.yaml
appPort: 8080
appHealthProbeTimeout: 1
containerImagePullPolicy: IfNotPresent
containerImage: ghcr.io/dapr/dapr-workflows-python-sdk:latest
- appID: backend
appDirPath: ./backend/
appProtocol: GRPC
appPort: 3000
unixDomainSocket: /tmp/test-socket
env:
DEBUG: true
containerImage: ghcr.io/dapr/dapr-workflows-csharp-sdk:latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 1
common:
resourcesPath: ./app/resources
appProtocol: HTTP
appHealthProbeTimeout: 10
env:
DEBUG: false
tty: sts
apps:
- appDirPath: ./webapp/
resourcesPath: ./resources
configFilePath: ./config.yaml
appPort: 8080
appHealthProbeTimeout: 1
containerImagePullPolicy: Invalid
containerImage: ghcr.io/dapr/dapr-workflows-python-sdk:latest
- appID: backend
appDirPath: ./backend/
appProtocol: GRPC
appPort: 3000
unixDomainSocket: /tmp/test-socket
env:
DEBUG: true
containerImage: ghcr.io/dapr/dapr-workflows-csharp-sdk:latest
Loading