Skip to content

Commit

Permalink
feat: catalog multicluster (#296)
Browse files Browse the repository at this point in the history
* feat: catalog multicluster

* feat: adding metaphor service to dev, stage adn prod

* feat: moving func to pkg
  • Loading branch information
CristhianF7 authored Feb 14, 2024
1 parent 8342c9b commit 03f4862
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 237 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/aws/aws-sdk-go-v2 v1.17.8
github.com/aws/aws-sdk-go-v2/config v1.18.19
github.com/aws/aws-sdk-go-v2/credentials v1.13.18
github.com/aws/aws-sdk-go-v2/service/ec2 v1.91.0
github.com/aws/aws-sdk-go-v2/service/eks v1.27.10
github.com/aws/aws-sdk-go-v2/service/route53 v1.27.5
github.com/caarlos0/env/v10 v10.0.0
Expand All @@ -30,13 +29,12 @@ require (
github.com/hashicorp/vault/api v1.9.0
github.com/joho/godotenv v1.5.1
github.com/kubefirst/metrics-client v0.3.0
github.com/kubefirst/runtime v0.3.35
github.com/kubefirst/runtime v0.4.1
github.com/minio/minio-go/v7 v7.0.49
github.com/nxadm/tail v1.4.8
github.com/otiai10/copy v1.7.0
github.com/rs/zerolog v1.29.1
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/sirupsen/logrus v1.9.0
github.com/swaggo/files v1.0.0
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.16.1
Expand All @@ -53,6 +51,7 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2/service/ec2 v1.91.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
Expand Down Expand Up @@ -109,7 +108,6 @@ require (
github.com/cloudflare/circl v1.1.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/digitalocean/godo v1.98.0 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubefirst/metrics-client v0.3.0 h1:zCug82pEzeWhHhpeYQvdhytRNDxrLxX18dPQ5PSxY3s=
github.com/kubefirst/metrics-client v0.3.0/go.mod h1:GR7wsMcyYhd+EU67PeuMCBYFE6OJ7P/j5OI5BLOoRMc=
github.com/kubefirst/runtime v0.3.35 h1:wn430Irf0E1vc3X0WX3lYBpyhQ5TN6xxMcargILA9uI=
github.com/kubefirst/runtime v0.3.35/go.mod h1:0CnYy+8JTG+/0f3QlkTQJqTT654Su6JXk30OufFVY98=
github.com/kubefirst/runtime v0.4.1 h1:0aqBZa3bPwHb5pFFA1eaZ5VObNjuVxDO4d8m9VFs4Jo=
github.com/kubefirst/runtime v0.4.1/go.mod h1:0CnYy+8JTG+/0f3QlkTQJqTT654Su6JXk30OufFVY98=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
Expand Down
10 changes: 5 additions & 5 deletions internal/db/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import (
)

// CreateClusterServiceList adds an entry for a cluster to the service list
func (mdbcl *MongoDBClient) CreateClusterServiceList(cl *pkgtypes.Cluster) error {
filter := bson.D{{Key: "cluster_name", Value: cl.ClusterName}}
func (mdbcl *MongoDBClient) CreateClusterServiceList(clusterName string) error {
filter := bson.D{{Key: "cluster_name", Value: clusterName}}
var result pkgtypes.Cluster
err := mdbcl.ServicesCollection.FindOne(mdbcl.Context, filter).Decode(&result)
if err != nil {
// This error means your query did not match any documents.
if err == mongo.ErrNoDocuments {
// Create if entry does not exist
_, err := mdbcl.ServicesCollection.InsertOne(mdbcl.Context, types.ClusterServiceList{
ClusterName: cl.ClusterName,
ClusterName: clusterName,
Services: []types.Service{},
})
if err != nil {
return fmt.Errorf("error inserting cluster service list for cluster %s: %s", cl.ClusterName, err)
return fmt.Errorf("error inserting cluster service list for cluster %s: %s", clusterName, err)
}
}
} else {
log.Info().Msgf("cluster service list record for %s already exists - skipping", cl.ClusterName)
log.Info().Msgf("cluster service list record for %s already exists - skipping", clusterName)
}

return nil
Expand Down
31 changes: 31 additions & 0 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/kubefirst/kubefirst-api/internal/constants"
"github.com/kubefirst/kubefirst-api/internal/db"
"github.com/kubefirst/kubefirst-api/internal/env"
internalTypes "github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/kubefirst-api/pkg/types"

log "github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -84,6 +86,35 @@ func CreateDefaultEnvironments(mgmtCluster types.Cluster) error {
Clusters: defaultClusters,
}

var fullDomainName string
if mgmtCluster.SubdomainName != "" {
fullDomainName = fmt.Sprintf("%s.%s", mgmtCluster.SubdomainName, mgmtCluster.DomainName)
} else {
fullDomainName = mgmtCluster.DomainName
}

for _, clusterName := range defaultClusterNames {
// Add to list
err := db.Client.CreateClusterServiceList(clusterName)
if err != nil {
return err
}

// Update list
err = db.Client.InsertClusterServiceListEntry(clusterName, &internalTypes.Service{
Name: "Metaphor",
Default: true,
Description: "A multi-environment demonstration space for frontend application best practices that's easy to apply to other projects.",
Image: "https://assets.kubefirst.com/console/metaphor.svg",
Links: []string{fmt.Sprintf("https://metaphor-%s.%s", clusterName, fullDomainName)},
Status: "",
})

if err != nil {
return err
}
}

// call api-ee to create clusters
return callApiEE(defaultEnvironmentSet)
}
Expand Down
20 changes: 19 additions & 1 deletion internal/router/api/v1/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func PostAddServiceToCluster(c *gin.Context) {
})
return
}

c.JSON(http.StatusOK, types.JSONSuccessResponse{
Message: fmt.Sprintf("service %s has been created", serviceName),
})
}

// DeleteServiceFromCluster godoc
Expand Down Expand Up @@ -241,11 +245,25 @@ func DeleteServiceFromCluster(c *gin.Context) {
return
}

err = services.DeleteService(&cl, serviceName)
// Bind to variable as application/json, handle error
var serviceDefinition pkgtypes.GitopsCatalogAppDeleteRequest
err = c.Bind(&serviceDefinition)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

err = services.DeleteService(&cl, serviceName, serviceDefinition)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

c.JSON(http.StatusOK, types.JSONSuccessResponse{
Message: fmt.Sprintf("service %s has been deleted", serviceName),
})
}
Loading

0 comments on commit 03f4862

Please sign in to comment.