From 30aafd49e651a8b8e1c85460bbf3209aca7981b5 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 11 Feb 2020 22:20:45 +0000 Subject: [PATCH] Add docker-registry app Adds a docker registry, which can be used through port-forwarding and an Ingress record / TLS cert. As per: https://github.com/alexellis/k8s-tls-registry Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- cmd/app.go | 1 + cmd/apps/registry_app.go | 181 +++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 + 4 files changed, 185 insertions(+) create mode 100644 cmd/apps/registry_app.go diff --git a/cmd/app.go b/cmd/app.go index c442cef8..0045836e 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -63,6 +63,7 @@ command.`, install.AddCommand(apps.MakeInstallIstio()) install.AddCommand(apps.MakeInstallCrossplane()) install.AddCommand(apps.MakeInstallMongoDB()) + install.AddCommand(apps.MakeInstallRegistry()) command.AddCommand(MakeInfo()) diff --git a/cmd/apps/registry_app.go b/cmd/apps/registry_app.go new file mode 100644 index 00000000..b8f35084 --- /dev/null +++ b/cmd/apps/registry_app.go @@ -0,0 +1,181 @@ +package apps + +import ( + "fmt" + "log" + "os" + "path" + + "golang.org/x/crypto/bcrypt" + + "github.com/alexellis/k3sup/pkg" + "github.com/alexellis/k3sup/pkg/config" + "github.com/alexellis/k3sup/pkg/env" + "github.com/alexellis/k3sup/pkg/helm" + "github.com/sethvargo/go-password/password" + "github.com/spf13/cobra" +) + +func MakeInstallRegistry() *cobra.Command { + var registry = &cobra.Command{ + Use: "docker-registry", + Short: "Install a Docker registry", + Long: `Install a Docker registry`, + Example: ` k3sup app install registry --namespace default`, + SilenceUsage: true, + } + + registry.Flags().StringP("namespace", "n", "default", "The namespace used for installation") + registry.Flags().Bool("update-repo", true, "Update the helm repo") + registry.Flags().Bool("helm3", true, "Use helm3, if set to false uses helm2") + registry.Flags().StringP("username", "u", "admin", "Username for the registry") + registry.Flags().StringP("password", "p", "", "Password for the registry, leave blank to generate") + + registry.RunE = func(command *cobra.Command, args []string) error { + kubeConfigPath := getDefaultKubeconfig() + + if command.Flags().Changed("kubeconfig") { + kubeConfigPath, _ = command.Flags().GetString("kubeconfig") + } + + updateRepo, _ := registry.Flags().GetBool("update-repo") + + fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath) + helm3, _ := command.Flags().GetBool("helm3") + + if helm3 { + fmt.Println("Using helm3") + } + + userPath, err := config.InitUserDir() + if err != nil { + return err + } + namespace, _ := command.Flags().GetString("namespace") + if namespace != "default" { + return fmt.Errorf(`to override the "default", install via tiller`) + } + + clientArch, clientOS := env.GetClientArch() + + fmt.Printf("Client: %s, %s\n", clientArch, clientOS) + log.Printf("User dir established as: %s\n", userPath) + + os.Setenv("HELM_HOME", path.Join(userPath, ".helm")) + + _, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, helm3) + if err != nil { + return err + } + + username, _ := command.Flags().GetString("username") + + pass, _ := command.Flags().GetString("password") + if len(pass) == 0 { + key, err := password.Generate(20, 10, 0, false, true) + if err != nil { + return err + } + + pass = key + } + + val, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost) + + if err != nil { + return err + } + + htPasswd := fmt.Sprintf("%s:%s\n", username, string(val)) + + err = addHelmRepo("stable", "https://kubernetes-charts.storage.googleapis.com", helm3) + if err != nil { + return err + } + + if updateRepo { + err = updateHelmRepos(helm3) + if err != nil { + return err + } + } + + chartPath := path.Join(os.TempDir(), "charts") + err = fetchChart(chartPath, "stable/docker-registry", helm3) + + if err != nil { + return err + } + + overrides := map[string]string{} + + overrides["persistence.enabled"] = "false" + overrides["secrets.htpasswd"] = string(htPasswd) + + arch := getNodeArchitecture() + fmt.Printf("Node architecture: %q\n", arch) + + fmt.Println("Chart path: ", chartPath) + + wait := false + ns := "default" + + if helm3 { + outputPath := path.Join(chartPath, "docker-registry") + + err := helm3Upgrade(outputPath, "stable/docker-registry", ns, + "values.yaml", + overrides, wait) + + if err != nil { + return err + } + } else { + outputPath := path.Join(chartPath, "docker-registry/rendered") + + err = templateChart(chartPath, + "docker-registry", + ns, + outputPath, + "values.yaml", + overrides) + + if err != nil { + return err + } + + err = kubectl("apply", "-R", "-f", outputPath) + + if err != nil { + return err + } + } + + fmt.Println(registryIngressInstallMsg) + fmt.Printf("Registry credentials: %s %s\nexport PASSWORD=%s\n", username, pass, pass) + + return nil + } + + return registry +} + +const registryIngressInfoMsg = `# Your docker-registry has been configured + +kubectl logs deploy/docker-registry + +export IP="192.168.0.11" # Set to WiFI/ethernet adapter +export PASSWORD="" # See below +kubectl port-forward svc/docker-registry --address 0.0.0.0 5000 & + +docker login $IP:5000 --username admin --password $PASSWORD +docker tag alpine:3.11 $IP:5000/alpine:3.11 +docker push $IP:5000/alpine:3.11 + +# Find out more at: +# https://github.com/helm/charts/tree/master/stable/registry` + +const registryIngressInstallMsg = `======================================================================= += docker-registry has been installed. = +=======================================================================` + + "\n\n" + registryIngressInfoMsg + "\n\n" + pkg.ThanksForUsing diff --git a/go.mod b/go.mod index d9b2bdc9..d6531faa 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/alexellis/k3sup go 1.12 require ( + github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46 github.com/alexellis/go-execute v0.0.0-20191207085904-961405ea7544 github.com/mitchellh/go-homedir v1.1.0 github.com/morikuni/aec v1.0.0 diff --git a/go.sum b/go.sum index 33ea4af2..1f1698ee 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46 h1:rs0kDBt2zF4/CM9rO5/iH+U22jnTygPlqWgX55Ufcxg= +github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46/go.mod h1:kC29dT1vFpj7py2OvG1khBdQpo3kInWP+6QipLbdngo= github.com/alexellis/go-execute v0.0.0-20191207085904-961405ea7544 h1:KjtKgzxk/0wfp7vZxWUYPMiEJKep7FJ7C7o/vtHyc/Q= github.com/alexellis/go-execute v0.0.0-20191207085904-961405ea7544/go.mod h1:zfRbgnPVxXCSpiKrg1CE72hNUWInqxExiaz2D9ppTts= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=