mirror of https://github.com/dapr/cli.git
add image pull policy (#1462)
* add image pull policy Signed-off-by: Filinto Duran <filinto@diagrid.io> * add allowed values Signed-off-by: Filinto Duran <filinto@diagrid.io> * feedback refactor allowed values name Signed-off-by: Filinto Duran <filinto@diagrid.io> * add unit tests Signed-off-by: Filinto Duran <filinto@diagrid.io> * lint Signed-off-by: Filinto Duran <filinto@diagrid.io> * lint Signed-off-by: Filinto Duran <filinto@diagrid.io> * more lint Signed-off-by: Filinto Duran <filinto@diagrid.io> * more lint Signed-off-by: Filinto Duran <filinto@diagrid.io> --------- Signed-off-by: Filinto Duran <filinto@diagrid.io> Co-authored-by: Anton Troshin <anton@diagrid.io> Co-authored-by: Mike Nguyen <hey@mike.ee>
This commit is contained in:
parent
dbbe022a8a
commit
efe1d6c1e2
|
@ -297,7 +297,7 @@ func createDeploymentConfig(client versioned.Interface, app runfileconfig.App) d
|
||||||
Name: app.AppID,
|
Name: app.AppID,
|
||||||
Image: app.ContainerImage,
|
Image: app.ContainerImage,
|
||||||
Env: getEnv(app),
|
Env: getEnv(app),
|
||||||
ImagePullPolicy: corev1.PullAlways,
|
ImagePullPolicy: corev1.PullPolicy(app.ContainerImagePullPolicy),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -41,8 +41,9 @@ type RunFileConfig struct {
|
||||||
|
|
||||||
// ContainerConfiguration represents the application container configuration parameters.
|
// ContainerConfiguration represents the application container configuration parameters.
|
||||||
type ContainerConfiguration struct {
|
type ContainerConfiguration struct {
|
||||||
ContainerImage string `yaml:"containerImage"`
|
ContainerImage string `yaml:"containerImage"`
|
||||||
CreateService bool `yaml:"createService"`
|
ContainerImagePullPolicy string `yaml:"containerImagePullPolicy"`
|
||||||
|
CreateService bool `yaml:"createService"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// App represents the configuration options for the apps in the run file.
|
// App represents the configuration options for the apps in the run file.
|
||||||
|
|
|
@ -26,6 +26,8 @@ import (
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var imagePullPolicyValuesAllowed = []string{"Always", "Never", "IfNotPresent"}
|
||||||
|
|
||||||
// Parse the provided run file into a RunFileConfig struct.
|
// Parse the provided run file into a RunFileConfig struct.
|
||||||
func (a *RunFileConfig) parseAppsConfig(runFilePath string) error {
|
func (a *RunFileConfig) parseAppsConfig(runFilePath string) error {
|
||||||
var err error
|
var err error
|
||||||
|
@ -97,6 +99,15 @@ func (a *RunFileConfig) validateRunConfig(runFilePath string) error {
|
||||||
if len(strings.TrimSpace(a.Apps[i].ResourcesPath)) > 0 {
|
if len(strings.TrimSpace(a.Apps[i].ResourcesPath)) > 0 {
|
||||||
a.Apps[i].ResourcesPaths = append(a.Apps[i].ResourcesPaths, a.Apps[i].ResourcesPath)
|
a.Apps[i].ResourcesPaths = append(a.Apps[i].ResourcesPaths, a.Apps[i].ResourcesPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check containerImagePullPolicy is valid.
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ limitations under the License.
|
||||||
package runfileconfig
|
package runfileconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -32,6 +33,9 @@ var (
|
||||||
runFileForPrecedenceRuleDaprDir = filepath.Join(".", "testdata", "test_run_config_precedence_rule_dapr_dir.yaml")
|
runFileForPrecedenceRuleDaprDir = filepath.Join(".", "testdata", "test_run_config_precedence_rule_dapr_dir.yaml")
|
||||||
runFileForLogDestination = filepath.Join(".", "testdata", "test_run_config_log_destination.yaml")
|
runFileForLogDestination = filepath.Join(".", "testdata", "test_run_config_log_destination.yaml")
|
||||||
runFileForMultiResourcePaths = filepath.Join(".", "testdata", "test_run_config_multiple_resources_paths.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) {
|
func TestRunConfigFile(t *testing.T) {
|
||||||
|
@ -251,6 +255,51 @@ func TestRunConfigFile(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContainerImagePullPolicy(t *testing.T) {
|
||||||
|
testcases := []struct {
|
||||||
|
name string
|
||||||
|
runfFile string
|
||||||
|
expectedPullPolicies []string
|
||||||
|
expectedBadPolicyValue string
|
||||||
|
expectedErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default value is Always",
|
||||||
|
runfFile: validRunFilePath,
|
||||||
|
expectedPullPolicies: []string{"Always", "Always"},
|
||||||
|
expectedErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "custom value is respected",
|
||||||
|
runfFile: runFileForContainerImagePullPolicy,
|
||||||
|
expectedPullPolicies: []string{"IfNotPresent", "Always"},
|
||||||
|
expectedErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid value is rejected",
|
||||||
|
runfFile: runFileForContainerImagePullPolicyInvalid,
|
||||||
|
expectedPullPolicies: []string{"Always", "Always"},
|
||||||
|
expectedBadPolicyValue: "Invalid",
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testcases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
config := RunFileConfig{}
|
||||||
|
config.parseAppsConfig(tc.runfFile)
|
||||||
|
err := config.validateRunConfig(tc.runfFile)
|
||||||
|
if tc.expectedErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), fmt.Sprintf("invalid containerImagePullPolicy: %s, allowed values: Always, Never, IfNotPresent", tc.expectedBadPolicyValue))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.expectedPullPolicies[0], config.Apps[0].ContainerImagePullPolicy)
|
||||||
|
assert.Equal(t, tc.expectedPullPolicies[1], config.Apps[1].ContainerImagePullPolicy)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMultiResourcePathsResolution(t *testing.T) {
|
func TestMultiResourcePathsResolution(t *testing.T) {
|
||||||
config := RunFileConfig{}
|
config := RunFileConfig{}
|
||||||
|
|
||||||
|
|
|
@ -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
|
24
pkg/runfileconfig/testdata/test_run_config_container_image_pull_policy_invalid.yaml
vendored
Normal file
24
pkg/runfileconfig/testdata/test_run_config_container_image_pull_policy_invalid.yaml
vendored
Normal file
|
@ -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…
Reference in New Issue