Skip to content

Commit

Permalink
Reorg the packages to avoid cycling issue
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Jan 16, 2025
1 parent 1115087 commit 01a0b22
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 44 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"embed"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/util/fs"
"time"

"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/util"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -20,7 +20,7 @@ import (
var crdFS embed.FS

func getK8sResources(scheme *runtime.Scheme, templateData any) ([]client.Object, error) {
rawResources, err := util.ConvertFSToBytes(crdFS, "resources", templateData)
rawResources, err := fs.ConvertFSToBytes(crdFS, "resources", templateData)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/gitrepository/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitrepository
import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/util/files"
"os"
"path/filepath"

Expand Down Expand Up @@ -136,7 +137,7 @@ func writeRepoContents(repo *v1alpha1.GitRepository, dstPath string, config v1al
return nil
}

err := util.CopyDirectory(repo.Spec.Source.Path, dstPath)
err := files.CopyDirectory(repo.Spec.Source.Path, dstPath)
if err != nil {
return fmt.Errorf("copying files: %w", 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 @@ -2,13 +2,13 @@ package localbuild

import (
"context"
"github.com/cnoe-io/idpbuilder/pkg/util/fs"
"testing"

argov1alpha1 "github.com/cnoe-io/argocd-api/api/argo/application/v1alpha1"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestGetRawInstallResources(t *testing.T) {
resourceFS: installArgoFS,
resourcePath: "resources/argo",
}
resources, err := util.ConvertFSToBytes(e.resourceFS, e.resourcePath,
resources, err := fs.ConvertFSToBytes(e.resourceFS, e.resourcePath,
v1alpha1.BuildCustomizationSpec{
Protocol: "",
Host: "",
Expand Down
7 changes: 3 additions & 4 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package k8s
import (
"context"
"fmt"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down
9 changes: 5 additions & 4 deletions pkg/k8s/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package k8s

import (
"embed"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/cnoe-io/idpbuilder/pkg/util/files"
"github.com/cnoe-io/idpbuilder/pkg/util/fs"
"k8s.io/apimachinery/pkg/runtime"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func BuildCustomizedManifests(filePath, fsPath string, resourceFS embed.FS, scheme *runtime.Scheme, templateData any) ([][]byte, error) {
rawResources, err := util.ConvertFSToBytes(resourceFS, fsPath, templateData)
rawResources, err := fs.ConvertFSToBytes(resourceFS, fsPath, templateData)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +28,7 @@ func BuildCustomizedManifests(filePath, fsPath string, resourceFS embed.FS, sche
}

func BuildCustomizedObjects(filePath, fsPath string, resourceFS embed.FS, scheme *runtime.Scheme, templateData any) ([]client.Object, error) {
rawResources, err := util.ConvertFSToBytes(resourceFS, fsPath, templateData)
rawResources, err := fs.ConvertFSToBytes(resourceFS, fsPath, templateData)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +51,7 @@ func applyOverrides(filePath string, originalFiles [][]byte, scheme *runtime.Sch
return nil, nil, err
}

rendered, err := util.ApplyTemplate(customBS, templateData)
rendered, err := files.ApplyTemplate(customBS, templateData)
if err != nil {
return nil, nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"embed"
"errors"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/cnoe-io/idpbuilder/pkg/util/files"
"io/fs"
"os"
"strconv"
"strings"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/log"
kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (c *Cluster) getConfig() ([]byte, error) {
}

var retBuff []byte
if retBuff, err = util.ApplyTemplate(rawConfigTempl, TemplateConfig{
if retBuff, err = files.ApplyTemplate(rawConfigTempl, TemplateConfig{
BuildCustomizationSpec: c.cfg,
KubernetesVersion: c.kubeVersion,
ExtraPortsMapping: portMappingPairs,
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/util/idp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -15,7 +16,7 @@ const (
)

func ArgocdBaseUrl(ctx context.Context) (string, error) {
idpConfig, err := GetIDPConfig(ctx)
idpConfig, err := idp.GetConfig(ctx)
if err != nil {
return "", fmt.Errorf("error fetching idp config: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/files.go → pkg/util/files/files.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package files

import (
"bytes"
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/fs.go → pkg/util/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package util
package fs

import (
"errors"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/util/files"
"io"
"io/fs"
"os"
Expand All @@ -29,7 +30,7 @@ func ConvertFSToBytes(inFS FS, name string, templateData any) ([][]byte, error)
return nil, err
}

if returnedRawResource, err := ApplyTemplate(rawResource, templateData); err == nil {
if returnedRawResource, err := files.ApplyTemplate(rawResource, templateData); err == nil {
rawResources = append(rawResources, returnedRawResource)
} else {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/fs_test.go → pkg/util/fs/fs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package fs

import (
"fmt"
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"fmt"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/util/idp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -113,7 +114,7 @@ func GetGiteaToken(ctx context.Context, baseUrl, username, password string) (str
}

func GiteaBaseUrl(ctx context.Context) (string, error) {
idpConfig, err := GetIDPConfig(ctx)
idpConfig, err := idp.GetConfig(ctx)
if err != nil {
return "", fmt.Errorf("error fetching idp config: %s", err)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/util/idp/idp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package idp

import (
"context"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetConfig(ctx context.Context) (v1alpha1.BuildCustomizationSpec, error) {
b := v1alpha1.BuildCustomizationSpec{}

kubeClient, err := k8s.GetKubeClient()
if err != nil {
return b, err
}

list, err := getLocalBuild(ctx, kubeClient)
if err != nil {
return b, err
}

// TODO: We assume that only one LocalBuild exists !
return list.Items[0].Spec.BuildCustomization, nil
}

func getLocalBuild(ctx context.Context, kubeClient client.Client) (v1alpha1.LocalbuildList, error) {
localBuildList := v1alpha1.LocalbuildList{}
return localBuildList, kubeClient.List(ctx, &localBuildList)
}
23 changes: 0 additions & 23 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/rand"
"crypto/tls"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"math"
"math/big"
mathrand "math/rand"
Expand All @@ -32,28 +31,6 @@ const (
StaticPassword = "developer"
)

func GetIDPConfig(ctx context.Context) (v1alpha1.BuildCustomizationSpec, error) {
b := v1alpha1.BuildCustomizationSpec{}

kubeClient, err := k8s.GetKubeClient()
if err != nil {
return b, err
}

list, err := getLocalBuild(ctx, kubeClient)
if err != nil {
return b, err
}

// TODO: We assume that only one LocalBuild exists !
return list.Items[0].Spec.BuildCustomization, nil
}

func getLocalBuild(ctx context.Context, kubeClient client.Client) (v1alpha1.LocalbuildList, error) {
localBuildList := v1alpha1.LocalbuildList{}
return localBuildList, kubeClient.List(ctx, &localBuildList)
}

func GetCLIStartTimeAnnotationValue(annotations map[string]string) (string, error) {
if annotations == nil {
return "", fmt.Errorf("this object's annotation is nil")
Expand Down

0 comments on commit 01a0b22

Please sign in to comment.