Skip to content

Commit

Permalink
Support creating gitserver from local path
Browse files Browse the repository at this point in the history
Signed-off-by: Greg Haynes <[email protected]>
  • Loading branch information
greghaynes committed Nov 10, 2023
1 parent b13a151 commit d750f52
Showing 1 changed file with 104 additions and 50 deletions.
154 changes: 104 additions & 50 deletions pkg/cmd/gitserver/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
"github.com/cnoe-io/idpbuilder/pkg/apps"
"github.com/cnoe-io/idpbuilder/pkg/controllers"
"github.com/cnoe-io/idpbuilder/pkg/controllers/gitserver"
"github.com/cnoe-io/idpbuilder/pkg/docker"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/kind"
Expand All @@ -26,9 +26,9 @@ import (

var (
// Flags
buildName string
serverName string
sourcePath string
serverName string
sourcePath string
kubeConfigPath string

setupLog = ctrl.Log.WithName("setup")
)
Expand All @@ -42,9 +42,9 @@ var CreateCmd = &cobra.Command{
}

func init() {
CreateCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name for build (Prefix for kind cluster name, pod names, etc)")
CreateCmd.PersistentFlags().StringVar(&serverName, "serverName", "", "Name of gitserver, must be unique within a build (name)")
CreateCmd.PersistentFlags().StringVar(&serverName, "serverName", "", "Name of gitserver")
CreateCmd.PersistentFlags().StringVar(&sourcePath, "source", "", "Path to directory to use as gitserver source")
CreateCmd.PersistentFlags().StringVar(&kubeConfigPath, "kubeConfigPath", filepath.Join(homedir.HomeDir(), ".kube", "config"), "Path to Kubernetes config.")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Expand All @@ -66,28 +66,91 @@ func preCreate(cmd *cobra.Command, args []string) error {
return nil
}

func create(cmd *cobra.Command, args []string) error {
ctx, ctxCancel := context.WithCancel(ctrl.SetupSignalHandler())
defer ctxCancel()
func createPackageGitServer(ctx context.Context, kubeClient client.Client, name string, imageId string) (*v1alpha1.GitServer, error) {
resource := v1alpha1.GitServer{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
},
}
if _, err := controllerutil.CreateOrUpdate(ctx, kubeClient, &resource, func() error {
resource.Spec = v1alpha1.GitServerSpec{
Source: v1alpha1.GitServerSource{
Image: imageId,
},
}
return nil
}); err != nil {
setupLog.Error(err, "Creating package gitserver")
return nil, err
}

key := client.ObjectKeyFromObject(&resource)
for {
setupLog.Info("Waiting for gitserver to become available")
if err := kubeClient.Get(ctx, key, &resource); err != nil {
setupLog.Error(err, "Getting gitserver object when waiting")
return nil, err
}

// Build a docker client
if !resource.Status.DeploymentAvailable {
time.Sleep(time.Second)
} else {
setupLog.Info("GitServer available")
break
}
}

return &resource, nil
}

func createPackageImage(ctx context.Context, name string) (*string, error) {
dockerClient, err := docker.GetDockerClient()
if err != nil {
return err
return nil, err
}

// Create gitserver image
crossplaneFS := os.DirFS(sourcePath)
imageTag := fmt.Sprintf("localhost:%d/%s-gitserver-%s", kind.ExposedRegistryPort, name)

Check failure on line 115 in pkg/cmd/gitserver/create/root.go

View workflow job for this annotation

GitHub Actions / build

fmt.Sprintf format %s reads arg #3, but call has 2 args
if _, err := apps.BuildAppsImage(ctx, dockerClient, []string{imageTag}, map[string]string{}, crossplaneFS); err != nil {
setupLog.Error(err, "Building package image")
return nil, err
}

// Push crossplane gitserver image
regImgId, err := apps.PushImage(ctx, dockerClient, imageTag)
if err != nil {
setupLog.Error(err, "Pushing package image")
return nil, err
}
if regImgId == nil {
err = fmt.Errorf("nil img id when pushing crossplane image")
setupLog.Error(err, "Pushing package image")
return nil, err
}
defer dockerClient.Close()
imageId := fmt.Sprintf("%s@%s", imageTag, *regImgId)
return &imageId, nil
}

// Build a kube client
func create(cmd *cobra.Command, args []string) error {
ctx := ctrl.SetupSignalHandler()
scheme := k8s.GetScheme()
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
managerExit := make(chan error)
ctx, ctxCancel := context.WithCancel(ctx)
defer ctxCancel()

// Get kube config
kubeConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
setupLog.Error(err, "Building kubeconfig from kind cluster")
setupLog.Error(err, "Error building kubeconfig from kind cluster")
return err
}

// Get kube client
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "Creating kubernetes client")
setupLog.Error(err, "Error creating kubernetes client")
return err
}

Expand All @@ -96,51 +159,42 @@ func create(cmd *cobra.Command, args []string) error {
Scheme: scheme,
})
if err != nil {
setupLog.Error(err, "Creating controller manager")
setupLog.Error(err, "Creating package controller manager")
return err
}

// Run controllers
managerExit := make(chan error)
if err := controllers.RunControllers(ctx, mgr, managerExit, ctxCancel); err != nil {
setupLog.Error(err, "Running controllers")
return err
}

// Build and push image
srcFS := os.DirFS(sourcePath)
imageTag := fmt.Sprintf("localhost:%d/%s-%s-%s", kind.ExposedRegistryPort, globals.ProjectName, buildName, serverName)
imageID, err := apps.BuildAppsImage(ctx, dockerClient, []string{imageTag}, map[string]string{}, srcFS)
// Run GitServer controller
appsFS, err := apps.GetAppsFS()
if err != nil {
setupLog.Error(err, "unable to find srv dir in apps fs")
return err
}
if imageID == nil {
return fmt.Errorf("failed to get image id after build")
if err := (&gitserver.GitServerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Content: appsFS,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create gitserver controller")
return err
}

regImgId, err := apps.PushImage(ctx, dockerClient, imageTag)
// Start our manager in another goroutine
setupLog.Info("starting manager")
go func() {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
managerExit <- err
}
managerExit <- nil
}()

imgId, err := createPackageImage(ctx, serverName)
if err != nil {
return err
}
if regImgId == nil {
return fmt.Errorf("failed to get registry image id after push")
}

// Create GitServer k8s resource
gitServer := v1alpha1.GitServer{
ObjectMeta: metav1.ObjectMeta{
Name: serverName,
Namespace: globals.GetProjectNamespace(buildName),
},
}
if _, err := controllerutil.CreateOrUpdate(ctx, kubeClient, &gitServer, func() error {
gitServer.Spec = v1alpha1.GitServerSpec{
Source: v1alpha1.GitServerSource{
Image: fmt.Sprintf("%s@%s", imageTag, *regImgId),
},
}
return nil
}); err != nil {
_, err = createPackageGitServer(ctx, kubeClient, serverName, *imgId)
if err != nil {
return err
}

Expand Down

0 comments on commit d750f52

Please sign in to comment.