Skip to content

Commit

Permalink
create-runtime-mutation-without-persisting (#74)
Browse files Browse the repository at this point in the history
* removed redundant args

* attempting to use a blocking interval check for persisted runtime

* modifications to wg and interval

* tested successfully

* better error handle

* added files

* better loop

* error handling

* removed redundant

* better loop

* prolonged timeout

* adding the cluster def to rt.Spec.Cluster

* 20 retries

* removed redundant

* bump

* deleted file

* without func param

* skipping redundant for and bumping go-sdk

* Delete yarn.lock

* without cluster labelkey

* without cluster label

* tidy

* bump go sdk

* Delete .yarn-integrity

* tidy
  • Loading branch information
elad-codefresh authored Aug 25, 2021
1 parent d9132be commit 0b8538e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.76
VERSION=v0.0.77
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
75 changes: 62 additions & 13 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"sync"
"time"

"github.com/codefresh-io/cli-v2/pkg/log"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/codefresh-io/cli-v2/pkg/util"
cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"

"github.com/Masterminds/semver/v3"
appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
Expand Down Expand Up @@ -189,28 +191,35 @@ func NewRuntimeInstallCommand() *cobra.Command {
}

func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
return fmt.Errorf("failed to download runtime definition: %w", err)
return err
}

server, err := util.CurrentServer()
if err != nil {
return fmt.Errorf("failed to get current server address: %w", err)
for _, rt := range runtimes {
if rt.Metadata.Name == opts.RuntimeName {
return fmt.Errorf("failed to create runtime: %s. A runtime by this name already exists", opts.RuntimeName)
}
}

runtimeVersion := "v99.99.99"
if rt.Spec.Version != nil { // in dev mode
runtimeVersion = rt.Spec.Version.String()
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
if err != nil {
return fmt.Errorf("failed to download runtime definition: %w", err)
}

runtimeCreationResponse, err := cfConfig.NewClient().V2().Runtime().Create(ctx, opts.RuntimeName, server, runtimeVersion)
runtimeCreationResponse, err := cfConfig.NewClient().V2().Runtime().Create(ctx, opts.RuntimeName)
if err != nil {
return fmt.Errorf("failed to create a new runtime: %w", err)
}

opts.RuntimeToken = runtimeCreationResponse.NewAccessToken

server, err := util.CurrentServer()
if err != nil {
return fmt.Errorf("failed to get current server address: %w", err)
}
rt.Spec.Cluster = server

log.G(ctx).WithField("version", rt.Spec.Version).Infof("installing runtime '%s'", opts.RuntimeName)
err = apcmd.RunRepoBootstrap(ctx, &apcmd.RepoBootstrapOptions{
AppSpecifier: rt.Spec.FullSpecifier(),
Expand Down Expand Up @@ -250,7 +259,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
}
}

if err = createEventsReporter(ctx, opts.insCloneOpts, opts); err != nil {
if err = createEventsReporter(ctx, opts.insCloneOpts, opts, rt); err != nil {
return fmt.Errorf("failed to create events-reporter: %w", err)
}

Expand All @@ -271,10 +280,43 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
return fmt.Errorf("failed to create `%s`: %w", store.Get().GitSourceName, err)
}

var wg sync.WaitGroup

wg.Add(1)
go intervalCheckIsRuntimePersisted(15000, ctx, opts.RuntimeName, &wg)
wg.Wait()

log.G(ctx).Infof("done installing runtime '%s'", opts.RuntimeName)
return nil
}

func intervalCheckIsRuntimePersisted(milliseconds int, ctx context.Context, runtimeName string, wg *sync.WaitGroup) {
interval := time.Duration(milliseconds) * time.Millisecond
ticker := time.NewTicker(interval)
var err error

for retries := 20; retries > 0; <-ticker.C {
fmt.Println("waiting for the runtime installation to complete...")
var runtimes []model.Runtime
runtimes, err = cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
continue
}

for _, rt := range runtimes {
if rt.Metadata.Name == runtimeName {
wg.Done()
ticker.Stop()
return
}
}

retries--
}

panic(fmt.Errorf("failed to complete the runtime installation due to error: %w", err))
}

func NewRuntimeListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list [runtime_name]",
Expand Down Expand Up @@ -534,7 +576,7 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
return err
}

func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions) error {
func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions, rt *runtime.Runtime) error {
runtimeTokenSecret, err := getRuntimeTokenSecret(opts.RuntimeName, opts.RuntimeToken)
if err != nil {
return fmt.Errorf("failed to create codefresh token secret: %w", err)
Expand Down Expand Up @@ -564,7 +606,7 @@ func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts
return err
}

if err := updateProject(repofs, opts.RuntimeName); err != nil {
if err := updateProject(repofs, opts.RuntimeName, rt); err != nil {
return err
}

Expand Down Expand Up @@ -616,7 +658,7 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
return err
}

func updateProject(repofs fs.FS, runtimeName string) error {
func updateProject(repofs fs.FS, runtimeName string, rt *runtime.Runtime) error {
projPath := repofs.Join(apstore.Default.ProjectsDir, runtimeName+".yaml")
project, appset, err := getProjectInfoFromFile(repofs, projPath)
if err != nil {
Expand All @@ -627,7 +669,14 @@ func updateProject(repofs fs.FS, runtimeName string) error {
project.ObjectMeta.Labels = make(map[string]string)
}

runtimeVersion := "v99.99.99"
if rt.Spec.Version != nil { // in dev mode
runtimeVersion = rt.Spec.Version.String()
}

project.ObjectMeta.Labels[store.Get().LabelKeyCFType] = store.Get().CFRuntimeType
project.ObjectMeta.Labels[store.Get().LabelKeyRuntimeVersion] = runtimeVersion

return repofs.WriteYamls(projPath, project, appset)
}

Expand Down
4 changes: 2 additions & 2 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cf version
### Linux
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.76/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-linux-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-linux-amd64 /usr/local/bin/cf
Expand All @@ -32,7 +32,7 @@ cf version
### Mac
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.76/cf-darwin-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-darwin-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/argoproj/argo-events v1.4.0
github.com/argoproj/argo-workflows/v3 v3.1.6
github.com/briandowns/spinner v1.16.0
github.com/codefresh-io/go-sdk v0.32.2
github.com/codefresh-io/go-sdk v0.32.4
github.com/fatih/color v1.12.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-git/go-billy/v5 v5.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/codefresh-io/go-sdk v0.32.2 h1:74RlRp6cpqwU8sf4l1vG2Xiv8ohnaLQjREkW0BAUwcU=
github.com/codefresh-io/go-sdk v0.32.2/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
github.com/codefresh-io/go-sdk v0.32.4 h1:4e05vkc8v275gR+eji32K/QfJd21zljHMjCBKeIvzVM=
github.com/codefresh-io/go-sdk v0.32.4/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
github.com/colinmarc/hdfs v1.1.4-0.20180802165501-48eb8d6c34a9/go.mod h1:0DumPviB681UcSuJErAbDIOx6SIaJWj463TymfZG02I=
github.com/colinmarc/hdfs v1.1.4-0.20180805212432-9746310a4d31/go.mod h1:vSBumefK4HA5uiRSwNP+3ofgrEoScpCS2MMWcWXEuQ4=
github.com/container-storage-interface/spec v1.3.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type (
Version *semver.Version `json:"version"`
BootstrapSpecifier string `json:"bootstrapSpecifier"`
Components []AppDef `json:"components"`
Cluster string `json:"cluster"`
}

CommonConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Store struct {
RuntimeFilename string
Version Version
WaitTimeout time.Duration
LabelKeyRuntimeVersion string
}

// Get returns the global store
Expand Down Expand Up @@ -97,6 +98,7 @@ func init() {
s.EventReportingEndpoint = "/2.0/api/events"
s.GitSourceName = "default-git-source"
s.LabelKeyCFType = "codefresh.io/entity"
s.LabelKeyRuntimeVersion = "codefresh.io/runtimeVersion"
s.MaxDefVersion = semver.MustParse(maxDefVersion)
s.RuntimeDefURL = RuntimeDefURL
s.RuntimeFilename = "runtime.yaml"
Expand Down

0 comments on commit 0b8538e

Please sign in to comment.