Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for --secret #60

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions internal/cmd/local/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/airbytehq/abctl/internal/cmd/local/migrate"
"github.com/airbytehq/abctl/internal/cmd/local/paths"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -203,6 +204,7 @@ type InstallOpts struct {

HelmChartVersion string
ValuesFile string
SecretsFile string
Migrate bool
Host string

Expand Down Expand Up @@ -302,13 +304,13 @@ func (c *Command) persistentVolumeClaim(ctx context.Context, namespace, name, vo

// Install handles the installation of Airbyte
func (c *Command) Install(ctx context.Context, opts InstallOpts) error {
var values string
var vals string
if opts.ValuesFile != "" {
raw, err := os.ReadFile(opts.ValuesFile)
if err != nil {
return fmt.Errorf("unable to read values file '%s': %w", opts.ValuesFile, err)
}
values = string(raw)
vals = string(raw)
}

go c.watchEvents(ctx)
Expand Down Expand Up @@ -369,6 +371,43 @@ func (c *Command) Install(ctx context.Context, opts InstallOpts) error {
airbyteValues = append(airbyteValues, fmt.Sprintf("global.imagePullSecrets[0].name=%s", dockerAuthSecretName))
}

{
if opts.SecretsFile != "" {
c.spinner.UpdateText("Creating secrets from --secrets")

raw, err := os.ReadFile(opts.SecretsFile)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Unable to read secrets file '%s': %s", opts.SecretsFile, err))
return fmt.Errorf("unable to read secrets file '%s': %w", opts.SecretsFile, err)
}
pterm.Debug.Println(string(raw))

var secret corev1.Secret
if err := yaml.Unmarshal(raw, &secret); err != nil {
pterm.Error.Println(fmt.Sprintf("Unable to unmarshal secrets file '%s': %s", opts.SecretsFile, err))
return fmt.Errorf("unable to unmarshal secrets file '%s': %w", opts.SecretsFile, err)
}

pterm.Debug.Println(fmt.Sprintf("%+v", secret))

pterm.Debug.Println(fmt.Sprintf("Found secret\n name: %s\n type: %s", secret.ObjectMeta.Name, secret.Type))
data := secret.Data
if len(secret.StringData) > 0 {
data = map[string][]byte{}
for k, v := range secret.StringData {
data[k] = []byte(v)
}
}

if err := c.k8s.SecretCreateOrUpdate(ctx, secret.Type, airbyteNamespace, secret.ObjectMeta.Name, data); err != nil {
pterm.Error.Println(fmt.Sprintf("Unable to create secret from file '%s'", opts.SecretsFile))
return fmt.Errorf("unable to create secret from file '%s': %w", opts.SecretsFile, err)
}

pterm.Success.Println("Secrets created or updated successfully")
}
}

if err := c.handleChart(ctx, chartRequest{
name: "airbyte",
repoName: airbyteRepoName,
Expand All @@ -378,7 +417,7 @@ func (c *Command) Install(ctx context.Context, opts InstallOpts) error {
chartVersion: opts.HelmChartVersion,
namespace: airbyteNamespace,
values: airbyteValues,
valuesYAML: values,
valuesYAML: vals,
}); err != nil {
return fmt.Errorf("unable to install airbyte chart: %w", err)
}
Expand Down
47 changes: 23 additions & 24 deletions internal/cmd/local/local_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func NewCmdInstall(provider k8s.Provider) *cobra.Command {
flagBasicAuthUser string
flagBasicAuthPass string

flagChartValuesFile string
flagChartVersion string
flagMigrate bool
flagPort int
flagHost string
flagChartValuesFile string
flagChartSecretsFile string
flagChartVersion string
flagMigrate bool
flagPort int
flagHost string

flagDockerServer string
flagDockerUser string
Expand Down Expand Up @@ -135,6 +136,7 @@ func NewCmdInstall(provider k8s.Provider) *cobra.Command {
BasicAuthPass: flagBasicAuthPass,
HelmChartVersion: flagChartVersion,
ValuesFile: flagChartValuesFile,
SecretsFile: flagChartSecretsFile,
Migrate: flagMigrate,
Docker: dockerClient,
Host: flagHost,
Expand All @@ -149,25 +151,12 @@ func NewCmdInstall(provider k8s.Provider) *cobra.Command {
opts.HelmChartVersion = ""
}

if env := os.Getenv(envBasicAuthUser); env != "" {
opts.BasicAuthUser = env
}
if env := os.Getenv(envBasicAuthPass); env != "" {
opts.BasicAuthPass = env
}

if env := os.Getenv(envDockerServer); env != "" {
opts.DockerServer = env
}
if env := os.Getenv(envDockerUser); env != "" {
opts.DockerUser = env
}
if env := os.Getenv(envDockerPass); env != "" {
opts.DockerPass = env
}
if env := os.Getenv(envDockerEmail); env != "" {
opts.DockerEmail = env
}
envOverride(&opts.BasicAuthUser, envBasicAuthUser)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 nice, much cleaner

envOverride(&opts.BasicAuthPass, envBasicAuthPass)
envOverride(&opts.DockerServer, envDockerServer)
envOverride(&opts.DockerUser, envDockerUser)
envOverride(&opts.DockerPass, envDockerPass)
envOverride(&opts.DockerEmail, envDockerEmail)

if err := lc.Install(cmd.Context(), opts); err != nil {
spinner.Fail("Unable to install Airbyte locally")
Expand All @@ -189,6 +178,7 @@ func NewCmdInstall(provider k8s.Provider) *cobra.Command {

cmd.Flags().StringVar(&flagChartVersion, "chart-version", "latest", "specify the Airbyte helm chart version to install")
cmd.Flags().StringVar(&flagChartValuesFile, "values", "", "the Airbyte helm chart values file to load")
cmd.Flags().StringVar(&flagChartSecretsFile, "secrets", "", "the Airbyte helm chart secrets file to load")
cmd.Flags().BoolVar(&flagMigrate, "migrate", false, "migrate data from docker compose installation")

cmd.Flags().StringVar(&flagDockerServer, "docker-server", "https://index.docker.io/v1/", "docker registry, can also be specified via "+envDockerServer)
Expand All @@ -200,3 +190,12 @@ func NewCmdInstall(provider k8s.Provider) *cobra.Command {

return cmd
}

// envOverride checks if the env exists and is not empty, if that is true
// update the original value to be the value returned from the env environment variable.
// Otherwise, leave the original value alone.
func envOverride(original *string, env string) {
if v := os.Getenv(env); v != "" {
*original = v
}
}