Init project
This commit is contained in:
63
internal/controller/helmdiff_controller.go
Normal file
63
internal/controller/helmdiff_controller.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
)
|
||||
|
||||
// HelmDiffReconciler reconciles a HelmDiff object
|
||||
type HelmDiffReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmdiffs,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmdiffs/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmdiffs/finalizers,verbs=update
|
||||
|
||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||
// move the current state of the cluster closer to the desired state.
|
||||
// TODO(user): Modify the Reconcile function to compare the state specified by
|
||||
// the HelmDiff object against the actual cluster state, and then
|
||||
// perform operations to make the cluster state reflect the state specified by
|
||||
// the user.
|
||||
//
|
||||
// For more details, check Reconcile and its Result here:
|
||||
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/reconcile
|
||||
func (r *HelmDiffReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
_ = logf.FromContext(ctx)
|
||||
|
||||
// TODO(user): your logic here
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *HelmDiffReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&yahov1alpha1.HelmDiff{}).
|
||||
Named("helmdiff").
|
||||
Complete(r)
|
||||
}
|
84
internal/controller/helmdiff_controller_test.go
Normal file
84
internal/controller/helmdiff_controller_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
)
|
||||
|
||||
var _ = Describe("HelmDiff Controller", func() {
|
||||
Context("When reconciling a resource", func() {
|
||||
const resourceName = "test-resource"
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
typeNamespacedName := types.NamespacedName{
|
||||
Name: resourceName,
|
||||
Namespace: "default", // TODO(user):Modify as needed
|
||||
}
|
||||
helmdiff := &yahov1alpha1.HelmDiff{}
|
||||
|
||||
BeforeEach(func() {
|
||||
By("creating the custom resource for the Kind HelmDiff")
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, helmdiff)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
resource := &yahov1alpha1.HelmDiff{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: resourceName,
|
||||
Namespace: "default",
|
||||
},
|
||||
// TODO(user): Specify other spec details if needed.
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO(user): Cleanup logic after each test, like removing the resource instance.
|
||||
resource := &yahov1alpha1.HelmDiff{}
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, resource)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("Cleanup the specific resource instance HelmDiff")
|
||||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
|
||||
})
|
||||
It("should successfully reconcile the resource", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &HelmDiffReconciler{
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
}
|
||||
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
|
||||
// Example: If you expect a certain status condition after reconciliation, verify it here.
|
||||
})
|
||||
})
|
||||
})
|
94
internal/controller/helmrelease_controller.go
Normal file
94
internal/controller/helmrelease_controller.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
"github.com/allanger/yaho/internal/downloader"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
)
|
||||
|
||||
// HelmReleaseReconciler reconciles a HelmRelease object
|
||||
type HelmReleaseReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases/finalizers,verbs=update
|
||||
|
||||
func (r *HelmReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
log := logf.FromContext(ctx)
|
||||
log.Info("reconciliation is started")
|
||||
|
||||
reconcilePeriod := 30 * time.Second
|
||||
reconcileResult := reconcile.Result{RequeueAfter: reconcilePeriod}
|
||||
helmReleaseCR := &yahov1alpha1.HelmRelease{}
|
||||
err := r.Get(ctx, req.NamespacedName, helmReleaseCR)
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
// Requested object not found, could have been deleted after reconcile request.
|
||||
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
|
||||
// Return and don't requeue
|
||||
return reconcileResult, nil
|
||||
}
|
||||
log.Error(err, "An unxexpected error has occurred during the reconciliation")
|
||||
// Error reading the object - requeue the request.
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
// Update object status always when function exit abnormally or through a panic.
|
||||
defer func() {
|
||||
if err := r.Status().Update(ctx, helmReleaseCR); err != nil {
|
||||
log.Error(err, "Failed to update status")
|
||||
}
|
||||
}()
|
||||
|
||||
// First setup all the controller logic
|
||||
// Pull the chart t a temporary directory
|
||||
//
|
||||
// TODO(user): your logic here
|
||||
path, err := downloader.PullChart(ctx, helmReleaseCR.Spec.Repository, helmReleaseCR.Spec.Chart, helmReleaseCR.Spec.Version, nil)
|
||||
if err != nil {
|
||||
log.Error(err, "An unexpected error has occurred while trying to fetch a chart")
|
||||
return reconcileResult, nil
|
||||
}
|
||||
log.Info("Pulled a chart", "path", path)
|
||||
//path, err := downloader.PullChart(repository, chart, version)
|
||||
//if err != nil { return ... }
|
||||
//err := helm.InstallOrUpdate(); if err != nil { return ...}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *HelmReleaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&yahov1alpha1.HelmRelease{}).
|
||||
Named("helmrelease").
|
||||
Complete(r)
|
||||
}
|
84
internal/controller/helmrelease_controller_test.go
Normal file
84
internal/controller/helmrelease_controller_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
)
|
||||
|
||||
var _ = Describe("HelmRelease Controller", func() {
|
||||
Context("When reconciling a resource", func() {
|
||||
const resourceName = "test-resource"
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
typeNamespacedName := types.NamespacedName{
|
||||
Name: resourceName,
|
||||
Namespace: "default", // TODO(user):Modify as needed
|
||||
}
|
||||
helmrelease := &yahov1alpha1.HelmRelease{}
|
||||
|
||||
BeforeEach(func() {
|
||||
By("creating the custom resource for the Kind HelmRelease")
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, helmrelease)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
resource := &yahov1alpha1.HelmRelease{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: resourceName,
|
||||
Namespace: "default",
|
||||
},
|
||||
// TODO(user): Specify other spec details if needed.
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO(user): Cleanup logic after each test, like removing the resource instance.
|
||||
resource := &yahov1alpha1.HelmRelease{}
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, resource)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("Cleanup the specific resource instance HelmRelease")
|
||||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
|
||||
})
|
||||
It("should successfully reconcile the resource", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &HelmReleaseReconciler{
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
}
|
||||
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
|
||||
// Example: If you expect a certain status condition after reconciliation, verify it here.
|
||||
})
|
||||
})
|
||||
})
|
63
internal/controller/helmvalues_controller.go
Normal file
63
internal/controller/helmvalues_controller.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
)
|
||||
|
||||
// HelmValuesReconciler reconciles a HelmValues object
|
||||
type HelmValuesReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmvalues,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmvalues/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmvalues/finalizers,verbs=update
|
||||
|
||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||
// move the current state of the cluster closer to the desired state.
|
||||
// TODO(user): Modify the Reconcile function to compare the state specified by
|
||||
// the HelmValues object against the actual cluster state, and then
|
||||
// perform operations to make the cluster state reflect the state specified by
|
||||
// the user.
|
||||
//
|
||||
// For more details, check Reconcile and its Result here:
|
||||
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/reconcile
|
||||
func (r *HelmValuesReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
_ = logf.FromContext(ctx)
|
||||
|
||||
// TODO(user): your logic here
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *HelmValuesReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&yahov1alpha1.HelmValues{}).
|
||||
Named("helmvalues").
|
||||
Complete(r)
|
||||
}
|
84
internal/controller/helmvalues_controller_test.go
Normal file
84
internal/controller/helmvalues_controller_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
)
|
||||
|
||||
var _ = Describe("HelmValues Controller", func() {
|
||||
Context("When reconciling a resource", func() {
|
||||
const resourceName = "test-resource"
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
typeNamespacedName := types.NamespacedName{
|
||||
Name: resourceName,
|
||||
Namespace: "default", // TODO(user):Modify as needed
|
||||
}
|
||||
helmvalues := &yahov1alpha1.HelmValues{}
|
||||
|
||||
BeforeEach(func() {
|
||||
By("creating the custom resource for the Kind HelmValues")
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, helmvalues)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
resource := &yahov1alpha1.HelmValues{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: resourceName,
|
||||
Namespace: "default",
|
||||
},
|
||||
// TODO(user): Specify other spec details if needed.
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO(user): Cleanup logic after each test, like removing the resource instance.
|
||||
resource := &yahov1alpha1.HelmValues{}
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, resource)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("Cleanup the specific resource instance HelmValues")
|
||||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
|
||||
})
|
||||
It("should successfully reconcile the resource", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &HelmValuesReconciler{
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
}
|
||||
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
|
||||
// Example: If you expect a certain status condition after reconciliation, verify it here.
|
||||
})
|
||||
})
|
||||
})
|
116
internal/controller/suite_test.go
Normal file
116
internal/controller/suite_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||
|
||||
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
||||
// +kubebuilder:scaffold:imports
|
||||
)
|
||||
|
||||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
||||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
||||
|
||||
var (
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
testEnv *envtest.Environment
|
||||
cfg *rest.Config
|
||||
k8sClient client.Client
|
||||
)
|
||||
|
||||
func TestControllers(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
|
||||
RunSpecs(t, "Controller Suite")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
|
||||
|
||||
ctx, cancel = context.WithCancel(context.TODO())
|
||||
|
||||
var err error
|
||||
err = yahov1alpha1.AddToScheme(scheme.Scheme)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// +kubebuilder:scaffold:scheme
|
||||
|
||||
By("bootstrapping test environment")
|
||||
testEnv = &envtest.Environment{
|
||||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
|
||||
ErrorIfCRDPathMissing: true,
|
||||
}
|
||||
|
||||
// Retrieve the first found binary directory to allow running tests from IDEs
|
||||
if getFirstFoundEnvTestBinaryDir() != "" {
|
||||
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
|
||||
}
|
||||
|
||||
// cfg is defined in this file globally.
|
||||
cfg, err = testEnv.Start()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cfg).NotTo(BeNil())
|
||||
|
||||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(k8sClient).NotTo(BeNil())
|
||||
})
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
By("tearing down the test environment")
|
||||
cancel()
|
||||
err := testEnv.Stop()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
// getFirstFoundEnvTestBinaryDir locates the first binary in the specified path.
|
||||
// ENVTEST-based tests depend on specific binaries, usually located in paths set by
|
||||
// controller-runtime. When running tests directly (e.g., via an IDE) without using
|
||||
// Makefile targets, the 'BinaryAssetsDirectory' must be explicitly configured.
|
||||
//
|
||||
// This function streamlines the process by finding the required binaries, similar to
|
||||
// setting the 'KUBEBUILDER_ASSETS' environment variable. To ensure the binaries are
|
||||
// properly set up, run 'make setup-envtest' beforehand.
|
||||
func getFirstFoundEnvTestBinaryDir() string {
|
||||
basePath := filepath.Join("..", "..", "bin", "k8s")
|
||||
entries, err := os.ReadDir(basePath)
|
||||
if err != nil {
|
||||
logf.Log.Error(err, "Failed to read directory", "path", basePath)
|
||||
return ""
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
return filepath.Join(basePath, entry.Name())
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
3
internal/downloader/git.go
Normal file
3
internal/downloader/git.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package downloader
|
||||
|
||||
// It should download a helm chart from git
|
3
internal/downloader/helm.go
Normal file
3
internal/downloader/helm.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package downloader
|
||||
|
||||
// It should download a helm chart
|
35
internal/downloader/types.go
Normal file
35
internal/downloader/types.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package downloader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/allanger/yaho/internal/tools/git"
|
||||
"github.com/allanger/yaho/internal/tools/helm"
|
||||
)
|
||||
|
||||
type PullOptions struct {}
|
||||
|
||||
// A very stupid check
|
||||
func isGitRepo(repository string) bool {
|
||||
return strings.HasPrefix(repository, "git@") || strings.HasSuffix(repository, ".git")
|
||||
}
|
||||
// Pull chart into a temporary directory
|
||||
func PullChart(ctx context.Context, repository, chart, version string, opts *PullOptions) (string, error) {
|
||||
if isGitRepo(repository) {
|
||||
git := git.GitLib{}
|
||||
path, err := git.CloneRepo(ctx, repository, version)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s/%s", path, chart), nil
|
||||
} else {
|
||||
helm := helm.HelmLib{}
|
||||
path, err := helm.PullChart(ctx, repository, chart, version)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
}
|
39
internal/tools/git/gitlib.go
Normal file
39
internal/tools/git/gitlib.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
)
|
||||
type GitLib struct {
|
||||
SSHKey string
|
||||
SSHKeyPassword string
|
||||
}
|
||||
|
||||
func (g *GitLib) CloneRepo (ctx context.Context, gitURL, revision string) (string, error) {
|
||||
log := log.FromContext(ctx)
|
||||
workdir, err := os.MkdirTemp("/tmp", "helmdownloader")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Info("A temporary directory is created", "path", workdir)
|
||||
if len(g.SSHKey) > 0 {
|
||||
keys, err := ssh.NewPublicKeys("git", []byte(g.SSHKey), g.SSHKeyPassword)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = git.PlainClone(workdir, false, &git.CloneOptions{URL: gitURL, Auth: keys})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
_, err = git.PlainClone(workdir, false, &git.CloneOptions{URL: gitURL})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return workdir, nil
|
||||
}
|
15
internal/tools/git/gitlib_test.go
Normal file
15
internal/tools/git/gitlib_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package git_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/allanger/yaho/internal/tools/git"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGitHttpClone(t *testing.T) {
|
||||
gitlib := &git.GitLib{}
|
||||
path, err := gitlib.CloneRepo("https://github.com/db-operator/db-operator.git", "main")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", path)
|
||||
}
|
91
internal/tools/helm/helmlib.go
Normal file
91
internal/tools/helm/helmlib.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"helm.sh/helm/pkg/chartutil"
|
||||
"helm.sh/helm/pkg/engine"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
"helm.sh/helm/v3/pkg/cli"
|
||||
"helm.sh/helm/v3/pkg/registry"
|
||||
"honnef.co/go/tools/go/loader"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
)
|
||||
|
||||
type HelmLib struct {}
|
||||
|
||||
const (
|
||||
helmRepo = "helm"
|
||||
ociRepo = "oci"
|
||||
)
|
||||
|
||||
func (h *HelmLib) PullChart(ctx context.Context, repository, chart, version string) (string, error) {
|
||||
log := log.FromContext(ctx)
|
||||
workdir, err := os.MkdirTemp("/tmp", "helmdownloader")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Info("A temporary directory is created", "path", workdir)
|
||||
config := new(action.Configuration)
|
||||
cl := cli.New()
|
||||
|
||||
registry, err := registry.NewClient()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
client := action.NewPullWithOpts(action.WithConfig(config))
|
||||
|
||||
prefix := repository[:strings.IndexByte(repository, ':')]
|
||||
var chartRemote string
|
||||
|
||||
switch prefix {
|
||||
case "oci":
|
||||
chartRemote = fmt.Sprintf("%s/%s", repository, chart)
|
||||
client.SetRegistryClient(registry)
|
||||
case "https", "http":
|
||||
client.RepoURL = repository
|
||||
chartRemote = chart
|
||||
default:
|
||||
return "", fmt.Errorf("unknown repo kind: %s", prefix)
|
||||
}
|
||||
client.Settings = cl
|
||||
client.UntarDir = workdir
|
||||
client.Version = version
|
||||
path, err := client.Run(chartRemote)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (h *HelmLib) InstallChart(ctx context.Context, path string, name, namespace string) error {
|
||||
log := log.FromContext(ctx)
|
||||
chartObj, err := loader.Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
values := chartutil.Values{}
|
||||
values["Values"] = nil
|
||||
values["Release"] = map[string]string{
|
||||
"Name": name,
|
||||
"Namespace": namespace,
|
||||
}
|
||||
values["Capabilities"] = map[string]map[string]string{
|
||||
"KubeVersion": {
|
||||
"Version": "v1.27.9",
|
||||
"GitVersion": "v1.27.9",
|
||||
},
|
||||
}
|
||||
files, err := engine.Engine{Strict: false}.Render(chartObj, values)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for file, data := range files {
|
||||
log.Info("File is rendered", "data", file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
24
internal/tools/helm/helmlib_test.go
Normal file
24
internal/tools/helm/helmlib_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package helm_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/allanger/yaho/internal/tools/helm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPullChart(t *testing.T) {
|
||||
helmlib := &helm.HelmLib{}
|
||||
path, err := helmlib.PullChart("https://coredns.github.io/helm", "coredns", "1.42.0")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", path)
|
||||
}
|
||||
|
||||
func TestPullChartOci(t *testing.T) {
|
||||
helmlib := &helm.HelmLib{}
|
||||
path, err := helmlib.PullChart("oci://ghcr.io/allanger/allangers-charts", "memos", "0.6.0")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", path)
|
||||
}
|
||||
|
||||
|
5
internal/tools/helm/types.go
Normal file
5
internal/tools/helm/types.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package helm
|
||||
|
||||
type Helm interface {
|
||||
PullChart() (string, error)
|
||||
}
|
Reference in New Issue
Block a user