Skip to content

Commit

Permalink
fix issue with unknown identity
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodeassassin committed Dec 6, 2022
1 parent 4a8d0b8 commit 108b8bc
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 50 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
BINARY_NAME=sym

build:
GOARCH=amd64 GOOS=darwin go build -o dist/${BINARY_NAME}-darwin main.go
GOARCH=amd64 GOOS=linux go build -o dist/${BINARY_NAME}-linux main.go
GOARCH=amd64 GOOS=window go build -o dist/${BINARY_NAME}-windows main.go
GOARCH=amd64 GOOS=darwin go build -o dist/${BINARY_NAME}-darwin ./cmd/sym/main.go
GOARCH=amd64 GOOS=linux go build -o dist/${BINARY_NAME}-linux ./cmd/sym/main.go
GOARCH=amd64 GOOS=window go build -o dist/${BINARY_NAME}-windows ./cmd/sym/main.go

run:
./${BINARY_NAME}
Expand Down
110 changes: 69 additions & 41 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package commands
import (
"context"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,17 +50,20 @@ func (c *RunCommand) Execute(command *cobra.Command, args []string) error {
return err
}

c.CommandOpts.Namespace = deploymentFlags.Namespace

identity, err := identity.NewClusterIdentity(c.Client, clusterName, deploymentFlags.IdentityOutputPath, merge)
_, err = c.Client.Cluster.Describe(clusterName)

if err != nil {
// cluster does not exist, create it
if err != nil && strings.Contains(err.Error(), "not found") {
c.CreateCluster(clusterName, region, deploymentFlags.IdentityOutputPath)
} else if err != nil {
return err
} else {
c.CommandOpts.Logger.Info().Msgf("Using existing cluster: %s", clusterName)
}

c.CommandOpts.Logger.Info().Msgf("Written identity to %s", identity.KubeConfigPath)
c.CommandOpts.Namespace = deploymentFlags.Namespace

projectConfig, err := project.NewProjectConfig(deploymentFlags.File, c.CommandOpts, c.Client, identity)
projectConfig, err := project.NewProjectConfig(deploymentFlags.File, c.CommandOpts, c.Client, nil)

if err != nil {
return err
Expand All @@ -77,12 +81,57 @@ func (c *RunCommand) Execute(command *cobra.Command, args []string) error {
return err
}

identity, err := identity.NewClusterIdentity(c.Client, clusterName, deploymentFlags.IdentityOutputPath, merge)

if err != nil {
return err
}

c.CommandOpts.Logger.Info().Msgf("Written identity to %s", identity.KubeConfigPath)

projectConfig.SetIdentity(identity)

// run helm
if projectConfig.Deploy != nil && projectConfig.Deploy.Helm != nil {
c.CommandOpts.Logger.Info().Msg("Installing helm chart...")

err = projectConfig.RunDeploy()

if err != nil {
return err
}
return nil
}

c.CommandOpts.Logger.Info().Msg("Run finished.")

return nil
}

func (c *RunCommand) Command() *cobra.Command {

cmd := &cobra.Command{
Use: "run",
Short: "Run steps defined in sym.yaml",
Long: ``,
RunE: c.Execute,
}

cmd.Flags().String("region", "germany-1", "Set the Symbiosis region")
cmd.Flags().String("cluster-name", fmt.Sprintf("run-%s", util.RandomString(8)), "Set the Cluster name of the newly created cluster")

symcommand.SetDeploymentFlags(cmd)

return cmd
}

func (c *RunCommand) CreateCluster(clusterName string, region string, outputPath string) error {
c.CommandOpts.Logger.Info().Msgf("Creating cluster: %s", clusterName)

// TODO: allow changing node pool size
numNodes := 2

_, err = c.Client.Cluster.Create(&symbiosis.ClusterInput{
_, err := c.Client.Cluster.Create(&symbiosis.ClusterInput{
Name: clusterName,
Nodes: []symbiosis.ClusterNodePoolInput{{
Name: fmt.Sprintf("%s-autopool", clusterName),
Expand All @@ -109,15 +158,26 @@ func (c *RunCommand) Execute(command *cobra.Command, args []string) error {
}

c.CommandOpts.Logger.Info().Msg("Cluster created, Creating identity...")

c.CommandOpts.Logger.Info().Msg("Cluster created, waiting for node pools to become active...")

identity, err := identity.NewClusterIdentity(c.Client, clusterName, outputPath, merge)

if err != nil {
return err
}

clientset, err := util.GetKubernetesClient(identity.KubeConfigPath)

if err != nil {
return err
}

it := 0
for {

readyNodes := 0

nodes, _ := projectConfig.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
nodes, _ := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})

for _, node := range nodes.Items {
inner:
Expand All @@ -143,41 +203,9 @@ func (c *RunCommand) Execute(command *cobra.Command, args []string) error {

c.CommandOpts.Logger.Info().Msg("Cluster ready for use")

// run helm
if projectConfig.Deploy != nil && projectConfig.Deploy.Helm != nil {
c.CommandOpts.Logger.Info().Msg("Installing helm chart...")

err = projectConfig.RunDeploy()

if err != nil {
return err
}
return nil
}

c.CommandOpts.Logger.Info().Msg("Run finished.")

return nil
}

func (c *RunCommand) Command() *cobra.Command {

cmd := &cobra.Command{
Use: "run",
Short: "Run steps defined in sym.yaml",
Long: ``,
RunE: c.Execute,
}

cmd.Flags().String("region", "germany-1", "Set the Symbiosis region")
cmd.Flags().String("cluster-name", fmt.Sprintf("run-%s", util.RandomString(8)), "Set the Cluster name of the newly created cluster")
cmd.Flags().String("identity-output-path", "", "Write the generated kubeConfig file to this location")

symcommand.SetDeploymentFlags(cmd)

return cmd
}

func (c *RunCommand) Init(client *symbiosis.Client, opts *symcommand.CommandOpts) {
c.Client = client
c.CommandOpts = opts
Expand Down
3 changes: 1 addition & 2 deletions pkg/builder/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,10 @@ func (b *HelmBuilder) expandPaths(path string) string {
return path
}

func NewHelmBuilder(deployments []HelmDeployment, dir string, opts *symcommand.CommandOpts, identity *identity.ClusterIdentity) *HelmBuilder {
func NewHelmBuilder(deployments []HelmDeployment, dir string, opts *symcommand.CommandOpts) *HelmBuilder {
return &HelmBuilder{
deployments: deployments,
dir: dir,
CommandOpts: opts,
identity: identity,
}
}
30 changes: 26 additions & 4 deletions pkg/project/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *ProjectConfig) Parse() error {
}

if p.Deploy.Helm != nil {
helm := builder.NewHelmBuilder(p.Deploy.Helm, filepath.Dir(p.Path), p.commandOpts, p.identity)
helm := builder.NewHelmBuilder(p.Deploy.Helm, filepath.Dir(p.Path), p.commandOpts)

p.builders = append(p.builders, helm)
}
Expand All @@ -121,6 +121,11 @@ func (p *ProjectConfig) Parse() error {
// store the raw config for further processing
p.rawConfig = f

// update identity for all configured builders
if p.identity != nil {
p.SetIdentity(p.identity)
}

return nil
}

Expand Down Expand Up @@ -209,6 +214,16 @@ func (p *ProjectConfig) PromptProject(path string) (*symbiosis.Project, error) {
return project, nil
}

func (p *ProjectConfig) SetIdentity(identity *identity.ClusterIdentity) {
p.identity = identity

if len(p.builders) > 0 {
for _, builder := range p.builders {
builder.SetIdentity(identity)
}
}
}

func NewProjectConfig(file string, opts *symcommand.CommandOpts, client *symbiosis.Client, identity *identity.ClusterIdentity) (*ProjectConfig, error) {
filePath, err := filepath.Abs(file)

Expand All @@ -218,10 +233,17 @@ func NewProjectConfig(file string, opts *symcommand.CommandOpts, client *symbios
return nil, err
}

clientset, err := util.GetKubernetesClient(identity.KubeConfigPath)
var clientset *kubernetes.Clientset

if err != nil {
return nil, err
if identity != nil {

cs, err := util.GetKubernetesClient(identity.KubeConfigPath)

if err != nil {
return nil, err
}

clientset = cs
}

var project *symbiosis.Project
Expand Down

0 comments on commit 108b8bc

Please sign in to comment.