Skip to content

Commit

Permalink
Merge pull request K0rdent#284 from Mirantis/ns
Browse files Browse the repository at this point in the history
Remove dependency on hmc-system namespace
  • Loading branch information
Kshatrix authored Sep 9, 2024
2 parents 2196956 + 85ace82 commit 0314342
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 54 deletions.
3 changes: 1 addition & 2 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const (
}
}`

ManagementName = "hmc"
ManagementNamespace = "hmc-system"
ManagementName = "hmc"

ManagementFinalizer = "hmc.mirantis.com/management"
)
Expand Down
3 changes: 0 additions & 3 deletions api/v1alpha1/template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
)

const (
// TemplatesNamespace is the namespace where all Templates are located
TemplatesNamespace = "hmc-system"

// ManagementKind is the string representation of a Management.
ManagementKind = "Management"
// TemplateKind is the string representation of a Template.
Expand Down
26 changes: 17 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func main() {
os.Exit(1)
}

currentNamespace := utils.CurrentNamespace()

if err = (&controller.TemplateReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand All @@ -177,18 +179,20 @@ func main() {
os.Exit(1)
}
if err = (&controller.ManagedClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
DynamicClient: dc,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
DynamicClient: dc,
SystemNamespace: currentNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ManagedCluster")
os.Exit(1)
}
if err = (&controller.ManagementReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
SystemNamespace: currentNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Management")
os.Exit(1)
Expand All @@ -203,14 +207,16 @@ func main() {
RegistryCredentialsSecret: registryCredentialsSecret,
InsecureRegistry: insecureRegistry,
HMCTemplatesChartName: hmcTemplatesChartName,
SystemNamespace: currentNamespace,
}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ReleaseController")
os.Exit(1)
}

if enableTelemetry {
if err = mgr.Add(&telemetry.Tracker{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
SystemNamespace: currentNamespace,
}); err != nil {
setupLog.Error(err, "unable to create telemetry tracker")
os.Exit(1)
Expand All @@ -229,7 +235,9 @@ func main() {
}

if enableWebhook {
if err := (&hmcwebhook.ManagedClusterValidator{}).SetupWebhookWithManager(mgr); err != nil {
if err := (&hmcwebhook.ManagedClusterValidator{
SystemNamespace: currentNamespace,
}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ManagedCluster")
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ import (
// ManagedClusterReconciler reconciles a ManagedCluster object
type ManagedClusterReconciler struct {
client.Client
Scheme *runtime.Scheme
Config *rest.Config
DynamicClient *dynamic.DynamicClient
Scheme *runtime.Scheme
Config *rest.Config
DynamicClient *dynamic.DynamicClient
SystemNamespace string
}

// Reconcile is part of the main kubernetes reconciliation loop which aims to
Expand Down Expand Up @@ -167,7 +168,7 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, l logr.Logger, ma
}()

template := &hmc.Template{}
templateRef := types.NamespacedName{Name: managedCluster.Spec.Template, Namespace: hmc.TemplatesNamespace}
templateRef := types.NamespacedName{Name: managedCluster.Spec.Template, Namespace: r.SystemNamespace}
if err := r.Get(ctx, templateRef, template); err != nil {
l.Error(err, "Failed to get Template")
errMsg := fmt.Sprintf("failed to get provided template: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/utils"
)

var _ = Describe("ManagedCluster Controller", func() {
Expand All @@ -50,11 +51,11 @@ var _ = Describe("ManagedCluster Controller", func() {

BeforeEach(func() {
By("creating hmc-system namespace")
err := k8sClient.Get(ctx, types.NamespacedName{Name: hmc.ManagementNamespace}, namespace)
err := k8sClient.Get(ctx, types.NamespacedName{Name: utils.DefaultSystemNamespace}, namespace)
if err != nil && errors.IsNotFound(err) {
namespace = &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: hmc.ManagementNamespace,
Name: utils.DefaultSystemNamespace,
},
}
Expect(k8sClient.Create(ctx, namespace)).To(Succeed())
Expand All @@ -66,7 +67,7 @@ var _ = Describe("ManagedCluster Controller", func() {
template = &hmc.Template{
ObjectMeta: metav1.ObjectMeta{
Name: templateName,
Namespace: hmc.TemplatesNamespace,
Namespace: utils.DefaultSystemNamespace,
},
Spec: hmc.TemplateSpec{
Helm: hmc.HelmSpec{
Expand Down Expand Up @@ -123,8 +124,9 @@ var _ = Describe("ManagedCluster Controller", func() {
By("Cleanup")

controllerReconciler := &ManagedClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Client: k8sClient,
Scheme: k8sClient.Scheme(),
SystemNamespace: utils.DefaultSystemNamespace,
}

Expect(k8sClient.Delete(ctx, managedCluster)).To(Succeed())
Expand All @@ -141,9 +143,10 @@ var _ = Describe("ManagedCluster Controller", func() {
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &ManagedClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Config: &rest.Config{},
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Config: &rest.Config{},
SystemNamespace: utils.DefaultSystemNamespace,
}

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
Expand Down
19 changes: 10 additions & 9 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import (
// ManagementReconciler reconciles a Management object
type ManagementReconciler struct {
client.Client
Scheme *runtime.Scheme
Config *rest.Config
Scheme *runtime.Scheme
Config *rest.Config
SystemNamespace string
}

func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -101,26 +102,26 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
for _, component := range components {
template := &hmc.Template{}
err := r.Get(ctx, types.NamespacedName{
Namespace: hmc.TemplatesNamespace,
Namespace: r.SystemNamespace,
Name: component.Template,
}, template)
if err != nil {
errMsg := fmt.Sprintf("Failed to get Template %s/%s: %s", hmc.TemplatesNamespace, component.Template, err)
errMsg := fmt.Sprintf("Failed to get Template %s/%s: %s", r.SystemNamespace, component.Template, err)
updateComponentsStatus(detectedComponents, &detectedProviders, component.Template, template.Status, errMsg)
errs = errors.Join(errs, errors.New(errMsg))
continue
}
if !template.Status.Valid {
errMsg := fmt.Sprintf("Template %s/%s is not marked as valid", hmc.TemplatesNamespace, component.Template)
errMsg := fmt.Sprintf("Template %s/%s is not marked as valid", r.SystemNamespace, component.Template)
updateComponentsStatus(detectedComponents, &detectedProviders, component.Template, template.Status, errMsg)
errs = errors.Join(errs, errors.New(errMsg))
continue
}

_, _, err = helm.ReconcileHelmRelease(ctx, r.Client, component.HelmReleaseName(), hmc.ManagementNamespace, component.Config,
_, _, err = helm.ReconcileHelmRelease(ctx, r.Client, component.HelmReleaseName(), r.SystemNamespace, component.Config,
nil, template.Status.ChartRef, defaultReconcileInterval, component.dependsOn)
if err != nil {
errMsg := fmt.Sprintf("error reconciling HelmRelease %s/%s: %s", hmc.ManagementNamespace, component.Template, err)
errMsg := fmt.Sprintf("error reconciling HelmRelease %s/%s: %s", r.SystemNamespace, component.Template, err)
updateComponentsStatus(detectedComponents, &detectedProviders, component.Template, template.Status, errMsg)
errs = errors.Join(errs, errors.New(errMsg))
continue
Expand Down Expand Up @@ -169,7 +170,7 @@ func (r *ManagementReconciler) removeHelmReleases(ctx context.Context, hmcReleas
l := log.FromContext(ctx)
l.Info("Suspending HMC Helm Release reconciles")
hmcRelease := &fluxv2.HelmRelease{}
err := r.Client.Get(ctx, types.NamespacedName{Namespace: hmc.ManagementNamespace, Name: hmcReleaseName}, hmcRelease)
err := r.Client.Get(ctx, types.NamespacedName{Namespace: r.SystemNamespace, Name: hmcReleaseName}, hmcRelease)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
Expand Down Expand Up @@ -251,7 +252,7 @@ func (r *ManagementReconciler) enableAdditionalComponents(ctx context.Context, m
capiOperatorValues = config["cluster-api-operator"].(map[string]interface{})
}

err := certmanager.VerifyAPI(ctx, r.Config, r.Scheme, hmc.ManagementNamespace)
err := certmanager.VerifyAPI(ctx, r.Config, r.Scheme, r.SystemNamespace)
if err != nil {
return fmt.Errorf("failed to check in the cert-manager API is installed: %v", err)
}
Expand Down
21 changes: 11 additions & 10 deletions internal/controller/release_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Poller struct {
RegistryCredentialsSecret string
InsecureRegistry bool
HMCTemplatesChartName string
SystemNamespace string
}

func (p *Poller) Start(ctx context.Context) error {
Expand Down Expand Up @@ -125,13 +126,13 @@ func (p *Poller) ensureManagement(ctx context.Context) error {
}, mgmtObj)
if err != nil {
if !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get %s/%s Management object", hmc.ManagementNamespace, hmc.ManagementName)
return fmt.Errorf("failed to get %s Management object", hmc.ManagementName)
}
mgmtObj.Spec.SetProvidersDefaults()

getter := helm.NewMemoryRESTClientGetter(p.Config, p.RESTMapper())
actionConfig := new(action.Configuration)
err = actionConfig.Init(getter, hmc.TemplatesNamespace, "secret", l.Info)
err = actionConfig.Init(getter, p.SystemNamespace, "secret", l.Info)
if err != nil {
return err
}
Expand Down Expand Up @@ -165,7 +166,7 @@ func (p *Poller) ensureManagement(ctx context.Context) error {

err = p.Create(ctx, mgmtObj)
if err != nil {
return fmt.Errorf("failed to create %s/%s Management object: %s", hmc.ManagementNamespace, hmc.ManagementName, err)
return fmt.Errorf("failed to create %s Management object: %s", hmc.ManagementName, err)
}
l.Info("Successfully created Management object with default configuration")
}
Expand All @@ -177,7 +178,7 @@ func (p *Poller) reconcileDefaultHelmRepo(ctx context.Context) error {
helmRepo := &sourcev1.HelmRepository{
ObjectMeta: metav1.ObjectMeta{
Name: defaultRepoName,
Namespace: hmc.TemplatesNamespace,
Namespace: p.SystemNamespace,
},
}
operation, err := ctrl.CreateOrUpdate(ctx, p.Client, helmRepo, func() error {
Expand All @@ -203,7 +204,7 @@ func (p *Poller) reconcileDefaultHelmRepo(ctx context.Context) error {
return err
}
if operation == controllerutil.OperationResultCreated || operation == controllerutil.OperationResultUpdated {
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmRepository", operation, hmc.TemplatesNamespace, defaultRepoName))
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmRepository", operation, p.SystemNamespace, defaultRepoName))
}
return nil
}
Expand All @@ -217,7 +218,7 @@ func (p *Poller) reconcileHMCTemplates(ctx context.Context) error {
helmChart := &sourcev1.HelmChart{
ObjectMeta: metav1.ObjectMeta{
Name: p.HMCTemplatesChartName,
Namespace: hmc.TemplatesNamespace,
Namespace: p.SystemNamespace,
},
}

Expand All @@ -241,25 +242,25 @@ func (p *Poller) reconcileHMCTemplates(ctx context.Context) error {
return err
}
if operation == controllerutil.OperationResultCreated || operation == controllerutil.OperationResultUpdated {
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmChart", operation, hmc.TemplatesNamespace, p.HMCTemplatesChartName))
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmChart", operation, p.SystemNamespace, p.HMCTemplatesChartName))
}

err, _ = helm.ArtifactReady(helmChart)
if err != nil {
return fmt.Errorf("HelmChart %s/%s Artifact is not ready: %w", hmc.TemplatesNamespace, p.HMCTemplatesChartName, err)
return fmt.Errorf("HelmChart %s/%s Artifact is not ready: %w", p.SystemNamespace, p.HMCTemplatesChartName, err)
}

chartRef := &hcv2.CrossNamespaceSourceReference{
Kind: helmChart.Kind,
Name: helmChart.Name,
Namespace: helmChart.Namespace,
}
_, operation, err = helm.ReconcileHelmRelease(ctx, p.Client, hmcTemplatesReleaseName, hmc.TemplatesNamespace, nil, nil, chartRef, defaultReconcileInterval, nil)
_, operation, err = helm.ReconcileHelmRelease(ctx, p.Client, hmcTemplatesReleaseName, p.SystemNamespace, nil, nil, chartRef, defaultReconcileInterval, nil)
if err != nil {
return err
}
if operation == controllerutil.OperationResultCreated || operation == controllerutil.OperationResultUpdated {
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmRelease", operation, hmc.TemplatesNamespace, hmcTemplatesReleaseName))
l.Info(fmt.Sprintf("Successfully %s %s/%s HelmRelease", operation, p.SystemNamespace, hmcTemplatesReleaseName))
}
return nil
}
5 changes: 4 additions & 1 deletion internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

hmcmirantiscomv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/utils"
hmcwebhook "github.com/Mirantis/hmc/internal/webhook"
//+kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -130,7 +131,9 @@ var _ = BeforeSuite(func() {
})
Expect(err).NotTo(HaveOccurred())

err = (&hmcwebhook.ManagedClusterValidator{}).SetupWebhookWithManager(mgr)
err = (&hmcwebhook.ManagedClusterValidator{
SystemNamespace: utils.DefaultSystemNamespace,
}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&hmcwebhook.ManagementValidator{}).SetupWebhookWithManager(mgr)
Expand Down
4 changes: 3 additions & 1 deletion internal/telemetry/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (

type Tracker struct {
crclient.Client

SystemNamespace string
}

const interval = 10 * time.Minute
Expand Down Expand Up @@ -69,7 +71,7 @@ func (t *Tracker) trackManagedClusterHeartbeat(ctx context.Context) error {

templates := make(map[string]v1alpha1.Template)
templatesList := &v1alpha1.TemplateList{}
err = t.List(ctx, templatesList, crclient.InNamespace(v1alpha1.TemplatesNamespace))
err = t.List(ctx, templatesList, crclient.InNamespace(t.SystemNamespace))
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions internal/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ import (
"context"
"errors"
"fmt"
"os"

apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
DefaultSystemNamespace = "hmc-system"
)

func EnsureDeleteAllOf(ctx context.Context, cl client.Client, gvk schema.GroupVersionKind, opts *client.ListOptions) error {
itemsList := &v1.PartialObjectMetadataList{}
itemsList.SetGroupVersionKind(gvk)
Expand All @@ -43,3 +48,15 @@ func EnsureDeleteAllOf(ctx context.Context, cl client.Client, gvk schema.GroupVe
}
return errs
}

func CurrentNamespace() string {
ns, found := os.LookupEnv("POD_NAMESPACE")
if found {
return ns
}
nsb, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err == nil && len(nsb) > 0 {
return string(nsb)
}
return DefaultSystemNamespace
}
Loading

0 comments on commit 0314342

Please sign in to comment.