-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improvement: refactor for etcd support * makefile: use gsed if using mac * refactor upsert functions * feat: create etcd instance for every vcluster created * release: update version * fix: issue with helm chart gen through make
- Loading branch information
1 parent
6ee1c37
commit e66b443
Showing
34 changed files
with
2,310 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package constants | ||
|
||
const ( | ||
UFFIZZI_APP_COMPONENT_LABEL = "app.uffizzi.com/component" | ||
VCLUSTER = "vcluster" | ||
ETCD = "etcd" | ||
UCLUSTER_NAME_PREFIX = "uc-" | ||
LOFT_HELM_REPO = "loft" | ||
BITNAMI_HELM_REPO = "bitnami" | ||
VCLUSTER_CHART_K3S = "vcluster" | ||
VCLUSTER_CHART_K8S = "vcluster-k8s" | ||
VCLUSTER_CHART_K3S_VERSION = "0.16.4" | ||
VCLUSTER_CHART_K8S_VERSION = "0.16.4" | ||
ETCD_CHART = "etcd" | ||
ETCD_CHART_VERSION = "9.5.6" | ||
BITNAMI_CHART_REPO_URL = "oci://registry-1.docker.io/bitnamicharts" | ||
LOFT_CHART_REPO_URL = "https://charts.loft.sh" | ||
VCLUSTER_K3S_DISTRO = "k3s" | ||
VCLUSTER_K8S_DISTRO = "k8s" | ||
K3S_DATASTORE_ENDPOINT = "K3S_DATASTORE_ENDPOINT" | ||
VCLUSTER_INGRESS_HOSTNAME = "VCLUSTER_INGRESS_HOST" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright 2023. | ||
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 uffizzicluster | ||
|
||
import ( | ||
"context" | ||
uclusteruffizzicomv1alpha1 "github.com/UffizziCloud/uffizzi-cluster-operator/api/v1alpha1" | ||
"github.com/UffizziCloud/uffizzi-cluster-operator/controllers/constants" | ||
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
controllerruntimesource "sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// UffizziClusterReconciler reconciles a UffizziCluster object | ||
type UffizziClusterEtcdReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
func (r *UffizziClusterEtcdReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
// check if the cluster is a k3s cluster | ||
// if it is, then we need to create the etcd cluster | ||
// default lifecycle operation | ||
logger := log.FromContext(ctx) | ||
|
||
// ---------------------- | ||
// UCLUSTER FIND AND CHECK | ||
// ---------------------- | ||
// Fetch the UffizziCluster instance in question and then see which kind of event might have been triggered | ||
uCluster := &uclusteruffizzicomv1alpha1.UffizziCluster{} | ||
if err := r.Get(ctx, req.NamespacedName, uCluster); err != nil { | ||
// Handle error | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if uCluster.Spec.ExternalDatastore == constants.ETCD { | ||
err := r.createBitnamiHelmRepo(ctx, req) | ||
if err != nil && k8serrors.IsAlreadyExists(err) { | ||
// logger.Info("Loft Helm Repo for UffizziCluster already exists", "NamespacedName", req.NamespacedName) | ||
} else { | ||
logger.Info("Bitnami Helm Repo for UffizziCluster created", "NamespacedName", req.NamespacedName) | ||
} | ||
// create a helm release for the etcd cluster | ||
// check if the helm release exists | ||
helmRelease := &fluxhelmv2beta1.HelmRelease{} | ||
err = r.Get(ctx, client.ObjectKey{ | ||
Namespace: uCluster.Namespace, | ||
Name: BuildEtcdHelmReleaseName(uCluster), | ||
}, helmRelease) | ||
if err != nil { | ||
// if the helm release does not exist, create it | ||
hr := buildEtcdHelmRelease(uCluster) | ||
// add UffizziCluster as the owner of this HelmRelease | ||
if err := ctrl.SetControllerReference(uCluster, hr, r.Scheme); err != nil { | ||
logger.Error(err, "unable to set the controller reference") | ||
return ctrl.Result{}, err | ||
} | ||
if err := r.Create(ctx, hr); err != nil { | ||
logger.Error(err, "unable to create the helm release") | ||
return ctrl.Result{}, err | ||
} | ||
} else { | ||
return ctrl.Result{}, nil | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func buildEtcdHelmRelease(uCluster *uclusteruffizzicomv1alpha1.UffizziCluster) *fluxhelmv2beta1.HelmRelease { | ||
return &fluxhelmv2beta1.HelmRelease{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: BuildEtcdHelmReleaseName(uCluster), | ||
Namespace: uCluster.Namespace, | ||
Labels: map[string]string{ | ||
constants.UFFIZZI_APP_COMPONENT_LABEL: constants.ETCD, | ||
}, | ||
}, | ||
Spec: fluxhelmv2beta1.HelmReleaseSpec{ | ||
ReleaseName: BuildEtcdHelmReleaseName(uCluster), | ||
Chart: fluxhelmv2beta1.HelmChartTemplate{ | ||
Spec: fluxhelmv2beta1.HelmChartTemplateSpec{ | ||
Chart: constants.ETCD_CHART, | ||
Version: constants.ETCD_CHART_VERSION, | ||
SourceRef: fluxhelmv2beta1.CrossNamespaceObjectReference{ | ||
Kind: "HelmRepository", | ||
Name: constants.BITNAMI_HELM_REPO, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func BuildEtcdHelmReleaseName(uCluster *uclusteruffizzicomv1alpha1.UffizziCluster) string { | ||
return constants.UCLUSTER_NAME_PREFIX + constants.ETCD + "-" + uCluster.Name | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *UffizziClusterEtcdReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&uclusteruffizzicomv1alpha1.UffizziCluster{}). | ||
// Watch HelmRelease reconciled by the Helm Controller | ||
Watches( | ||
&controllerruntimesource.Kind{Type: &fluxhelmv2beta1.HelmRelease{}}, | ||
&handler.EnqueueRequestForOwner{ | ||
IsController: true, | ||
OwnerType: &uclusteruffizzicomv1alpha1.UffizziCluster{}, | ||
}). | ||
Complete(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uffizzicluster | ||
|
||
import ( | ||
"context" | ||
"github.com/UffizziCloud/uffizzi-cluster-operator/controllers/constants" | ||
fluxsourcev1 "github.com/fluxcd/source-controller/api/v1beta2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
func (r *UffizziClusterEtcdReconciler) createHelmRepo(ctx context.Context, name, namespace, url string) error { | ||
// Create HelmRepository in the same namespace as the HelmRelease | ||
helmRepo := &fluxsourcev1.HelmRepository{ | ||
ObjectMeta: ctrl.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Spec: fluxsourcev1.HelmRepositorySpec{ | ||
URL: url, | ||
Type: "oci", | ||
}, | ||
} | ||
|
||
err := r.Create(ctx, helmRepo) | ||
return err | ||
} | ||
|
||
func (r *UffizziClusterEtcdReconciler) createBitnamiHelmRepo(ctx context.Context, req ctrl.Request) error { | ||
return r.createHelmRepo(ctx, constants.BITNAMI_HELM_REPO, req.Namespace, constants.BITNAMI_CHART_REPO_URL) | ||
} |
2 changes: 1 addition & 1 deletion
2
controllers/conditions.go → controllers/uffizzicluster/conditions.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
controllers/conditions_test.go → ...rollers/uffizzicluster/conditions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package controllers | ||
package uffizzicluster | ||
|
||
import ( | ||
"testing" | ||
|
Oops, something went wrong.