From 329b3912a04fc4bcf0f29d4262a0ef03c58a385b Mon Sep 17 00:00:00 2001 From: Cannon Palms Date: Mon, 9 May 2022 15:00:03 -0400 Subject: [PATCH 01/12] feat(priority-classes): Support podPriorityClass def on node config --- api/v1alpha1/nificluster_types.go | 11 +++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 5 +++++ .../crd/bases/nifi.konpyutaika.com_nificlusters.yaml | 8 ++++++++ .../crds/nifi.konpyutaika.com_nificlusters.yaml | 8 ++++++++ pkg/resources/nifi/pod.go | 2 +- 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/api/v1alpha1/nificluster_types.go b/api/v1alpha1/nificluster_types.go index c44d06936c..5910d4f6e1 100644 --- a/api/v1alpha1/nificluster_types.go +++ b/api/v1alpha1/nificluster_types.go @@ -298,6 +298,9 @@ type NodeConfig struct { Tolerations []corev1.Toleration `json:"tolerations,omitempty"` // podMetadata allows to add additionnal metadata to the node pods PodMetadata Metadata `json:"podMetadata,omitempty"` + // priorityClassName can be used to set the priority class applied to the node + // +optional + PriorityClassName *string `json:"priorityClassName,omitempty"` } type Metadata struct { @@ -655,6 +658,14 @@ func (nConfig *NodeConfig) GetResources() *corev1.ResourceRequirements { } } +// GetPriorityClass returns the name of the priority class to use for the given node +func (nConfig *NodeConfig) GetPriorityClass() string { + if nConfig.PriorityClassName != nil { + return *nConfig.PriorityClassName + } + return "" +} + // func (nConfig *NodeConfig) GetRunAsUser() *int64 { var defaultUserID int64 = 1000 diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6fa0bee286..ce7ea0f714 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -1224,6 +1224,11 @@ func (in *NodeConfig) DeepCopyInto(out *NodeConfig) { } } in.PodMetadata.DeepCopyInto(&out.PodMetadata) + if in.PriorityClassName != nil { + in, out := &in.PriorityClassName, &out.PriorityClassName + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeConfig. diff --git a/config/crd/bases/nifi.konpyutaika.com_nificlusters.yaml b/config/crd/bases/nifi.konpyutaika.com_nificlusters.yaml index b69c5a7ce0..5cb4edf373 100644 --- a/config/crd/bases/nifi.konpyutaika.com_nificlusters.yaml +++ b/config/crd/bases/nifi.konpyutaika.com_nificlusters.yaml @@ -3427,6 +3427,10 @@ spec: associated https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set type: object type: object + priorityClassName: + description: priorityClassName can be used to set the priority + class applied to the node + type: string provenanceStorage: description: provenanceStorage allow to specify the maximum amount of data provenance information to store at a time https://nifi.apache.org/docs/nifi-docs/html/administration-guide.html#write-ahead-provenance-repository-properties @@ -5670,6 +5674,10 @@ spec: associated https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set type: object type: object + priorityClassName: + description: priorityClassName can be used to set the priority + class applied to the node + type: string provenanceStorage: description: provenanceStorage allow to specify the maximum amount of data provenance information to store at a time diff --git a/helm/nifikop/crds/nifi.konpyutaika.com_nificlusters.yaml b/helm/nifikop/crds/nifi.konpyutaika.com_nificlusters.yaml index b69c5a7ce0..5cb4edf373 100644 --- a/helm/nifikop/crds/nifi.konpyutaika.com_nificlusters.yaml +++ b/helm/nifikop/crds/nifi.konpyutaika.com_nificlusters.yaml @@ -3427,6 +3427,10 @@ spec: associated https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set type: object type: object + priorityClassName: + description: priorityClassName can be used to set the priority + class applied to the node + type: string provenanceStorage: description: provenanceStorage allow to specify the maximum amount of data provenance information to store at a time https://nifi.apache.org/docs/nifi-docs/html/administration-guide.html#write-ahead-provenance-repository-properties @@ -5670,6 +5674,10 @@ spec: associated https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set type: object type: object + priorityClassName: + description: priorityClassName can be used to set the priority + class applied to the node + type: string provenanceStorage: description: provenanceStorage allow to specify the maximum amount of data provenance information to store at a time diff --git a/pkg/resources/nifi/pod.go b/pkg/resources/nifi/pod.go index 2d9a8aa702..4419af2e55 100644 --- a/pkg/resources/nifi/pod.go +++ b/pkg/resources/nifi/pod.go @@ -169,7 +169,7 @@ done`, DNSPolicy: corev1.DNSClusterFirst, ImagePullSecrets: nodeConfig.GetImagePullSecrets(), ServiceAccountName: nodeConfig.GetServiceAccount(), - Priority: util.Int32Pointer(0), + PriorityClassName: nodeConfig.GetPriorityClass(), SchedulerName: "default-scheduler", Tolerations: nodeConfig.GetTolerations(), NodeSelector: nodeConfig.GetNodeSelector(), From 2f97bc18a5dcd58a5dd7aae80f9604c9e2b18676 Mon Sep 17 00:00:00 2001 From: Cannon Palms Date: Mon, 9 May 2022 15:12:05 -0400 Subject: [PATCH 02/12] docs(priority-classes): Add podPriorityClass docs to node config docs --- site/docs/5_references/1_nifi_cluster/3_node_config.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/site/docs/5_references/1_nifi_cluster/3_node_config.md b/site/docs/5_references/1_nifi_cluster/3_node_config.md index 2cfdba7cd0..5d1733dd6f 100644 --- a/site/docs/5_references/1_nifi_cluster/3_node_config.md +++ b/site/docs/5_references/1_nifi_cluster/3_node_config.md @@ -31,6 +31,8 @@ NodeConfig defines the node configuration # nodeAffinity: # imagePullPolicy define the pull policy for NiFi cluster docker image imagePullPolicy: IfNotPresent + # priorityClassName define the name of the priority class to be applied to these nodes + priorityClassName: "example-priority-class-name" # externalVolumeConfigs specifies a list of volume to mount into the main container. externalVolumeConfigs: - name: example-volume @@ -82,6 +84,7 @@ NodeConfig defines the node configuration | nodeSelector | map\[string\]string |nodeSelector can be specified, which set the pod to fit on a node [nodeselector](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector)|No|nil| | tolerations | \[ \][Toleration](https://godoc.org/k8s.io/api/core/v1#Toleration) |tolerations can be specified, which set the pod's tolerations [taint-and-toleration](https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/#concepts).|No|nil| | podMetadata | [Metadata](#metadata) |define additionnal metadata to merge to the pod associated.|No|nil| +| priorityClassName | string | Specify the name of the priority class to apply to pods created with this node config | No | nil| ## StorageConfig From 867d27fa6897894333f8e1d53cff0e2d6604baf2 Mon Sep 17 00:00:00 2001 From: Juldrixx Date: Thu, 12 May 2022 07:43:50 -0500 Subject: [PATCH 03/12] Allow the operator to take over existing parameter context --- api/v1alpha1/nifiparametercontext_types.go | 9 +++++++ api/v1alpha1/zz_generated.deepcopy.go | 5 ++++ ...konpyutaika.com_nifiparametercontexts.yaml | 4 +++ .../nifiparametercontext_controller.go | 25 ++++++++++++----- ...konpyutaika.com_nifiparametercontexts.yaml | 4 +++ .../parametercontext/parametercontext.go | 27 +++++++++++++++++++ pkg/nificlient/client.go | 8 +++--- pkg/nificlient/parametercontext.go | 17 ++++++++++++ .../5_references/4_nifi_parameter_context.md | 1 + 9 files changed, 91 insertions(+), 9 deletions(-) diff --git a/api/v1alpha1/nifiparametercontext_types.go b/api/v1alpha1/nifiparametercontext_types.go index beffc68fe6..45806a42b5 100644 --- a/api/v1alpha1/nifiparametercontext_types.go +++ b/api/v1alpha1/nifiparametercontext_types.go @@ -17,6 +17,8 @@ type NifiParameterContextSpec struct { ClusterRef ClusterReference `json:"clusterRef,omitempty"` // a list of secret containing sensitive parameters (the key will name of the parameter). SecretRefs []SecretReference `json:"secretRefs,omitempty"` + // whether or not the operator should not take over an existing parameter context if its name is the same. + DisableTakeOver *bool `json:"disableTakeOver,omitempty"` } type Parameter struct { @@ -83,3 +85,10 @@ type NifiParameterContextList struct { func init() { SchemeBuilder.Register(&NifiParameterContext{}, &NifiParameterContextList{}) } + +func (d *NifiParameterContextSpec) IsTakeOverEnabled() bool { + if d.DisableTakeOver == nil { + return true + } + return !*d.DisableTakeOver +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 841c969185..eb0b2e9de0 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -755,6 +755,11 @@ func (in *NifiParameterContextSpec) DeepCopyInto(out *NifiParameterContextSpec) *out = make([]SecretReference, len(*in)) copy(*out, *in) } + if in.DisableTakeOver != nil { + in, out := &in.DisableTakeOver, &out.DisableTakeOver + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NifiParameterContextSpec. diff --git a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml index 8261cdb133..3d4ec7ff28 100644 --- a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -50,6 +50,10 @@ spec: description: description: the Description of the Parameter Context. type: string + disableTakeOver: + description: whether or not the operator should not take over an existing + parameter context if its name is the same. + type: boolean parameters: description: a list of non-sensitive Parameters. items: diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index 93eeecd363..f104602bb8 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -18,9 +18,12 @@ package controllers import ( "context" - "emperror.dev/errors" "encoding/json" "fmt" + "reflect" + "time" + + "emperror.dev/errors" "github.com/banzaicloud/k8s-objectmatcher/patch" "github.com/konpyutaika/nifikop/pkg/clientwrappers/parametercontext" errorfactory "github.com/konpyutaika/nifikop/pkg/errorfactory" @@ -31,9 +34,7 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" - "reflect" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "time" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" @@ -223,9 +224,21 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl r.Recorder.Event(instance, corev1.EventTypeNormal, "Creating", fmt.Sprintf("Creating parameter context %s", instance.Name)) - status, err := parametercontext.CreateParameterContext(instance, parameterSecrets, clientConfig) - if err != nil { - return RequeueWithError(r.Log, "failure creating parameter context", err) + var status *v1alpha1.NifiParameterContextStatus + + if instance.Spec.IsTakeOverEnabled() { + status, err = parametercontext.FindParameterContextByName(instance, clientConfig) + if err != nil { + return RequeueWithError(r.Log, "failure finding parameter context", err) + } + } + + if status == nil { + // Create NiFi parameter context + status, err = parametercontext.CreateParameterContext(instance, parameterSecrets, clientConfig) + if err != nil { + return RequeueWithError(r.Log, "failure creating parameter context", err) + } } instance.Status = *status diff --git a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml index 8261cdb133..3d4ec7ff28 100644 --- a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -50,6 +50,10 @@ spec: description: description: the Description of the Parameter Context. type: string + disableTakeOver: + description: whether or not the operator should not take over an existing + parameter context if its name is the same. + type: boolean parameters: description: a list of non-sensitive Parameters. items: diff --git a/pkg/clientwrappers/parametercontext/parametercontext.go b/pkg/clientwrappers/parametercontext/parametercontext.go index d63d780efd..ee501789c1 100644 --- a/pkg/clientwrappers/parametercontext/parametercontext.go +++ b/pkg/clientwrappers/parametercontext/parametercontext.go @@ -36,6 +36,33 @@ func ExistParameterContext(parameterContext *v1alpha1.NifiParameterContext, conf return entity != nil, nil } +func FindParameterContextByName(parameterContext *v1alpha1.NifiParameterContext, config *clientconfig.NifiConfig) (*v1alpha1.NifiParameterContextStatus, error) { + + nClient, err := common.NewClusterConnection(log, config) + if err != nil { + return nil, err + } + + entities, err := nClient.GetParameterContexts() + if err := clientwrappers.ErrorGetOperation(log, err, "Get parameter-contexts"); err != nil { + if err == nificlient.ErrNifiClusterReturned404 { + return nil, nil + } + return nil, err + } + + for _, entity := range entities { + if parameterContext.GetName() == entity.Component.Name { + return &v1alpha1.NifiParameterContextStatus{ + Id: entity.Id, + Version: *entity.Revision.Version, + }, nil + } + } + + return nil, nil +} + func CreateParameterContext(parameterContext *v1alpha1.NifiParameterContext, parameterSecrets []*corev1.Secret, config *clientconfig.NifiConfig) (*v1alpha1.NifiParameterContextStatus, error) { diff --git a/pkg/nificlient/client.go b/pkg/nificlient/client.go index 4124a880a7..185a5c8a29 100644 --- a/pkg/nificlient/client.go +++ b/pkg/nificlient/client.go @@ -2,14 +2,15 @@ package nificlient import ( "context" - "emperror.dev/errors" "fmt" - "github.com/konpyutaika/nifikop/pkg/util/clientconfig" "net/http" "net/url" - ctrl "sigs.k8s.io/controller-runtime" "time" + "emperror.dev/errors" + "github.com/konpyutaika/nifikop/pkg/util/clientconfig" + ctrl "sigs.k8s.io/controller-runtime" + nigoapi "github.com/erdrix/nigoapi/pkg/nifi" "github.com/konpyutaika/nifikop/pkg/errorfactory" ) @@ -92,6 +93,7 @@ type NifiClient interface { UpdateInputPortRunStatus(id string, entity nigoapi.PortRunStatusEntity) (*nigoapi.ProcessorEntity, error) // Parameter context func + GetParameterContexts() ([]nigoapi.ParameterContextEntity, error) GetParameterContext(id string) (*nigoapi.ParameterContextEntity, error) CreateParameterContext(entity nigoapi.ParameterContextEntity) (*nigoapi.ParameterContextEntity, error) RemoveParameterContext(entity nigoapi.ParameterContextEntity) error diff --git a/pkg/nificlient/parametercontext.go b/pkg/nificlient/parametercontext.go index 549f1f85d1..962fedf5d1 100644 --- a/pkg/nificlient/parametercontext.go +++ b/pkg/nificlient/parametercontext.go @@ -7,6 +7,23 @@ import ( nigoapi "github.com/erdrix/nigoapi/pkg/nifi" ) +func (n *nifiClient) GetParameterContexts() ([]nigoapi.ParameterContextEntity, error) { + // Get nigoapi client, favoring the one associated to the coordinator node. + client, context := n.privilegeCoordinatorClient() + if client == nil { + log.Error(ErrNoNodeClientsAvailable, "Error during creating node client") + return nil, ErrNoNodeClientsAvailable + } + + // Request on Nifi Rest API to get the parameter contexts informations + pcEntity, rsp, body, err := client.FlowApi.GetParameterContexts(context) + if err := errorGetOperation(rsp, body, err); err != nil { + return nil, err + } + + return pcEntity.ParameterContexts, nil +} + func (n *nifiClient) GetParameterContext(id string) (*nigoapi.ParameterContextEntity, error) { // Get nigoapi client, favoring the one associated to the coordinator node. client, context := n.privilegeCoordinatorClient() diff --git a/site/docs/5_references/4_nifi_parameter_context.md b/site/docs/5_references/4_nifi_parameter_context.md index 63a3996004..185856cb48 100644 --- a/site/docs/5_references/4_nifi_parameter_context.md +++ b/site/docs/5_references/4_nifi_parameter_context.md @@ -44,6 +44,7 @@ spec: |parameters|\[ \][Parameter](#parameter)| a list of non-sensitive Parameters. |Yes| - | |secretRefs|\[ \][SecretReference](#secretreference)| a list of secret containing sensitive parameters (the key will name of the parameter) |No| - | |clusterRef|[ClusterReference](./2_nifi_user.md#clusterreference)| contains the reference to the NifiCluster with the one the user is linked. |Yes| - | +|disableTakeOver|bool| whether or not the operator should not take over an existing parameter context if its name is the same. |No| - | ## NifiParameterContextStatus From 23c0309a7ce8bc7b83e17b261731eb2db3bc390d Mon Sep 17 00:00:00 2001 From: Juldrixx Date: Thu, 12 May 2022 07:51:18 -0500 Subject: [PATCH 04/12] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62f1c6d180..db386054e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Added +- [PR #95](https://github.com/konpyutaika/nifikop/pull/95) - **[Operator/NiFiParameterContext]** Allow the operator to take over existing parameter context. + ### Changed ### Deprecated From 6dfd3cfc1399481833b43f65d97b9b8dafda97a6 Mon Sep 17 00:00:00 2001 From: Juldrixx Date: Thu, 12 May 2022 07:43:50 -0500 Subject: [PATCH 05/12] Allow the operator to take over existing parameter context --- api/v1alpha1/nifiparametercontext_types.go | 9 +++++++ api/v1alpha1/zz_generated.deepcopy.go | 5 ++++ ...konpyutaika.com_nifiparametercontexts.yaml | 4 +++ .../nifiparametercontext_controller.go | 25 ++++++++++++----- ...konpyutaika.com_nifiparametercontexts.yaml | 4 +++ .../parametercontext/parametercontext.go | 27 +++++++++++++++++++ pkg/nificlient/client.go | 8 +++--- pkg/nificlient/parametercontext.go | 17 ++++++++++++ .../5_references/4_nifi_parameter_context.md | 1 + 9 files changed, 91 insertions(+), 9 deletions(-) diff --git a/api/v1alpha1/nifiparametercontext_types.go b/api/v1alpha1/nifiparametercontext_types.go index beffc68fe6..45806a42b5 100644 --- a/api/v1alpha1/nifiparametercontext_types.go +++ b/api/v1alpha1/nifiparametercontext_types.go @@ -17,6 +17,8 @@ type NifiParameterContextSpec struct { ClusterRef ClusterReference `json:"clusterRef,omitempty"` // a list of secret containing sensitive parameters (the key will name of the parameter). SecretRefs []SecretReference `json:"secretRefs,omitempty"` + // whether or not the operator should not take over an existing parameter context if its name is the same. + DisableTakeOver *bool `json:"disableTakeOver,omitempty"` } type Parameter struct { @@ -83,3 +85,10 @@ type NifiParameterContextList struct { func init() { SchemeBuilder.Register(&NifiParameterContext{}, &NifiParameterContextList{}) } + +func (d *NifiParameterContextSpec) IsTakeOverEnabled() bool { + if d.DisableTakeOver == nil { + return true + } + return !*d.DisableTakeOver +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6fa0bee286..e64dbcb10c 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -780,6 +780,11 @@ func (in *NifiParameterContextSpec) DeepCopyInto(out *NifiParameterContextSpec) *out = make([]SecretReference, len(*in)) copy(*out, *in) } + if in.DisableTakeOver != nil { + in, out := &in.DisableTakeOver, &out.DisableTakeOver + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NifiParameterContextSpec. diff --git a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml index 8261cdb133..3d4ec7ff28 100644 --- a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -50,6 +50,10 @@ spec: description: description: the Description of the Parameter Context. type: string + disableTakeOver: + description: whether or not the operator should not take over an existing + parameter context if its name is the same. + type: boolean parameters: description: a list of non-sensitive Parameters. items: diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index 93eeecd363..f104602bb8 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -18,9 +18,12 @@ package controllers import ( "context" - "emperror.dev/errors" "encoding/json" "fmt" + "reflect" + "time" + + "emperror.dev/errors" "github.com/banzaicloud/k8s-objectmatcher/patch" "github.com/konpyutaika/nifikop/pkg/clientwrappers/parametercontext" errorfactory "github.com/konpyutaika/nifikop/pkg/errorfactory" @@ -31,9 +34,7 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" - "reflect" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "time" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" @@ -223,9 +224,21 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl r.Recorder.Event(instance, corev1.EventTypeNormal, "Creating", fmt.Sprintf("Creating parameter context %s", instance.Name)) - status, err := parametercontext.CreateParameterContext(instance, parameterSecrets, clientConfig) - if err != nil { - return RequeueWithError(r.Log, "failure creating parameter context", err) + var status *v1alpha1.NifiParameterContextStatus + + if instance.Spec.IsTakeOverEnabled() { + status, err = parametercontext.FindParameterContextByName(instance, clientConfig) + if err != nil { + return RequeueWithError(r.Log, "failure finding parameter context", err) + } + } + + if status == nil { + // Create NiFi parameter context + status, err = parametercontext.CreateParameterContext(instance, parameterSecrets, clientConfig) + if err != nil { + return RequeueWithError(r.Log, "failure creating parameter context", err) + } } instance.Status = *status diff --git a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml index 8261cdb133..3d4ec7ff28 100644 --- a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -50,6 +50,10 @@ spec: description: description: the Description of the Parameter Context. type: string + disableTakeOver: + description: whether or not the operator should not take over an existing + parameter context if its name is the same. + type: boolean parameters: description: a list of non-sensitive Parameters. items: diff --git a/pkg/clientwrappers/parametercontext/parametercontext.go b/pkg/clientwrappers/parametercontext/parametercontext.go index d63d780efd..ee501789c1 100644 --- a/pkg/clientwrappers/parametercontext/parametercontext.go +++ b/pkg/clientwrappers/parametercontext/parametercontext.go @@ -36,6 +36,33 @@ func ExistParameterContext(parameterContext *v1alpha1.NifiParameterContext, conf return entity != nil, nil } +func FindParameterContextByName(parameterContext *v1alpha1.NifiParameterContext, config *clientconfig.NifiConfig) (*v1alpha1.NifiParameterContextStatus, error) { + + nClient, err := common.NewClusterConnection(log, config) + if err != nil { + return nil, err + } + + entities, err := nClient.GetParameterContexts() + if err := clientwrappers.ErrorGetOperation(log, err, "Get parameter-contexts"); err != nil { + if err == nificlient.ErrNifiClusterReturned404 { + return nil, nil + } + return nil, err + } + + for _, entity := range entities { + if parameterContext.GetName() == entity.Component.Name { + return &v1alpha1.NifiParameterContextStatus{ + Id: entity.Id, + Version: *entity.Revision.Version, + }, nil + } + } + + return nil, nil +} + func CreateParameterContext(parameterContext *v1alpha1.NifiParameterContext, parameterSecrets []*corev1.Secret, config *clientconfig.NifiConfig) (*v1alpha1.NifiParameterContextStatus, error) { diff --git a/pkg/nificlient/client.go b/pkg/nificlient/client.go index 4124a880a7..185a5c8a29 100644 --- a/pkg/nificlient/client.go +++ b/pkg/nificlient/client.go @@ -2,14 +2,15 @@ package nificlient import ( "context" - "emperror.dev/errors" "fmt" - "github.com/konpyutaika/nifikop/pkg/util/clientconfig" "net/http" "net/url" - ctrl "sigs.k8s.io/controller-runtime" "time" + "emperror.dev/errors" + "github.com/konpyutaika/nifikop/pkg/util/clientconfig" + ctrl "sigs.k8s.io/controller-runtime" + nigoapi "github.com/erdrix/nigoapi/pkg/nifi" "github.com/konpyutaika/nifikop/pkg/errorfactory" ) @@ -92,6 +93,7 @@ type NifiClient interface { UpdateInputPortRunStatus(id string, entity nigoapi.PortRunStatusEntity) (*nigoapi.ProcessorEntity, error) // Parameter context func + GetParameterContexts() ([]nigoapi.ParameterContextEntity, error) GetParameterContext(id string) (*nigoapi.ParameterContextEntity, error) CreateParameterContext(entity nigoapi.ParameterContextEntity) (*nigoapi.ParameterContextEntity, error) RemoveParameterContext(entity nigoapi.ParameterContextEntity) error diff --git a/pkg/nificlient/parametercontext.go b/pkg/nificlient/parametercontext.go index 549f1f85d1..962fedf5d1 100644 --- a/pkg/nificlient/parametercontext.go +++ b/pkg/nificlient/parametercontext.go @@ -7,6 +7,23 @@ import ( nigoapi "github.com/erdrix/nigoapi/pkg/nifi" ) +func (n *nifiClient) GetParameterContexts() ([]nigoapi.ParameterContextEntity, error) { + // Get nigoapi client, favoring the one associated to the coordinator node. + client, context := n.privilegeCoordinatorClient() + if client == nil { + log.Error(ErrNoNodeClientsAvailable, "Error during creating node client") + return nil, ErrNoNodeClientsAvailable + } + + // Request on Nifi Rest API to get the parameter contexts informations + pcEntity, rsp, body, err := client.FlowApi.GetParameterContexts(context) + if err := errorGetOperation(rsp, body, err); err != nil { + return nil, err + } + + return pcEntity.ParameterContexts, nil +} + func (n *nifiClient) GetParameterContext(id string) (*nigoapi.ParameterContextEntity, error) { // Get nigoapi client, favoring the one associated to the coordinator node. client, context := n.privilegeCoordinatorClient() diff --git a/site/docs/5_references/4_nifi_parameter_context.md b/site/docs/5_references/4_nifi_parameter_context.md index 63a3996004..185856cb48 100644 --- a/site/docs/5_references/4_nifi_parameter_context.md +++ b/site/docs/5_references/4_nifi_parameter_context.md @@ -44,6 +44,7 @@ spec: |parameters|\[ \][Parameter](#parameter)| a list of non-sensitive Parameters. |Yes| - | |secretRefs|\[ \][SecretReference](#secretreference)| a list of secret containing sensitive parameters (the key will name of the parameter) |No| - | |clusterRef|[ClusterReference](./2_nifi_user.md#clusterreference)| contains the reference to the NifiCluster with the one the user is linked. |Yes| - | +|disableTakeOver|bool| whether or not the operator should not take over an existing parameter context if its name is the same. |No| - | ## NifiParameterContextStatus From b2ab6b9f2d4e6d497737684d3b23badfa944e991 Mon Sep 17 00:00:00 2001 From: Juldrixx Date: Thu, 12 May 2022 09:24:32 -0500 Subject: [PATCH 06/12] Update logic of takeOver --- controllers/nifiparametercontext_controller.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index f104602bb8..82ac13a1dd 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -226,11 +226,9 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl var status *v1alpha1.NifiParameterContextStatus - if instance.Spec.IsTakeOverEnabled() { - status, err = parametercontext.FindParameterContextByName(instance, clientConfig) - if err != nil { - return RequeueWithError(r.Log, "failure finding parameter context", err) - } + status, err = parametercontext.FindParameterContextByName(instance, clientConfig) + if err != nil { + return RequeueWithError(r.Log, "failure finding parameter context", err) } if status == nil { @@ -239,6 +237,12 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl if err != nil { return RequeueWithError(r.Log, "failure creating parameter context", err) } + } else { + // ParameterContext name already used + if !instance.Spec.IsTakeOverEnabled() { + // TakeOver disabled + return RequeueWithError(r.Log, "parameter context name already used and takeOver disabled", err) + } } instance.Status = *status From af6506e1ce4ef658d0c56def38d80cbd01838de8 Mon Sep 17 00:00:00 2001 From: Juldrixx <31806759+juldrixx@users.noreply.github.com> Date: Thu, 12 May 2022 17:47:52 +0200 Subject: [PATCH 07/12] Update controllers/nifiparametercontext_controller.go Co-authored-by: michael81877 <86672176+michael81877@users.noreply.github.com> --- controllers/nifiparametercontext_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index 82ac13a1dd..f0bbad9ab5 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -241,7 +241,7 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl // ParameterContext name already used if !instance.Spec.IsTakeOverEnabled() { // TakeOver disabled - return RequeueWithError(r.Log, "parameter context name already used and takeOver disabled", err) + return RequeueWithError(r.Log, fmt.Sprintf("parameter context name %s already used and takeOver disabled", instance.GetName()), err) } } From 7ccb1113e63f11a1b60a6518e6b9ec4ee6aed3e7 Mon Sep 17 00:00:00 2001 From: Juldrixx Date: Thu, 12 May 2022 10:54:30 -0500 Subject: [PATCH 08/12] Update docs --- api/v1alpha1/nifiparametercontext_types.go | 2 +- .../crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml | 2 +- .../crds/nifi.konpyutaika.com_nifiparametercontexts.yaml | 2 +- site/docs/5_references/4_nifi_parameter_context.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/v1alpha1/nifiparametercontext_types.go b/api/v1alpha1/nifiparametercontext_types.go index 45806a42b5..3b15352e52 100644 --- a/api/v1alpha1/nifiparametercontext_types.go +++ b/api/v1alpha1/nifiparametercontext_types.go @@ -17,7 +17,7 @@ type NifiParameterContextSpec struct { ClusterRef ClusterReference `json:"clusterRef,omitempty"` // a list of secret containing sensitive parameters (the key will name of the parameter). SecretRefs []SecretReference `json:"secretRefs,omitempty"` - // whether or not the operator should not take over an existing parameter context if its name is the same. + // whether or not the operator should take over an existing parameter context if its name is the same. DisableTakeOver *bool `json:"disableTakeOver,omitempty"` } diff --git a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml index 3d4ec7ff28..1c9543776a 100644 --- a/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/config/crd/bases/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -51,7 +51,7 @@ spec: description: the Description of the Parameter Context. type: string disableTakeOver: - description: whether or not the operator should not take over an existing + description: whether or not the operator should take over an existing parameter context if its name is the same. type: boolean parameters: diff --git a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml index 3d4ec7ff28..1c9543776a 100644 --- a/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml +++ b/helm/nifikop/crds/nifi.konpyutaika.com_nifiparametercontexts.yaml @@ -51,7 +51,7 @@ spec: description: the Description of the Parameter Context. type: string disableTakeOver: - description: whether or not the operator should not take over an existing + description: whether or not the operator should take over an existing parameter context if its name is the same. type: boolean parameters: diff --git a/site/docs/5_references/4_nifi_parameter_context.md b/site/docs/5_references/4_nifi_parameter_context.md index 185856cb48..253c418d9c 100644 --- a/site/docs/5_references/4_nifi_parameter_context.md +++ b/site/docs/5_references/4_nifi_parameter_context.md @@ -44,7 +44,7 @@ spec: |parameters|\[ \][Parameter](#parameter)| a list of non-sensitive Parameters. |Yes| - | |secretRefs|\[ \][SecretReference](#secretreference)| a list of secret containing sensitive parameters (the key will name of the parameter) |No| - | |clusterRef|[ClusterReference](./2_nifi_user.md#clusterreference)| contains the reference to the NifiCluster with the one the user is linked. |Yes| - | -|disableTakeOver|bool| whether or not the operator should not take over an existing parameter context if its name is the same. |No| - | +|disableTakeOver|bool| whether or not the operator should take over an existing parameter context if its name is the same. |No| - | ## NifiParameterContextStatus From 07f71eeb3681c14a98bba5518d2361d5c07a4a40 Mon Sep 17 00:00:00 2001 From: Cannon Palms Date: Tue, 17 May 2022 11:56:13 -0400 Subject: [PATCH 09/12] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2247846b8e..1fff284186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Added - [PR #76](https://github.com/konpyutaika/nifikop/pull/76) - **[Operator/NiFiCluster]** Add ability to override default authorizers.xml template. +- [PR #96](https://github.com/konpyutaika/nifikop/pull/96) - **[Operator/NifiCluster]** Add ability to specify pod priority class ### Changed From 47da97cdf9d5c79304b9994da583bdf34d019f48 Mon Sep 17 00:00:00 2001 From: Juldrixx <31806759+juldrixx@users.noreply.github.com> Date: Wed, 18 May 2022 21:04:27 +0200 Subject: [PATCH 10/12] Update controllers/nifiparametercontext_controller.go Co-authored-by: Alexandre Guitton <10503351+erdrix@users.noreply.github.com> --- controllers/nifiparametercontext_controller.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index f0bbad9ab5..ad281ab5a8 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -231,18 +231,16 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl return RequeueWithError(r.Log, "failure finding parameter context", err) } + if status != nil && !instance.Spec.IsTakeOverEnabled() { + // TakeOver disabled + return RequeueWithError(r.Log, fmt.Sprintf("parameter context name %s already used and takeOver disabled", instance.GetName()), err) + } if status == nil { // Create NiFi parameter context status, err = parametercontext.CreateParameterContext(instance, parameterSecrets, clientConfig) if err != nil { return RequeueWithError(r.Log, "failure creating parameter context", err) } - } else { - // ParameterContext name already used - if !instance.Spec.IsTakeOverEnabled() { - // TakeOver disabled - return RequeueWithError(r.Log, fmt.Sprintf("parameter context name %s already used and takeOver disabled", instance.GetName()), err) - } } instance.Status = *status From ee9bd06d10dae347c6055bacbe4da9e70974f3fd Mon Sep 17 00:00:00 2001 From: Michael H Date: Fri, 10 Jun 2022 09:19:03 +0100 Subject: [PATCH 11/12] handle case where NiFi cluster has been destroyed before resources referencing the cluster revert unnecessary change add logging for finalizer removal update logging to be clear about what resources are getting updated --- controllers/nifidataflow_controller.go | 11 +++++++++-- controllers/nifiparametercontext_controller.go | 17 ++++++++++++----- controllers/nifiregistryclient_controller.go | 11 +++++++++-- controllers/nifiuser_controller.go | 14 +++++++++++--- controllers/nifiusergroup_controller.go | 11 +++++++++-- 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/controllers/nifidataflow_controller.go b/controllers/nifidataflow_controller.go index c9ae1ad785..4235204a5a 100644 --- a/controllers/nifidataflow_controller.go +++ b/controllers/nifidataflow_controller.go @@ -235,6 +235,13 @@ func (r *NifiDataflowReconciler) Reconcile(ctx context.Context, req ctrl.Request r.Recorder.Event(instance, corev1.EventTypeWarning, "ReferenceClusterError", fmt.Sprintf("Failed to create HTTP client for the referenced cluster : %s in %s", instance.Spec.ClusterRef.Name, currentClusterRef.Namespace)) + // the cluster is gone, so just remove the finalizer + if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) { + if err = r.removeFinalizer(ctx, instance); err != nil { + return RequeueWithError(r.Log, fmt.Sprintf("failed to remove finalizer from NifiDataflow %s", instance.Name), err) + } + return Reconciled() + } // the cluster does not exist - should have been caught pre-flight return RequeueWithError(r.Log, "failed to create HTTP client the for referenced cluster", err) } @@ -491,8 +498,7 @@ func (r *NifiDataflowReconciler) updateAndFetchLatest(ctx context.Context, func (r *NifiDataflowReconciler) checkFinalizers(ctx context.Context, flow *v1alpha1.NifiDataflow, config *clientconfig.NifiConfig) (reconcile.Result, error) { - - r.Log.Info("NiFi dataflow is marked for deletion") + r.Log.Info(fmt.Sprintf("NiFi dataflow %s is marked for deletion", flow.Name)) var err error if util.StringSliceContains(flow.GetFinalizers(), dataflowFinalizer) { if err = r.finalizeNifiDataflow(flow, config); err != nil { @@ -512,6 +518,7 @@ func (r *NifiDataflowReconciler) checkFinalizers(ctx context.Context, flow *v1al } func (r *NifiDataflowReconciler) removeFinalizer(ctx context.Context, flow *v1alpha1.NifiDataflow) error { + r.Log.V(5).Info(fmt.Sprintf("Removing finalizer for NifiDataflow %s", flow.Name)) flow.SetFinalizers(util.StringSliceRemove(flow.GetFinalizers(), dataflowFinalizer)) _, err := r.updateAndFetchLatest(ctx, flow) return err diff --git a/controllers/nifiparametercontext_controller.go b/controllers/nifiparametercontext_controller.go index ad281ab5a8..4a16282991 100644 --- a/controllers/nifiparametercontext_controller.go +++ b/controllers/nifiparametercontext_controller.go @@ -171,6 +171,13 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl r.Recorder.Event(instance, corev1.EventTypeWarning, "ReferenceClusterError", fmt.Sprintf("Failed to create HTTP client for the referenced cluster : %s in %s", instance.Spec.ClusterRef.Name, clusterRef.Namespace)) + // the cluster is gone, so just remove the finalizer + if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) { + if err = r.removeFinalizer(ctx, instance); err != nil { + return RequeueWithError(r.Log, fmt.Sprintf("failed to remove finalizer from NifiParameterContext %s", instance.Name), err) + } + return Reconciled() + } // the cluster does not exist - should have been caught pre-flight return RequeueWithError(r.Log, "failed to create HTTP client the for referenced cluster", err) } @@ -335,8 +342,7 @@ func (r *NifiParameterContextReconciler) checkFinalizers( parameterContext *v1alpha1.NifiParameterContext, parameterSecrets []*corev1.Secret, config *clientconfig.NifiConfig) (reconcile.Result, error) { - - r.Log.Info("NiFi parameter context is marked for deletion") + r.Log.Info(fmt.Sprintf("NiFi parameter context %s is marked for deletion", parameterContext.Name)) var err error if util.StringSliceContains(parameterContext.GetFinalizers(), parameterContextFinalizer) { if err = r.finalizeNifiParameterContext(parameterContext, parameterSecrets, config); err != nil { @@ -349,9 +355,10 @@ func (r *NifiParameterContextReconciler) checkFinalizers( return Reconciled() } -func (r *NifiParameterContextReconciler) removeFinalizer(ctx context.Context, flow *v1alpha1.NifiParameterContext) error { - flow.SetFinalizers(util.StringSliceRemove(flow.GetFinalizers(), parameterContextFinalizer)) - _, err := r.updateAndFetchLatest(ctx, flow) +func (r *NifiParameterContextReconciler) removeFinalizer(ctx context.Context, paramCtxt *v1alpha1.NifiParameterContext) error { + r.Log.V(5).Info(fmt.Sprintf("Removing finalizer for NifiParameterContext %s", paramCtxt.Name)) + paramCtxt.SetFinalizers(util.StringSliceRemove(paramCtxt.GetFinalizers(), parameterContextFinalizer)) + _, err := r.updateAndFetchLatest(ctx, paramCtxt) return err } diff --git a/controllers/nifiregistryclient_controller.go b/controllers/nifiregistryclient_controller.go index 5de1bd6f8b..a005baa924 100644 --- a/controllers/nifiregistryclient_controller.go +++ b/controllers/nifiregistryclient_controller.go @@ -146,6 +146,13 @@ func (r *NifiRegistryClientReconciler) Reconcile(ctx context.Context, req ctrl.R r.Recorder.Event(instance, corev1.EventTypeWarning, "ReferenceClusterError", fmt.Sprintf("Failed to create HTTP client for the referenced cluster : %s in %s", instance.Spec.ClusterRef.Name, clusterRef.Namespace)) + // the cluster is gone, so just remove the finalizer + if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) { + if err = r.removeFinalizer(ctx, instance); err != nil { + return RequeueWithError(r.Log, fmt.Sprintf("failed to remove finalizer from NifiRegistryClient %s", instance.Name), err) + } + return Reconciled() + } // the cluster does not exist - should have been caught pre-flight return RequeueWithError(r.Log, "failed to create HTTP client the for referenced cluster", err) } @@ -291,8 +298,7 @@ func (r *NifiRegistryClientReconciler) updateAndFetchLatest(ctx context.Context, func (r *NifiRegistryClientReconciler) checkFinalizers(ctx context.Context, reqLogger logr.Logger, registryClient *v1alpha1.NifiRegistryClient, config *clientconfig.NifiConfig) (reconcile.Result, error) { - - reqLogger.Info("NiFi registry client is marked for deletion") + reqLogger.Info(fmt.Sprintf("NiFi registry client %s is marked for deletion", registryClient.Name)) var err error if util.StringSliceContains(registryClient.GetFinalizers(), registryClientFinalizer) { if err = r.finalizeNifiRegistryClient(reqLogger, registryClient, config); err != nil { @@ -306,6 +312,7 @@ func (r *NifiRegistryClientReconciler) checkFinalizers(ctx context.Context, reqL } func (r *NifiRegistryClientReconciler) removeFinalizer(ctx context.Context, registryClient *v1alpha1.NifiRegistryClient) error { + r.Log.V(5).Info(fmt.Sprintf("Removing finalizer for NifiRegistryClient %s", registryClient.Name)) registryClient.SetFinalizers(util.StringSliceRemove(registryClient.GetFinalizers(), registryClientFinalizer)) _, err := r.updateAndFetchLatest(ctx, registryClient) return err diff --git a/controllers/nifiuser_controller.go b/controllers/nifiuser_controller.go index 0c7f660b91..3013478056 100644 --- a/controllers/nifiuser_controller.go +++ b/controllers/nifiuser_controller.go @@ -225,6 +225,13 @@ func (r *NifiUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c r.Recorder.Event(instance, corev1.EventTypeWarning, "ReferenceClusterError", fmt.Sprintf("Failed to create HTTP client for the referenced cluster : %s in %s", instance.Spec.ClusterRef.Name, clusterRef.Namespace)) + // the cluster is gone, so just remove the finalizer + if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) { + if err = r.removeFinalizer(ctx, instance); err != nil { + return RequeueWithError(r.Log, fmt.Sprintf("failed to remove finalizer from NifiUser %s", instance.Name), err) + } + return Reconciled() + } // the cluster does not exist - should have been caught pre-flight return RequeueWithError(r.Log, "failed to create HTTP client the for referenced cluster", err) } @@ -384,7 +391,7 @@ func (r *NifiUserReconciler) updateAndFetchLatest(ctx context.Context, user *v1a } func (r *NifiUserReconciler) checkFinalizers(ctx context.Context, user *v1alpha1.NifiUser, config *clientconfig.NifiConfig) (reconcile.Result, error) { - r.Log.Info("NiFi user is marked for deletion") + r.Log.Info(fmt.Sprintf("NiFi user %s is marked for deletion", user.Name)) var err error if util.StringSliceContains(user.GetFinalizers(), userFinalizer) { if err = r.finalizeNifiUser(user, config); err != nil { @@ -399,6 +406,7 @@ func (r *NifiUserReconciler) checkFinalizers(ctx context.Context, user *v1alpha1 } func (r *NifiUserReconciler) removeFinalizer(ctx context.Context, user *v1alpha1.NifiUser) error { + r.Log.V(5).Info(fmt.Sprintf("Removing finalizer for NifiUser %s", user.Name)) user.SetFinalizers(util.StringSliceRemove(user.GetFinalizers(), userFinalizer)) _, err := r.updateAndFetchLatest(ctx, user) return err @@ -408,12 +416,12 @@ func (r *NifiUserReconciler) finalizeNifiUser(user *v1alpha1.NifiUser, config *c if err := usercli.RemoveUser(user, config); err != nil { return err } - r.Log.Info("Delete user") + r.Log.Info(fmt.Sprintf("Deleted user %s", user.Name)) return nil } func (r *NifiUserReconciler) addFinalizer(user *v1alpha1.NifiUser) { - r.Log.Info("Adding Finalizer for the NifiUser") + r.Log.Info(fmt.Sprintf("Adding Finalizer for the NifiUser %s", user.Name)) user.SetFinalizers(append(user.GetFinalizers(), userFinalizer)) return } diff --git a/controllers/nifiusergroup_controller.go b/controllers/nifiusergroup_controller.go index 394b0e773d..6aae240ac6 100644 --- a/controllers/nifiusergroup_controller.go +++ b/controllers/nifiusergroup_controller.go @@ -188,6 +188,13 @@ func (r *NifiUserGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques r.Recorder.Event(instance, corev1.EventTypeWarning, "ReferenceClusterError", fmt.Sprintf("Failed to create HTTP client for the referenced cluster : %s in %s", instance.Spec.ClusterRef.Name, clusterRef.Namespace)) + // the cluster is gone, so just remove the finalizer + if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) { + if err = r.removeFinalizer(ctx, instance); err != nil { + return RequeueWithError(r.Log, fmt.Sprintf("failed to remove finalizer from NifiUserGroup %s", instance.Name), err) + } + return Reconciled() + } // the cluster does not exist - should have been caught pre-flight return RequeueWithError(r.Log, "failed to create HTTP client the for referenced cluster", err) } @@ -328,8 +335,7 @@ func (r *NifiUserGroupReconciler) updateAndFetchLatest(ctx context.Context, func (r *NifiUserGroupReconciler) checkFinalizers(ctx context.Context, userGroup *v1alpha1.NifiUserGroup, users []*v1alpha1.NifiUser, config *clientconfig.NifiConfig) (reconcile.Result, error) { - - r.Log.Info("NiFi user group is marked for deletion") + r.Log.Info(fmt.Sprintf("NiFi user group %s is marked for deletion", userGroup.Name)) var err error if util.StringSliceContains(userGroup.GetFinalizers(), userGroupFinalizer) { if err = r.finalizeNifiNifiUserGroup(userGroup, users, config); err != nil { @@ -343,6 +349,7 @@ func (r *NifiUserGroupReconciler) checkFinalizers(ctx context.Context, userGroup } func (r *NifiUserGroupReconciler) removeFinalizer(ctx context.Context, userGroup *v1alpha1.NifiUserGroup) error { + r.Log.V(5).Info(fmt.Sprintf("Removing finalizer for NifiUserGroup %s", userGroup.Name)) userGroup.SetFinalizers(util.StringSliceRemove(userGroup.GetFinalizers(), userGroupFinalizer)) _, err := r.updateAndFetchLatest(ctx, userGroup) return err From 5d2073ae2f3f8dcde69c7101c20d99de4e728ea2 Mon Sep 17 00:00:00 2001 From: Michael H Date: Fri, 10 Jun 2022 09:59:16 +0100 Subject: [PATCH 12/12] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7386ceaa7c..43a96fdb5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ - [PR #76](https://github.com/konpyutaika/nifikop/pull/88) - **[Operator/NiFiCluster]** Re-ordering config out of sync steps. - [PR #93](https://github.com/konpyutaika/nifikop/pull/93) - **[Documentation]** Remove serviceAnnotations mentions and fix docs. - +- [PR #101](https://github.com/konpyutaika/nifikop/pull/101) - **[Operator]** Handle finalizer removal case where `NifiCluster` is aggressively torn down and pods are no longer available to communicate with. ## v0.10.0