Skip to content

Commit 5879d91

Browse files
Change --from-dir init flow (#945)
* fix --from-dir flag init Signed-off-by: Mukundan Sundararajan <[email protected]> * fix env var not set for k8s tests also Signed-off-by: Mukundan Sundararajan <[email protected]> * fix k8s test Signed-off-by: Mukundan Sundararajan <[email protected]> * address review comments Signed-off-by: Mukundan Sundararajan <[email protected]> * fix typo Signed-off-by: Mukundan Sundararajan <[email protected]> * addressed comments Signed-off-by: Mukundan Sundararajan <[email protected]> * Update tests/e2e/common/common.go Signed-off-by: Mukundan Sundararajan <[email protected]> Co-authored-by: Shubham Sharma <[email protected]> * fix typo Signed-off-by: Mukundan Sundararajan <[email protected]> Co-authored-by: Shubham Sharma <[email protected]>
1 parent 1331e29 commit 5879d91

File tree

9 files changed

+372
-120
lines changed

9 files changed

+372
-120
lines changed

cmd/init.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cmd
1616
import (
1717
"fmt"
1818
"os"
19+
"strings"
1920

2021
"github.com/spf13/cobra"
2122
"github.com/spf13/viper"
@@ -52,7 +53,8 @@ var InitCmd = &cobra.Command{
5253
# Initialize Dapr in self-hosted mode
5354
dapr init
5455
55-
#Initialize Dapr in self-hosted mode with a provided docker image registry. Image looked up as <registry-url>/<image>
56+
# Initialize Dapr in self-hosted mode with a provided docker image registry. Image looked up as <registry-url>/<image>.
57+
# Check docs or README for more information on the format of the image path that is required.
5658
dapr init --image-registry <registry-url>
5759
5860
# Initialize Dapr in Kubernetes
@@ -103,10 +105,15 @@ dapr init --from-dir <path-to-directory>
103105
dockerNetwork = viper.GetString("network")
104106
imageRegistryURL = viper.GetString("image-registry")
105107
}
106-
if fromDir != "" {
107-
print.WarningStatusEvent(os.Stdout, "Local bundle installation using from-dir flag is currently a preview feature.")
108+
// If both --image-registry and --from-dir flags are given, error out saying only one can be given.
109+
if len(strings.TrimSpace(imageRegistryURL)) != 0 && len(strings.TrimSpace(fromDir)) != 0 {
110+
print.FailureStatusEvent(os.Stderr, "both --image-registry and --from-dir flags cannot be given at the same time")
111+
os.Exit(1)
112+
}
113+
if len(strings.TrimSpace(fromDir)) != 0 {
114+
print.WarningStatusEvent(os.Stdout, "Local bundle installation using --from-dir flag is currently a preview feature and is subject to change. It is only available from CLI version 1.7 onwards.")
108115
}
109-
if imageRegistryURL != "" {
116+
if len(strings.TrimSpace(imageRegistryURL)) != 0 {
110117
print.WarningStatusEvent(os.Stdout, "Flag --image-registry is a preview feature and is subject to change. It is only available from CLI version 1.7 onwards.")
111118
}
112119
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRegistryURL, fromDir)
@@ -142,9 +149,9 @@ func init() {
142149
InitCmd.Flags().BoolVarP(&enableMTLS, "enable-mtls", "", true, "Enable mTLS in your cluster")
143150
InitCmd.Flags().BoolVarP(&enableHA, "enable-ha", "", false, "Enable high availability (HA) mode")
144151
InitCmd.Flags().String("network", "", "The Docker network on which to deploy the Dapr runtime")
145-
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory instead of from network to init")
152+
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory for self-hosted installation")
146153
InitCmd.Flags().BoolP("help", "h", false, "Print this help message")
147154
InitCmd.Flags().StringArrayVar(&values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
148-
InitCmd.Flags().String("image-registry", "", "Custom/Private docker image repository url")
155+
InitCmd.Flags().String("image-registry", "", "Custom/Private docker image repository URL")
149156
RootCmd.AddCommand(InitCmd)
150157
}

pkg/standalone/bundle.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2021 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package standalone
15+
16+
import (
17+
"encoding/json"
18+
"fmt"
19+
"io/ioutil"
20+
"strings"
21+
)
22+
23+
const bundleDetailsFileName = "details.json"
24+
25+
type bundleDetails struct {
26+
RuntimeVersion *string `json:"daprd"`
27+
DashboardVersion *string `json:"dashboard"`
28+
CLIVersion *string `json:"cli"`
29+
BinarySubDir *string `json:"daprBinarySubDir"`
30+
ImageSubDir *string `json:"dockerImageSubDir"`
31+
DaprImageName *string `json:"daprImageName"`
32+
DaprImageFileName *string `json:"daprImageFileName"`
33+
}
34+
35+
// readAndParseDetails reads the file in detailsFilePath and tries to parse it into the bundleDetails struct.
36+
func (b *bundleDetails) readAndParseDetails(detailsFilePath string) error {
37+
bytes, err := ioutil.ReadFile(detailsFilePath)
38+
if err != nil {
39+
return err
40+
}
41+
42+
err = json.Unmarshal(bytes, &b)
43+
if err != nil {
44+
return err
45+
}
46+
if isStringNilOrEmpty(b.RuntimeVersion) || isStringNilOrEmpty(b.DashboardVersion) ||
47+
isStringNilOrEmpty(b.DaprImageName) || isStringNilOrEmpty(b.DaprImageFileName) ||
48+
isStringNilOrEmpty(b.BinarySubDir) || isStringNilOrEmpty(b.ImageSubDir) {
49+
return fmt.Errorf("required fields are missing in %s", detailsFilePath)
50+
}
51+
return nil
52+
}
53+
54+
func isStringNilOrEmpty(val *string) bool {
55+
return val == nil || strings.TrimSpace(*val) == ""
56+
}
57+
58+
func (b *bundleDetails) getPlacementImageName() string {
59+
return *b.DaprImageName
60+
}
61+
62+
func (b *bundleDetails) getPlacementImageFileName() string {
63+
return *b.DaprImageFileName
64+
}

pkg/standalone/bundle_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2021 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package standalone
15+
16+
import (
17+
"os"
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestParseDetails(t *testing.T) {
24+
correctDetails := `{
25+
"daprd" : "1.7.0-rc.2",
26+
"dashboard": "0.10.0-rc.2",
27+
"cli": "1.7.0-rc.2",
28+
"daprBinarySubDir": "dist",
29+
"dockerImageSubDir": "docker",
30+
"daprImageName": "daprio/dapr:1.7.2-rc.2",
31+
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
32+
}`
33+
f, err := os.CreateTemp("", "*-details.json")
34+
if err != nil {
35+
t.Fatalf("error creating temp directory for testing: %s", err)
36+
}
37+
defer os.Remove(f.Name())
38+
f.WriteString(correctDetails)
39+
f.Close()
40+
bd := bundleDetails{}
41+
err = bd.readAndParseDetails(f.Name())
42+
assert.NoError(t, err, "expected no error on parsing correct details in file")
43+
assert.Equal(t, "1.7.0-rc.2", *bd.RuntimeVersion, "expected versions to match")
44+
assert.Equal(t, "0.10.0-rc.2", *bd.DashboardVersion, "expected versions to match")
45+
assert.Equal(t, "dist", *bd.BinarySubDir, "expected value to match")
46+
assert.Equal(t, "docker", *bd.ImageSubDir, "expected value to match")
47+
assert.Equal(t, "daprio/dapr:1.7.2-rc.2", bd.getPlacementImageName(), "expected value to match")
48+
assert.Equal(t, "daprio-dapr-1.7.2-rc.2.tar.gz", bd.getPlacementImageFileName(), "expected value to match")
49+
}
50+
51+
func TestParseDetailsMissingDetails(t *testing.T) {
52+
missingDetails := `{
53+
"daprd" : "1.7.0-rc.2",
54+
"dashboard": "0.10.0-rc.2",
55+
"cli": "1.7.0-rc.2",
56+
"daprImageName": "daprio/dapr:1.7.2-rc.2"
57+
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
58+
}`
59+
f, err := os.CreateTemp("", "*-details.json")
60+
if err != nil {
61+
t.Fatalf("error creating temp directory for testing: %s", err)
62+
}
63+
defer os.Remove(f.Name())
64+
f.WriteString(missingDetails)
65+
f.Close()
66+
bd := bundleDetails{}
67+
err = bd.readAndParseDetails(f.Name())
68+
assert.Error(t, err, "expected error on parsing missing details in file")
69+
}
70+
71+
func TestParseDetailsEmptyDetails(t *testing.T) {
72+
missingDetails := `{
73+
"daprd" : "",
74+
"dashboard": "",
75+
"cli": "1.7.0-rc.2",
76+
"daprBinarySubDir": "dist",
77+
"dockerImageSubDir": "docker",
78+
"daprImageName": "daprio/dapr:1.7.2-rc.2",
79+
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
80+
}`
81+
f, err := os.CreateTemp("", "*-details.json")
82+
if err != nil {
83+
t.Fatalf("error creating temp directory for testing: %s", err)
84+
}
85+
defer os.Remove(f.Name())
86+
f.WriteString(missingDetails)
87+
f.Close()
88+
bd := bundleDetails{}
89+
err = bd.readAndParseDetails(f.Name())
90+
assert.Error(t, err, "expected error on parsing missing details in file")
91+
}
92+
93+
func TestParseDetailsMissingFile(t *testing.T) {
94+
f, err := os.CreateTemp("", "*-details.json")
95+
if err != nil {
96+
t.Fatalf("error creating temp directory for testing: %s", err)
97+
}
98+
f.Close()
99+
os.Remove(f.Name())
100+
bd := bundleDetails{}
101+
err = bd.readAndParseDetails(f.Name())
102+
assert.Error(t, err, "expected error on parsing missing details file")
103+
}

pkg/standalone/docker.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ func runDockerLoad(in io.Reader) error {
5353
return nil
5454
}
5555

56-
func loadDocker(dir string, dockerImage string) error {
56+
func loadDocker(dir string, dockerImageFileName string) error {
5757
var imageFile io.Reader
5858
var err error
59-
imageFile, err = os.Open(path_filepath.Join(dir, imageFileName(dockerImage)))
59+
imageFile, err = os.Open(path_filepath.Join(dir, dockerImageFileName))
6060
if err != nil {
61-
return fmt.Errorf("fail to read docker image file %s: %w", dockerImage, err)
61+
return fmt.Errorf("fail to read docker image file %s: %w", dockerImageFileName, err)
6262
}
6363
err = runDockerLoad(imageFile)
6464
if err != nil {
65-
return fmt.Errorf("fail to load docker image %s: %w", dockerImage, err)
65+
return fmt.Errorf("fail to load docker image from file %s: %w", dockerImageFileName, err)
6666
}
6767

6868
return nil
@@ -121,14 +121,7 @@ func parseDockerError(component string, err error) error {
121121
return err
122122
}
123123

124-
func imageFileName(image string) string {
125-
filename := image + ".tar.gz"
126-
filename = strings.ReplaceAll(filename, "/", "-")
127-
filename = strings.ReplaceAll(filename, ":", "-")
128-
return filename
129-
}
130-
131-
func TryPullImage(imageName string) bool {
124+
func tryPullImage(imageName string) bool {
132125
args := []string{
133126
"pull",
134127
imageName,

0 commit comments

Comments
 (0)