Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jokestax committed Sep 3, 2024
2 parents 83b3118 + 6fa2d22 commit 7def846
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 154 deletions.
4 changes: 4 additions & 0 deletions cmd/akamai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func Create() *cobra.Command {
createCmd.Flags().StringVar(&adminTeamName, "admin-team-name", "admins", "admin team name for this repo")
createCmd.Flags().StringVar(&developerTeamName, "developer-team-name", "developers", "developer team name for this repo")
createCmd.Flags().BoolVar(&installKubefirstProFlag, "install-kubefirst-pro", true, "whether or not to install kubefirst pro")
createCmd.Flags().StringVar(&gitopsRepoName, "gitops-repo-name", "gitops", "the custom gitops name")
createCmd.Flags().StringVar(&metaphorRepoName, "metaphor-repo-name", "metaphor", "the custom metaphor name")
createCmd.Flags().StringVar(&adminTeamName, "admin-team-name", "admins", "admin team name for this repo")
createCmd.Flags().StringVar(&developerTeamName, "developer-team-name", "developers", "developer team name for this repo")

return createCmd
}
Expand Down
68 changes: 6 additions & 62 deletions cmd/k3d/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
//}

// Instantiate K3d config
config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag, gitopsRepoName, metaphorRepoName, adminTeamName, developerTeamName)
config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag, gitopsRepoName, metaphorRepoName, adminTeamName, developerTeamName)
config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag)
switch gitProviderFlag {
case "github":
config.GithubToken = cGitToken
Expand Down Expand Up @@ -395,8 +394,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
log.Info().Msgf("kubefirst version configs.K1Version: %s ", configs.K1Version)
log.Info().Msgf("cloning gitops-template repo url: %s ", gitopsTemplateURLFlag)
log.Info().Msgf("cloning gitops-template repo branch: %s ", gitopsTemplateBranchFlag)
log.Info().Msgf("branch %s\b", gitopsTemplateBranchFlag)
log.Info().Msgf("branch %s ", gitopsTemplateBranchFlag)

atlantisWebhookSecret := viper.GetString("secrets.atlantis-webhook")
if atlantisWebhookSecret == "" {
atlantisWebhookSecret = utils.Random(20)
Expand Down Expand Up @@ -556,8 +554,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
if !viper.GetBool("kubefirst-checks.tools-downloaded") {
log.Info().Msg("installing kubefirst dependencies")

err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol, config.GitopsRepoName, config.MetaphorRepoName, config.AdminTeamName, config.DeveloperTeamName)
err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol, config.GitopsRepoName, config.MetaphorRepoName, config.AdminTeamName, config.DeveloperTeamName)
err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol)
if err != nil {
return err
}
Expand All @@ -573,8 +570,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
metaphorTemplateTokens := k3d.MetaphorTokenValues{
ClusterName: clusterNameFlag,
CloudRegion: cloudRegionFlag,
ContainerRegistryURL: fmt.Sprintf("%s/%s/%s", containerRegistryHost, cGitOwner, config.MetaphorRepoName),
ContainerRegistryURL: fmt.Sprintf("%s/%s/%s", containerRegistryHost, cGitOwner, config.MetaphorRepoName),
ContainerRegistryURL: fmt.Sprintf("%s/%s/metaphor", containerRegistryHost, cGitOwner),
DomainName: k3d.DomainName,
MetaphorDevelopmentIngressURL: fmt.Sprintf("metaphor-development.%s", k3d.DomainName),
MetaphorStagingIngressURL: fmt.Sprintf("metaphor-staging.%s", k3d.DomainName),
Expand Down Expand Up @@ -616,10 +612,6 @@ func runK3d(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
viper.Set("adminTeamName", config.AdminTeamName)
viper.Set("developerTeamName", config.DeveloperTeamName)
viper.Set("adminTeamName", config.AdminTeamName)
viper.Set("developerTeamName", config.DeveloperTeamName)

// todo emit init telemetry end
viper.Set("kubefirst-checks.gitops-ready-to-push", true)
Expand Down Expand Up @@ -1072,53 +1064,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
log.Info().Msg("applying the registry application to argocd")
registryApplicationObject := argocd.GetArgoCDApplicationObject(gitopsRepoURL, fmt.Sprintf("registry/%s", clusterNameFlag))

err = k3d.RestartDeployment(context.Background(), kcfg.Clientset, "argocd", "argocd-applicationset-controller")
err = k3d.RestartDeployment(context.Background(), kcfg.Clientset, "argocd", "argocd-applicationset-controller")
if err != nil {
log.Info().Msgf("Error executing kubectl command: %v\n", err)
return err
}


retryAttempts := 2
for attempt := 1; attempt <= retryAttempts; attempt++ {
log.Info().Msgf("Attempt #%d to create Argo CD application...\n", attempt)

app, err := argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})
return fmt.Errorf("error in restarting argocd controller %w", err)
}

err = wait.PollImmediate(5*time.Second, 20*time.Second, func() (bool, error) {
_, err := argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})
if err != nil {
if errors.Is(err, syscall.ECONNREFUSED) {
return false, nil // retry if we can't connect to it
}

if apierrors.IsAlreadyExists(err) {
return true, nil // application already exists
}

return false, fmt.Errorf("error creating argocd application : %w", err)
}
return true, nil
})
if err != nil {
return fmt.Errorf("error creating argocd application : %w", err)
}

log.Info().Msg("Argo CD application created successfully")
return false, nil
}
return true, nil
})

if err != nil {
return fmt.Errorf("error creating argocd application : %w", err)
}

log.Info().Msg("Argo CD application created successfully\n")

_, _ = argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})
viper.Set("kubefirst-checks.argocd-create-registry", true)
viper.WriteConfig()
telemetry.SendEvent(segClient, telemetry.CreateRegistryCompleted, "")
Expand Down Expand Up @@ -1225,8 +1171,7 @@ func runK3d(cmd *cobra.Command, args []string) error {

// define upload object
objectName := fmt.Sprintf("terraform/%s/terraform.tfstate", config.GitProvider)
filePath := config.K1Dir + fmt.Sprintf("/%s/%s", objectName, config.GitopsRepoName)
filePath := config.K1Dir + fmt.Sprintf("/%s/%s", objectName, config.GitopsRepoName)
filePath := config.K1Dir + fmt.Sprintf("/gitops/%s", objectName)
contentType := "xl.meta"
bucketName := "kubefirst-state-store"
log.Info().Msgf("BucketName: %s", bucketName)
Expand Down Expand Up @@ -1496,4 +1441,3 @@ func runK3d(cmd *cobra.Command, args []string) error {

return nil
}

8 changes: 6 additions & 2 deletions cmd/k3d/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ func destroyK3d(cmd *cobra.Command, args []string) error {

// Check for existing port forwards before continuing

gitopsRepoName, metaphorRepoName := common.Getgitmeta(clusterName)
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(clusterName)

err := k8s.CheckForExistingPortForwards(9000)
if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

err = k8s.CheckForExistingPortForwards(9000)
if err != nil {
log.Error().Msgf("%s - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error())
return fmt.Errorf("%s (maybe the handoff screen is still open in another terminal) - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error())
Expand Down
7 changes: 6 additions & 1 deletion cmd/k3d/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func unsealVault(cmd *cobra.Command, args []string) error {
if !flags.SetupComplete {
return fmt.Errorf("there doesn't appear to be an active k3d cluster")
}
gitopsRepoName, metaphorRepoName := common.Getgitmeta(viper.GetString("flags.cluster-name"))
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(viper.GetString("flags.cluster-name"))

if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

config := k3d.GetConfig(
viper.GetString("flags.cluster-name"),
flags.GitProvider,
Expand Down
89 changes: 0 additions & 89 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,85 +114,6 @@ func GetRootCredentials(cmd *cobra.Command, args []string) error {
return nil
}

func GetGitmeta(clusterName string) (string, string, error) {

var gitopsFound, metaphorFound bool
homePath, err := os.UserHomeDir()
dirs, err := ioutil.ReadDir(fmt.Sprintf("%s/.k1/%s", homePath, clusterName))
var gitopsRepoName, metaphorRepoName string

homePath, err := os.UserHomeDir()
if err != nil {
log.Info().Msg("Error reading directory")
return "cantfindgit", "cantfindmeta"
}

for _, direc := range dirs {
if direc.IsDir() {
parentdir, err := ioutil.ReadDir(fmt.Sprintf("%s/.k1/%s/%s", homePath, clusterName, direc.Name()))
return "", "", fmt.Errorf("unable to get user's home directory: %w", err)
}

basePath := filepath.Join(homePath, ".k1", clusterName)

err = filepath.WalkDir(basePath, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}

if info.IsDir() {
relPath, err := filepath.Rel(basePath, path)

if err != nil {
return fmt.Errorf("error while finding repository : %w", err)
}

if relPath == "." || strings.Count(relPath, string(os.PathSeparator)) == 1 {
if info.Name() == "registry" {
if !gitopsFound {
gitopsRepoName = filepath.Dir(relPath)
gitopsFound = true
} else if dir.Name() == ".github" {
metaphorRepoName = direc.Name()
}
}
if info.Name() == ".github" {
if !metaphorFound {
metaphorRepoName = filepath.Dir(relPath)
metaphorFound = true
}
}
}

}
}
}
if metaphorFound && gitopsFound {
return fs.SkipDir
}

return nil
})

if err != nil {
log.Info().Msg("Error reading directory")
return "", "", fmt.Errorf("Error Reading %w", err)
}

if !gitopsFound {
log.Info().Msg("Gitops Repo not found")
return "cantfindgit", "cantfindmeta"
return "", "", fmt.Errorf("Gitopsrepo Not Found")
}

if !metaphorFound {
log.Info().Msg("Metaphor Repo not found")
return "", "", fmt.Errorf("MetaphorRepo Not Found")
}

return gitopsRepoName, metaphorRepoName, nil
}

func Destroy(cmd *cobra.Command, args []string) error {
// Determine if there are active instal ls
gitProvider := viper.GetString("flags.git-provider")
Expand All @@ -204,12 +125,6 @@ func Destroy(cmd *cobra.Command, args []string) error {
clusterName := viper.GetString("flags.cluster-name")
domainName := viper.GetString("flags.domain-name")

gitopsRepoName, metaphorRepoName := Getgitmeta(clusterName)
gitopsRepoName, metaphorRepoName, err := GetGitmeta(clusterName)

if err != nil {
return err
}
// Switch based on git provider, set params
cGitOwner := ""
switch gitProvider {
Expand All @@ -230,10 +145,6 @@ func Destroy(cmd *cobra.Command, args []string) error {
gitProtocol,
os.Getenv("CF_API_TOKEN"),
os.Getenv("CF_ORIGIN_CA_ISSUER_API_TOKEN"),
gitopsRepoName,
metaphorRepoName,
gitopsRepoName,
metaphorRepoName,
)

progress.AddStep("Destroying k3d")
Expand Down

0 comments on commit 7def846

Please sign in to comment.