-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
114 lines (100 loc) · 3.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package main
import (
"fmt"
"github.com/konstructio/kubefirst-api/docs"
"github.com/konstructio/kubefirst-api/internal/env"
api "github.com/konstructio/kubefirst-api/internal/router"
"github.com/konstructio/kubefirst-api/internal/secrets"
"github.com/konstructio/kubefirst-api/internal/services"
apitelemetry "github.com/konstructio/kubefirst-api/internal/telemetry"
"github.com/konstructio/kubefirst-api/internal/utils"
"github.com/konstructio/kubefirst-api/pkg/types"
"github.com/kubefirst/metrics-client/pkg/telemetry"
log "github.com/rs/zerolog/log"
)
// @title Kubefirst API
// @version 1.0
// @description Kubefirst API
// @contact.name Kubefirst
// @contact.email [email protected]
// @host localhost:port
// @BasePath /api/v1
func main() {
env, err := env.GetEnv(false)
if err != nil {
log.Fatal().Msg(err.Error())
}
if env.IsClusterZero {
log.Info().Msg("IS_CLUSTER_ZERO is set to true, skipping import cluster logic")
} else {
log.Info().Msg("checking for cluster import secret for management cluster")
// Import if needed
importedCluster, err := secrets.ImportClusterIfEmpty()
if err != nil {
log.Fatal().Msg(err.Error())
} else {
log.Info().Msgf("adding default services for cluster %s", importedCluster.ClusterName)
if err := services.AddDefaultServices(importedCluster); err != nil {
log.Fatal().Msg(err.Error())
}
if importedCluster.PostInstallCatalogApps != nil {
go importResourcesInCluster(importedCluster)
}
}
}
// Programmatically set swagger info
docs.SwaggerInfo.Title = "Kubefirst API"
docs.SwaggerInfo.Description = "Kubefirst API"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = fmt.Sprintf("localhost:%v", env.ServerPort)
docs.SwaggerInfo.BasePath = "/api/v1"
docs.SwaggerInfo.Schemes = []string{"http"}
// Telemetry handler
telemetryEvent := telemetry.TelemetryEvent{
CliVersion: env.KubefirstVersion,
CloudProvider: env.CloudProvider,
ClusterID: env.ClusterID,
ClusterType: env.ClusterType,
DomainName: env.DomainName,
ErrorMessage: "",
GitProvider: env.GitProvider,
InstallMethod: env.InstallMethod,
KubefirstClient: "api",
KubefirstTeam: env.KubefirstTeam,
KubefirstTeamInfo: env.KubefirstTeamInfo,
MachineID: env.ClusterID,
MetricName: telemetry.ClusterInstallCompleted,
ParentClusterId: env.ParentClusterID,
UserId: env.ClusterID,
}
if !env.IsClusterZero {
// Subroutine to automatically update gitops catalog
go utils.ScheduledGitopsCatalogUpdate()
}
go apitelemetry.Heartbeat(telemetryEvent)
// API
r := api.SetupRouter()
err = r.Run(fmt.Sprintf(":%v", env.ServerPort))
if err != nil {
log.Fatal().Msgf("Error starting API: %s", err)
}
}
func importResourcesInCluster(importedCluster *types.Cluster) {
for _, catalogApp := range importedCluster.PostInstallCatalogApps {
log.Info().Msgf("installing catalog application %s", catalogApp.Name)
request := &types.GitopsCatalogAppCreateRequest{
User: "kbot",
SecretKeys: catalogApp.SecretKeys,
ConfigKeys: catalogApp.ConfigKeys,
}
err := services.CreateService(importedCluster, catalogApp.Name, &catalogApp, request, true)
if err != nil {
log.Info().Msgf("Error creating default environments %s", err.Error())
}
}
}