mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-09 00:16:46 +00:00
Fix for #201
Old secrets are now copied, rather than moved, so that any existing functions do not need to be redeployed by the user. As a maintenance task, users should remove the older secrets. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
parent
06fbca83bf
commit
195e81f595
@ -109,7 +109,16 @@ func binExists(folder, name string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func ensureSecretsDir(folder string) error {
|
||||||
|
if _, err := os.Stat(folder); err != nil {
|
||||||
|
err = os.MkdirAll(folder, secretDirPermission)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func ensureWorkingDir(folder string) error {
|
func ensureWorkingDir(folder string) error {
|
||||||
if _, err := os.Stat(folder); err != nil {
|
if _, err := os.Stat(folder); err != nil {
|
||||||
err = os.MkdirAll(folder, workingDirectoryPermission)
|
err = os.MkdirAll(folder, workingDirectoryPermission)
|
||||||
|
@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -21,6 +22,8 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const secretDirPermission = 0755
|
||||||
|
|
||||||
func makeProviderCmd() *cobra.Command {
|
func makeProviderCmd() *cobra.Command {
|
||||||
var command = &cobra.Command{
|
var command = &cobra.Command{
|
||||||
Use: "provider",
|
Use: "provider",
|
||||||
@ -82,25 +85,25 @@ func makeProviderCmd() *cobra.Command {
|
|||||||
|
|
||||||
invokeResolver := handlers.NewInvokeResolver(client)
|
invokeResolver := handlers.NewInvokeResolver(client)
|
||||||
|
|
||||||
userSecretPath := path.Join(wd, "secrets")
|
baseUserSecretsPath := path.Join(wd, "secrets")
|
||||||
|
if err := moveSecretsToDefaultNamespaceSecrets(
|
||||||
err = moveSecretsToDefaultNamespaceSecrets(userSecretPath, faasd.FunctionNamespace)
|
baseUserSecretsPath,
|
||||||
if err != nil {
|
faasd.FunctionNamespace); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
bootstrapHandlers := types.FaaSHandlers{
|
bootstrapHandlers := types.FaaSHandlers{
|
||||||
FunctionProxy: proxy.NewHandlerFunc(*config, invokeResolver),
|
FunctionProxy: proxy.NewHandlerFunc(*config, invokeResolver),
|
||||||
DeleteHandler: handlers.MakeDeleteHandler(client, cni),
|
DeleteHandler: handlers.MakeDeleteHandler(client, cni),
|
||||||
DeployHandler: handlers.MakeDeployHandler(client, cni, userSecretPath, alwaysPull),
|
DeployHandler: handlers.MakeDeployHandler(client, cni, baseUserSecretsPath, alwaysPull),
|
||||||
FunctionReader: handlers.MakeReadHandler(client),
|
FunctionReader: handlers.MakeReadHandler(client),
|
||||||
ReplicaReader: handlers.MakeReplicaReaderHandler(client),
|
ReplicaReader: handlers.MakeReplicaReaderHandler(client),
|
||||||
ReplicaUpdater: handlers.MakeReplicaUpdateHandler(client, cni),
|
ReplicaUpdater: handlers.MakeReplicaUpdateHandler(client, cni),
|
||||||
UpdateHandler: handlers.MakeUpdateHandler(client, cni, userSecretPath, alwaysPull),
|
UpdateHandler: handlers.MakeUpdateHandler(client, cni, baseUserSecretsPath, alwaysPull),
|
||||||
HealthHandler: func(w http.ResponseWriter, r *http.Request) {},
|
HealthHandler: func(w http.ResponseWriter, r *http.Request) {},
|
||||||
InfoHandler: handlers.MakeInfoHandler(Version, GitCommit),
|
InfoHandler: handlers.MakeInfoHandler(Version, GitCommit),
|
||||||
ListNamespaceHandler: handlers.MakeNamespacesLister(client),
|
ListNamespaceHandler: handlers.MakeNamespacesLister(client),
|
||||||
SecretHandler: handlers.MakeSecretHandler(client, userSecretPath),
|
SecretHandler: handlers.MakeSecretHandler(client, baseUserSecretsPath),
|
||||||
LogHandler: logs.NewLogHandlerFunc(faasdlogs.New(), config.ReadTimeout),
|
LogHandler: logs.NewLogHandlerFunc(faasdlogs.New(), config.ReadTimeout),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,29 +119,58 @@ func makeProviderCmd() *cobra.Command {
|
|||||||
* Mutiple namespace support was added after release 0.13.0
|
* Mutiple namespace support was added after release 0.13.0
|
||||||
* Function will help users to migrate on multiple namespace support of faasd
|
* Function will help users to migrate on multiple namespace support of faasd
|
||||||
*/
|
*/
|
||||||
func moveSecretsToDefaultNamespaceSecrets(secretPath string, namespace string) error {
|
func moveSecretsToDefaultNamespaceSecrets(baseSecretPath string, defaultNamespace string) error {
|
||||||
newSecretPath := path.Join(secretPath, namespace)
|
newSecretPath := path.Join(baseSecretPath, defaultNamespace)
|
||||||
|
|
||||||
err := ensureWorkingDir(newSecretPath)
|
err := ensureSecretsDir(newSecretPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(secretPath)
|
files, err := ioutil.ReadDir(baseSecretPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if !f.IsDir() {
|
if !f.IsDir() {
|
||||||
oldPath := path.Join(secretPath, f.Name())
|
|
||||||
newPath := path.Join(newSecretPath, f.Name())
|
newPath := path.Join(newSecretPath, f.Name())
|
||||||
err = os.Rename(oldPath, newPath)
|
|
||||||
if err != nil {
|
// A non-nil error means the file wasn't found in the
|
||||||
return err
|
// destination path
|
||||||
|
if _, err := os.Stat(newPath); err != nil {
|
||||||
|
oldPath := path.Join(baseSecretPath, f.Name())
|
||||||
|
|
||||||
|
if err := copyFile(oldPath, newPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[Migration] Copied %s to %s", oldPath, newPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyFile(src, dst string) error {
|
||||||
|
inputFile, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("opening %s failed %w", src, err)
|
||||||
|
}
|
||||||
|
defer inputFile.Close()
|
||||||
|
|
||||||
|
outputFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_APPEND, secretDirPermission)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("opening %s failed %w", dst, err)
|
||||||
|
}
|
||||||
|
defer outputFile.Close()
|
||||||
|
|
||||||
|
// Changed from os.Rename due to issue in #201
|
||||||
|
if _, err := io.Copy(outputFile, inputFile); err != nil {
|
||||||
|
return fmt.Errorf("writing into %s failed %w", outputFile.Name(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user