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

Add force and reset flags to flux reconcile hr #4437

Merged
merged 5 commits into from Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/flux/create_helmrelease.go
Expand Up @@ -24,11 +24,12 @@ import (
"strings"
"time"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/transform"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"

"github.com/spf13/cobra"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -38,7 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var createHelmReleaseCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/create_kustomization.go
Expand Up @@ -29,7 +29,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/apis/meta"

Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/delete_helmrelease.go
Expand Up @@ -19,7 +19,7 @@ package main
import (
"github.com/spf13/cobra"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var deleteHelmReleaseCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/events.go
Expand Up @@ -39,7 +39,7 @@ import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
autov1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/events_test.go
Expand Up @@ -78,7 +78,7 @@ spec:
timeout: 1m0s
url: ssh://git@github.com/example/repo
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
name: podinfo
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/export_helmrelease.go
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var exportHelmReleaseCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/get_all.go
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/spf13/cobra"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
notificationv1 "github.com/fluxcd/notification-controller/api/v1"
notificationv1b3 "github.com/fluxcd/notification-controller/api/v1beta3"
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/get_helmrelease.go
Expand Up @@ -25,7 +25,7 @@ import (
"golang.org/x/text/language"
"k8s.io/apimachinery/pkg/runtime"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var getHelmReleaseCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/helmrelease.go
Expand Up @@ -19,7 +19,7 @@ package main
import (
"sigs.k8s.io/controller-runtime/pkg/client"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

// helmv2.HelmRelease
Expand Down
27 changes: 20 additions & 7 deletions cmd/flux/reconcile.go
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/pkg/apis/meta"

"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -166,14 +167,26 @@ func requestReconciliation(ctx context.Context, kubeClient client.Client,
return err
}
patch := client.MergeFrom(object.DeepCopy())
if ann := object.GetAnnotations(); ann == nil {
object.SetAnnotations(map[string]string{
meta.ReconcileRequestAnnotation: time.Now().Format(time.RFC3339Nano),
})
} else {
ann[meta.ReconcileRequestAnnotation] = time.Now().Format(time.RFC3339Nano)
object.SetAnnotations(ann)

// Add a timestamp annotation to trigger a reconciliation.
ts := time.Now().Format(time.RFC3339Nano)
annotations := object.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
annotations[meta.ReconcileRequestAnnotation] = ts

// HelmRelease specific annotations to force or reset a release.
if gvk.Kind == helmv2.HelmReleaseKind {
if rhrArgs.syncForce {
annotations[helmv2.ForceRequestAnnotation] = ts
}
if rhrArgs.syncReset {
annotations[helmv2.ResetRequestAnnotation] = ts
}
}

object.SetAnnotations(annotations)
return kubeClient.Patch(ctx, object, patch)
})
}
7 changes: 5 additions & 2 deletions cmd/flux/reconcile_helmrelease.go
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
)

Expand All @@ -46,13 +46,16 @@ The reconcile kustomization command triggers a reconciliation of a HelmRelease r

type reconcileHelmReleaseFlags struct {
syncHrWithSource bool
syncForce bool
syncReset bool
}

var rhrArgs reconcileHelmReleaseFlags

func init() {
reconcileHrCmd.Flags().BoolVar(&rhrArgs.syncHrWithSource, "with-source", false, "reconcile HelmRelease source")

reconcileHrCmd.Flags().BoolVar(&rhrArgs.syncForce, "force", false, "force a one-off install or upgrade of the HelmRelease resource")
reconcileHrCmd.Flags().BoolVar(&rhrArgs.syncReset, "reset", false, "reset the reset the failure count for this HelmRelease resource")
reconcileCmd.AddCommand(reconcileHrCmd)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/resume_helmrelease.go
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/spf13/cobra"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var resumeHrCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/stats.go
Expand Up @@ -27,7 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/fluxcd/cli-utils/pkg/kstatus/status"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
autov1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/suspend_helmrelease.go
Expand Up @@ -19,7 +19,7 @@ package main
import (
"github.com/spf13/cobra"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
)

var suspendHrCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/export/helm-release.yaml
@@ -1,5 +1,5 @@
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
name: flux-system
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/export/objects.yaml
Expand Up @@ -124,7 +124,7 @@ spec:
timeout: 1m0s
url: https://stefanprodan.github.io/podinfo
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
name: flux-system
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/trace/deployment.yaml
Expand Up @@ -34,7 +34,7 @@ spec:
command: [ "echo hello world" ]
image: busybox
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
labels:
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/trace/helmrelease-oci.yaml
Expand Up @@ -9,7 +9,7 @@ kind: Namespace
metadata:
name: {{ .ns }}
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
labels:
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/trace/helmrelease.yaml
Expand Up @@ -9,7 +9,7 @@ kind: Namespace
metadata:
name: {{ .ns }}
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
labels:
Expand Down
7 changes: 4 additions & 3 deletions cmd/flux/trace.go
Expand Up @@ -33,13 +33,14 @@ import (
"k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/fluxcd/flux2/v2/internal/utils"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
fluxmeta "github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/oci"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"

"github.com/fluxcd/flux2/v2/internal/utils"
)

var traceCmd = &cobra.Command{
Expand All @@ -63,7 +64,7 @@ You can also trace multiple objects with different resource kinds using <resourc

# API Version and Kind can also be specified explicitly
# Note that either both, kind and api-version, or neither have to be specified.
flux trace redis --kind=helmrelease --api-version=helm.toolkit.fluxcd.io/v2beta1 -n redis`,
flux trace redis --kind=helmrelease --api-version=helm.toolkit.fluxcd.io/v2beta2 -n redis`,
RunE: traceCmdRun,
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/flux/trace_test.go
Expand Up @@ -47,7 +47,7 @@ func TestTrace(t *testing.T) {
},
{
"HelmRelease",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta1",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta2",
"testdata/trace/helmrelease.yaml",
"testdata/trace/helmrelease.golden",
map[string]string{
Expand All @@ -59,7 +59,7 @@ func TestTrace(t *testing.T) {
},
{
"HelmRelease from OCI registry",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta1",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta2",
"testdata/trace/helmrelease-oci.yaml",
"testdata/trace/helmrelease-oci.golden",
map[string]string{
Expand Down
32 changes: 9 additions & 23 deletions cmd/flux/tree_kustomization.go
Expand Up @@ -32,10 +32,11 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/yaml"

"github.com/fluxcd/cli-utils/pkg/object"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/ssa"

Expand Down Expand Up @@ -208,27 +209,16 @@ func getHelmReleaseInventory(ctx context.Context, objectKey client.ObjectKey, ku
return nil, nil
}

storageNamespace := hr.GetNamespace()
if hr.Spec.StorageNamespace != "" {
storageNamespace = hr.Spec.StorageNamespace
}

storageName := hr.GetName()
if hr.Spec.ReleaseName != "" {
storageName = hr.Spec.ReleaseName
} else if hr.Spec.TargetNamespace != "" {
storageName = strings.Join([]string{hr.Spec.TargetNamespace, hr.Name}, "-")
}

storageVersion := hr.Status.LastReleaseRevision
// skip release if it failed to install
if storageVersion < 1 {
storageNamespace := hr.Status.StorageNamespace
latest := hr.Status.History.Latest()
if len(storageNamespace) == 0 || latest == nil {
// Skip release if it has no current
return nil, nil
}

storageKey := client.ObjectKey{
Namespace: storageNamespace,
Name: fmt.Sprintf("sh.helm.release.v1.%s.v%v", storageName, storageVersion),
Name: fmt.Sprintf("sh.helm.release.v1.%s.v%v", latest.Name, latest.Version),
}

storageSecret := &corev1.Secret{}
Expand Down Expand Up @@ -279,12 +269,8 @@ func getHelmReleaseInventory(ctx context.Context, objectKey client.ObjectKey, ku
// set the namespace on namespaced objects
for _, obj := range objects {
if obj.GetNamespace() == "" {
if isNamespaced, _ := utils.IsAPINamespaced(obj, kubeClient.Scheme(), kubeClient.RESTMapper()); isNamespaced {
if hr.Spec.TargetNamespace != "" {
obj.SetNamespace(hr.Spec.TargetNamespace)
} else {
obj.SetNamespace(hr.GetNamespace())
}
if isNamespaced, _ := apiutil.IsObjectNamespaced(obj, kubeClient.Scheme(), kubeClient.RESTMapper()); isNamespaced {
obj.SetNamespace(latest.Namespace)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/flags/crds.go
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"strings"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"

"github.com/fluxcd/flux2/v2/internal/utils"
)
Expand Down