Deployments to work with specific namespaces instead of all namespaces (#2635)

* remove namespaceAll in deployments
* remove GetNamespace function from informer
* added logger and improvement for naming
* move reaper logic to utils

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Shubham Bansal
2022-11-29 17:09:51 +05:30
committed by GitHub
co-authored by Sanket Sudake
parent 0aec9e139e
commit 526b5f0beb
11 changed files with 366 additions and 241 deletions
+3 -28
View File
@@ -1,8 +1,6 @@
package utils
import (
"os"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -13,37 +11,14 @@ import (
"k8s.io/client-go/tools/cache"
metricsapi "k8s.io/metrics/pkg/apis/metrics"
v1 "github.com/fission/fission/pkg/apis/core/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
"github.com/fission/fission/pkg/generated/clientset/versioned"
genInformer "github.com/fission/fission/pkg/generated/informers/externalversions"
)
const additionalNamespaces string = "FISSION_RESOURCE_NAMESPACES"
func GetNamespaces() []string {
envValue := os.Getenv(additionalNamespaces)
if len(envValue) == 0 {
return []string{
metav1.NamespaceAll,
}
}
informerNS := make([]string, 0)
lstNamespaces := strings.Split(envValue, ",")
for _, namespace := range lstNamespaces {
//check to handle string with additional comma at the end of string. eg- ns1,ns2,
if namespace != "" {
informerNS = append(informerNS, namespace)
}
}
return informerNS
}
func GetInformersForNamespaces(client versioned.Interface, defaultSync time.Duration, kind string) map[string]cache.SharedIndexInformer {
informers := make(map[string]cache.SharedIndexInformer)
for _, ns := range GetNamespaces() {
for _, ns := range DefaultNSResolver().FissionResourceNS {
factory := genInformer.NewFilteredSharedInformerFactory(client, defaultSync, ns, nil).Core().V1()
switch kind {
case fv1.CanaryConfigResource:
@@ -79,8 +54,8 @@ func GetInformerFactoryByReadyPod(client kubernetes.Interface, namespace string,
return informerFactory, nil
}
func GetInformerFactoryByExecutor(client kubernetes.Interface, executorType v1.ExecutorType, defaultResync time.Duration) (k8sInformers.SharedInformerFactory, error) {
executorLabel, err := labels.NewRequirement(v1.EXECUTOR_TYPE, selection.DoubleEquals, []string{string(executorType)})
func GetInformerFactoryByExecutor(client kubernetes.Interface, executorType fv1.ExecutorType, defaultResync time.Duration) (k8sInformers.SharedInformerFactory, error) {
executorLabel, err := labels.NewRequirement(fv1.EXECUTOR_TYPE, selection.DoubleEquals, []string{string(executorType)})
if err != nil {
return nil, err
}
+105 -8
View File
@@ -2,21 +2,38 @@ package utils
import (
"os"
"strings"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fission/fission/pkg/utils/loggerfactory"
)
const (
ENV_FUNCTION_NAMESPACE string = "FISSION_FUNCTION_NAMESPACE"
ENV_BUILDER_NAMESPACE string = "FISSION_BUILDER_NAMESPACE"
ENV_DEFAULT_NAMESPACE string = "FISSION_DEFAULT_NAMESPACE"
ENV_FUNCTION_NAMESPACE string = "FISSION_FUNCTION_NAMESPACE"
ENV_BUILDER_NAMESPACE string = "FISSION_BUILDER_NAMESPACE"
ENV_DEFAULT_NAMESPACE string = "FISSION_DEFAULT_NAMESPACE"
ENV_ADDITIONAL_NAMESPACE string = "FISSION_RESOURCE_NAMESPACES"
)
type NamespaceResolver struct {
FunctionNamespace string
BuiderNamespace string
DefaultNamespace string
}
type (
NamespaceResolver struct {
FunctionNamespace string
BuiderNamespace string
DefaultNamespace string
FissionResourceNS map[string]string
Logger *zap.Logger
}
options struct {
functionNS bool
builderNS bool
defaultNs bool
}
option func(options *options) *options
)
var nsResolver *NamespaceResolver
@@ -25,7 +42,87 @@ func init() {
FunctionNamespace: os.Getenv(ENV_FUNCTION_NAMESPACE),
BuiderNamespace: os.Getenv(ENV_BUILDER_NAMESPACE),
DefaultNamespace: os.Getenv(ENV_DEFAULT_NAMESPACE),
FissionResourceNS: getNamespaces(),
Logger: loggerfactory.GetLogger(),
}
nsResolver.Logger.Debug("namespaces", zap.String("function_namespace", nsResolver.FunctionNamespace),
zap.String("builder_namespace", nsResolver.BuiderNamespace),
zap.String("default_namespace", nsResolver.DefaultNamespace),
zap.Any("fission_resource_namespace", listNamespaces(nsResolver.FissionResourceNS)))
}
// listNamespaces => convert namespaces from map to slice
func listNamespaces(namespaces map[string]string) []string {
ns := make([]string, 0)
for _, namespace := range namespaces {
ns = append(ns, namespace)
}
return ns
}
func WithBuilderNs() option {
return func(options *options) *options {
options.builderNS = true
return options
}
}
func WithFunctionNs() option {
return func(options *options) *options {
options.functionNS = true
return options
}
}
func WithDefaultNs() option {
return func(options *options) *options {
options.defaultNs = true
return options
}
}
func (nsr *NamespaceResolver) FissionNSWithOptions(option ...option) map[string]string {
var options options
for _, opt := range option {
options = *opt(&options)
}
fissionResourceNS := make(map[string]string)
for k, v := range nsr.FissionResourceNS {
fissionResourceNS[k] = v
}
if options.functionNS && nsr.FunctionNamespace != "" {
fissionResourceNS[nsr.FunctionNamespace] = nsr.FunctionNamespace
}
if options.builderNS && nsr.BuiderNamespace != "" {
fissionResourceNS[nsr.BuiderNamespace] = nsr.BuiderNamespace
}
if options.defaultNs && nsr.DefaultNamespace != "" {
fissionResourceNS[nsr.DefaultNamespace] = nsr.DefaultNamespace
}
nsr.Logger.Debug("fission resource namespaces", zap.Any("namespaces", listNamespaces(fissionResourceNS)))
return fissionResourceNS
}
func getNamespaces() map[string]string {
envValue := os.Getenv(ENV_ADDITIONAL_NAMESPACE)
if len(envValue) == 0 {
return map[string]string{
metav1.NamespaceDefault: metav1.NamespaceDefault,
}
}
lstNamespaces := strings.Split(envValue, ",")
namespaces := make(map[string]string, len(lstNamespaces))
for _, namespace := range lstNamespaces {
//check to handle string with additional comma at the end of string. eg- ns1,ns2,
if namespace != "" {
namespaces[namespace] = namespace
}
}
return namespaces
}
func (nsr *NamespaceResolver) GetBuilderNS(namespace string) string {
+3 -1
View File
@@ -1,6 +1,8 @@
package utils
import "testing"
import (
"testing"
)
func TestNamespaceResolver(t *testing.T) {
t.Run("GetBuilderNS", func(t *testing.T) {