Skip to content

Commit

Permalink
make the serving port for idpBuilder configurable
Browse files Browse the repository at this point in the history
- add a --port flag to configure the default port
- add go templates directives to override embedded resource manifests
- template kind cluster config
- add a sample script to tweak the port for the reference example

Signed-off-by: Nima Kaviani <[email protected]>
  • Loading branch information
nimakaviani committed Feb 24, 2024
1 parent 6b704e6 commit 7fd76c5
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 73 deletions.
2 changes: 1 addition & 1 deletion examples/local-backup/kind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ nodes:
containerPath: /backup
extraPortMappings:
- containerPort: 443
hostPort: 8443
hostPort: {{ .Port }}
protocol: TCP
2 changes: 2 additions & 0 deletions examples/ref-implementation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ idpbuilder create --package-dir examples/ref-implementation

This will take ~6 minutes for everything to come up. To track the progress, you can go to the [ArgoCD UI](https://argocd.cnoe.localtest.me:8443/applications).

**_NOTE:_**: _This example assumes that you run the reference implementation with the default port configguration of 8443 for the idpBuilder. If you happen to configure a different port for the idpBuilder, the manifests in the reference example need to be updated and be configured with the new port. you can use the [replace-port.sh](replace-port.sh) to change the port as desired prior to applying the manifest as instructed in the command above._

### What was installed?

1. **Argo Workflows** to enable workflow orchestrations.
Expand Down
20 changes: 20 additions & 0 deletions examples/ref-implementation/replace-port.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Check if the new port number is provided as an argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 NEW_PORT"
exit 1
fi

# Assign the first script argument to NEW_PORT
NEW_PORT="$1"

# Base directory to start from, "." means the current directory
BASE_DIRECTORY="."

# Find all .yaml files recursively starting from the base directory
# and perform an in-place search and replace from 8443 to the new port
find "$BASE_DIRECTORY" -type f -name "*.yaml" -exec sed -i '' "s/8443/${NEW_PORT}/g" {} +

echo "Replacement complete. All occurrences of 8443 have been changed to ${NEW_PORT}."

2 changes: 1 addition & 1 deletion hack/ingress-nginx/deployment-ingress-nginx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
- --controller-class=k8s.io/ingress-nginx
- --ingress-class=nginx
- --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
- --validating-webhook=:8443
- --validating-webhook=:{{ .Port }}
- --validating-webhook-certificate=/usr/local/certificates/cert
- --validating-webhook-key=/usr/local/certificates/key
- --watch-ingress-without-class=true
Expand Down
4 changes: 2 additions & 2 deletions hack/ingress-nginx/service-ingress-nginx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
ipFamilyPolicy: SingleStack
ports:
- appProtocol: https
name: https-8443
port: 8443
name: https-{{ .Port }}
port: {{ .Port }}
protocol: TCP
targetPort: https
14 changes: 9 additions & 5 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package build
import (
"context"
"fmt"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/controllers"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/cnoe-io/idpbuilder/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand All @@ -14,7 +17,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"time"
)

var (
Expand All @@ -23,6 +25,7 @@ var (

type Build struct {
name string
cfg util.TemplateConfig
kindConfigPath string
kubeConfigPath string
kubeVersion string
Expand All @@ -33,7 +36,7 @@ type Build struct {
CancelFunc context.CancelFunc
}

func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, customPackageDirs []string, exitOnSync bool, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, cfg util.TemplateConfig, customPackageDirs []string, exitOnSync bool, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
return &Build{
name: name,
kindConfigPath: kindConfigPath,
Expand All @@ -43,13 +46,14 @@ func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMappi
customPackageDirs: customPackageDirs,
exitOnSync: exitOnSync,
scheme: scheme,
cfg: cfg,
CancelFunc: ctxCancel,
}
}

func (b *Build) ReconcileKindCluster(ctx context.Context, recreateCluster bool) error {
// Initialize Kind Cluster
cluster, err := kind.NewCluster(b.name, b.kubeVersion, b.kubeConfigPath, b.kindConfigPath, b.extraPortsMapping)
cluster, err := kind.NewCluster(b.name, b.kubeVersion, b.kubeConfigPath, b.kindConfigPath, b.extraPortsMapping, b.cfg)
if err != nil {
setupLog.Error(err, "Error Creating kind cluster")
return err
Expand Down Expand Up @@ -89,15 +93,15 @@ func (b *Build) GetKubeClient(kubeConfig *rest.Config) (client.Client, error) {

func (b *Build) ReconcileCRDs(ctx context.Context, kubeClient client.Client) error {
// Ensure idpbuilder CRDs
if err := controllers.EnsureCRDs(ctx, b.scheme, kubeClient); err != nil {
if err := controllers.EnsureCRDs(ctx, b.scheme, kubeClient, b.cfg); err != nil {
setupLog.Error(err, "Error creating idpbuilder CRDs")
return err
}
return nil
}

func (b *Build) RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error) error {
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc, b.exitOnSync)
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc, b.exitOnSync, b.cfg)
}

func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cnoe-io/idpbuilder/pkg/build"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -19,6 +20,7 @@ import (
var (
// Flags
recreateCluster bool
port string
buildName string
kubeVersion string
extraPortsMapping string
Expand All @@ -37,6 +39,7 @@ var CreateCmd = &cobra.Command{
func init() {
CreateCmd.PersistentFlags().BoolVar(&recreateCluster, "recreate", false, "Delete cluster first if it already exists.")
CreateCmd.PersistentFlags().StringVar(&buildName, "build-name", "localdev", "Name for build (Prefix for kind cluster name, pod names, etc).")
CreateCmd.PersistentFlags().StringVar(&port, "port", "8443", "Port number under which idpBuilder tools are accessible.")
CreateCmd.PersistentFlags().StringVar(&kubeVersion, "kube-version", "v1.27.3", "Version of the kind kubernetes cluster to create.")
CreateCmd.PersistentFlags().StringVar(&extraPortsMapping, "extra-ports", "", "List of extra ports to expose on the docker container and kubernetes cluster as nodePort (e.g. \"22:32222,9090:39090,etc\").")
CreateCmd.PersistentFlags().StringVar(&kindConfigPath, "kind-config", "", "Path of the kind config file to be used instead of the default.")
Expand Down Expand Up @@ -78,14 +81,14 @@ func create(cmd *cobra.Command, args []string) error {
exitOnSync = !noExit
}

b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, absDirPaths, exitOnSync, k8s.GetScheme(), ctxCancel)
b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, util.TemplateConfig{Port: port}, absDirPaths, exitOnSync, k8s.GetScheme(), ctxCancel)

if err := b.Run(ctx, recreateCluster); err != nil {
return err
}

fmt.Print("\n\n########################### Finished Creating IDP Successfully! ############################\n\n\n")
fmt.Print("Can Access ArgoCD at https://argocd.cnoe.localtest.me:8443/\nUsername: admin\n")
fmt.Printf("Can Access ArgoCD at https://argocd.cnoe.localtest.me:%s/\nUsername: admin\n", port)
fmt.Print(`Password can be retrieved by running: kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d`, "\n")

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
//go:embed resources/*.yaml
var crdFS embed.FS

func getK8sResources(scheme *runtime.Scheme) ([]client.Object, error) {
rawResources, err := util.ConvertFSToBytes(crdFS, "resources")
func getK8sResources(scheme *runtime.Scheme, template interface{}) ([]client.Object, error) {
rawResources, err := util.ConvertFSToBytes(crdFS, "resources", template)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,8 +87,8 @@ func EnsureCRD(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Cl
return nil
}

func EnsureCRDs(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Client) error {
installObjs, err := getK8sResources(scheme)
func EnsureCRDs(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Client, template interface{}) error {
installObjs, err := getK8sResources(scheme, template)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Reconciler struct {
client.Client
Recorder record.EventRecorder
Scheme *runtime.Scheme
Config util.TemplateConfig
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -75,7 +76,12 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
return ctrl.Result{}, fmt.Errorf("reading file %s: %w", resource.Spec.ArgoCD.ApplicationFile, err)
}

objs, err := k8s.ConvertYamlToObjects(r.Scheme, b)
var returnedRawResource []byte
if returnedRawResource, err = util.ApplyTemplate(b, r.Config); err != nil {
return ctrl.Result{}, err
}

objs, err := k8s.ConvertYamlToObjects(r.Scheme, returnedRawResource)
if err != nil {
return ctrl.Result{}, fmt.Errorf("converting yaml to object %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type RepositoryReconciler struct {
GiteaClientFunc GiteaClientFunc
Recorder record.EventRecorder
Scheme *runtime.Scheme
Config util.TemplateConfig
}

func getRepositoryName(repo v1alpha1.GitRepository) string {
Expand Down Expand Up @@ -180,7 +181,7 @@ func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v
return fmt.Errorf("cloning repo: %w", err)
}

err = writeRepoContents(repo, tempDir)
err = writeRepoContents(repo, tempDir, r.Config)
if err != nil {
return err
}
Expand Down Expand Up @@ -275,9 +276,9 @@ func (r *RepositoryReconciler) shouldProcess(repo v1alpha1.GitRepository) bool {
return true
}

func writeRepoContents(repo *v1alpha1.GitRepository, dstPath string) error {
func writeRepoContents(repo *v1alpha1.GitRepository, dstPath string, template interface{}) error {
if repo.Spec.Source.EmbeddedAppName != "" {
resources, err := localbuild.GetEmbeddedRawInstallResources(repo.Spec.Source.EmbeddedAppName)
resources, err := localbuild.GetEmbeddedRawInstallResources(repo.Spec.Source.EmbeddedAppName, template)
if err != nil {
return fmt.Errorf("getting embedded resource; %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/localbuild/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
argocdNamespace string = "argocd"
)

func RawArgocdInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(installArgoFS, "resources/argo")
func RawArgocdInstallResources(tmpl interface{}) ([][]byte, error) {
return util.ConvertFSToBytes(installArgoFS, "resources/argo", tmpl)
}

func (r *LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (r *LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Reque
skipReadinessCheck: true,
}

if result, err := argocd.Install(ctx, req, resource, r.Client, r.Scheme); err != nil {
if result, err := argocd.Install(ctx, req, resource, r.Client, r.Scheme, r.Config); err != nil {
return result, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/localbuild/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestGetRawInstallResources(t *testing.T) {
resourceFS: installArgoFS,
resourcePath: "resources/argo",
}
resources, err := e.rawInstallResources()
resources, err := e.rawInstallResources(struct{ Port string }{"8443"})
if err != nil {
t.Fatalf("GetRawInstallResources() error: %v", err)
}
Expand All @@ -31,7 +31,7 @@ func TestGetK8sInstallResources(t *testing.T) {
resourceFS: installArgoFS,
resourcePath: "resources/argo",
}
objs, err := e.installResources(k8s.GetScheme())
objs, err := e.installResources(k8s.GetScheme(), struct{ Port string }{"8443"})
if err != nil {
t.Fatalf("GetK8sInstallResources() error: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type LocalbuildReconciler struct {
CancelFunc context.CancelFunc
ExitOnSync bool
shouldShutdown bool
Config util.TemplateConfig
}

type subReconciler func(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error)
Expand Down Expand Up @@ -399,14 +400,14 @@ func getCustomPackageName(fileName, appName string) string {
return fmt.Sprintf("%s-%s", strings.ToLower(s[0]), appName)
}

func GetEmbeddedRawInstallResources(name string) ([][]byte, error) {
func GetEmbeddedRawInstallResources(name string, template interface{}) ([][]byte, error) {
switch name {
case "argocd":
return RawArgocdInstallResources()
return RawArgocdInstallResources(template)
case "gitea":
return RawGiteaInstallResources()
return RawGiteaInstallResources(template)
case "nginx":
return RawNginxInstallResources()
return RawNginxInstallResources(template)
default:
return nil, fmt.Errorf("unsupported embedded app name %s", name)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package localbuild
import (
"context"
"embed"
"fmt"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/util"
Expand All @@ -15,7 +16,7 @@ const (
giteaNamespace = "gitea"
giteaAdminSecret = "gitea-admin-secret"
// this is the URL accessible outside cluster. resolves to localhost
giteaIngressURL = "https://gitea.cnoe.localtest.me:8443"
giteaIngressURL = "https://gitea.cnoe.localtest.me:%s"
// this is the URL accessible within cluster for ArgoCD to fetch resources.
// resolves to cluster ip
giteaSvcURL = "http://my-gitea-http.gitea.svc.cluster.local:3000"
Expand All @@ -24,8 +25,8 @@ const (
//go:embed resources/gitea/k8s/*
var installGiteaFS embed.FS

func RawGiteaInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(installGiteaFS, "resources/gitea/k8s")
func RawGiteaInstallResources(tmpl interface{}) ([][]byte, error) {
return util.ConvertFSToBytes(installGiteaFS, "resources/gitea/k8s", tmpl)
}

func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
Expand All @@ -43,10 +44,10 @@ func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Requ
},
}

if result, err := gitea.Install(ctx, req, resource, r.Client, r.Scheme); err != nil {
if result, err := gitea.Install(ctx, req, resource, r.Client, r.Scheme, r.Config); err != nil {
return result, err
}
resource.Status.Gitea.ExternalURL = giteaIngressURL
resource.Status.Gitea.ExternalURL = fmt.Sprintf(giteaIngressURL, r.Config.Port)
resource.Status.Gitea.InternalURL = giteaSvcURL
resource.Status.Gitea.AdminUserSecretName = giteaAdminSecret
resource.Status.Gitea.AdminUserSecretNamespace = giteaNamespace
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/localbuild/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ type EmbeddedInstallation struct {
resourceFS embed.FS
}

func (e *EmbeddedInstallation) rawInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(e.resourceFS, e.resourcePath)
func (e *EmbeddedInstallation) rawInstallResources(template interface{}) ([][]byte, error) {
return util.ConvertFSToBytes(e.resourceFS, e.resourcePath, template)
}

func (e *EmbeddedInstallation) installResources(scheme *runtime.Scheme) ([]client.Object, error) {
rawResources, err := e.rawInstallResources()
func (e *EmbeddedInstallation) installResources(scheme *runtime.Scheme, template interface{}) ([]client.Object, error) {
rawResources, err := e.rawInstallResources(template)
if err != nil {
return nil, err
}
Expand All @@ -60,11 +60,11 @@ func (e *EmbeddedInstallation) newNamespace(namespace string) *corev1.Namespace
}
}

func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild, cli client.Client, sc *runtime.Scheme) (ctrl.Result, error) {
func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild, cli client.Client, sc *runtime.Scheme, cfg util.TemplateConfig) (ctrl.Result, error) {
log := log.FromContext(ctx)

nsClient := client.NewNamespacedClient(cli, e.namespace)
installObjs, err := e.installResources(sc)
installObjs, err := e.installResources(sc, cfg)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/localbuild/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
//go:embed resources/nginx/k8s/*
var installNginxFS embed.FS

func RawNginxInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(installNginxFS, "resources/nginx/k8s")
func RawNginxInstallResources(tmpl interface{}) ([][]byte, error) {
return util.ConvertFSToBytes(installNginxFS, "resources/nginx/k8s", tmpl)
}

func (r *LocalbuildReconciler) ReconcileNginx(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
Expand All @@ -36,7 +36,7 @@ func (r *LocalbuildReconciler) ReconcileNginx(ctx context.Context, req ctrl.Requ
},
}

if result, err := nginx.Install(ctx, req, resource, r.Client, r.Scheme); err != nil {
if result, err := nginx.Install(ctx, req, resource, r.Client, r.Scheme, r.Config); err != nil {
return result, err
}

Expand Down
Loading

0 comments on commit 7fd76c5

Please sign in to comment.