Skip to content

Commit 256083a

Browse files
kaanescapeQuentinN42
authored andcommitted
--wip-- [skip ci]
1 parent 41a455e commit 256083a

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

pkg/locations/private/kube/integration.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import (
99
"github.com/oapi-codegen/runtime/types"
1010
)
1111

12-
func UpsertIntegration(ctx context.Context, locationId *types.UUID, locationName string) error {
13-
client, err := api.NewAPIClient()
14-
if err != nil {
15-
return err
16-
}
12+
func UpsertIntegration(ctx context.Context, client *api.ClientWithResponses, locationId *types.UUID, locationName string) error {
1713
integrations, err := client.GetV1IntegrationsKubernetesWithResponse(ctx)
1814
if err != nil {
1915
return err
@@ -24,24 +20,27 @@ func UpsertIntegration(ctx context.Context, locationId *types.UUID, locationName
2420
log.Info("Integration already exists, skipping")
2521
return nil
2622
}
27-
log.Info("Integration not found, creating")
28-
res, err := client.PostV1IntegrationsKubernetesWithResponse(ctx, api.PostV1IntegrationsKubernetesJSONRequestBody{
29-
LocationId: locationId,
30-
Name: locationName,
31-
})
32-
if err != nil {
33-
return err
34-
}
35-
if res.JSON200 != nil {
36-
log.Info("Integration created successfully")
37-
return nil
38-
} else if res.JSON400 != nil {
39-
return fmt.Errorf("unable to create integration: %s", res.JSON400.Message)
40-
} else if res.JSON500 != nil {
41-
return fmt.Errorf("unable to create integration: %s", res.JSON500.Message)
42-
} else {
43-
return fmt.Errorf("unable to create integration: Unknown error")
23+
}
24+
25+
log.Info("Integration not found, creating")
26+
res, err := client.PostV1IntegrationsKubernetesWithResponse(ctx, api.PostV1IntegrationsKubernetesJSONRequestBody{
27+
LocationId: locationId,
28+
Name: locationName,
29+
})
30+
if err != nil {
31+
return err
32+
}
33+
if res.JSON200 != nil {
34+
log.Info("Integration created successfully")
35+
return nil
36+
} else if res.JSON400 != nil {
37+
for _, evt := range res.JSON400.Events {
38+
log.Error("Event: %s", evt.Logline)
4439
}
40+
return fmt.Errorf("unable to create integration: %s", res.JSON400.Message)
41+
} else if res.JSON500 != nil {
42+
return fmt.Errorf("unable to create integration: %s", res.JSON500.Message)
43+
} else {
44+
return fmt.Errorf("unable to create integration: Unknown error")
4545
}
46-
return nil
4746
}

pkg/locations/private/kube/main.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync/atomic"
88
"time"
99

10+
"github.com/Escape-Technologies/cli/pkg/api"
1011
"github.com/Escape-Technologies/cli/pkg/log"
1112
"github.com/oapi-codegen/runtime/types"
1213
"k8s.io/client-go/rest"
@@ -34,7 +35,7 @@ func inferConfig() (*rest.Config, error) {
3435
}
3536
}
3637

37-
func connectAndRun(ctx context.Context, cfg *rest.Config, isConnected *atomic.Bool) error {
38+
func connectAndRun(ctx context.Context, client *api.ClientWithResponses, cfg *rest.Config, isConnected *atomic.Bool, locationId *types.UUID, locationName string) error {
3839
ctx, cancel := context.WithCancel(ctx)
3940
defer cancel()
4041

@@ -64,6 +65,14 @@ func connectAndRun(ctx context.Context, cfg *rest.Config, isConnected *atomic.Bo
6465
lis.Close()
6566
return
6667
}
68+
log.Debug("Connected to k8s API")
69+
log.Info("Upserting integration")
70+
err = UpsertIntegration(ctx, client, locationId, locationName)
71+
if err != nil {
72+
log.Error("Error upserting integration: %s", err)
73+
return
74+
}
75+
6776
<-ctx.Done()
6877
lis.Close()
6978
}()
@@ -73,14 +82,11 @@ func connectAndRun(ctx context.Context, cfg *rest.Config, isConnected *atomic.Bo
7382
if err != nil {
7483
return fmt.Errorf("error serving: %w", err)
7584
}
76-
log.Debug("Connected to k8s API")
77-
isConnected.Store(true)
78-
log.Info("Healthy: %t", isConnected.Load())
7985
return nil
8086
}
8187

8288

83-
func Start(ctx context.Context, locationId *types.UUID, locationName string, healthy *atomic.Bool) {
89+
func Start(ctx context.Context, client *api.ClientWithResponses, locationId *types.UUID, locationName string, healthy *atomic.Bool) {
8490
cfg, err := inferConfig()
8591
if err != nil {
8692
log.Info("Not connected to k8s API")
@@ -89,25 +95,14 @@ func Start(ctx context.Context, locationId *types.UUID, locationName string, hea
8995
}
9096
for {
9197
log.Info("Healthy: %t", healthy.Load())
92-
err = connectAndRun(ctx, cfg, healthy)
93-
log.Info("Healthy: %t", healthy.Load())
98+
err = connectAndRun(ctx, client, cfg, healthy, locationId, locationName)
9499
if err != nil {
95100
log.Error("Error connecting to k8s API: %s", err)
96-
return
97101
}
98-
time.Sleep(1 * time.Second)
99102
if ctx.Err() != nil {
100103
return
101104
}
102105
log.Info("Healthy: %t", healthy.Load())
103-
if healthy.Load() {
104-
log.Info("Upserting integration")
105-
err = UpsertIntegration(ctx, locationId, locationName)
106-
if err != nil {
107-
log.Error("Error upserting integration: %s", err)
108-
return
109-
}
110-
}
111106
}
112107

113108

pkg/locations/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Start(ctx context.Context, client *api.ClientWithResponses, name string) er
3232
return err
3333
}
3434
if location.JSON200 != nil {
35-
go kube.Start(ctx, location.JSON200.Id, *location.JSON200.Name, healthy)
35+
go kube.Start(ctx, client, location.JSON200.Id, *location.JSON200.Name, healthy)
3636
for {
3737
err := private.StartLocation(ctx, location.JSON200.Id.String(), sshPrivateKey, healthy)
3838
if err != nil {

0 commit comments

Comments
 (0)