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
0 commit comments