Skip to content

Commit

Permalink
feat: runtime migration (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianF7 committed Jul 16, 2024
1 parent ceed503 commit ccd331f
Show file tree
Hide file tree
Showing 48 changed files with 215 additions and 91 deletions.
1 change: 1 addition & 0 deletions internal/configs/config.go → configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ environment variables and general config data.
*/

const DefaultK1Version = "development"
const DefaultGitOpsTemplateBranch = "main"

// K1Version is used on version command. The value is dynamically updated on build time via ldflag. Built Kubefirst
// versions will follow semver value like 1.9.0, when not using the built version, "development" is used.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"net/http"
"testing"

"github.com/kubefirst/kubefirst-api/configs"
pkg "github.com/kubefirst/kubefirst-api/internal"
"github.com/kubefirst/kubefirst-api/internal/configs"
"github.com/spf13/viper"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"fmt"
Expand Down
6 changes: 1 addition & 5 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

const (
gitopsTemplateVersion = "v2.4.12"
)

type ClusterController struct {
CloudProvider string
CloudRegion string
Expand Down Expand Up @@ -210,7 +206,7 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition)
if def.GitopsTemplateBranch != "" {
clctrl.GitopsTemplateBranch = def.GitopsTemplateBranch
} else {
clctrl.GitopsTemplateBranch = gitopsTemplateVersion
clctrl.GitopsTemplateBranch = env.KubefirstVersion
}

if def.GitopsTemplateURL != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Env struct {
ServerPort string `env:"SERVER_PORT" envDefault:"8081"`
K1AccessToken string `env:"K1_ACCESS_TOKEN"`
KubefirstVersion string `env:"KUBEFIRST_VERSION" envDefault:"development"`
KubefirstVersion string `env:"KUBEFIRST_VERSION" envDefault:"main"`
CloudProvider string `env:"CLOUD_PROVIDER"`
ClusterId string `env:"CLUSTER_ID"`
ClusterType string `env:"CLUSTER_TYPE"`
Expand Down
4 changes: 2 additions & 2 deletions internal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"errors"
Expand All @@ -24,7 +24,7 @@ import (

"github.com/kubefirst/kubefirst-api/internal/progressPrinter"

"github.com/kubefirst/kubefirst-api/internal/configs"
"github.com/kubefirst/kubefirst-api/configs"
"github.com/spf13/viper"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/k3d/detokenize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strconv"
"strings"

"github.com/kubefirst/kubefirst-api/internal/configs"
"github.com/kubefirst/kubefirst-api/configs"
)

// detokenizeGitGitops - Translate tokens by values on a given path
Expand Down
32 changes: 1 addition & 31 deletions pkg/k8s/secret.go → internal/k8s/k8s.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package k8s

import (
"context"
"encoding/json"

"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// CreateSecretV2 creates a Kubernetes Secret
func CreateSecretV2(clientset *kubernetes.Clientset, secret *v1.Secret) error {
_, err := clientset.CoreV1().Secrets(secret.Namespace).Create(
context.Background(),
secret,
metav1.CreateOptions{},
)
if err != nil {
return err
}
log.Info().Msgf("created Secret %s in Namespace %s\n", secret.Name, secret.Namespace)
return nil
}

// ReadSecretV2 reads the content of a Kubernetes Secret
func ReadSecretV2(clientset *kubernetes.Clientset, namespace string, secretName string) (map[string]interface{}, error) {
func ReadSecretV2Old(clientset *kubernetes.Clientset, namespace string, secretName string) (map[string]interface{}, error) {
secret, err := clientset.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil {
log.Warn().Msgf("no secret found: %s\n", err)
Expand Down Expand Up @@ -78,11 +56,3 @@ func UpdateSecretV2(clientset *kubernetes.Clientset, namespace string, secretNam
log.Info().Msgf("updated Secret %s in Namespace %s\n", currentSecret.Name, currentSecret.Namespace)
return nil
}

func mapToJSON(data map[string]interface{}) (string, error) {
jsonBytes, err := json.Marshal(data)
if err != nil {
return "", err // Handle error.
}
return string(jsonBytes), nil
}
2 changes: 1 addition & 1 deletion internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion internal/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion internal/progress_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions internal/router/api/v1/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kubefirst/kubefirst-api/internal/k8s"
"github.com/kubefirst/kubefirst-api/internal/secrets"
"github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/kubefirst-api/internal/utils"
"github.com/kubefirst/kubefirst-api/pkg/k8s"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -37,7 +37,7 @@ func GetClusterSecret(c *gin.Context) {
}

kcfg := utils.GetKubernetesClient(clusterName)
kubefirstSecrets, _ := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", secret)
kubefirstSecrets, _ := k8s.ReadSecretV2Old(kcfg.Clientset, "kubefirst", secret)

jsonString, err := secrets.MapToStructuredJSON(kubefirstSecrets)

Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"fmt"

"github.com/kubefirst/kubefirst-api/pkg/k8s"
"github.com/kubefirst/kubefirst-api/internal/k8s"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -42,7 +42,7 @@ func DeleteCluster(clientSet *kubernetes.Clientset, clusterName string) error {
func GetCluster(clientSet *kubernetes.Clientset, clusterName string) (pkgtypes.Cluster, error) {
cluster := pkgtypes.Cluster{}

clusterSecret, err := k8s.ReadSecretV2(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_CLUSTER_PREFIX, clusterName))
clusterSecret, err := k8s.ReadSecretV2Old(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_CLUSTER_PREFIX, clusterName))
if err != nil {
return cluster, fmt.Errorf("secret not found: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/kubefirst/kubefirst-api/internal/k8s"
"github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/kubefirst-api/pkg/k8s"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -35,7 +35,7 @@ func GetEnvironments(clientSet *kubernetes.Clientset) ([]pkgtypes.Environment, e
func GetEnvironment(clientSet *kubernetes.Clientset, name string) (pkgtypes.Environment, error) {
environment := pkgtypes.Environment{}

kubefirstSecrets, _ := k8s.ReadSecretV2(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_ENVIRONMENT_PREFIX, name))
kubefirstSecrets, _ := k8s.ReadSecretV2Old(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_ENVIRONMENT_PREFIX, name))
jsonString, _ := MapToStructuredJSON(kubefirstSecrets)

jsonData, err := json.Marshal(jsonString)
Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/gitopsCatalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"

"github.com/kubefirst/kubefirst-api/internal/gitopsCatalog"
"github.com/kubefirst/kubefirst-api/pkg/k8s"
"github.com/kubefirst/kubefirst-api/internal/k8s"
"github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -48,7 +48,7 @@ func CreateGitopsCatalogApps(clientSet *kubernetes.Clientset, catalogApps types.
func GetGitopsCatalogApps(clientSet *kubernetes.Clientset) (types.GitopsCatalogApps, error) {
catalogApps := types.GitopsCatalogApps{}

kubefirstSecrets, err := k8s.ReadSecretV2(clientSet, "kubefirst", KUBEFIRST_CATALOG_SECRET_NAME)
kubefirstSecrets, err := k8s.ReadSecretV2Old(clientSet, "kubefirst", KUBEFIRST_CATALOG_SECRET_NAME)
if err != nil {
return catalogApps, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/kubefirst/kubefirst-api/internal/constants"
"github.com/kubefirst/kubefirst-api/internal/env"
"github.com/kubefirst/kubefirst-api/pkg/k8s"
"github.com/kubefirst/kubefirst-api/internal/k8s"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
"github.com/rs/zerolog/log"
)
Expand All @@ -34,7 +34,7 @@ func ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster, error) {

kcfg := k8s.CreateKubeConfig(true, "")
log.Info().Msg("reading secret kubefirst-initial-state to determine if import is needed")
secData, err := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", "kubefirst-initial-state")
secData, err := k8s.ReadSecretV2Old(kcfg.Clientset, "kubefirst", "kubefirst-initial-state")
if err != nil {
log.Info().Msgf("error reading secret kubefirst-initial-state. %s", err)
return cluster, err
Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"fmt"

"github.com/kubefirst/kubefirst-api/pkg/k8s"
"github.com/kubefirst/kubefirst-api/internal/k8s"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -20,7 +20,7 @@ import (
func GetSecretReference(clientSet *kubernetes.Clientset, secretName string) (pkgtypes.SecretListReference, error) {
secretReference := pkgtypes.SecretListReference{}

kubefirstSecrets, err := k8s.ReadSecretV2(clientSet, "kubefirst", secretName)
kubefirstSecrets, err := k8s.ReadSecretV2Old(clientSet, "kubefirst", secretName)
if err != nil {
return secretReference, fmt.Errorf("secret not found: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/secrets/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"fmt"

"github.com/kubefirst/kubefirst-api/pkg/k8s"
"github.com/kubefirst/kubefirst-api/internal/k8s"
"github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -97,7 +97,7 @@ func GetService(clientSet *kubernetes.Clientset, clusterName string, serviceName
func GetServices(clientSet *kubernetes.Clientset, clusterName string) (types.ClusterServiceList, error) {
clusterServices := types.ClusterServiceList{}

kubefirstSecrets, err := k8s.ReadSecretV2(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_SERVICES_PREFIX, clusterName))
kubefirstSecrets, err := k8s.ReadSecretV2Old(clientSet, "kubefirst", fmt.Sprintf("%s-%s", KUBEFIRST_SERVICES_PREFIX, clusterName))

jsonString, err := MapToStructuredJSON(kubefirstSecrets)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

"github.com/kubefirst/kubefirst-api/internal/constants"
"github.com/kubefirst/kubefirst-api/internal/env"
"github.com/kubefirst/kubefirst-api/internal/k8s"
"github.com/kubefirst/kubefirst-api/internal/secrets"
"github.com/kubefirst/kubefirst-api/pkg/k8s"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down
2 changes: 1 addition & 1 deletion internal/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package pkg
package internal

import "syscall"

Expand Down
7 changes: 7 additions & 0 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package argocd

import "github.com/kubefirst/kubefirst-api/internal/argocd"

var ArgocdSecretClient = argocd.ArgocdSecretClient
var GetArgocdTokenV2 = argocd.GetArgocdTokenV2
var GetArgoCDApplicationObject = argocd.GetArgoCDApplicationObject
6 changes: 6 additions & 0 deletions pkg/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
awsinternal "github.com/kubefirst/kubefirst-api/internal/aws"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -39,3 +40,8 @@ func NewEKSServiceAccountClientV1() aws.Config {

return awsClient
}

type AWSConfiguration = awsinternal.AWSConfiguration
type QuotaDetailResponse = awsinternal.QuotaDetailResponse

var NewAwsV2 = awsinternal.NewAwsV2
9 changes: 9 additions & 0 deletions pkg/configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package configs

import "github.com/kubefirst/kubefirst-api/configs"

const DefaultK1Version = configs.DefaultK1Version

var K1Version = configs.K1Version
var ReadConfig = configs.ReadConfig
var InitializeViperConfig = configs.InitializeViperConfig
13 changes: 13 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constants

import internal "github.com/kubefirst/kubefirst-api/internal"

const MinimumAvailableDiskSize = internal.MinimumAvailableDiskSize
const MinioDefaultUsername = internal.MinioDefaultUsername
const MinioDefaultPassword = internal.MinioDefaultPassword
const KubefirstManifestRepoRef = internal.KubefirstManifestRepoRef
const MinioPortForwardEndpoint = internal.MinioPortForwardEndpoint
const MinioRegion = internal.MinioRegion

var ArgoCDLocalURLTLS = internal.ArgoCDLocalURLTLS
var KubefirstConsoleLocalURLTLS = internal.KubefirstConsoleLocalURLTLS
7 changes: 7 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package docker

import internal "github.com/kubefirst/kubefirst-api/internal/docker"

type DockerClientWrapper = internal.DockerClientWrapper

var NewDockerClient = internal.NewDockerClient
Loading

0 comments on commit ccd331f

Please sign in to comment.