Skip to content

Commit efe1d6c

Browse files
filintodantontroshinmikeee
authored
add image pull policy (#1462)
* add image pull policy Signed-off-by: Filinto Duran <[email protected]> * add allowed values Signed-off-by: Filinto Duran <[email protected]> * feedback refactor allowed values name Signed-off-by: Filinto Duran <[email protected]> * add unit tests Signed-off-by: Filinto Duran <[email protected]> * lint Signed-off-by: Filinto Duran <[email protected]> * lint Signed-off-by: Filinto Duran <[email protected]> * more lint Signed-off-by: Filinto Duran <[email protected]> * more lint Signed-off-by: Filinto Duran <[email protected]> --------- Signed-off-by: Filinto Duran <[email protected]> Co-authored-by: Anton Troshin <[email protected]> Co-authored-by: Mike Nguyen <[email protected]>
1 parent dbbe022 commit efe1d6c

6 files changed

+112
-3
lines changed

pkg/kubernetes/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func createDeploymentConfig(client versioned.Interface, app runfileconfig.App) d
297297
Name: app.AppID,
298298
Image: app.ContainerImage,
299299
Env: getEnv(app),
300-
ImagePullPolicy: corev1.PullAlways,
300+
ImagePullPolicy: corev1.PullPolicy(app.ContainerImagePullPolicy),
301301
},
302302
},
303303
},

pkg/runfileconfig/run_file_config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ type RunFileConfig struct {
4141

4242
// ContainerConfiguration represents the application container configuration parameters.
4343
type ContainerConfiguration struct {
44-
ContainerImage string `yaml:"containerImage"`
45-
CreateService bool `yaml:"createService"`
44+
ContainerImage string `yaml:"containerImage"`
45+
ContainerImagePullPolicy string `yaml:"containerImagePullPolicy"`
46+
CreateService bool `yaml:"createService"`
4647
}
4748

4849
// App represents the configuration options for the apps in the run file.

pkg/runfileconfig/run_file_config_parser.go

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"gopkg.in/yaml.v2"
2727
)
2828

29+
var imagePullPolicyValuesAllowed = []string{"Always", "Never", "IfNotPresent"}
30+
2931
// Parse the provided run file into a RunFileConfig struct.
3032
func (a *RunFileConfig) parseAppsConfig(runFilePath string) error {
3133
var err error
@@ -97,6 +99,15 @@ func (a *RunFileConfig) validateRunConfig(runFilePath string) error {
9799
if len(strings.TrimSpace(a.Apps[i].ResourcesPath)) > 0 {
98100
a.Apps[i].ResourcesPaths = append(a.Apps[i].ResourcesPaths, a.Apps[i].ResourcesPath)
99101
}
102+
103+
// Check containerImagePullPolicy is valid.
104+
if a.Apps[i].ContainerImagePullPolicy != "" {
105+
if !utils.Contains(imagePullPolicyValuesAllowed, a.Apps[i].ContainerImagePullPolicy) {
106+
return fmt.Errorf("invalid containerImagePullPolicy: %s, allowed values: %s", a.Apps[i].ContainerImagePullPolicy, strings.Join(imagePullPolicyValuesAllowed, ", "))
107+
}
108+
} else {
109+
a.Apps[i].ContainerImagePullPolicy = "Always"
110+
}
100111
}
101112
return nil
102113
}

pkg/runfileconfig/run_file_config_parser_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
package runfileconfig
1515

1616
import (
17+
"fmt"
1718
"os"
1819
"path/filepath"
1920
"strings"
@@ -32,6 +33,9 @@ var (
3233
runFileForPrecedenceRuleDaprDir = filepath.Join(".", "testdata", "test_run_config_precedence_rule_dapr_dir.yaml")
3334
runFileForLogDestination = filepath.Join(".", "testdata", "test_run_config_log_destination.yaml")
3435
runFileForMultiResourcePaths = filepath.Join(".", "testdata", "test_run_config_multiple_resources_paths.yaml")
36+
37+
runFileForContainerImagePullPolicy = filepath.Join(".", "testdata", "test_run_config_container_image_pull_policy.yaml")
38+
runFileForContainerImagePullPolicyInvalid = filepath.Join(".", "testdata", "test_run_config_container_image_pull_policy_invalid.yaml")
3539
)
3640

3741
func TestRunConfigFile(t *testing.T) {
@@ -251,6 +255,51 @@ func TestRunConfigFile(t *testing.T) {
251255
})
252256
}
253257

258+
func TestContainerImagePullPolicy(t *testing.T) {
259+
testcases := []struct {
260+
name string
261+
runfFile string
262+
expectedPullPolicies []string
263+
expectedBadPolicyValue string
264+
expectedErr bool
265+
}{
266+
{
267+
name: "default value is Always",
268+
runfFile: validRunFilePath,
269+
expectedPullPolicies: []string{"Always", "Always"},
270+
expectedErr: false,
271+
},
272+
{
273+
name: "custom value is respected",
274+
runfFile: runFileForContainerImagePullPolicy,
275+
expectedPullPolicies: []string{"IfNotPresent", "Always"},
276+
expectedErr: false,
277+
},
278+
{
279+
name: "invalid value is rejected",
280+
runfFile: runFileForContainerImagePullPolicyInvalid,
281+
expectedPullPolicies: []string{"Always", "Always"},
282+
expectedBadPolicyValue: "Invalid",
283+
expectedErr: true,
284+
},
285+
}
286+
287+
for _, tc := range testcases {
288+
t.Run(tc.name, func(t *testing.T) {
289+
config := RunFileConfig{}
290+
config.parseAppsConfig(tc.runfFile)
291+
err := config.validateRunConfig(tc.runfFile)
292+
if tc.expectedErr {
293+
assert.Error(t, err)
294+
assert.Contains(t, err.Error(), fmt.Sprintf("invalid containerImagePullPolicy: %s, allowed values: Always, Never, IfNotPresent", tc.expectedBadPolicyValue))
295+
return
296+
}
297+
assert.Equal(t, tc.expectedPullPolicies[0], config.Apps[0].ContainerImagePullPolicy)
298+
assert.Equal(t, tc.expectedPullPolicies[1], config.Apps[1].ContainerImagePullPolicy)
299+
})
300+
}
301+
}
302+
254303
func TestMultiResourcePathsResolution(t *testing.T) {
255304
config := RunFileConfig{}
256305

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: 1
2+
common:
3+
resourcesPath: ./app/resources
4+
appProtocol: HTTP
5+
appHealthProbeTimeout: 10
6+
env:
7+
DEBUG: false
8+
tty: sts
9+
apps:
10+
- appDirPath: ./webapp/
11+
resourcesPath: ./resources
12+
configFilePath: ./config.yaml
13+
appPort: 8080
14+
appHealthProbeTimeout: 1
15+
containerImagePullPolicy: IfNotPresent
16+
containerImage: ghcr.io/dapr/dapr-workflows-python-sdk:latest
17+
- appID: backend
18+
appDirPath: ./backend/
19+
appProtocol: GRPC
20+
appPort: 3000
21+
unixDomainSocket: /tmp/test-socket
22+
env:
23+
DEBUG: true
24+
containerImage: ghcr.io/dapr/dapr-workflows-csharp-sdk:latest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: 1
2+
common:
3+
resourcesPath: ./app/resources
4+
appProtocol: HTTP
5+
appHealthProbeTimeout: 10
6+
env:
7+
DEBUG: false
8+
tty: sts
9+
apps:
10+
- appDirPath: ./webapp/
11+
resourcesPath: ./resources
12+
configFilePath: ./config.yaml
13+
appPort: 8080
14+
appHealthProbeTimeout: 1
15+
containerImagePullPolicy: Invalid
16+
containerImage: ghcr.io/dapr/dapr-workflows-python-sdk:latest
17+
- appID: backend
18+
appDirPath: ./backend/
19+
appProtocol: GRPC
20+
appPort: 3000
21+
unixDomainSocket: /tmp/test-socket
22+
env:
23+
DEBUG: true
24+
containerImage: ghcr.io/dapr/dapr-workflows-csharp-sdk:latest

0 commit comments

Comments
 (0)