Skip to content

Commit

Permalink
Work In Progress adding Sveltos
Browse files Browse the repository at this point in the history
  • Loading branch information
wahabmk committed Sep 10, 2024
1 parent 0314342 commit 7bcdf94
Show file tree
Hide file tree
Showing 87 changed files with 18,299 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ dev-push: docker-build helm-push

.PHONY: dev-templates
dev-templates: templates-generate
$(KUBECTL) -n $(NAMESPACE) apply -f templates/hmc-templates/files/templates
KUBECTL=$(KUBECTL) NAMESPACE=$(NAMESPACE) hack/run_templates.sh

.PHONY: dev-aws-creds
dev-aws-creds: yq
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,13 @@ spec:
3. Remove the `hmc-system` namespace:

`kubectl delete ns hmc-system`





.PHONY: dev-templates-test
dev-templates-test: templates-generate
@for f in $(shell ls templates/hmc-templates/files/templates/); \
do echo $${f}; \
done
3 changes: 3 additions & 0 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (m *ManagementSpec) SetProvidersDefaults() {
{
Template: "cluster-api-provider-azure",
},
{
Template: "projectsveltos",
},
}
}

Expand Down
47 changes: 47 additions & 0 deletions hack/run_templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
# Copyright 2024
#
# 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.

set -eu

echo_cmd() {
echo "$@"
"$@"
}

KUBECTL=${KUBECTL:-kubectl}
NAMESPACE=${NAMESPACE:-hmc-system}

# Output directory for the generated Template manifests
TEMPLATES_OUTPUT_DIR=${TEMPLATES_OUTPUT_DIR:-templates/hmc-templates/files/templates}

for templ in $TEMPLATES_OUTPUT_DIR/*; do
ns=$(cat $templ | grep '^ namespace:' | awk '{print $2}')
if [[ -z $ns ]]; then
echo_cmd $KUBECTL -n $NAMESPACE apply -f $templ
else
if out=$(KUBECTL create ns $ns 2>&1 > /dev/null); then
echo "successfully created namespace '$ns' within template '$(basename $templ)'"
echo_cmd $KUBECTL apply -f $templ
else
if [[ $out == *'(AlreadyExists)'* ]]; then
echo "The namespace '$ns' within template '$(basename $templ)' already exists so applying..."
echo_cmd $KUBECTL apply -f $templ
else
echo $out
exit 1
fi
fi
fi
done
19 changes: 19 additions & 0 deletions hack/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import yaml
import subprocess

kubectl = os.environ['KUBECTL'] if os.environ['KUBECTL'] != "" else "kubectl"
namespace = os.environ['NAMESPACE'] if os.environ['NAMESPACE'] != "" else "hmc-system"
template_dir = "templates/hmc-templates/files/templates"

def get_namespace(templ) -> str:
x = yaml.safe_load(templ)
print(x)
print("--------------")

for x in os.listdir(template_dir):
filepath = os.path.join(template_dir, x)
with open(filepath, "r") as f:
get_namespace(f.read())
args = "{kubectl} -n {namespace} apply -f {filepath}".format(kubectl=kubectl, namespace=namespace, filepath=filepath).split()
# print(args)
3 changes: 2 additions & 1 deletion hack/templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ for chart in $TEMPLATES_DIR/*; do
name=$(grep '^name:' $chart/Chart.yaml | awk '{print $2}')
if [ "$name" = "$HMC_TEMPLATES_CHART_NAME" ]; then continue; fi
version=$(grep '^version:' $chart/Chart.yaml | awk '{print $2}')

namespace=$(grep '^ namespace:' $chart/Chart.yaml | awk '{print $2}')
cat <<EOF > $TEMPLATES_OUTPUT_DIR/$name.yaml
apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: $name
${namespace:+namespace: $namespace}
spec:
helm:
chartName: $name
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, l logr.Logger, ma
}

hr, _, err := helm.ReconcileHelmRelease(ctx, r.Client, managedCluster.Name, managedCluster.Namespace, managedCluster.Spec.Config,
ownerRef, template.Status.ChartRef, defaultReconcileInterval, nil)
ownerRef, template.Status.ChartRef, defaultReconcileInterval, nil, "", false)
if err != nil {
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.HelmReleaseReadyCondition,
Expand Down
10 changes: 9 additions & 1 deletion internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag

components := wrappedComponents(management)
for _, component := range components {
fmt.Printf("\n>>>>>>>>>>>>>>>>>>>>>>>>> component.Template = %s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", component.Template)
template := &hmc.Template{}
err := r.Get(ctx, types.NamespacedName{
// WAHAB: 3
Namespace: r.SystemNamespace,
Name: component.Template,
}, template)
Expand All @@ -118,8 +120,14 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
continue
}

targetNamespace := ""
createNamespace := false
if component.Template == "projectsveltos" {
targetNamespace = "projectsveltos"
createNamespace = true
}
_, _, err = helm.ReconcileHelmRelease(ctx, r.Client, component.HelmReleaseName(), r.SystemNamespace, component.Config,
nil, template.Status.ChartRef, defaultReconcileInterval, component.dependsOn)
nil, template.Status.ChartRef, defaultReconcileInterval, component.dependsOn, targetNamespace, createNamespace)
if err != nil {
errMsg := fmt.Sprintf("error reconciling HelmRelease %s/%s: %s", r.SystemNamespace, component.Template, err)
updateComponentsStatus(detectedComponents, &detectedProviders, component.Template, template.Status, errMsg)
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/release_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (p *Poller) ensureManagement(ctx context.Context) error {
Raw: rawConfig,
}

// WAHAB: 2
err = p.Create(ctx, mgmtObj)
if err != nil {
return fmt.Errorf("failed to create %s Management object: %s", hmc.ManagementName, err)
Expand Down Expand Up @@ -255,7 +256,7 @@ func (p *Poller) reconcileHMCTemplates(ctx context.Context) error {
Name: helmChart.Name,
Namespace: helmChart.Namespace,
}
_, operation, err = helm.ReconcileHelmRelease(ctx, p.Client, hmcTemplatesReleaseName, p.SystemNamespace, nil, nil, chartRef, defaultReconcileInterval, nil)
_, operation, err = helm.ReconcileHelmRelease(ctx, p.Client, hmcTemplatesReleaseName, p.SystemNamespace, nil, nil, chartRef, defaultReconcileInterval, nil, "", false)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ func (r *TemplateReconciler) reconcileHelmChart(ctx context.Context, template *h
helmChart.Spec = sourcev1.HelmChartSpec{
Chart: template.Spec.Helm.ChartName,
Version: template.Spec.Helm.ChartVersion,
// WAHAB 4: Due to this, the Template object for projectsveltos will
// have to be within the hmc-system namespace. Because the helm-templates Flux source
// is within the hmc-system namespace.
SourceRef: sourcev1.LocalHelmChartSourceReference{
Kind: sourcev1.HelmRepositoryKind,
Name: defaultRepoName,
Expand Down
22 changes: 17 additions & 5 deletions internal/helm/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func ReconcileHelmRelease(
chartRef *hcv2.CrossNamespaceSourceReference,
reconcileInterval time.Duration,
dependsOn []meta.NamespacedObjectReference,
targetNamespace string,
createNamespace bool,
) (*hcv2.HelmRelease, controllerutil.OperationResult, error) {
helmRelease := &hcv2.HelmRelease{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -56,12 +58,22 @@ func ReconcileHelmRelease(
helmRelease.OwnerReferences = []metav1.OwnerReference{*ownerReference}
}
helmRelease.Spec = hcv2.HelmReleaseSpec{
ChartRef: chartRef,
Interval: metav1.Duration{Duration: reconcileInterval},
ReleaseName: name,
Values: values,
DependsOn: dependsOn,
ChartRef: chartRef,
Interval: metav1.Duration{Duration: reconcileInterval},
ReleaseName: name,
Values: values,
DependsOn: dependsOn,
TargetNamespace: targetNamespace,
Install: &hcv2.Install{
CreateNamespace: createNamespace,
},
}

// if createNamespace {
// helmRelease.Spec.Install = &hcv2.Install{
// CreateNamespace: true,
// }
// }
return nil
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions templates/hmc-templates/files/templates/aws-hosted-cp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: aws-hosted-cp

spec:
helm:
chartName: aws-hosted-cp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: aws-standalone-cp

spec:
helm:
chartName: aws-standalone-cp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: azure-hosted-cp

spec:
helm:
chartName: azure-hosted-cp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: azure-standalone-cp

spec:
helm:
chartName: azure-standalone-cp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: cluster-api-provider-aws

spec:
helm:
chartName: cluster-api-provider-aws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: cluster-api-provider-azure

spec:
helm:
chartName: cluster-api-provider-azure
Expand Down
1 change: 1 addition & 0 deletions templates/hmc-templates/files/templates/cluster-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: cluster-api

spec:
helm:
chartName: cluster-api
Expand Down
1 change: 1 addition & 0 deletions templates/hmc-templates/files/templates/hmc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: hmc

spec:
helm:
chartName: hmc
Expand Down
1 change: 1 addition & 0 deletions templates/hmc-templates/files/templates/k0smotron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: k0smotron

spec:
helm:
chartName: k0smotron
Expand Down
9 changes: 9 additions & 0 deletions templates/hmc-templates/files/templates/projectsveltos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: Template
metadata:
name: projectsveltos

spec:
helm:
chartName: projectsveltos
chartVersion: 0.37.1
23 changes: 23 additions & 0 deletions templates/projectsveltos/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
25 changes: 25 additions & 0 deletions templates/projectsveltos/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v2
name: projectsveltos
description: Projectsveltos helm chart for Kubernetes
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.37.1
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.37.0"
icon: https://github.com/projectsveltos/sveltos/raw/main/docs/assets/logo.png
annotations:
hmc.mirantis.com/type: provider
# namespace: projectsveltos
Loading

0 comments on commit 7bcdf94

Please sign in to comment.