<commit_before>/*
Copyright 2019 The Kubernetes Authors.

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 tenantnamespace

import (
	"context"
	"fmt"

	tenancyv1alpha1 "github.com/multi-tenancy/tenant/pkg/apis/tenancy/v1alpha1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/util/retry"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/source"
)

var log = logf.Log.WithName("controller")

const (
	// TenantAdminNamespaceAnnotation is the key for tenantAdminNamespace annotation
	TenantAdminNamespaceAnnotation = "x-k8s.io/tenantAdminNamespace"
)

// Add creates a new TenantNamespace Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
	return add(mgr, newReconciler(mgr))
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
	return &ReconcileTenantNamespace{Client: mgr.GetClient(), scheme: mgr.GetScheme()}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
	// Create a new controller
	c, err := controller.New("tenantnamespace-controller", mgr, controller.Options{Reconciler: r})
	if err != nil {
		return err
	}

	// Watch for changes to TenantNamespace
	err = c.Watch(&source.Kind{Type: &tenancyv1alpha1.TenantNamespace{}}, &handler.EnqueueRequestForObject{})
	if err != nil {
		return err
	}

	// Watch for changes to namespaces
	err = c.Watch(&source.Kind{Type: &corev1.Namespace{}}, &enqueueTenantNamespace{})
	if err != nil {
		return err
	}
	return nil
}

var _ reconcile.Reconciler = &ReconcileTenantNamespace{}

// ReconcileTenantNamespace reconciles a TenantNamespace object
type ReconcileTenantNamespace struct {
	client.Client
	scheme *runtime.Scheme
}

// Find the tenant namespace name based on prefix requirement
func (r *ReconcileTenantNamespace) getNamespaceName(prefix bool, instance *tenancyv1alpha1.TenantNamespace) string {
	name := instance.Spec.Name
	if name == "" {
		name = instance.Name
	}
	if prefix {
		name = instance.Namespace + "-" + name
	}
	return name
}

// Add a ownerReference and tenant admin namespace annotation to input namespace
func (r *ReconcileTenantNamespace) updateNamespace(ns *corev1.Namespace, tenantAdminNamespaceName *string, ownerRef *metav1.OwnerReference) error {
	nsClone := ns.DeepCopy()
	err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
		nsClone.OwnerReferences = append(nsClone.OwnerReferences, *ownerRef)
		if nsClone.Annotations == nil {
			nsClone.Annotations = make(map[string]string)
			nsClone.Annotations[TenantAdminNamespaceAnnotation] = *tenantAdminNamespaceName
		}
		updateErr := r.Update(context.TODO(), nsClone)
		if updateErr == nil {
			return nil
		}
		key := types.NamespacedName{
			Name: nsClone.Name,
		}
		if err := r.Get(context.TODO(), key, nsClone); err != nil {
			log.Info("Fail to fetch namespace on update failure", "namespace", nsClone.Name)
		}
		return updateErr
	})
	return err
}<commit_msg>Fix a small bug (#81)<commit_after>/*
Copyright 2019 The Kubernetes Authors.

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 tenantnamespace

import (
	"context"
	"fmt"

	tenancyv1alpha1 "github.com/multi-tenancy/tenant/pkg/apis/tenancy/v1alpha1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/util/retry"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/source"
)

var log = logf.Log.WithName("controller")

const (
	// TenantAdminNamespaceAnnotation is the key for tenantAdminNamespace annotation
	TenantAdminNamespaceAnnotation = "x-k8s.io/tenantAdminNamespace"
)

// Add creates a new TenantNamespace Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
	return add(mgr, newReconciler(mgr))
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
	return &ReconcileTenantNamespace{Client: mgr.GetClient(), scheme: mgr.GetScheme()}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
	// Create a new controller
	c, err := controller.New("tenantnamespace-controller", mgr, controller.Options{Reconciler: r})
	if err != nil {
		return err
	}

	// Watch for changes to TenantNamespace
	err = c.Watch(&source.Kind{Type: &tenancyv1alpha1.TenantNamespace{}}, &handler.EnqueueRequestForObject{})
	if err != nil {
		return err
	}

	// Watch for changes to namespaces
	err = c.Watch(&source.Kind{Type: &corev1.Namespace{}}, &enqueueTenantNamespace{})
	if err != nil {
		return err
	}
	return nil
}

var _ reconcile.Reconciler = &ReconcileTenantNamespace{}

// ReconcileTenantNamespace reconciles a TenantNamespace object
type ReconcileTenantNamespace struct {
	client.Client
	scheme *runtime.Scheme
}

// Find the tenant namespace name based on prefix requirement
func (r *ReconcileTenantNamespace) getNamespaceName(prefix bool, instance *tenancyv1alpha1.TenantNamespace) string {
	name := instance.Spec.Name
	if name == "" {
		name = instance.Name
	}
	if prefix {
		name = instance.Namespace + "-" + name
	}
	return name
}

// Add a ownerReference and tenant admin namespace annotation to input namespace
func (r *ReconcileTenantNamespace) updateNamespace(ns *corev1.Namespace, tenantAdminNamespaceName *string, ownerRef *metav1.OwnerReference) error {
	nsClone := ns.DeepCopy()
	err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
		nsClone.OwnerReferences = append(nsClone.OwnerReferences, *ownerRef)
		if nsClone.Annotations == nil {
			nsClone.Annotations = make(map[string]string)
		}
		nsClone.Annotations[TenantAdminNamespaceAnnotation] = *tenantAdminNamespaceName
		updateErr := r.Update(context.TODO(), nsClone)
		if updateErr == nil {
			return nil
		}
		key := types.NamespacedName{
			Name: nsClone.Name,
		}
		if err := r.Get(context.TODO(), key, nsClone); err != nil {
			log.Info("Fail to fetch namespace on update failure", "namespace", nsClone.Name)
		}
		return updateErr
	})
	return err
}
{'fraction_non_alphanumeric': 0.10579439252336449, 'fraction_numerical': 0.009470404984423676, 'mean_word_length': 4.676096181046676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3633864', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2626, 'n_words': 700}