Skip to content

Commit 0b8538e

Browse files
create-runtime-mutation-without-persisting (#74)
* 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
1 parent d9132be commit 0b8538e

File tree

7 files changed

+71
-19
lines changed

7 files changed

+71
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.76
1+
VERSION=v0.0.77
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/runtime.go

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"os"
21+
"sync"
2122
"time"
2223

2324
"github.com/codefresh-io/cli-v2/pkg/log"
@@ -26,6 +27,7 @@ import (
2627
"github.com/codefresh-io/cli-v2/pkg/util"
2728
cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
2829
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
30+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
2931

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

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

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

202-
runtimeVersion := "v99.99.99"
203-
if rt.Spec.Version != nil { // in dev mode
204-
runtimeVersion = rt.Spec.Version.String()
205+
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
206+
if err != nil {
207+
return fmt.Errorf("failed to download runtime definition: %w", err)
205208
}
206209

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

212215
opts.RuntimeToken = runtimeCreationResponse.NewAccessToken
213216

217+
server, err := util.CurrentServer()
218+
if err != nil {
219+
return fmt.Errorf("failed to get current server address: %w", err)
220+
}
221+
rt.Spec.Cluster = server
222+
214223
log.G(ctx).WithField("version", rt.Spec.Version).Infof("installing runtime '%s'", opts.RuntimeName)
215224
err = apcmd.RunRepoBootstrap(ctx, &apcmd.RepoBootstrapOptions{
216225
AppSpecifier: rt.Spec.FullSpecifier(),
@@ -250,7 +259,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
250259
}
251260
}
252261

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

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

283+
var wg sync.WaitGroup
284+
285+
wg.Add(1)
286+
go intervalCheckIsRuntimePersisted(15000, ctx, opts.RuntimeName, &wg)
287+
wg.Wait()
288+
274289
log.G(ctx).Infof("done installing runtime '%s'", opts.RuntimeName)
275290
return nil
276291
}
277292

293+
func intervalCheckIsRuntimePersisted(milliseconds int, ctx context.Context, runtimeName string, wg *sync.WaitGroup) {
294+
interval := time.Duration(milliseconds) * time.Millisecond
295+
ticker := time.NewTicker(interval)
296+
var err error
297+
298+
for retries := 20; retries > 0; <-ticker.C {
299+
fmt.Println("waiting for the runtime installation to complete...")
300+
var runtimes []model.Runtime
301+
runtimes, err = cfConfig.NewClient().V2().Runtime().List(ctx)
302+
if err != nil {
303+
continue
304+
}
305+
306+
for _, rt := range runtimes {
307+
if rt.Metadata.Name == runtimeName {
308+
wg.Done()
309+
ticker.Stop()
310+
return
311+
}
312+
}
313+
314+
retries--
315+
}
316+
317+
panic(fmt.Errorf("failed to complete the runtime installation due to error: %w", err))
318+
}
319+
278320
func NewRuntimeListCommand() *cobra.Command {
279321
cmd := &cobra.Command{
280322
Use: "list [runtime_name]",
@@ -534,7 +576,7 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
534576
return err
535577
}
536578

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

567-
if err := updateProject(repofs, opts.RuntimeName); err != nil {
609+
if err := updateProject(repofs, opts.RuntimeName, rt); err != nil {
568610
return err
569611
}
570612

@@ -616,7 +658,7 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
616658
return err
617659
}
618660

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

672+
runtimeVersion := "v99.99.99"
673+
if rt.Spec.Version != nil { // in dev mode
674+
runtimeVersion = rt.Spec.Version.String()
675+
}
676+
630677
project.ObjectMeta.Labels[store.Get().LabelKeyCFType] = store.Get().CFRuntimeType
678+
project.ObjectMeta.Labels[store.Get().LabelKeyRuntimeVersion] = runtimeVersion
679+
631680
return repofs.WriteYamls(projPath, project, appset)
632681
}
633682

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cf version
2020
### Linux
2121
```bash
2222
# download and extract the binary
23-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.76/cf-linux-amd64.tar.gz | tar zx
23+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-linux-amd64.tar.gz | tar zx
2424

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

3737
# move the binary to your $PATH
3838
mv ./cf-darwin-amd64 /usr/local/bin/cf

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/argoproj/argo-events v1.4.0
1111
github.com/argoproj/argo-workflows/v3 v3.1.6
1212
github.com/briandowns/spinner v1.16.0
13-
github.com/codefresh-io/go-sdk v0.32.2
13+
github.com/codefresh-io/go-sdk v0.32.4
1414
github.com/fatih/color v1.12.0
1515
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1616
github.com/go-git/go-billy/v5 v5.3.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
268268
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
269269
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
270270
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
271-
github.com/codefresh-io/go-sdk v0.32.2 h1:74RlRp6cpqwU8sf4l1vG2Xiv8ohnaLQjREkW0BAUwcU=
272-
github.com/codefresh-io/go-sdk v0.32.2/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
271+
github.com/codefresh-io/go-sdk v0.32.4 h1:4e05vkc8v275gR+eji32K/QfJd21zljHMjCBKeIvzVM=
272+
github.com/codefresh-io/go-sdk v0.32.4/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
273273
github.com/colinmarc/hdfs v1.1.4-0.20180802165501-48eb8d6c34a9/go.mod h1:0DumPviB681UcSuJErAbDIOx6SIaJWj463TymfZG02I=
274274
github.com/colinmarc/hdfs v1.1.4-0.20180805212432-9746310a4d31/go.mod h1:vSBumefK4HA5uiRSwNP+3ofgrEoScpCS2MMWcWXEuQ4=
275275
github.com/container-storage-interface/spec v1.3.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=

pkg/runtime/runtime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type (
5454
Version *semver.Version `json:"version"`
5555
BootstrapSpecifier string `json:"bootstrapSpecifier"`
5656
Components []AppDef `json:"components"`
57+
Cluster string `json:"cluster"`
5758
}
5859

5960
CommonConfig struct {

pkg/store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type Store struct {
6969
RuntimeFilename string
7070
Version Version
7171
WaitTimeout time.Duration
72+
LabelKeyRuntimeVersion string
7273
}
7374

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

0 commit comments

Comments
 (0)