Skip to content

Commit

Permalink
Change --from-dir init flow (#945)
Browse files Browse the repository at this point in the history
* fix --from-dir flag init

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix env var not set for k8s tests also

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix k8s test

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* address review comments

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix typo

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* addressed comments

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* Update tests/e2e/common/common.go

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

Co-authored-by: Shubham Sharma <shubhash@microsoft.com>

* fix typo

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

Co-authored-by: Shubham Sharma <shubhash@microsoft.com>
  • Loading branch information
mukundansundar and shubham1172 authored Apr 4, 2022
1 parent 1331e29 commit 5879d91
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 120 deletions.
19 changes: 13 additions & 6 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -52,7 +53,8 @@ var InitCmd = &cobra.Command{
# Initialize Dapr in self-hosted mode
dapr init
#Initialize Dapr in self-hosted mode with a provided docker image registry. Image looked up as <registry-url>/<image>
# Initialize Dapr in self-hosted mode with a provided docker image registry. Image looked up as <registry-url>/<image>.
# Check docs or README for more information on the format of the image path that is required.
dapr init --image-registry <registry-url>
# Initialize Dapr in Kubernetes
Expand Down Expand Up @@ -103,10 +105,15 @@ dapr init --from-dir <path-to-directory>
dockerNetwork = viper.GetString("network")
imageRegistryURL = viper.GetString("image-registry")
}
if fromDir != "" {
print.WarningStatusEvent(os.Stdout, "Local bundle installation using from-dir flag is currently a preview feature.")
// If both --image-registry and --from-dir flags are given, error out saying only one can be given.
if len(strings.TrimSpace(imageRegistryURL)) != 0 && len(strings.TrimSpace(fromDir)) != 0 {
print.FailureStatusEvent(os.Stderr, "both --image-registry and --from-dir flags cannot be given at the same time")
os.Exit(1)
}
if len(strings.TrimSpace(fromDir)) != 0 {
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.")
}
if imageRegistryURL != "" {
if len(strings.TrimSpace(imageRegistryURL)) != 0 {
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.")
}
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRegistryURL, fromDir)
Expand Down Expand Up @@ -142,9 +149,9 @@ func init() {
InitCmd.Flags().BoolVarP(&enableMTLS, "enable-mtls", "", true, "Enable mTLS in your cluster")
InitCmd.Flags().BoolVarP(&enableHA, "enable-ha", "", false, "Enable high availability (HA) mode")
InitCmd.Flags().String("network", "", "The Docker network on which to deploy the Dapr runtime")
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory instead of from network to init")
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory for self-hosted installation")
InitCmd.Flags().BoolP("help", "h", false, "Print this help message")
InitCmd.Flags().StringArrayVar(&values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
InitCmd.Flags().String("image-registry", "", "Custom/Private docker image repository url")
InitCmd.Flags().String("image-registry", "", "Custom/Private docker image repository URL")
RootCmd.AddCommand(InitCmd)
}
64 changes: 64 additions & 0 deletions pkg/standalone/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package standalone

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)

const bundleDetailsFileName = "details.json"

type bundleDetails struct {
RuntimeVersion *string `json:"daprd"`
DashboardVersion *string `json:"dashboard"`
CLIVersion *string `json:"cli"`
BinarySubDir *string `json:"daprBinarySubDir"`
ImageSubDir *string `json:"dockerImageSubDir"`
DaprImageName *string `json:"daprImageName"`
DaprImageFileName *string `json:"daprImageFileName"`
}

// readAndParseDetails reads the file in detailsFilePath and tries to parse it into the bundleDetails struct.
func (b *bundleDetails) readAndParseDetails(detailsFilePath string) error {
bytes, err := ioutil.ReadFile(detailsFilePath)
if err != nil {
return err
}

err = json.Unmarshal(bytes, &b)
if err != nil {
return err
}
if isStringNilOrEmpty(b.RuntimeVersion) || isStringNilOrEmpty(b.DashboardVersion) ||
isStringNilOrEmpty(b.DaprImageName) || isStringNilOrEmpty(b.DaprImageFileName) ||
isStringNilOrEmpty(b.BinarySubDir) || isStringNilOrEmpty(b.ImageSubDir) {
return fmt.Errorf("required fields are missing in %s", detailsFilePath)
}
return nil
}

func isStringNilOrEmpty(val *string) bool {
return val == nil || strings.TrimSpace(*val) == ""
}

func (b *bundleDetails) getPlacementImageName() string {
return *b.DaprImageName
}

func (b *bundleDetails) getPlacementImageFileName() string {
return *b.DaprImageFileName
}
103 changes: 103 additions & 0 deletions pkg/standalone/bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package standalone

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseDetails(t *testing.T) {
correctDetails := `{
"daprd" : "1.7.0-rc.2",
"dashboard": "0.10.0-rc.2",
"cli": "1.7.0-rc.2",
"daprBinarySubDir": "dist",
"dockerImageSubDir": "docker",
"daprImageName": "daprio/dapr:1.7.2-rc.2",
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
}`
f, err := os.CreateTemp("", "*-details.json")
if err != nil {
t.Fatalf("error creating temp directory for testing: %s", err)
}
defer os.Remove(f.Name())
f.WriteString(correctDetails)
f.Close()
bd := bundleDetails{}
err = bd.readAndParseDetails(f.Name())
assert.NoError(t, err, "expected no error on parsing correct details in file")
assert.Equal(t, "1.7.0-rc.2", *bd.RuntimeVersion, "expected versions to match")
assert.Equal(t, "0.10.0-rc.2", *bd.DashboardVersion, "expected versions to match")
assert.Equal(t, "dist", *bd.BinarySubDir, "expected value to match")
assert.Equal(t, "docker", *bd.ImageSubDir, "expected value to match")
assert.Equal(t, "daprio/dapr:1.7.2-rc.2", bd.getPlacementImageName(), "expected value to match")
assert.Equal(t, "daprio-dapr-1.7.2-rc.2.tar.gz", bd.getPlacementImageFileName(), "expected value to match")
}

func TestParseDetailsMissingDetails(t *testing.T) {
missingDetails := `{
"daprd" : "1.7.0-rc.2",
"dashboard": "0.10.0-rc.2",
"cli": "1.7.0-rc.2",
"daprImageName": "daprio/dapr:1.7.2-rc.2"
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
}`
f, err := os.CreateTemp("", "*-details.json")
if err != nil {
t.Fatalf("error creating temp directory for testing: %s", err)
}
defer os.Remove(f.Name())
f.WriteString(missingDetails)
f.Close()
bd := bundleDetails{}
err = bd.readAndParseDetails(f.Name())
assert.Error(t, err, "expected error on parsing missing details in file")
}

func TestParseDetailsEmptyDetails(t *testing.T) {
missingDetails := `{
"daprd" : "",
"dashboard": "",
"cli": "1.7.0-rc.2",
"daprBinarySubDir": "dist",
"dockerImageSubDir": "docker",
"daprImageName": "daprio/dapr:1.7.2-rc.2",
"daprImageFileName": "daprio-dapr-1.7.2-rc.2.tar.gz"
}`
f, err := os.CreateTemp("", "*-details.json")
if err != nil {
t.Fatalf("error creating temp directory for testing: %s", err)
}
defer os.Remove(f.Name())
f.WriteString(missingDetails)
f.Close()
bd := bundleDetails{}
err = bd.readAndParseDetails(f.Name())
assert.Error(t, err, "expected error on parsing missing details in file")
}

func TestParseDetailsMissingFile(t *testing.T) {
f, err := os.CreateTemp("", "*-details.json")
if err != nil {
t.Fatalf("error creating temp directory for testing: %s", err)
}
f.Close()
os.Remove(f.Name())
bd := bundleDetails{}
err = bd.readAndParseDetails(f.Name())
assert.Error(t, err, "expected error on parsing missing details file")
}
17 changes: 5 additions & 12 deletions pkg/standalone/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ func runDockerLoad(in io.Reader) error {
return nil
}

func loadDocker(dir string, dockerImage string) error {
func loadDocker(dir string, dockerImageFileName string) error {
var imageFile io.Reader
var err error
imageFile, err = os.Open(path_filepath.Join(dir, imageFileName(dockerImage)))
imageFile, err = os.Open(path_filepath.Join(dir, dockerImageFileName))
if err != nil {
return fmt.Errorf("fail to read docker image file %s: %w", dockerImage, err)
return fmt.Errorf("fail to read docker image file %s: %w", dockerImageFileName, err)
}
err = runDockerLoad(imageFile)
if err != nil {
return fmt.Errorf("fail to load docker image %s: %w", dockerImage, err)
return fmt.Errorf("fail to load docker image from file %s: %w", dockerImageFileName, err)
}

return nil
Expand Down Expand Up @@ -121,14 +121,7 @@ func parseDockerError(component string, err error) error {
return err
}

func imageFileName(image string) string {
filename := image + ".tar.gz"
filename = strings.ReplaceAll(filename, "/", "-")
filename = strings.ReplaceAll(filename, ":", "-")
return filename
}

func TryPullImage(imageName string) bool {
func tryPullImage(imageName string) bool {
args := []string{
"pull",
imageName,
Expand Down
Loading

0 comments on commit 5879d91

Please sign in to comment.