Skip to content

Commit

Permalink
Merge pull request #95 from konpyutaika/feature/takeover_parametercon…
Browse files Browse the repository at this point in the history
…text

Allow the operator to take over existing parameter context
  • Loading branch information
erdrix authored May 18, 2022
2 parents 68ef998 + 4fc7797 commit 697f59f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 #95](https://github.com/konpyutaika/nifikop/pull/95) - **[Operator/NiFiParameterContext]** Allow the operator to take over existing parameter context.
- [PR #96](https://github.com/konpyutaika/nifikop/pull/96) - **[Operator/NifiCluster]** Add ability to specify pod priority class

### Changed
Expand Down
9 changes: 9 additions & 0 deletions api/v1alpha1/nifiparametercontext_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 take over an existing parameter context if its name is the same.
DisableTakeOver *bool `json:"disableTakeOver,omitempty"`
}

type Parameter struct {
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ spec:
description:
description: the Description of the Parameter Context.
type: string
disableTakeOver:
description: whether or not the operator should take over an existing
parameter context if its name is the same.
type: boolean
parameters:
description: a list of non-sensitive Parameters.
items:
Expand Down
25 changes: 20 additions & 5 deletions controllers/nifiparametercontext_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -223,9 +224,23 @@ 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)
var status *v1alpha1.NifiParameterContextStatus

status, err = parametercontext.FindParameterContextByName(instance, clientConfig)
if err != nil {
return RequeueWithError(r.Log, "failure creating parameter context", err)
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)
}
}

instance.Status = *status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ spec:
description:
description: the Description of the Parameter Context.
type: string
disableTakeOver:
description: whether or not the operator should take over an existing
parameter context if its name is the same.
type: boolean
parameters:
description: a list of non-sensitive Parameters.
items:
Expand Down
27 changes: 27 additions & 0 deletions pkg/clientwrappers/parametercontext/parametercontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
8 changes: 5 additions & 3 deletions pkg/nificlient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions pkg/nificlient/parametercontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions site/docs/5_references/4_nifi_parameter_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 take over an existing parameter context if its name is the same. |No| - |
## NifiParameterContextStatus
Expand Down

0 comments on commit 697f59f

Please sign in to comment.