Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: eks distro support #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 18 additions & 68 deletions src/controllers/uffizzicluster/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
fluxsourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/pkg/errors"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -26,43 +27,17 @@ func (r *UffizziClusterReconciler) deleteLoftHelmRepo(ctx context.Context, req c
return r.deleteHelmRepo(ctx, constants.LOFT_HELM_REPO, req.Namespace)
}

func (r *UffizziClusterReconciler) upsertVClusterK3SHelmRelease(update bool, ctx context.Context, uCluster *uclusteruffizzicomv1alpha1.UffizziCluster) (*fluxhelmv2beta1.HelmRelease, error) {
func (r *UffizziClusterReconciler) upsertVClusterHelmRelease(update bool, ctx context.Context, uCluster *uclusteruffizzicomv1alpha1.UffizziCluster) (*fluxhelmv2beta1.HelmRelease, error) {
patch := client.MergeFrom(uCluster.DeepCopy())

vclusterK3sHelmValues, helmReleaseName := vcluster.BuildK3SHelmValues(uCluster)
helmValuesJSONObj, err := build.HelmValuesToJSON(vclusterK3sHelmValues)
vclusterHelmValues, helmReleaseName := vcluster.BuildK3SHelmValues(uCluster)
helmValuesJSONObj, err := build.HelmValuesToJSON(vclusterHelmValues)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal helm values")
}

// Create a new HelmRelease
newHelmRelease := &fluxhelmv2beta1.HelmRelease{
ObjectMeta: ctrl.ObjectMeta{
Name: helmReleaseName,
Namespace: uCluster.Namespace,
Labels: map[string]string{
constants.UFFIZZI_APP_COMPONENT_LABEL: constants.VCLUSTER,
},
},
Spec: fluxhelmv2beta1.HelmReleaseSpec{
Upgrade: &fluxhelmv2beta1.Upgrade{
Force: false,
},
Chart: fluxhelmv2beta1.HelmChartTemplate{
Spec: fluxhelmv2beta1.HelmChartTemplateSpec{
Chart: constants.VCLUSTER_CHART_K3S,
Version: constants.VCLUSTER_CHART_K3S_VERSION,
SourceRef: fluxhelmv2beta1.CrossNamespaceObjectReference{
Kind: "HelmRepository",
Name: constants.LOFT_HELM_REPO,
Namespace: uCluster.Namespace,
},
},
},
ReleaseName: helmReleaseName,
Values: &helmValuesJSONObj,
},
}
newHelmRelease := r.newHelmRelease(uCluster, helmValuesJSONObj, helmReleaseName, uCluster.Spec.Distro)

if err := controllerutil.SetControllerReference(uCluster, newHelmRelease, r.Scheme); err != nil {
return nil, errors.Wrap(err, "failed to set controller reference")
Expand Down Expand Up @@ -95,11 +70,16 @@ func (r *UffizziClusterReconciler) upsertVClusterK3SHelmRelease(update bool, ctx
return newHelmRelease, nil
}

func (r *UffizziClusterReconciler) upsertVClusterK8sHelmRelease(update bool, ctx context.Context, uCluster *uclusteruffizzicomv1alpha1.UffizziCluster) (*fluxhelmv2beta1.HelmRelease, error) {
vclusterK8sHelmValues, helmReleaseName := vcluster.BuildK8SHelmValues(uCluster)
helmValuesJSONObj, err := build.HelmValuesToJSON(vclusterK8sHelmValues)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal helm values")
func (r *UffizziClusterReconciler) newHelmRelease(uCluster *uclusteruffizzicomv1alpha1.UffizziCluster, helmValuesJSONObj v1.JSON, helmReleaseName string, vclusterDistro string) *fluxhelmv2beta1.HelmRelease {
chartName := constants.VCLUSTER_CHART_K3S
chartVersion := constants.VCLUSTER_CHART_K3S_VERSION

if vclusterDistro == constants.VCLUSTER_K8S_DISTRO {
chartName = constants.VCLUSTER_CHART_K8S
chartVersion = constants.VCLUSTER_CHART_K8S_VERSION
} else if vclusterDistro == constants.VCLUSTER_EKS_DISTRO {
chartName = constants.VCLUSTER_CHART_EKS
chartVersion = constants.VCLUSTER_CHART_EKS_VERSION
}

// Create a new HelmRelease
Expand All @@ -117,8 +97,8 @@ func (r *UffizziClusterReconciler) upsertVClusterK8sHelmRelease(update bool, ctx
},
Chart: fluxhelmv2beta1.HelmChartTemplate{
Spec: fluxhelmv2beta1.HelmChartTemplateSpec{
Chart: constants.VCLUSTER_CHART_K8S,
Version: constants.VCLUSTER_CHART_K8S_VERSION,
Chart: chartName,
Version: chartVersion,
SourceRef: fluxhelmv2beta1.CrossNamespaceObjectReference{
Kind: "HelmRepository",
Name: constants.LOFT_HELM_REPO,
Expand All @@ -131,37 +111,7 @@ func (r *UffizziClusterReconciler) upsertVClusterK8sHelmRelease(update bool, ctx
},
}

if err := controllerutil.SetControllerReference(uCluster, newHelmRelease, r.Scheme); err != nil {
return nil, errors.Wrap(err, "failed to set controller reference")
}
// get the helm release spec in string
newHelmReleaseSpecBytes, err := json.Marshal(newHelmRelease.Spec)
if err != nil {
return nil, errors.Wrap(err, "Failed to marshal current spec")
}
newHelmReleaseSpec := string(newHelmReleaseSpecBytes)
// upsert
if !update && uCluster.Status.LastAppliedHelmReleaseSpec == nil {
if err := r.Create(ctx, newHelmRelease); err != nil {
return nil, errors.Wrap(err, "failed to create HelmRelease")
}
patch := client.MergeFrom(uCluster.DeepCopy())
uCluster.Status.LastAppliedHelmReleaseSpec = &newHelmReleaseSpec
if err := r.Status().Patch(ctx, uCluster, patch); err != nil {
return nil, errors.Wrap(err, "Failed to update the default UffizziCluster lastAppliedHelmReleaseSpec")
}

} else if uCluster.Status.LastAppliedHelmReleaseSpec != nil {
// create helm release if there is no existing helm release to update
if update && *uCluster.Status.LastAppliedHelmReleaseSpec != newHelmReleaseSpec {
if err := r.updateHelmRelease(newHelmRelease, uCluster, ctx); err != nil {
return nil, errors.Wrap(err, "failed to update HelmRelease")
}
return nil, errors.Wrap(err, "couldn't update HelmRelease as LastAppliedHelmReleaseSpec does not exist on resource")
}
}

return newHelmRelease, nil
return newHelmRelease
}

func (r *UffizziClusterReconciler) updateHelmRelease(newHelmRelease *fluxhelmv2beta1.HelmRelease, uCluster *uclusteruffizzicomv1alpha1.UffizziCluster, ctx context.Context) error {
Expand Down
30 changes: 7 additions & 23 deletions src/controllers/uffizzicluster/uffizzicluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,9 @@ func (r *UffizziClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque
var updatedHelmRelease *fluxhelmv2beta1.HelmRelease
if lifecycleOpType == constants.LIFECYCLE_OP_TYPE_UPDATE {
if currentSpec != lastAppliedSpec {
if uCluster.Spec.Distro == constants.VCLUSTER_K8S_DISTRO {
if updatedHelmRelease, err = r.upsertVClusterK8sHelmRelease(true, ctx, uCluster); err != nil {
logger.Error(err, "Failed to update HelmRelease")
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
}
} else {
if updatedHelmRelease, err = r.upsertVClusterK3SHelmRelease(true, ctx, uCluster); err != nil {
logger.Info("Failed to update HelmRelease with error, reconciling", "Error", err.Error())
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
}
if updatedHelmRelease, err = r.upsertVClusterHelmRelease(true, ctx, uCluster); err != nil {
logger.Error(err, "Failed to update HelmRelease")
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
}
}
}
Expand All @@ -266,19 +259,10 @@ func (r *UffizziClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque
func (r *UffizziClusterReconciler) createVClusterHelmChart(ctx context.Context, uCluster *v1alpha1.UffizziCluster, newHelmRelease *fluxhelmv2beta1.HelmRelease) (*fluxhelmv2beta1.HelmRelease, ctrl.Result, error) {
logger := log.FromContext(ctx)
var err error = nil
if uCluster.Spec.Distro == constants.VCLUSTER_K8S_DISTRO {
newHelmRelease, err = r.upsertVClusterK8sHelmRelease(false, ctx, uCluster)
if err != nil {
logger.Error(err, "Failed to create HelmRelease")
return nil, ctrl.Result{Requeue: true}, err
}
} else {
// default to k3s
newHelmRelease, err = r.upsertVClusterK3SHelmRelease(false, ctx, uCluster)
if err != nil {
logger.Error(err, "Failed to create HelmRelease")
return nil, ctrl.Result{Requeue: true}, err
}
newHelmRelease, err = r.upsertVClusterHelmRelease(false, ctx, uCluster)
if err != nil {
logger.Error(err, "Failed to create HelmRelease")
return nil, ctrl.Result{Requeue: true}, err
}
return newHelmRelease, ctrl.Result{}, nil
}
Expand Down
12 changes: 5 additions & 7 deletions src/pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ const (
BITNAMI_HELM_REPO = "bitnami"
VCLUSTER_CHART_K3S = "vcluster"
VCLUSTER_CHART_K8S = "vcluster-k8s"
VCLUSTER_CHART_K3S_VERSION = "0.19.4"
VCLUSTER_CHART_K8S_VERSION = "0.19.4"
VCLUSTER_CHART_EKS = "vcluster-eks"
VCLUSTER_CHART_K3S_VERSION = "0.19.5"
VCLUSTER_CHART_K8S_VERSION = "0.19.5"
VCLUSTER_CHART_EKS_VERSION = "0.19.5"
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"
NODESELECTOR_GKE = "gvisor"
VCLUSTER_EKS_DISTRO = "eks"
K3S_DATASTORE_ENDPOINT = "K3S_DATASTORE_ENDPOINT"
VCLUSTER_INGRESS_HOSTNAME = "VCLUSTER_INGRESS_HOST"
DEFAULT_K3S_VERSION = "rancher/k3s:v1.27.3-k3s1"
UCLUSTER_SYNC_PLUGIN_TAG = "uffizzi/ucluster-sync-plugin:v0.2.4"
OCI_TYPE = "oci"
PREMIUM_RWO_STORAGE_CLASS = "premium-rwo"
STANDARD_STORAGE_CLASS = "standard"
SANDBOX_GKE_IO_RUNTIME = "sandbox.gke.io/runtime"
GVISOR = "gvisor"
VCLUSTER_MANAGED_BY_KEY = "vcluster.loft.sh/managed-by"
WORKLOAD_TYPE_DEPLOYMENT = "deployment"
WORKLOAD_TYPE_STATEFULSET = "statefulset"
)

type LIFECYCLE_OP_TYPE string
Expand Down
Loading