Skip to content

Commit

Permalink
1.update scan device logical; 2.support secret update tool
Browse files Browse the repository at this point in the history
  • Loading branch information
doubletao318 committed Aug 5, 2021
1 parent a63d1b3 commit e59580d
Show file tree
Hide file tree
Showing 47 changed files with 1,627 additions and 457 deletions.
10 changes: 6 additions & 4 deletions Makefile-CSI
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PLATFORM=PLATFORM
export GOPATH:=$(GOPATH):$(shell pwd)
$(info ${GOPATH})

all:COMMON_1 DIFF

COMMON_1:
Expand All @@ -9,10 +9,12 @@ COMMON_1:
DIFF:
ifeq (${PLATFORM}, X86)
go build -o ./bin/huawei-csi ./src/csi
go build -o ./bin/secretGenerate ./src/tools/secretGenerate
go build -o ./${PACKAGE}/bin/secretGenerate ./src/tools/secretGenerate
go build -o ./${PACKAGE}/bin/secretUpdate ./src/tools/secretUpdate
endif

ifeq (${PLATFORM}, ARM)
GOOS=linux GOARCH=arm64 go build -o ./bin/huawei-csi ./src/csi
GOOS=linux GOARCH=arm64 go build -o ./bin/secretGenerate ./src/tools/secretGenerate
endif
GOOS=linux GOARCH=arm64 go build -o ./${PACKAGE}/bin/secretGenerate ./src/tools/secretGenerate
GOOS=linux GOARCH=arm64 go build -o ./${PACKAGE}/bin/secretUpdate ./src/tools/secretUpdate
endif
33 changes: 26 additions & 7 deletions src/cli/client/cli_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Interface interface {
GetConfigMap(configMapName string) (*v1.ConfigMap, error)
CheckConfigMapExists(configMapName string) (bool, error)
CreateObjectByYAML(yaml string) error
GetSecret(secretName string) (*v1.Secret, error)
}

func NewCliClient(namespace string, k8sTimeout time.Duration) (Interface, error) {
Expand Down Expand Up @@ -256,6 +257,29 @@ func (c *KubectlClient) getCurrentNamespace() (string, error) {
return namespace, nil
}

// GetSecret get secret object by name
func (c *KubectlClient) GetSecret(secretName string) (*v1.Secret, error) {
cmdArgs := []string{"get", "secret", secretName, "--namespace", c.namespace, "-o=json"}
cmd := exec.Command(c.cli, cmdArgs...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}

var createdSecret v1.Secret
if err := json.NewDecoder(stdout).Decode(&createdSecret); err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
return nil, err
}

return &createdSecret, nil
}

func (c *KubectlClient) CheckSecretExists(secretName string) (bool, error) {
args := []string{"get", "secret", secretName, "--namespace", c.namespace, "--ignore-not-found"}
out, err := exec.Command(c.cli, args...).CombinedOutput()
Expand Down Expand Up @@ -341,7 +365,7 @@ func (c *KubectlClient) createObjectByYAML(yaml string) error {

go func() {
defer stdin.Close()
_, _ = stdin.Write([]byte(yaml)) //nolint
_, _ = stdin.Write([]byte(yaml)) // nolint
}()

out, err := cmd.CombinedOutput()
Expand All @@ -355,25 +379,20 @@ func (c *KubectlClient) createObjectByYAML(yaml string) error {
}

func (c *KubectlClient) updateObjectByYAML(yaml string) error {

args := []string{fmt.Sprintf("--namespace=%s", c.namespace), "apply", "-f", "-"}
cmd := exec.Command(c.cli, args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}

go func() {
defer stdin.Close()
_, _ = stdin.Write([]byte(yaml)) //nolint
_, _ = stdin.Write([]byte(yaml)) // nolint
}()

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s; %v", string(out), err)
}

log.Debug("Applied changes to Kubernetes object by YAML.")

return nil
}
89 changes: 64 additions & 25 deletions src/cli/command/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

// CSIConfig is to store the base config object
type CSIConfig struct {
Backends []map[string]interface{} `json:"backends"`
}

// CSISecret is to store the secret object
type CSISecret struct {
Secrets map[string]interface{} `json:"secrets"`
}

const (
KubernetesCSIVersionMin = "v1.13.0"

Expand All @@ -31,11 +37,12 @@ const (
var (
client k8sClient.Interface
storageConfig CSIConfig
storageSecret CSISecret
storageNamespace string
)

func Init() {
if len(os.Args) >= 2 {
if len(os.Args) >= inputArgsLength {
storageNamespace = os.Args[1]
} else {
storageNamespace = HUAWEINamespace
Expand Down Expand Up @@ -79,7 +86,7 @@ func installSecret() {
func initClient() (k8sClient.Interface, error) {
client, err := k8sClient.NewCliClient(storageNamespace, 180*time.Second)
if err != nil {
return nil, fmt.Errorf("could not new a Kubernetes client, err: %v", err)
return nil, fmt.Errorf("Could not new a Kubernetes client, err: %v", err)
}

return client, nil
Expand Down Expand Up @@ -145,6 +152,58 @@ func terminalInput(keyText, objectName string) (string, error) {
return plainText, nil
}

func getPassword(keyText, backendName string) string {
for {
fmt.Printf("Enter backend %s's password: \n", backendName)
inputPassword1, err := terminalInput(keyText, "password")
if err != nil {
msg := fmt.Sprintf("Input password error %v, try it again", err)
log.Errorln(msg)
continue
}

fmt.Println("Please enter the password again: ")
inputPassword2, err := terminalInput(keyText, "password")
if err != nil {
msg := fmt.Sprintf("Input password error %v, try it again", err)
log.Errorln(msg)
continue
}

if inputPassword1 != inputPassword2 {
msg := fmt.Sprintln("The two passwords are inconsistent. Please enter again.")
log.Errorln(msg)
fmt.Println(msg)
} else {
return inputPassword1
}
}
}

func generateSecret(backendName string) (map[string]string, error) {
keyText, err := generateKeyText()
if err != nil {
log.Errorf("Generate random string error %v", err)
return nil, err
}

fmt.Printf("Enter backend %s's user: ", backendName)
inputUser, err := terminalInput(keyText, "user")
if err != nil {
log.Errorf("Input user error %v", err)
return nil, err
}
fmt.Printf("%s\n", inputUser)

inputPassword := getPassword(keyText, backendName)
secretInfo := map[string]string{
"user": inputUser,
"password": inputPassword,
"keyText": keyText,
}
return secretInfo, nil
}

func createSecret() error {
// step 1. query the configMap to get the all backend names
configMap, err := client.GetConfigMap(HUAWEICSIConfigMap)
Expand Down Expand Up @@ -175,31 +234,11 @@ func createSecret() error {
fmt.Println(msg)
log.Infoln(msg)

keyText, err := generateKeyText()
secretInfo, err := generateSecret(config["name"].(string))
if err != nil {
log.Errorf("Generate random string error %v", err)
recordErrorf("generate Secret info error: %v", err)
return err
}

fmt.Println("Enter backend user: ")
inputUser, err := terminalInput(keyText, "user")
if err != nil {
log.Errorf("Input user error %v", err)
return err
}

fmt.Println("Enter backend password: ")
inputPassword, err := terminalInput(keyText, "password")
if err != nil {
log.Errorf("Input password error %v", err)
return err
}

secretInfo := map[string]string{
"user": inputUser,
"password": inputPassword,
"keyText": keyText,
}
secretBytes, _ := json.Marshal(secretInfo)
secretMap[config["name"].(string)] = string(secretBytes)
fmt.Printf("\n")
Expand All @@ -214,6 +253,6 @@ func createSecret() error {
log.Errorf("could not create Huawei CSI Secret; secret YAML: %s, err: %v", secretYAML, err)
return err
}
log.Infoln("Created Huawei csi secret.")
recordInfof("*********************Create CSI Secret Successful**********************\n")
return nil
}
Loading

0 comments on commit e59580d

Please sign in to comment.