Skip to content

Commit

Permalink
fix: Properly track clusterIds in KluctlDeployment status
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Jul 12, 2023
1 parent a9e87e4 commit 4edb897
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/kluctl_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func (pt *preparedTarget) handleCommandResult(ctx context.Context, cmdErr error,
}

var err error
cmdResult.Command.KluctlDeployment.ClusterId, err = pt.pp.r.getClusterId(ctx)
cmdResult.Command.KluctlDeployment.ClusterId, err = k8s2.GetClusterId(ctx, pt.pp.r.Client)
if err != nil {
return err
}
Expand Down Expand Up @@ -721,7 +721,7 @@ func (pt *preparedTarget) handleValidateResult(ctx context.Context, cmdErr error
}

var err error
validateResult.KluctlDeployment.ClusterId, err = pt.pp.r.getClusterId(ctx)
validateResult.KluctlDeployment.ClusterId, err = k8s2.GetClusterId(ctx, pt.pp.r.Client)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/controllers/kluctldeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ func (r *KluctlDeploymentReconciler) doReconcile(
GitRepoKey: obj.Spec.Source.URL.RepoKey(),
SubDir: path.Clean(obj.Spec.Source.Path),
}
clusterId, err := targetContext.SharedContext.K.GetClusterId()
if err != nil {
return doFailPrepare(err)
}
obj.Status.TargetKey = &result.TargetKey{
TargetName: targetContext.Target.Name,
Discriminator: targetContext.Target.Discriminator,
ClusterId: clusterId,
}

if obj.Status.ProjectKey.SubDir == "." {
Expand Down
11 changes: 0 additions & 11 deletions pkg/controllers/kluctldeployment_controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"context"
kluctlv1 "github.com/kluctl/kluctl/v2/api/v1beta1"
"github.com/kluctl/kluctl/v2/pkg/utils/flux_utils/meta"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/reference"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (r *KluctlDeploymentReconciler) event(ctx context.Context, obj *kluctlv1.KluctlDeployment, warning bool, msg string, metadata map[string]string) {
Expand Down Expand Up @@ -72,12 +70,3 @@ func (r *KluctlDeploymentReconciler) recordSuspension(ctx context.Context, obj *
r.MetricsRecorder.RecordSuspend(*objRef, obj.Spec.Suspend)
}
}

func (r *KluctlDeploymentReconciler) getClusterId(ctx context.Context) (string, error) {
var kubeSystemNs corev1.Namespace
err := r.Get(ctx, client.ObjectKey{Name: "kube-system"}, &kubeSystemNs)
if err != nil {
return "", err
}
return string(kubeSystemNs.UID), nil
}
13 changes: 6 additions & 7 deletions pkg/k8s/k8s_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ func (k *K8sCluster) ReadWrite() *K8sCluster {
}

func (k *K8sCluster) GetClusterId() (string, error) {
kubeSystemNs, _, err := k.GetSingleObject(
k8s.NewObjectRef("", "v1", "Namespace", "kube-system", ""))
var clusterId string
_, err := k.clients.withCClientFromPool(true, func(c client.Client) error {
var err error
clusterId, err = GetClusterId(k.ctx, c)
return err
})
if err != nil {
return "", err
}
// we reuse the kube-system namespace uid as global cluster id
clusterId := kubeSystemNs.GetK8sUid()
if clusterId == "" {
return "", fmt.Errorf("kube-system namespace has no uid")
}
return clusterId, nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/k8s/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package k8s

import (
"context"
"fmt"
"github.com/kluctl/kluctl/v2/pkg/types/k8s"
"github.com/kluctl/kluctl/v2/pkg/utils/uo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func UnwrapListItems(o *uo.UnstructuredObject, withListCallback bool, cb func(o *uo.UnstructuredObject) error) error {
Expand Down Expand Up @@ -48,3 +52,17 @@ func FixNamespaceInRef(ref k8s.ObjectRef, namespaced bool, def string) k8s.Objec
}
return ref
}

func GetClusterId(ctx context.Context, c client.Client) (string, error) {
// we reuse the kube-system namespace uid as global cluster id
var ns corev1.Namespace
err := c.Get(ctx, client.ObjectKey{Name: "kube-system"}, &ns)
if err != nil {
return "", err
}
clusterId := ns.UID
if clusterId == "" {
return "", fmt.Errorf("kube-system namespace has no uid")
}
return string(clusterId), nil
}

0 comments on commit 4edb897

Please sign in to comment.