<commit_before>// Copyright 2018 Authors of Cilium
//
// 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 utils

import (
	"fmt"
	"reflect"
	"sync"
	"time"

	"github.com/cilium/cilium/pkg/controller"
	"github.com/cilium/cilium/pkg/serializer"
	"github.com/cilium/cilium/pkg/versioned"

	"github.com/prometheus/client_golang/prometheus"
	"k8s.io/api/core/v1"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/cache"
)

var (
	// k8sSyncCM has all controllers that are in charge of syncing up the
	// watched objects with Kubernetes API-server.
	k8sSyncCM = controller.NewManager()

	// listers maps an interface to a lister.
	listers = map[reflect.Type]lister{}
	// equals maps an interface to an equal function.
	equals = map[reflect.Type]versioned.DeepEqualFunc{}
	// casts maps an interface to a castToDeepCopy function.
	casts = map[reflect.Type]castToDeepCopy{}
	// resourcers maps an interface to a resource name (string).
	resourcers = map[reflect.Type]string{}
)

// castToDeepCopy returns the interface passed deep copied into its own object.
type castToDeepCopy func(i interface{}) meta_v1.Object

// lister returns a function that, when called, returns a versioned.Map
// of all objects the lister function can retrieve.
type lister func(client interface{}) func() (versioned.Map, error)

// ControllerSyncer implements the cache.Controller interface, in particular
// the HasSynced method with the help of our own ResourceEventHandler
// implementation.
type ControllerSyncer struct {
	c   cache.Controller
	reh ResourceEventHandler
}

// Run starts the controller, which will be stopped when stopCh is closed.
func (c *ControllerSyncer) Run(stopCh <-chan struct{}) {
	c.c.Run(stopCh)
}

// HasSynced returns true if the controller has synced and the resource event
// handler has handled all requests.
func (c *ControllerSyncer) HasSynced() bool {
	return c.reh.HasSynced()
}

// LastSyncResourceVersion is the resource version observed when last synced
// with the underlying store.
func (c *ControllerSyncer) LastSyncResourceVersion() string {
	return c.c.LastSyncResourceVersion()
}

// ResourceEventHandler is a wrapper for the cache.ResourceEventHandler
// interface with the addition of the HasSynced() bool method which allows
// the caller to know if all events were processed by the ResourceEventHandler.
type ResourceEventHandler interface {
	cache.ResourceEventHandler
	HasSynced() bool
}

// ResourceEventHandlerSyncer implements the ResourceEventHandler
type ResourceEventHandlerSyncer struct {
	// reh is the cache.ResourceEventHandler underlying implementation.<commit_msg>k8s/utils: make the ControllerSynced fields public<commit_after>// Copyright 2018 Authors of Cilium
//
// 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 utils

import (
	"fmt"
	"reflect"
	"sync"
	"time"

	"github.com/cilium/cilium/pkg/controller"
	"github.com/cilium/cilium/pkg/serializer"
	"github.com/cilium/cilium/pkg/versioned"

	"github.com/prometheus/client_golang/prometheus"
	"k8s.io/api/core/v1"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/cache"
)

var (
	// k8sSyncCM has all controllers that are in charge of syncing up the
	// watched objects with Kubernetes API-server.
	k8sSyncCM = controller.NewManager()

	// listers maps an interface to a lister.
	listers = map[reflect.Type]lister{}
	// equals maps an interface to an equal function.
	equals = map[reflect.Type]versioned.DeepEqualFunc{}
	// casts maps an interface to a castToDeepCopy function.
	casts = map[reflect.Type]castToDeepCopy{}
	// resourcers maps an interface to a resource name (string).
	resourcers = map[reflect.Type]string{}
)

// castToDeepCopy returns the interface passed deep copied into its own object.
type castToDeepCopy func(i interface{}) meta_v1.Object

// lister returns a function that, when called, returns a versioned.Map
// of all objects the lister function can retrieve.
type lister func(client interface{}) func() (versioned.Map, error)

// ControllerSyncer implements the cache.Controller interface, in particular
// the HasSynced method with the help of our own ResourceEventHandler
// implementation.
type ControllerSyncer struct {
	Controller           cache.Controller
	ResourceEventHandler ResourceEventHandler
}

// Run starts the controller, which will be stopped when stopCh is closed.
func (c *ControllerSyncer) Run(stopCh <-chan struct{}) {
	c.Controller.Run(stopCh)
}

// HasSynced returns true if the controller has synced and the resource event
// handler has handled all requests.
func (c *ControllerSyncer) HasSynced() bool {
	return c.ResourceEventHandler.HasSynced()
}

// LastSyncResourceVersion is the resource version observed when last synced
// with the underlying store.
func (c *ControllerSyncer) LastSyncResourceVersion() string {
	return c.Controller.LastSyncResourceVersion()
}

// ResourceEventHandler is a wrapper for the cache.ResourceEventHandler
// interface with the addition of the HasSynced() bool method which allows
// the caller to know if all events were processed by the ResourceEventHandler.
type ResourceEventHandler interface {
	cache.ResourceEventHandler
	HasSynced() bool
}

// ResourceEventHandlerSyncer implements the ResourceEventHandler
type ResourceEventHandlerSyncer struct {
	// reh is the cache.ResourceEventHandler underlying implementation.
{'fraction_non_alphanumeric': 0.08781086889775867, 'fraction_numerical': 0.005987104697574455, 'mean_word_length': 4.335790335790335, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3186110', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1851, 'n_words': 682}