Skip to content

Commit

Permalink
add kustomization validation service
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Jun 19, 2023
1 parent 0b9f022 commit 91549a0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,56 +1,65 @@
package kustomizeclient

import (
"errors"
"fmt"
config "momentum/momentum-core/momentum-config"
services "momentum/momentum-core/momentum-services"
tree "momentum/momentum-core/momentum-tree"
utils "momentum/momentum-core/momentum-utils"

"github.com/pocketbase/pocketbase/models"
"github.com/spf13/cobra"
kustomize "sigs.k8s.io/kustomize/kustomize/v5/commands"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

const KUSTOMIZE_BUILD_COMMAND_NAME = "build"

type KustomizationValidationService struct {
config *config.MomentumConfig
repositoryService *services.RepositoryService
}

func (kustomizationService *KustomizationValidationService) Validate(repo *models.Record) bool {
func NewKustomizationValidationService(config *config.MomentumConfig, repositoryService *services.RepositoryService) *KustomizationValidationService {

if repo.Collection().Name != config.TABLE_REPOSITORIES_NAME {
fmt.Println("cannot validate non repositories collection record")
return false
}
validator := new(KustomizationValidationService)

validator.config = config
validator.repositoryService = repositoryService

return validator
}

func (kustomizationService *KustomizationValidationService) Validate(repoName string) (bool, error) {

repoName := repo.GetString(config.TABLE_REPOSITORIES_FIELD_NAME)
path := utils.BuildPath(kustomizationService.config.ValidationTmpDir(), repoName)
src := utils.BuildPath(kustomizationService.config.DataDir(), repoName)

err := kustomizationService.prepareValidation(path, src)
if err != nil {
fmt.Println("error while validating kustomize structure:", err.Error())
fmt.Println("error while validating kustomize structure (prepare):", err.Error())
kustomizationService.validationCleanup(path)
return false
return false, err
}
err = kustomizationService.check(path)

repoTree, err := tree.Parse(path, []string{".git"})
if err != nil {
fmt.Println("error while validating kustomize structure:", err.Error())
kustomizationService.validationCleanup(path)
return false
fmt.Println("failed parsing validation directory")
return false, err
}

success := kustomizationService.checkSuccessful(path)
for _, app := range repoTree.Apps() {
err = kustomizationService.check(app.FullPath())
if err != nil {
fmt.Println("error while validating kustomize structure (check):", err.Error())
kustomizationService.validationCleanup(path)
return false, err
}
}

err = kustomizationService.validationCleanup(path)
if err != nil {
fmt.Println("error while validating kustomize structure:", err.Error())
fmt.Println("error while validating kustomize structure (cleanup):", err.Error())
return false, err
}

return success
return true, nil
}

func (kustomizationService *KustomizationValidationService) prepareValidation(path string, src string) error {
Expand All @@ -61,29 +70,15 @@ func (kustomizationService *KustomizationValidationService) prepareValidation(pa

func (kustomizationService *KustomizationValidationService) check(path string) error {

var buildCmd *cobra.Command = nil
for _, cmd := range kustomize.NewDefaultCommand().Commands() {
if cmd.Name() == KUSTOMIZE_BUILD_COMMAND_NAME {
buildCmd = cmd
break
}
}

if buildCmd == nil {
return errors.New("build command was not found")
}
fs := filesys.MakeFsOnDisk()

// TODO: FIND OUT HOW TO SET PATH HERE???
buildCmd.Args = kustomizationService.kustomizeBuildArgs
kustomizer := krusty.MakeKustomizer(krusty.MakeDefaultOptions())

err := buildCmd.Execute()

return err
}

func (kustomizationService *KustomizationValidationService) checkSuccessful(path string) bool {

return true
_, err := kustomizer.Run(fs, path)
if err != nil {
return err
}
return nil
}

func (kustomizationService *KustomizationValidationService) validationCleanup(path string) error {
Expand All @@ -94,8 +89,3 @@ func (kustomizationService *KustomizationValidationService) validationCleanup(pa
}
return nil
}

func (kustomizationService *KustomizationValidationService) kustomizeBuildArgs(cmd *cobra.Command, args []string) error {

return nil
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package momentumcontrollers

import (
"errors"
"fmt"
"strings"

gitclient "momentum/git-client"
kustomizeclient "momentum/kustomize-client"
config "momentum/momentum-core/momentum-config"
services "momentum/momentum-core/momentum-services"
tree "momentum/momentum-core/momentum-tree"
Expand All @@ -24,15 +26,21 @@ type RepositoryController struct {
repositoryService *services.RepositoryService
deploymentService *services.DeploymentService
repositoryAddedEventChannel chan *RepositoryAddedEvent
kustomizeValidation *kustomizeclient.KustomizationValidationService
}

func NewRepositoryController(repoService *services.RepositoryService, deploymentService *services.DeploymentService, repositoryAddedEventChannel chan *RepositoryAddedEvent) *RepositoryController {
func NewRepositoryController(
repoService *services.RepositoryService,
deploymentService *services.DeploymentService,
repositoryAddedEventChannel chan *RepositoryAddedEvent,
kustomizeValidator *kustomizeclient.KustomizationValidationService) *RepositoryController {

repoController := new(RepositoryController)

repoController.repositoryService = repoService
repoController.deploymentService = deploymentService
repoController.repositoryAddedEventChannel = repositoryAddedEventChannel
repoController.kustomizeValidation = kustomizeValidator

return repoController
}
Expand All @@ -56,6 +64,14 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi
return err
}

validationSuccessful, err := rc.kustomizeValidation.Validate(repoName)
if !validationSuccessful {
if err != nil {
return err
}
return errors.New("the repository could not be validated with kustomize")
}

repo, err := tree.Parse(path, []string{".git"})
if err != nil {
return err
Expand Down
16 changes: 15 additions & 1 deletion momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package momentumcore

import (
"errors"
"fmt"

kustomizeclient "momentum/kustomize-client"
conf "momentum/momentum-core/momentum-config"
controllers "momentum/momentum-core/momentum-controllers"
services "momentum/momentum-core/momentum-services"
Expand Down Expand Up @@ -31,6 +33,8 @@ type MomentumDispatcher struct {
ApplicationsController *controllers.ApplicationController
StagesController *controllers.StageController
DeploymentController *controllers.DeploymentController

kustomizeValidator *kustomizeclient.KustomizationValidationService
}

func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *MomentumDispatcher {
Expand All @@ -45,7 +49,9 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
appService := services.NewApplicationService(pb.Dao(), stageService)
repoService := services.NewRepositoryService(pb.Dao(), appService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL)
dispatcher.kustomizeValidator = kustomizeclient.NewKustomizationValidationService(dispatcher.Config, repoService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL, dispatcher.kustomizeValidator)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService, repoService)
dispatcher.StagesController = controllers.NewStageController(stageService)
dispatcher.DeploymentController = controllers.NewDeploymentController(deploymentService, repoService)
Expand Down Expand Up @@ -146,6 +152,14 @@ func (d *MomentumDispatcher) setupRepositoryAddedEventChannelObserver() {
return err
}

validationSuccessful, err := d.kustomizeValidator.Validate(event.RepositoryName)
if !validationSuccessful {
if err != nil {
return nil
}
return errors.New("validation failed for repository on repository added event")
}

return nil
})
}
7 changes: 1 addition & 6 deletions momentum-backend/momentum-core/momentum-utils/filehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ func FileCopy(from string, to string) bool {
func DirCopy(from string, to string) (string, error) {

if !IsDirectory(from) || FileExists(to) {
return to, errors.New("From-Path must be directory and To-Path must be non existent.")
}

err := DirCreate(to)
if err != nil {
return to, err
return to, errors.New("from path must be directory and to path must be non existent")
}

return to, dirCopyRecursive(from, to, "")
Expand Down

0 comments on commit 91549a0

Please sign in to comment.