Skip to content

Commit

Permalink
Merge pull request Mirantis#409 from zerospiel/human_linter
Browse files Browse the repository at this point in the history
Chores with human linter
  • Loading branch information
Kshatrix authored Sep 28, 2024
2 parents 037a0e2 + 011ff4d commit 973489e
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 125 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/credential_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ const (

// CredentialSpec defines the desired state of Credential
type CredentialSpec struct {
// Reference to the Credential Identity
IdentityRef *corev1.ObjectReference `json:"identityRef"`
// +optional
Description string `json:"description,omitempty"`
// Description of the Credential object
Description string `json:"description,omitempty"` // WARN: noop
}

// CredentialStatus defines the observed state of Credential
Expand Down
3 changes: 0 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func main() {
}
if err = (&controller.ManagedClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
DynamicClient: dc,
}).SetupWithManager(mgr); err != nil {
Expand All @@ -227,7 +226,6 @@ func main() {
}
if err = (&controller.TemplateManagementReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
SystemNamespace: currentNamespace,
}).SetupWithManager(mgr); err != nil {
Expand Down Expand Up @@ -259,7 +257,6 @@ func main() {

if err = (&controller.CredentialReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Credential")
os.Exit(1)
Expand Down
52 changes: 24 additions & 28 deletions internal/controller/credential_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,25 @@ import (

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

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

// CredentialReconciler reconciles a Credential object
type CredentialReconciler struct {
client.Client
Scheme *runtime.Scheme
}

func (r *CredentialReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx).WithValues("CredentialController", req.NamespacedName)
l := ctrl.LoggerFrom(ctx)
l.Info("Credential reconcile start")

cred := &hmc.Credential{}
err := r.Client.Get(ctx, req.NamespacedName, cred)
if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{},
fmt.Errorf("error getting Credential object: %s", err)
if err := r.Client.Get(ctx, req.NamespacedName, cred); err != nil {
l.Error(err, "unable to fetch Credential")
return ctrl.Result{}, client.IgnoreNotFound(err)
}

clIdty := &unstructured.Unstructured{}
Expand All @@ -54,40 +48,42 @@ func (r *CredentialReconciler) Reconcile(ctx context.Context, req ctrl.Request)
clIdty.SetName(cred.Spec.IdentityRef.Name)
clIdty.SetNamespace(cred.Spec.IdentityRef.Namespace)

err = r.Client.Get(ctx, client.ObjectKey{
if err := r.Client.Get(ctx, client.ObjectKey{
Name: cred.Spec.IdentityRef.Name,
Namespace: cred.Spec.IdentityRef.Namespace,
}, clIdty)
if err != nil {
}, clIdty); err != nil {
if apierrors.IsNotFound(err) {
stateErr := r.setState(ctx, cred, hmc.CredentialNotFound)
if stateErr != nil {
err = errors.Join(err, stateErr)
}
return ctrl.Result{},
fmt.Errorf("cluster identity not found: %s", err)

l.Error(err, "ClusterIdentity not found")

return ctrl.Result{}, err
}
return ctrl.Result{},
fmt.Errorf("failed to get ClusterIdentity object: %s", err)

l.Error(err, "failed to get ClusterIdentity")

return ctrl.Result{}, err
}

err = r.setState(ctx, cred, hmc.CredentialReady)
if err != nil {
return ctrl.Result{},
fmt.Errorf("failed to set Credential state: %s", err)
if err := r.setState(ctx, cred, hmc.CredentialReady); err != nil {
l.Error(err, "failed to set Credential state")

return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

func (r *CredentialReconciler) setState(ctx context.Context, cred *hmc.Credential,
state hmc.CredentialState,
) error {
func (r *CredentialReconciler) setState(ctx context.Context, cred *hmc.Credential, state hmc.CredentialState) error {
cred.Status.State = state
err := r.Client.Status().Update(ctx, cred)
if err != nil {
return err

if err := r.Client.Status().Update(ctx, cred); err != nil {
return fmt.Errorf("failed to update Credential %s/%s status: %w", cred.Namespace, cred.Name, err)
}

return nil
}

Expand Down
23 changes: 11 additions & 12 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
Expand All @@ -55,7 +53,6 @@ import (
// ManagedClusterReconciler reconciles a ManagedCluster object
type ManagedClusterReconciler struct {
client.Client
Scheme *runtime.Scheme
Config *rest.Config
DynamicClient *dynamic.DynamicClient
}
Expand Down Expand Up @@ -87,14 +84,16 @@ var (
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx).WithValues("ManagedClusterController", req.NamespacedName)
l := ctrl.LoggerFrom(ctx)
l.Info("Reconciling ManagedCluster")

managedCluster := &hmc.ManagedCluster{}
if err := r.Get(ctx, req.NamespacedName, managedCluster); err != nil {
if apierrors.IsNotFound(err) {
l.Info("ManagedCluster not found, ignoring since object must be deleted")
return ctrl.Result{}, nil
}

l.Error(err, "Failed to get ManagedCluster")
return ctrl.Result{}, err
}
Expand All @@ -106,7 +105,7 @@ func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque

if managedCluster.Status.ObservedGeneration == 0 {
mgmt := &hmc.Management{}
mgmtRef := types.NamespacedName{Name: hmc.ManagementName}
mgmtRef := client.ObjectKey{Name: hmc.ManagementName}
if err := r.Get(ctx, mgmtRef, mgmt); err != nil {
l.Error(err, "Failed to get Management object")
return ctrl.Result{}, err
Expand Down Expand Up @@ -193,7 +192,7 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, l logr.Logger, ma
}()

template := &hmc.ClusterTemplate{}
templateRef := types.NamespacedName{Name: managedCluster.Spec.Template, Namespace: managedCluster.Namespace}
templateRef := client.ObjectKey{Name: managedCluster.Spec.Template, Namespace: managedCluster.Namespace}
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 Expand Up @@ -422,7 +421,7 @@ func (r *ManagedClusterReconciler) getSource(ctx context.Context, ref *hcv2.Cros
if ref == nil {
return nil, fmt.Errorf("helm chart source is not provided")
}
chartRef := types.NamespacedName{Namespace: ref.Namespace, Name: ref.Name}
chartRef := client.ObjectKey{Namespace: ref.Namespace, Name: ref.Name}
hc := sourcev1.HelmChart{}
if err := r.Client.Get(ctx, chartRef, &hc); err != nil {
return nil, err
Expand All @@ -432,7 +431,7 @@ func (r *ManagedClusterReconciler) getSource(ctx context.Context, ref *hcv2.Cros

func (r *ManagedClusterReconciler) Delete(ctx context.Context, l logr.Logger, managedCluster *hmc.ManagedCluster) (ctrl.Result, error) {
hr := &hcv2.HelmRelease{}
err := r.Get(ctx, types.NamespacedName{
err := r.Get(ctx, client.ObjectKey{
Name: managedCluster.Name,
Namespace: managedCluster.Namespace,
}, hr)
Expand Down Expand Up @@ -503,9 +502,9 @@ func (r *ManagedClusterReconciler) releaseCluster(ctx context.Context, namespace

func (r *ManagedClusterReconciler) getProviders(ctx context.Context, templateNamespace, templateName string) ([]string, error) {
template := &hmc.ClusterTemplate{}
templateRef := types.NamespacedName{Name: templateName, Namespace: templateNamespace}
templateRef := client.ObjectKey{Name: templateName, Namespace: templateNamespace}
if err := r.Get(ctx, templateRef, template); err != nil {
log.FromContext(ctx).Error(err, "Failed to get ClusterTemplate", "namespace", templateNamespace, "name", templateName)
ctrl.LoggerFrom(ctx).Error(err, "Failed to get ClusterTemplate", "namespace", templateNamespace, "name", templateName)
return nil, err
}
return template.Status.Providers.InfrastructureProviders, nil
Expand All @@ -532,7 +531,7 @@ func (r *ManagedClusterReconciler) removeClusterFinalizer(ctx context.Context, c
originalCluster := *cluster
finalizersUpdated := controllerutil.RemoveFinalizer(cluster, hmc.BlockingFinalizer)
if finalizersUpdated {
log.FromContext(ctx).Info("Allow to stop cluster", "finalizer", hmc.BlockingFinalizer)
ctrl.LoggerFrom(ctx).Info("Allow to stop cluster", "finalizer", hmc.BlockingFinalizer)
if err := r.Client.Patch(ctx, cluster, client.MergeFrom(&originalCluster)); err != nil {
return fmt.Errorf("failed to patch cluster %s/%s: %w", cluster.Namespace, cluster.Name, err)
}
Expand Down Expand Up @@ -580,7 +579,7 @@ func (r *ManagedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Watches(&hcv2.HelmRelease{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []ctrl.Request {
managedCluster := hmc.ManagedCluster{}
managedClusterRef := types.NamespacedName{
managedClusterRef := client.ObjectKey{
Namespace: o.GetNamespace(),
Name: o.GetName(),
}
Expand Down
2 changes: 0 additions & 2 deletions internal/controller/managedcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ var _ = Describe("ManagedCluster Controller", func() {

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

Expect(k8sClient.Delete(ctx, managedCluster)).To(Succeed())
Expand All @@ -148,7 +147,6 @@ var _ = Describe("ManagedCluster Controller", func() {
By("Reconciling the created resource")
controllerReconciler := &ManagedClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Config: &rest.Config{},
}

Expand Down
25 changes: 12 additions & 13 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/certmanager"
Expand All @@ -49,15 +47,16 @@ type ManagementReconciler struct {
}

func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx).WithValues("ManagementController", req.NamespacedName)
log.IntoContext(ctx, l)
l := ctrl.LoggerFrom(ctx)
l.Info("Reconciling Management")

management := &hmc.Management{}
if err := r.Get(ctx, req.NamespacedName, management); err != nil {
if apierrors.IsNotFound(err) {
l.Info("Management not found, ignoring since object must be deleted")
return ctrl.Result{}, nil
}

l.Error(err, "Failed to get Management")
return ctrl.Result{}, err
}
Expand All @@ -71,7 +70,7 @@ func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Management) (ctrl.Result, error) {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)

finalizersUpdated := controllerutil.AddFinalizer(management, hmc.ManagementFinalizer)
if finalizersUpdated {
Expand All @@ -82,7 +81,7 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
}

release := &hmc.Release{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: management.Spec.Release}, release); err != nil {
if err := r.Client.Get(ctx, client.ObjectKey{Name: management.Spec.Release}, release); err != nil {
l.Error(err, "failed to get Release object")
return ctrl.Result{}, err
}
Expand All @@ -100,7 +99,7 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
components := wrappedComponents(management, release)
for _, component := range components {
template := &hmc.ProviderTemplate{}
err := r.Get(ctx, types.NamespacedName{
err := r.Get(ctx, client.ObjectKey{
Name: component.Template,
}, template)
if err != nil {
Expand Down Expand Up @@ -147,7 +146,7 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
}

func (r *ManagementReconciler) Delete(ctx context.Context, management *hmc.Management) (ctrl.Result, error) {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)
listOpts := &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{hmc.HMCManagedLabelKey: hmc.HMCManagedLabelValue}),
}
Expand All @@ -170,10 +169,10 @@ func (r *ManagementReconciler) Delete(ctx context.Context, management *hmc.Manag
}

func (r *ManagementReconciler) removeHelmReleases(ctx context.Context, hmcReleaseName string, opts *client.ListOptions) error {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)
l.Info("Suspending HMC Helm Release reconciles")
hmcRelease := &fluxv2.HelmRelease{}
err := r.Client.Get(ctx, types.NamespacedName{Namespace: r.SystemNamespace, Name: hmcReleaseName}, hmcRelease)
err := r.Client.Get(ctx, client.ObjectKey{Namespace: r.SystemNamespace, Name: hmcReleaseName}, hmcRelease)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
Expand All @@ -193,7 +192,7 @@ func (r *ManagementReconciler) removeHelmReleases(ctx context.Context, hmcReleas
}

func (r *ManagementReconciler) removeHelmCharts(ctx context.Context, opts *client.ListOptions) error {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)
l.Info("Ensuring all HelmCharts owned by HMC are removed")
gvk := sourcev1.GroupVersion.WithKind(sourcev1.HelmChartKind)
if err := utils.EnsureDeleteAllOf(ctx, r.Client, gvk, opts); err != nil {
Expand All @@ -204,7 +203,7 @@ func (r *ManagementReconciler) removeHelmCharts(ctx context.Context, opts *clien
}

func (r *ManagementReconciler) removeHelmRepositories(ctx context.Context, opts *client.ListOptions) error {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)
l.Info("Ensuring all HelmRepositories owned by HMC are removed")
gvk := sourcev1.GroupVersion.WithKind(sourcev1.HelmRepositoryKind)
if err := utils.EnsureDeleteAllOf(ctx, r.Client, gvk, opts); err != nil {
Expand Down Expand Up @@ -266,7 +265,7 @@ func wrappedComponents(mgmt *hmc.Management, release *hmc.Release) []component {
// enableAdditionalComponents enables the admission controller and cluster api operator
// once the cert manager is ready
func (r *ManagementReconciler) enableAdditionalComponents(ctx context.Context, mgmt *hmc.Management) error {
l := log.FromContext(ctx)
l := ctrl.LoggerFrom(ctx)

hmcComponent := &mgmt.Spec.Core.HMC
config := make(map[string]any)
Expand Down
Loading

0 comments on commit 973489e

Please sign in to comment.