Skip to content

Commit

Permalink
fix(kube): assorted fixes related to running microk8s
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveRuble committed Mar 8, 2021
1 parent 76fe37e commit f86754b
Show file tree
Hide file tree
Showing 23 changed files with 327 additions and 140 deletions.
33 changes: 15 additions & 18 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,29 @@ var appValuesCmd = addCommand(appCmd, &cobra.Command{

env := b.GetCurrentEnvironment()

clusters := viper.GetStringSlice(argDeployAppClusters)
if len(clusters) == 0 {
clusters = []string{env.DefaultCluster}
cluster := viper.GetString(ArgGlobalCluster)
if len(cluster) == 0 {
cluster = env.DefaultCluster
}

for _, cluster := range clusters {
ctx := b.NewContext()
ctx.Log().Infof("Values for cluster %s", cluster)
appDeploy, err := getAppDeploy(b, cluster, app)
if err != nil {
return err
}
values, err := appDeploy.GetResolvedValues(ctx)
if err != nil {
return err
}
ctx := b.NewContext()
ctx.Log().Infof("Values for cluster %s", cluster)
appDeploy, err := getAppDeploy(b, cluster, app)
if err != nil {
return err
}
values, err := appDeploy.GetResolvedValues(ctx)
if err != nil {
return err
}

y, _ := yaml.MarshalString(values)
y, _ := yaml.MarshalString(values)

fmt.Println(y)
}
fmt.Println(y)

return nil
},
}, func(cmd *cobra.Command) {
cmd.Flags().StringSlice(argDeployAppClusters, []string{}, "Clusters to target (defaults to default cluster for environment)")
})

var appBumpCmd = addCommand(appCmd, &cobra.Command{
Expand Down
9 changes: 5 additions & 4 deletions cmd/deploy_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var _ = addCommand(deployCmd, &cobra.Command{
}, deployAppFlags)

const (
argDeployAppClusters = "clusters"
argDeployAppRecycle = "recycle"
argDeployAppTag = "tag"
argDeployAppDiffOnly = "diff-only"
Expand All @@ -50,13 +49,13 @@ func deployAppFlags(cmd *cobra.Command) {
cmd.Flags().String(argDeployAppTag, "", "Tag to use when deploying the app or apps.")
cmd.Flags().Bool(argDeployPlanIgnoreDeps, true, "Don't validate dependencies.")
cmd.Flags().Bool(argDeployPlanAutoDeps, false, "Automatically include dependencies.")
cmd.Flags().StringSlice(argDeployAppClusters, []string{}, "List of specific clusters to deploy to.")
cmd.Flags().Bool(argAppDeployPreview, false, "Just dump the values which would be used to deploy, then exit.")
cmd.Flags().Bool(ArgAppLatest, false, "Force bosun to pull the latest of the app and deploy that.")
cmd.Flags().Bool(argDeployAppRecycle, false, "Recycle the app after deploying it.")
cmd.Flags().Bool(argDeployAppDiffOnly, false, "Display the impact of running the deploy but don't actually run it.")
cmd.Flags().StringSliceP(ArgAppValueSet, "v", []string{}, "Additional value sets to include in this deploy.")
cmd.Flags().StringSliceP(ArgAppSet, "s", []string{}, "Value overrides to set in this deploy, as key=value pairs.")
cmd.Flags().String(ArgGlobalCluster, "","Value overrides to set in this deploy, as key=value pairs.")
}

func deployApp(cmd *cobra.Command, args []string) error {
Expand All @@ -69,9 +68,11 @@ func deployApp(cmd *cobra.Command, args []string) error {
}
ctx := b.NewContext()

env := b.GetCurrentEnvironment()

apps := args
if len(apps) == 0 {
for _, a := range p.GetApps(ctx) {
for _, a := range p.GetApps(ctx).FilterByEnvironment(env) {
apps = append(apps, a.Name)
}
sort.Strings(apps)
Expand Down Expand Up @@ -177,7 +178,7 @@ func deployApps(b *bosun.Bosun, p *bosun.Platform, appNames []string, valueSets
}
}

clustersWhitelist := viper.GetStringSlice(argDeployAppClusters)
clustersWhitelist := viper.GetStringSlice(ArgGlobalCluster)
if len(clustersWhitelist) > 0 {
executeRequest.Clusters = map[string]bool{}
for _, cluster := range clustersWhitelist {
Expand Down
100 changes: 73 additions & 27 deletions cmd/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func init() {

// kubeCmd represents the kube command
var kubeCmd = &cobra.Command{
Use: "kube {kube-layout}",
Use: "kube {kube-layout}",
Aliases: []string{"k"},
Args: cobra.ExactArgs(1),
Short: "Group of commands wrapping kubectl.",
Long: `You must have the cluster set in kubectl.`,
Args: cobra.ExactArgs(1),
Short: "Group of commands wrapping kubectl.",
Long: `You must have the cluster set in kubectl.`,
}

var dashboardTokenCmd = &cobra.Command{
Expand Down Expand Up @@ -146,7 +146,8 @@ var kubeConfigureClusterCmd = addCommand(kubeCmd, &cobra.Command{
role := core.ClusterRole(viper.GetString(ArgConfigureClusterRole))
ctx := b.NewContext()

err := env.Clusters.HandleConfigureKubeContextRequest(kube.ConfigureKubeContextRequest{
err := env.Clusters.HandleConfigureRequest(kube.ConfigureRequest{
Action: kube.ConfigureContextAction{},
Log: ctx.Log(),
Name: name,
Force: ctx.GetParameters().Force,
Expand All @@ -167,28 +168,6 @@ const (
ArgConfigureClusterRole = "role"
)

var kubeAddNamespaceCmd = addCommand(kubeCmd, &cobra.Command{
Use: "add-namespace {name}",
Args: cobra.ExactArgs(1),
Short: "Adds a namespace to your cluster. ",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

name := args[0]

_, err := pkg.NewShellExe("kubectl", "create", "namespace", name).RunOut()
if err != nil {
if strings.Contains(err.Error(), "AlreadyExists") {
color.Yellow("Namespace %q already exists.", name)
return nil
}
return err
}

return nil
},
})

var dashboardCmd = addCommand(kubeCmd, &cobra.Command{
Use: "dashboard",
Short: "Opens dashboard for current cluster.",
Expand Down Expand Up @@ -223,6 +202,73 @@ var dashboardCmd = addCommand(kubeCmd, &cobra.Command{
cmd.Flags().Bool(ArgKubeDashboardUrl, false, "Display dashboard URL instead of opening a browser")
})

var kubeConfigureLoopbackCmd = addCommand(kubeCmd, &cobra.Command{
Use: "configure-loopback",
Short: "Wires up a local network with the IP addresses the .red domains point to.",
RunE: func(cmd *cobra.Command, args []string) error {

return kube.ConfigureMickok8sNetworking()

},
}, func(cmd *cobra.Command) {
})

var kubeConfigureNamespacesCmd = addCommand(kubeCmd, &cobra.Command{
Use: "configure-namespaces",
Short: "Deploys the namespaces for the cluster",
RunE: func(cmd *cobra.Command, args []string) error {

b := MustGetBosun()

name := viper.GetString(ArgConfigureClusterName)
env := b.GetCurrentEnvironment()

role := core.ClusterRole(viper.GetString(ArgConfigureClusterRole))
ctx := b.NewContext()

err := env.Clusters.HandleConfigureRequest(kube.ConfigureRequest{
Action: kube.ConfigureNamespacesAction{},
Log: ctx.Log(),
Name: name,
Force: ctx.GetParameters().Force,
Role: role,
ExecutionContext: ctx,
PullSecrets: env.PullSecrets,
})

return err
},
}, func(cmd *cobra.Command) {
})

var kubeConfigurePullSecretsCmd = addCommand(kubeCmd, &cobra.Command{
Use: "configure-pull-secrets",
Short: "Deploys the pull secrets for the cluster",
RunE: func(cmd *cobra.Command, args []string) error {

b := MustGetBosun()

name := viper.GetString(ArgConfigureClusterName)
env := b.GetCurrentEnvironment()

role := core.ClusterRole(viper.GetString(ArgConfigureClusterRole))
ctx := b.NewContext()

err := env.Clusters.HandleConfigureRequest(kube.ConfigureRequest{
Action: kube.ConfigurePullSecretsAction{},
Log: ctx.Log(),
Name: name,
Force: ctx.GetParameters().Force,
Role: role,
ExecutionContext: ctx,
PullSecrets: env.PullSecrets,
})

return err
},
}, func(cmd *cobra.Command) {
})

var pullSecretCmd = addCommand(kubeCmd, &cobra.Command{
Use: "pull-secret [username] [password]",
Args: cobra.RangeArgs(0, 2),
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var minikubeUpCmd = addCommand(minikubeCmd, &cobra.Command{
}

env := b.GetCurrentEnvironment()
err := konfigs.HandleConfigureKubeContextRequest(kube.ConfigureKubeContextRequest{
err := konfigs.HandleConfigureRequest(kube.ConfigureRequest{
Name: "minikube",
Log: ctx.Log(),
ExecutionContext: ctx,
Expand Down
4 changes: 2 additions & 2 deletions cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ var _ = addCommand(platformCmd, &cobra.Command{
}

ctx := b.NewContext()

env := b.GetCurrentEnvironment()
var appNames []string
for _, app := range p.GetApps(ctx) {
for _, app := range p.GetApps(ctx).FilterByEnvironment(env) {
appNames = append(appNames, app.Name)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const (
ArgGlobalNoReport = "no-report"
ArgGlobalOutput = "output"
ArgGlobalProfile = "profile"
ArgGlobalCluster = "cluster"
)

func init() {
Expand All @@ -153,7 +154,7 @@ func init() {
rootCmd.PersistentFlags().Bool(ArgGlobalNoReport, false, "Disable reporting of deploys to github.")
rootCmd.PersistentFlags().Bool(ArgGlobalSudo, false, "Use sudo when running commands like docker.")
rootCmd.PersistentFlags().String(ArgGlobalConfirmedEnv, "", "Set to confirm that the environment is correct when targeting a protected environment.")
_ = rootCmd.PersistentFlags().MarkHidden(ArgGlobalConfirmedEnv)
rootCmd.PersistentFlags().String(ArgGlobalCluster, "", "Set to target a specific cluster.")
rootCmd.PersistentFlags().Bool(ArgGlobalProfile, false, "Dump profiling info.")
_ = rootCmd.PersistentFlags().MarkHidden(ArgGlobalProfile)

Expand Down
46 changes: 39 additions & 7 deletions cmd/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Any values provided using --values will be in {{ .Values.xxx }}

app := mustGetApp(b, args[0:1])


appDeploy, err := getAppDeploy(b, cluster, app)
if err != nil {
return err
Expand Down Expand Up @@ -146,9 +145,10 @@ Otherwise, this will do nothing.
Example: "vault bootstrap-dev",
RunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())
var err error

g := globalParameters{}
err := g.init()
err = g.init()
if err != nil {
return err
}
Expand All @@ -160,14 +160,46 @@ Otherwise, this will do nothing.
return err
}

initializer := pkg.VaultInitializer{
Client: vaultClient,
VaultNamespace: viper.GetString(ArgVaultNamespace),
b := MustGetBosun()

env := b.GetCurrentEnvironment()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

err = initializer.InitNonProd()
platformApps := p.GetKnownAppMap()

return err
vaultPlatformApp, ok := platformApps["vault"]

if !ok {
return errors.New("vault not found in platform apps")
}

cluster, err := env.GetClusterByName(env.ClusterName)
if err != nil {
return err
}

for _, requestedNamespaceRole := range vaultPlatformApp.NamespaceRoles {
for namespaceRole, namespaceConfig := range cluster.Namespaces {
if namespaceRole == requestedNamespaceRole {

initializer := pkg.VaultInitializer{
Client: vaultClient,
VaultNamespace: namespaceConfig.Name,
}

err = initializer.InitNonProd()
if err != nil {
return err
}
}
}
}

return nil
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/app_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (a BosunAction) Execute(ctx ActionContext) error {
stepArgs = append(stepArgs, "--dry-run")
}

stepArgs = append(stepArgs, "--cluster", ctx.GetStringValue(core.KeyCluster))
stepArgs = append(stepArgs, "--clusters", ctx.GetStringValue(core.KeyCluster))

log := ctx.WithLogField("args", stepArgs).Log()
log.WithField("args", stepArgs).Info("Executing step")
Expand Down
4 changes: 3 additions & 1 deletion pkg/bosun/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,11 +1273,13 @@ func (p *Platform) SetReleaseManifest(slot string, manifest *ReleaseManifest) {
p.ReleaseMetadata = updatedMetadata
}

func (p *Platform) GetApps(ctx filter.MatchMapArgContainer) []*PlatformAppConfig {
func (p *Platform) GetApps(ctx filter.MatchMapArgContainer) PlatformAppConfigs {

var out []*PlatformAppConfig
for _, app := range p.Apps {
if app.TargetFilters.Matches(ctx.GetMatchMapArgs()) {


out = append(out, app)
}
}
Expand Down
Loading

0 comments on commit f86754b

Please sign in to comment.