Skip to content

Commit

Permalink
feat: azure domains endpoint (#469)
Browse files Browse the repository at this point in the history
* feat: resource groups and domain endpoints

* chore: feedback

* fix: return proper http codes

* feat: store kubefirst pro flag and cluster secret reference creation

* chore: go mod tidy

* feat: remove resource groups endpoint

* chore: remove empty line

* chore: fix unrelated linting issues
  • Loading branch information
CristhianF7 authored Dec 5, 2024
1 parent ed85444 commit b04a85f
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ require (
golang.org/x/mod v0.22.0
golang.org/x/oauth2 v0.24.0
golang.org/x/sync v0.9.0
golang.org/x/term v0.26.0
golang.org/x/text v0.20.0
google.golang.org/api v0.209.0
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -295,6 +294,7 @@ require (
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.27.0 // indirect
google.golang.org/genproto v0.0.0-20241113202542-65e8d215514f // indirect
Expand Down
25 changes: 25 additions & 0 deletions internal/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,28 @@ func NewClient(clientID, clientSecret, subscriptionID, tenantID string) (*Client
subscriptionID: subscriptionID,
}, nil
}

func (c *Client) GetDNSDomains(ctx context.Context, resourceGroup string) ([]string, error) {
client, err := c.newDNSClientFactory()
if err != nil {
return nil, err
}

var domains []string
pager := client.NewZonesClient().NewListByResourceGroupPager(resourceGroup, nil)

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list DNS zones: %w", err)
}

for _, zone := range page.Value {
if zone.Name != nil {
domains = append(domains, *zone.Name)
}
}
}

return domains, nil
}
1 change: 1 addition & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ func (clctrl *ClusterController) InitController(def *types.ClusterDefinition) er
NodeCount: clctrl.NodeCount,
LogFileName: def.LogFileName,
PostInstallCatalogApps: clctrl.PostInstallCatalogApps,
InstallKubefirstPro: clctrl.InstallKubefirstPro,
}

if !recordExists {
Expand Down
11 changes: 8 additions & 3 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

func NewEnvironment(envDef types.Environment) (types.Environment, error) {
func NewEnvironment(envDef types.Environment) (*types.Environment, error) {
// Create new environment
envDef.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC()))

kcfg := utils.GetKubernetesClient("TODO: Secrets")
newEnv, err := secrets.InsertEnvironment(kcfg.Clientset, envDef)
return newEnv, fmt.Errorf("error creating new environment in db: %w", err)
if err != nil {
return nil, fmt.Errorf("error creating new environment in db: %w", err)
}

return &newEnv, nil
}

func CreateDefaultClusters(mgmtCluster types.Cluster) error {
Expand Down Expand Up @@ -78,11 +82,12 @@ func CreateDefaultClusters(mgmtCluster types.Cluster) error {
}

var err error
vcluster.Environment, err = NewEnvironment(vcluster.Environment)
newEnv, err := NewEnvironment(vcluster.Environment)
if err != nil {
log.Error().Msgf("error creating default environment in db for env %s", err)
return fmt.Errorf("error creating default environment in db for environment %q: %w", clusterName, err)
}
vcluster.Environment = *newEnv
defaultClusters = append(defaultClusters, vcluster)
}

Expand Down
47 changes: 40 additions & 7 deletions internal/router/api/v1/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
cloudflare_api "github.com/cloudflare/cloudflare-go"
"github.com/gin-gonic/gin"
awsinternal "github.com/konstructio/kubefirst-api/internal/aws"
"github.com/konstructio/kubefirst-api/internal/azure"
"github.com/konstructio/kubefirst-api/internal/civo"
cloudflare "github.com/konstructio/kubefirst-api/internal/cloudflare"
"github.com/konstructio/kubefirst-api/internal/digitalocean"
Expand Down Expand Up @@ -73,7 +74,7 @@ func PostDomains(c *gin.Context) {

domains, err := client.ListDomains(context.Background(), &linodego.ListOptions{})
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand Down Expand Up @@ -119,6 +120,38 @@ func PostDomains(c *gin.Context) {
return
}
domainListResponse.Domains = domains

case "azure":
err = domainListRequest.AzureAuth.ValidateAuthCredentials()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

azureClient, err := azure.NewClient(
domainListRequest.AzureAuth.ClientID,
domainListRequest.AzureAuth.ClientSecret,
domainListRequest.AzureAuth.SubscriptionID,
domainListRequest.AzureAuth.TenantID,
)
if err != nil {
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

domains, err := azureClient.GetDNSDomains(context.Background(), domainListRequest.ResourceGroup)
if err != nil {
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

domainListResponse.Domains = domains
case "cloudflare":
// check for token, make sure it aint blank
if domainListRequest.CloudflareAuth.APIToken == "" {
Expand All @@ -130,7 +163,7 @@ func PostDomains(c *gin.Context) {

client, err := cloudflare_api.NewWithAPIToken(domainListRequest.CloudflareAuth.APIToken)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: fmt.Sprintf("Could not create cloudflare client, %v", err),
})
return
Expand All @@ -143,7 +176,7 @@ func PostDomains(c *gin.Context) {

domains, err := cloudflareConf.GetDNSDomains()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand All @@ -165,7 +198,7 @@ func PostDomains(c *gin.Context) {

domains, err := civoConf.GetDNSDomains()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand All @@ -185,7 +218,7 @@ func PostDomains(c *gin.Context) {

domains, err := digitaloceanConf.GetDNSDomains()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand All @@ -205,7 +238,7 @@ func PostDomains(c *gin.Context) {

domains, err := vultrConf.GetDNSDomains()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand All @@ -228,7 +261,7 @@ func PostDomains(c *gin.Context) {

domains, err := googleConf.GetDNSDomains()
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
c.JSON(http.StatusInternalServerError, types.JSONFailureResponse{
Message: err.Error(),
})
return
Expand Down
8 changes: 4 additions & 4 deletions internal/secrets/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func InsertCluster(clientSet kubernetes.Interface, cl pkgtypes.Cluster) error {
if err := UpsertSecretReference(clientSet, secretName, secretReference); err != nil {
return fmt.Errorf("when inserting cluster: error creating secret reference: %w", err)
}
}

if err := AddSecretReferenceItem(clientSet, secretName, cl.ClusterName); err != nil {
return fmt.Errorf("when inserting cluster: error adding secret reference item: %w", err)
} else {
if err := AddSecretReferenceItem(clientSet, secretName, cl.ClusterName); err != nil {
return fmt.Errorf("when inserting cluster: error adding secret reference item: %w", err)
}
}

bytes, err := json.Marshal(cl)
Expand Down
2 changes: 2 additions & 0 deletions internal/types/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
// DomainListRequest
type DomainListRequest struct {
CloudRegion string `json:"cloud_region"`
ResourceGroup string `json:"resource_group"`
AkamaiAuth pkgtypes.AkamaiAuth `json:"akamai_auth,omitempty"`
AWSAuth pkgtypes.AWSAuth `json:"aws_auth,omitempty"`
CivoAuth pkgtypes.CivoAuth `json:"civo_auth,omitempty"`
DigitaloceanAuth pkgtypes.DigitaloceanAuth `json:"do_auth,omitempty"`
VultrAuth pkgtypes.VultrAuth `json:"vultr_auth,omitempty"`
CloudflareAuth pkgtypes.CloudflareAuth `json:"cloudflare_auth,omitempty"`
GoogleAuth pkgtypes.GoogleAuth `bson:"google_auth,omitempty" json:"google_auth,omitempty"`
AzureAuth pkgtypes.AzureAuth `bson:"azure_auth,omitempty" json:"azure_auth,omitempty"`
}

// DomainListResponse
Expand Down
8 changes: 0 additions & 8 deletions pkg/types/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ type AWSAuth struct {
SessionToken string `bson:"session_token" json:"session_token"`
}

// AzureAuth holds necessary auth credentials for interacting with azure
type AzureAuth struct {
ClientID string `bson:"client_id" json:"client_id"`
ClientSecret string `bson:"client_secret" json:"client_secret"`
TenantID string `bson:"tenant_id" json:"tenant_id"`
SubscriptionID string `bson:"subscription_id" json:"subscription_id"`
}

// CivoAuth holds necessary auth credentials for interacting with civo
type CivoAuth struct {
Token string `bson:"token" json:"token"`
Expand Down
22 changes: 22 additions & 0 deletions pkg/types/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

import "fmt"

// AzureAuth holds necessary auth credentials for interacting with azure
type AzureAuth struct {
ClientID string `bson:"client_id" json:"client_id"`
ClientSecret string `bson:"client_secret" json:"client_secret"`
TenantID string `bson:"tenant_id" json:"tenant_id"`
SubscriptionID string `bson:"subscription_id" json:"subscription_id"`
}

func (auth *AzureAuth) ValidateAuthCredentials() error {
if auth.ClientID == "" ||
auth.ClientSecret == "" ||
auth.SubscriptionID == "" ||
auth.TenantID == "" {
return fmt.Errorf("missing authentication credentials in request, please check and try again")
}

return nil
}
1 change: 1 addition & 0 deletions pkg/types/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type Cluster struct {
UseTelemetry bool `bson:"use_telemetry"`

// Checks
InstallKubefirstPro bool `bson:"install_kubefirst_pro,omitempty" json:"install_kubefirst_pro,omitempty"`
InstallToolsCheck bool `bson:"install_tools_check" json:"install_tools_check"`
DomainLivenessCheck bool `bson:"domain_liveness_check" json:"domain_liveness_check"`
StateStoreCredsCheck bool `bson:"state_store_creds_check" json:"state_store_creds_check"`
Expand Down

0 comments on commit b04a85f

Please sign in to comment.