This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
controller_common.go
173 lines (155 loc) · 5.82 KB
/
controller_common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2020 Orange SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package apis
package controllers
import (
"fmt"
"github.com/Orange-OpenSource/nifikop/pkg/util/clientconfig"
"time"
"emperror.dev/errors"
"github.com/Orange-OpenSource/nifikop/api/v1alpha1"
"github.com/Orange-OpenSource/nifikop/pkg/errorfactory"
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// clusterRefLabel is the label key used for referencing NifiUsers/NifiDataflow
// to a NifiCluster
var ClusterRefLabel = "nifiCluster"
// requeueWithError is a convenience wrapper around logging an error message
// separate from the stacktrace and then passing the error through to the controller
// manager
func RequeueWithError(logger logr.Logger, msg string, err error) (reconcile.Result, error) {
// Info log the error message and then let the reconciler dump the stacktrace
logger.Info(msg)
return reconcile.Result{}, err
}
func Requeue() (reconcile.Result, error) {
return reconcile.Result{Requeue: true}, nil
}
func RequeueAfter(time time.Duration) (reconcile.Result, error) {
return reconcile.Result{Requeue: true, RequeueAfter: time}, nil
}
// reconciled returns an empty result with nil error to signal a successful reconcile
// to the controller manager
func Reconciled() (reconcile.Result, error) {
return reconcile.Result{}, nil
}
// clusterLabelString returns the label value for a cluster reference
func ClusterLabelString(cluster *v1alpha1.NifiCluster) string {
return fmt.Sprintf("%s.%s", cluster.Name, cluster.Namespace)
}
// checkNodeConnectionError is a convenience wrapper for returning from common
// node connection errors
func CheckNodeConnectionError(logger logr.Logger, err error) (ctrl.Result, error) {
switch errors.Cause(err).(type) {
case errorfactory.NodesUnreachable:
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.NodesNotReady:
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.ResourceNotReady:
logger.Info("Needed resource for node connection not found, may not be ready")
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Duration(5) * time.Second,
}, nil
default:
return RequeueWithError(logger, err.Error(), err)
}
}
// applyClusterRefLabel ensures a map of labels contains a reference to a parent nifi cluster
func ApplyClusterRefLabel(cluster *v1alpha1.NifiCluster, labels map[string]string) map[string]string {
labelValue := ClusterLabelString(cluster)
if labels == nil {
labels = make(map[string]string, 0)
}
if label, ok := labels[ClusterRefLabel]; ok {
if label != labelValue {
labels[ClusterRefLabel] = labelValue
}
} else {
labels[ClusterRefLabel] = labelValue
}
return labels
}
// applyClusterRefLabel ensures a map of labels contains a reference to a parent nifi cluster
func ApplyClusterReferenceLabel(cluster clientconfig.ClusterConnect, labels map[string]string) map[string]string {
labelValue := cluster.ClusterLabelString()
if labels == nil {
labels = make(map[string]string, 0)
}
if label, ok := labels[ClusterRefLabel]; ok {
if label != labelValue {
labels[ClusterRefLabel] = labelValue
}
} else {
labels[ClusterRefLabel] = labelValue
}
return labels
}
// getClusterRefNamespace returns the expected namespace for a Nifi cluster
// referenced by a user/dataflow CR. It takes the namespace of the CR as the first
// argument and the reference itself as the second.
func GetClusterRefNamespace(ns string, ref v1alpha1.ClusterReference) string {
clusterNamespace := ref.Namespace
if clusterNamespace == "" {
return ns
}
return clusterNamespace
}
// GetRegistryClientRefNamespace returns the expected namespace for a Nifi registry client
// referenced by a dataflow CR. It takes the namespace of the CR as the first
// argument and the reference itself as the second.
func GetRegistryClientRefNamespace(ns string, ref v1alpha1.RegistryClientReference) string {
registryClientNamespace := ref.Namespace
if registryClientNamespace == "" {
return ns
}
return registryClientNamespace
}
// GetParameterContextRefNamespace returns the expected namespace for a Nifi parameter context
// referenced by a dataflow CR. It takes the namespace of the CR as the first
// argument and the reference itself as the second.
func GetParameterContextRefNamespace(ns string, ref v1alpha1.ParameterContextReference) string {
parameterContextNamespace := ref.Namespace
if parameterContextNamespace == "" {
return ns
}
return parameterContextNamespace
}
// GetSecretRefNamespace returns the expected namespace for a Nifi secret
// referenced by a parameter context CR. It takes the namespace of the CR as the first
// argument and the reference itself as the second.
func GetSecretRefNamespace(ns string, ref v1alpha1.SecretReference) string {
secretNamespace := ref.Namespace
if secretNamespace == "" {
return ns
}
return secretNamespace
}
// GetUserRefNamespace returns the expected namespace for a Nifi user
// referenced by a parameter context CR. It takes the namespace of the CR as the first
// argument and the reference itself as the second.
func GetUserRefNamespace(ns string, ref v1alpha1.UserReference) string {
userNamespace := ref.Namespace
if userNamespace == "" {
return ns
}
return userNamespace
}