mirror of https://github.com/grafana/grafana
Grafana app platform: an aggregator cmd and package (#79948)
parent
26f54a2fc7
commit
48612063dd
@ -0,0 +1,12 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
set -o errexit |
||||
set -o nounset |
||||
set -o pipefail |
||||
|
||||
rm -rf data/grafana-aggregator |
||||
|
||||
mkdir -p data/grafana-aggregator |
||||
openssl req -nodes -new -x509 -keyout data/grafana-aggregator/ca.key -out data/grafana-aggregator/ca.crt |
||||
openssl req -out data/grafana-aggregator/client.csr -new -newkey rsa:4096 -nodes -keyout data/grafana-aggregator/client.key -subj "/CN=development/O=system:masters" |
||||
openssl x509 -req -days 365 -in data/grafana-aggregator/client.csr -CA data/grafana-aggregator/ca.crt -CAkey data/grafana-aggregator/ca.key -set_serial 01 -sha256 -out data/grafana-aggregator/client.crt |
@ -0,0 +1,44 @@ |
||||
# aggregator |
||||
|
||||
This is a package that is intended to power the aggregation of microservices within Grafana. The concept |
||||
as well as implementation is largely borrowed from [kube-aggregator](https://github.com/kubernetes/kube-aggregator). |
||||
|
||||
## Why aggregate services? |
||||
|
||||
Grafana's future architecture will entail the same API Server design as that of Kubernetes API Servers. API Servers |
||||
provide a standard way of stitching together API Groups through discovery and shared routing patterns that allows |
||||
them to aggregate to a parent API Server in a seamless manner. Since we desire to break Grafana monolith up into |
||||
more functionally divided microservices, aggregation does the job of still being able to provide these services |
||||
under a single address. Other benefits of aggregation include free health checks and being able to independently |
||||
roll out features for each service without downtime. |
||||
|
||||
To read more about the concept, see |
||||
[here](https://kubernetes.io/docs/tasks/extend-kubernetes/setup-extension-api-server/). |
||||
|
||||
Note that, this aggregation will be a totally internal detail to Grafana. External fully functional APIServers that |
||||
may themselves act as parent API Servers to Grafana will never be made aware of them. Any of the `APIService` |
||||
related to Grafana Groups registered in a real K8s environment will take the address of Grafana's |
||||
parent server (which will bundle grafana-aggregator). |
||||
|
||||
### kube-aggregator versus grafana-aggregator |
||||
|
||||
The `grafana-aggregator` component will work similarly to how `kube-aggregator` works for `kube-apiserver`, the major |
||||
difference being that it doesn't require core V1 APIs such as `Service`. Early on, we decided to not have core V1 |
||||
APIs in the root Grafana API Server. In order to still be able to implement aggregation, we do the following in this Go |
||||
package: |
||||
|
||||
1. We do not start the core shared informer factories as well as any default controllers that utilize them. |
||||
This is achieved using `DisabledPostStartHooks` facility under the GenericAPIServer's RecommendedConfig. |
||||
2. We provide an `externalname` Kind API implementation under `service.grafana.app` group which works functionally |
||||
equivalent to the idea with the same name under `core/v1/Service`. |
||||
3. Lastly, we swap the default available condition controller with the custom one written by us. This one is based on |
||||
our `externalname` (`service.grafana.app`) implementation. We register separate `PostStartHooks` |
||||
using `AddPostStartHookOrDie` on the GenericAPIServer to start the corresponding custom controller as well as |
||||
requisite informer factories for our own `externalname` Kind. |
||||
|
||||
### Gotchas (Pay Attention) |
||||
|
||||
1. `grafana-aggregator` uses file storage under `/tmp`. System restarts won't preserve any configuration. |
||||
1. Ensure any `externalname` and `APIService` configuration is in place post system restarts when developing locally. |
||||
2. Since `grafana-aggregator` outputs configuration (TLS and kubeconfig) that is used in the invocation of aggregated |
||||
servers, ensure you start the aggregated service after launching the aggregator during local development. |
@ -0,0 +1,507 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Provenance-includes-location: https://github.com/kubernetes/kubernetes/blob/master/cmd/kube-apiserver/app/aggregator.go
|
||||
// Provenance-includes-license: Apache-2.0
|
||||
// Provenance-includes-copyright: The Kubernetes Authors.
|
||||
// Provenance-includes-location: https://github.com/kubernetes/kubernetes/blob/master/cmd/kube-apiserver/app/server.go
|
||||
// Provenance-includes-license: Apache-2.0
|
||||
// Provenance-includes-copyright: The Kubernetes Authors.
|
||||
|
||||
package aggregator |
||||
|
||||
import ( |
||||
"crypto/tls" |
||||
"fmt" |
||||
"io" |
||||
"net" |
||||
"net/http" |
||||
"strings" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/spf13/pflag" |
||||
|
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
serviceclientset "github.com/grafana/grafana/pkg/generated/clientset/versioned" |
||||
informersv0alpha1 "github.com/grafana/grafana/pkg/generated/informers/externalversions" |
||||
"github.com/grafana/grafana/pkg/registry/apis/service" |
||||
grafanaAPIServer "github.com/grafana/grafana/pkg/services/grafana-apiserver" |
||||
filestorage "github.com/grafana/grafana/pkg/services/grafana-apiserver/storage/file" |
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/runtime/schema" |
||||
"k8s.io/apimachinery/pkg/runtime/serializer" |
||||
utilnet "k8s.io/apimachinery/pkg/util/net" |
||||
"k8s.io/apimachinery/pkg/util/sets" |
||||
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi" |
||||
genericfeatures "k8s.io/apiserver/pkg/features" |
||||
genericapiserver "k8s.io/apiserver/pkg/server" |
||||
"k8s.io/apiserver/pkg/server/healthz" |
||||
"k8s.io/apiserver/pkg/server/options" |
||||
"k8s.io/apiserver/pkg/server/resourceconfig" |
||||
utilfeature "k8s.io/apiserver/pkg/util/feature" |
||||
"k8s.io/client-go/informers" |
||||
"k8s.io/client-go/kubernetes/fake" |
||||
"k8s.io/client-go/tools/cache" |
||||
"k8s.io/klog/v2" |
||||
v1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" |
||||
v1helper "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/helper" |
||||
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1" |
||||
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver" |
||||
aggregatorscheme "k8s.io/kube-aggregator/pkg/apiserver/scheme" |
||||
apiregistrationclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset" |
||||
apiregistrationclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1" |
||||
apiregistrationInformers "k8s.io/kube-aggregator/pkg/client/informers/externalversions/apiregistration/v1" |
||||
"k8s.io/kube-aggregator/pkg/controllers/autoregister" |
||||
aggregatoropenapi "k8s.io/kube-aggregator/pkg/generated/openapi" |
||||
"k8s.io/kube-openapi/pkg/common" |
||||
) |
||||
|
||||
type ExtraOptions struct { |
||||
ProxyClientCertFile string |
||||
ProxyClientKeyFile string |
||||
} |
||||
|
||||
// AggregatorServerOptions contains the state for the aggregator apiserver
|
||||
type AggregatorServerOptions struct { |
||||
Builders []grafanaAPIServer.APIGroupBuilder |
||||
RecommendedOptions *options.RecommendedOptions |
||||
ExtraOptions *ExtraOptions |
||||
AlternateDNS []string |
||||
|
||||
sharedInformerFactory informersv0alpha1.SharedInformerFactory |
||||
|
||||
StdOut io.Writer |
||||
StdErr io.Writer |
||||
} |
||||
|
||||
func NewAggregatorServerOptions(out, errOut io.Writer) *AggregatorServerOptions { |
||||
return &AggregatorServerOptions{ |
||||
StdOut: out, |
||||
StdErr: errOut, |
||||
ExtraOptions: &ExtraOptions{}, |
||||
Builders: []grafanaAPIServer.APIGroupBuilder{ |
||||
service.NewServiceAPIBuilder(), |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) LoadAPIGroupBuilders() error { |
||||
// Install schemas
|
||||
for _, b := range o.Builders { |
||||
if err := b.InstallSchema(aggregatorscheme.Scheme); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) Config(codecs serializer.CodecFactory) (*genericapiserver.RecommendedConfig, error) { |
||||
if err := o.RecommendedOptions.SecureServing.MaybeDefaultWithSelfSignedCerts( |
||||
"localhost", o.AlternateDNS, []net.IP{net.IPv4(127, 0, 0, 1)}, |
||||
); err != nil { |
||||
return nil, fmt.Errorf("error creating self-signed certificates: %v", err) |
||||
} |
||||
|
||||
o.RecommendedOptions.Authentication.RemoteKubeConfigFileOptional = true |
||||
o.RecommendedOptions.Authorization.RemoteKubeConfigFileOptional = true |
||||
|
||||
o.RecommendedOptions.Admission = nil |
||||
|
||||
if o.RecommendedOptions.CoreAPI.CoreAPIKubeconfigPath == "" { |
||||
o.RecommendedOptions.CoreAPI = nil |
||||
} |
||||
|
||||
serverConfig := genericapiserver.NewRecommendedConfig(codecs) |
||||
|
||||
if o.RecommendedOptions.CoreAPI == nil { |
||||
if err := o.ModifiedApplyTo(serverConfig); err != nil { |
||||
return nil, err |
||||
} |
||||
} else { |
||||
if err := o.RecommendedOptions.ApplyTo(serverConfig); err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
return serverConfig, nil |
||||
} |
||||
|
||||
// A copy of ApplyTo in recommended.go, but for >= 0.28, server pkg in apiserver does a bit extra causing
|
||||
// a panic when CoreAPI is set to nil
|
||||
func (o *AggregatorServerOptions) ModifiedApplyTo(config *genericapiserver.RecommendedConfig) error { |
||||
if err := o.RecommendedOptions.Etcd.ApplyTo(&config.Config); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.EgressSelector.ApplyTo(&config.Config); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.Traces.ApplyTo(config.Config.EgressSelector, &config.Config); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.SecureServing.ApplyTo(&config.Config.SecureServing, &config.Config.LoopbackClientConfig); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.Authentication.ApplyTo(&config.Config.Authentication, config.SecureServing, config.OpenAPIConfig); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.Authorization.ApplyTo(&config.Config.Authorization); err != nil { |
||||
return err |
||||
} |
||||
if err := o.RecommendedOptions.Audit.ApplyTo(&config.Config); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// TODO: determine whether we need flow control (API priority and fairness)
|
||||
//if err := o.RecommendedOptions.Features.ApplyTo(&config.Config); err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
if err := o.RecommendedOptions.CoreAPI.ApplyTo(config); err != nil { |
||||
return err |
||||
} |
||||
|
||||
_, err := o.RecommendedOptions.ExtraAdmissionInitializers(config) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) getMergedOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { |
||||
// Add OpenAPI specs for each group+version
|
||||
prerequisiteAPIs := grafanaAPIServer.GetOpenAPIDefinitions(o.Builders)(ref) |
||||
aggregatorAPIs := aggregatoropenapi.GetOpenAPIDefinitions(ref) |
||||
|
||||
for k, v := range prerequisiteAPIs { |
||||
aggregatorAPIs[k] = v |
||||
} |
||||
|
||||
return aggregatorAPIs |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) AddFlags(fs *pflag.FlagSet) { |
||||
if o == nil { |
||||
return |
||||
} |
||||
|
||||
o.RecommendedOptions.AddFlags(fs) |
||||
|
||||
fs.StringVar(&o.ExtraOptions.ProxyClientCertFile, "proxy-client-cert-file", o.ExtraOptions.ProxyClientCertFile, |
||||
"path to proxy client cert file") |
||||
|
||||
fs.StringVar(&o.ExtraOptions.ProxyClientKeyFile, "proxy-client-key-file", o.ExtraOptions.ProxyClientKeyFile, |
||||
"path to proxy client cert file") |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) CreateAggregatorConfig() (*aggregatorapiserver.Config, error) { |
||||
sharedConfig, err := o.Config(aggregatorscheme.Codecs) |
||||
if err != nil { |
||||
klog.Errorf("Error translating server options to config: %s", err) |
||||
return nil, err |
||||
} |
||||
|
||||
commandOptions := *o.RecommendedOptions |
||||
|
||||
// make a shallow copy to let us twiddle a few things
|
||||
// most of the config actually remains the same. We only need to mess with a couple items related to the particulars of the aggregator
|
||||
genericConfig := sharedConfig.Config |
||||
|
||||
genericConfig.PostStartHooks = map[string]genericapiserver.PostStartHookConfigEntry{} |
||||
genericConfig.RESTOptionsGetter = nil |
||||
// prevent generic API server from installing the OpenAPI handler. Aggregator server
|
||||
// has its own customized OpenAPI handler.
|
||||
genericConfig.SkipOpenAPIInstallation = true |
||||
mergedResourceConfig, err := resourceconfig.MergeAPIResourceConfigs(aggregatorapiserver.DefaultAPIResourceConfigSource(), nil, aggregatorscheme.Scheme) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
genericConfig.MergedResourceConfig = mergedResourceConfig |
||||
|
||||
namer := openapinamer.NewDefinitionNamer(aggregatorscheme.Scheme) |
||||
genericConfig.OpenAPIV3Config = genericapiserver.DefaultOpenAPIV3Config(o.getMergedOpenAPIDefinitions, namer) |
||||
genericConfig.OpenAPIV3Config.Info.Title = "Kubernetes" |
||||
genericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(o.getMergedOpenAPIDefinitions, namer) |
||||
genericConfig.OpenAPIConfig.Info.Title = "Kubernetes" |
||||
|
||||
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.StorageVersionAPI) && |
||||
utilfeature.DefaultFeatureGate.Enabled(genericfeatures.APIServerIdentity) { |
||||
// Add StorageVersionPrecondition handler to aggregator-apiserver.
|
||||
// The handler will block write requests to built-in resources until the
|
||||
// target resources' storage versions are up-to-date.
|
||||
genericConfig.BuildHandlerChainFunc = genericapiserver.BuildHandlerChainWithStorageVersionPrecondition |
||||
} |
||||
|
||||
// copy the etcd options so we don't mutate originals.
|
||||
// we assume that the etcd options have been completed already. avoid messing with anything outside
|
||||
// of changes to StorageConfig as that may lead to unexpected behavior when the options are applied.
|
||||
etcdOptions := *commandOptions.Etcd |
||||
etcdOptions.StorageConfig.Codec = aggregatorscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion, |
||||
v1beta1.SchemeGroupVersion, |
||||
servicev0alpha1.SchemeGroupVersion) |
||||
etcdOptions.StorageConfig.EncodeVersioner = runtime.NewMultiGroupVersioner(v1.SchemeGroupVersion, |
||||
schema.GroupKind{Group: v1beta1.GroupName}, |
||||
schema.GroupKind{Group: servicev0alpha1.GROUP}) |
||||
// etcdOptions.StorageConfig.Transport.ServerList = []string{"127.0.0.1:2379"}
|
||||
etcdOptions.SkipHealthEndpoints = true // avoid double wiring of health checks
|
||||
if err := etcdOptions.ApplyTo(&genericConfig); err != nil { |
||||
return nil, err |
||||
} |
||||
genericConfig.RESTOptionsGetter = filestorage.NewRESTOptionsGetter("/tmp/grafana.aggregator", etcdOptions.StorageConfig) |
||||
|
||||
versionedInformers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), 10*time.Minute) |
||||
|
||||
serviceClient, err := serviceclientset.NewForConfig(genericConfig.LoopbackClientConfig) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
o.sharedInformerFactory = informersv0alpha1.NewSharedInformerFactory( |
||||
serviceClient, |
||||
5*time.Minute, // this is effectively used as a refresh interval right now. Might want to do something nicer later on.
|
||||
) |
||||
serviceResolver := NewExternalNameResolver(o.sharedInformerFactory.Service().V0alpha1().ExternalNames().Lister()) |
||||
|
||||
genericConfig.DisabledPostStartHooks = genericConfig.DisabledPostStartHooks.Insert("apiservice-status-available-controller") |
||||
genericConfig.DisabledPostStartHooks = genericConfig.DisabledPostStartHooks.Insert("start-kube-aggregator-informers") |
||||
|
||||
aggregatorConfig := &aggregatorapiserver.Config{ |
||||
GenericConfig: &genericapiserver.RecommendedConfig{ |
||||
Config: genericConfig, |
||||
SharedInformerFactory: versionedInformers, |
||||
ClientConfig: genericConfig.LoopbackClientConfig, |
||||
}, |
||||
ExtraConfig: aggregatorapiserver.ExtraConfig{ |
||||
ProxyClientCertFile: o.ExtraOptions.ProxyClientCertFile, |
||||
ProxyClientKeyFile: o.ExtraOptions.ProxyClientKeyFile, |
||||
// NOTE: while ProxyTransport can be skipped in the configuration, it allows honoring
|
||||
// DISABLE_HTTP2, HTTPS_PROXY and NO_PROXY env vars as needed
|
||||
ProxyTransport: createProxyTransport(), |
||||
}, |
||||
} |
||||
|
||||
aggregatorConfig.ExtraConfig.ServiceResolver = serviceResolver |
||||
|
||||
// we need to clear the poststarthooks so we don't add them multiple times to all the servers (that fails)
|
||||
aggregatorConfig.GenericConfig.PostStartHooks = map[string]genericapiserver.PostStartHookConfigEntry{} |
||||
|
||||
return aggregatorConfig, nil |
||||
} |
||||
|
||||
func (o *AggregatorServerOptions) CreateAggregatorServer(aggregatorConfig *aggregatorapiserver.Config, delegateAPIServer genericapiserver.DelegationTarget) (*aggregatorapiserver.APIAggregator, error) { |
||||
completedConfig := aggregatorConfig.Complete() |
||||
aggregatorServer, err := completedConfig.NewWithDelegate(delegateAPIServer) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
// create controllers for auto-registration
|
||||
apiRegistrationClient, err := apiregistrationclient.NewForConfig(aggregatorConfig.GenericConfig.LoopbackClientConfig) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
autoRegistrationController := autoregister.NewAutoRegisterController(aggregatorServer.APIRegistrationInformers.Apiregistration().V1().APIServices(), apiRegistrationClient) |
||||
apiServices := apiServicesToRegister(delegateAPIServer, autoRegistrationController) |
||||
|
||||
// Imbue all builtin group-priorities onto the aggregated discovery
|
||||
if aggregatorConfig.GenericConfig.AggregatedDiscoveryGroupManager != nil { |
||||
for gv, entry := range apiVersionPriorities { |
||||
aggregatorConfig.GenericConfig.AggregatedDiscoveryGroupManager.SetGroupVersionPriority(metav1.GroupVersion(gv), int(entry.group), int(entry.version)) |
||||
} |
||||
} |
||||
|
||||
err = aggregatorServer.GenericAPIServer.AddPostStartHook("kube-apiserver-autoregistration", func(context genericapiserver.PostStartHookContext) error { |
||||
go func() { |
||||
autoRegistrationController.Run(5, context.StopCh) |
||||
}() |
||||
return nil |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
err = aggregatorServer.GenericAPIServer.AddBootSequenceHealthChecks( |
||||
makeAPIServiceAvailableHealthCheck( |
||||
"autoregister-completion", |
||||
apiServices, |
||||
aggregatorServer.APIRegistrationInformers.Apiregistration().V1().APIServices(), |
||||
), |
||||
) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
apiregistrationClient, err := apiregistrationclientset.NewForConfig(completedConfig.GenericConfig.LoopbackClientConfig) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
availableController, err := NewAvailableConditionController( |
||||
aggregatorServer.APIRegistrationInformers.Apiregistration().V1().APIServices(), |
||||
o.sharedInformerFactory.Service().V0alpha1().ExternalNames(), |
||||
apiregistrationClient.ApiregistrationV1(), |
||||
nil, |
||||
(func() ([]byte, []byte))(nil), |
||||
completedConfig.ExtraConfig.ServiceResolver, |
||||
) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
aggregatorServer.GenericAPIServer.AddPostStartHookOrDie("apiservice-status-override-available-controller", func(context genericapiserver.PostStartHookContext) error { |
||||
// if we end up blocking for long periods of time, we may need to increase workers.
|
||||
go availableController.Run(5, context.StopCh) |
||||
return nil |
||||
}) |
||||
|
||||
aggregatorServer.GenericAPIServer.AddPostStartHookOrDie("start-grafana-aggregator-informers", func(context genericapiserver.PostStartHookContext) error { |
||||
o.sharedInformerFactory.Start(context.StopCh) |
||||
aggregatorServer.APIRegistrationInformers.Start(context.StopCh) |
||||
return nil |
||||
}) |
||||
|
||||
// Install the API Group+version
|
||||
for _, b := range o.Builders { |
||||
g, err := b.GetAPIGroupInfo(aggregatorscheme.Scheme, aggregatorscheme.Codecs, aggregatorConfig.GenericConfig.RESTOptionsGetter) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if g == nil || len(g.PrioritizedVersions) < 1 { |
||||
continue |
||||
} |
||||
err = aggregatorServer.GenericAPIServer.InstallAPIGroup(g) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
return aggregatorServer, nil |
||||
} |
||||
|
||||
func makeAPIService(gv schema.GroupVersion) *v1.APIService { |
||||
apiServicePriority, ok := apiVersionPriorities[gv] |
||||
if !ok { |
||||
// if we aren't found, then we shouldn't register ourselves because it could result in a CRD group version
|
||||
// being permanently stuck in the APIServices list.
|
||||
klog.Infof("Skipping APIService creation for %v", gv) |
||||
return nil |
||||
} |
||||
return &v1.APIService{ |
||||
ObjectMeta: metav1.ObjectMeta{Name: gv.Version + "." + gv.Group}, |
||||
Spec: v1.APIServiceSpec{ |
||||
Group: gv.Group, |
||||
Version: gv.Version, |
||||
GroupPriorityMinimum: apiServicePriority.group, |
||||
VersionPriority: apiServicePriority.version, |
||||
}, |
||||
} |
||||
} |
||||
|
||||
// makeAPIServiceAvailableHealthCheck returns a healthz check that returns healthy
|
||||
// once all of the specified services have been observed to be available at least once.
|
||||
func makeAPIServiceAvailableHealthCheck(name string, apiServices []*v1.APIService, apiServiceInformer apiregistrationInformers.APIServiceInformer) healthz.HealthChecker { |
||||
// Track the auto-registered API services that have not been observed to be available yet
|
||||
pendingServiceNamesLock := &sync.RWMutex{} |
||||
pendingServiceNames := sets.NewString() |
||||
for _, service := range apiServices { |
||||
pendingServiceNames.Insert(service.Name) |
||||
} |
||||
|
||||
// When an APIService in the list is seen as available, remove it from the pending list
|
||||
handleAPIServiceChange := func(service *v1.APIService) { |
||||
pendingServiceNamesLock.Lock() |
||||
defer pendingServiceNamesLock.Unlock() |
||||
if !pendingServiceNames.Has(service.Name) { |
||||
return |
||||
} |
||||
if v1helper.IsAPIServiceConditionTrue(service, v1.Available) { |
||||
pendingServiceNames.Delete(service.Name) |
||||
} |
||||
} |
||||
|
||||
// Watch add/update events for APIServices
|
||||
_, _ = apiServiceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ |
||||
AddFunc: func(obj interface{}) { handleAPIServiceChange(obj.(*v1.APIService)) }, |
||||
UpdateFunc: func(old, new interface{}) { handleAPIServiceChange(new.(*v1.APIService)) }, |
||||
}) |
||||
|
||||
// Don't return healthy until the pending list is empty
|
||||
return healthz.NamedCheck(name, func(r *http.Request) error { |
||||
pendingServiceNamesLock.RLock() |
||||
defer pendingServiceNamesLock.RUnlock() |
||||
if pendingServiceNames.Len() > 0 { |
||||
return fmt.Errorf("missing APIService: %v", pendingServiceNames.List()) |
||||
} |
||||
return nil |
||||
}) |
||||
} |
||||
|
||||
// priority defines group priority that is used in discovery. This controls
|
||||
// group position in the kubectl output.
|
||||
type priority struct { |
||||
// group indicates the order of the group relative to other groups.
|
||||
group int32 |
||||
// version indicates the relative order of the version inside of its group.
|
||||
version int32 |
||||
} |
||||
|
||||
// The proper way to resolve this letting the aggregator know the desired group and version-within-group order of the underlying servers
|
||||
// is to refactor the genericapiserver.DelegationTarget to include a list of priorities based on which APIs were installed.
|
||||
// This requires the APIGroupInfo struct to evolve and include the concept of priorities and to avoid mistakes, the core storage map there needs to be updated.
|
||||
// That ripples out every bit as far as you'd expect, so for 1.7 we'll include the list here instead of being built up during storage.
|
||||
var apiVersionPriorities = map[schema.GroupVersion]priority{ |
||||
{Group: "", Version: "v1"}: {group: 18000, version: 1}, |
||||
// to my knowledge, nothing below here collides
|
||||
{Group: "admissionregistration.k8s.io", Version: "v1"}: {group: 16700, version: 15}, |
||||
{Group: "admissionregistration.k8s.io", Version: "v1beta1"}: {group: 16700, version: 12}, |
||||
{Group: "admissionregistration.k8s.io", Version: "v1alpha1"}: {group: 16700, version: 9}, |
||||
// Append a new group to the end of the list if unsure.
|
||||
// You can use min(existing group)-100 as the initial value for a group.
|
||||
// Version can be set to 9 (to have space around) for a new group.
|
||||
} |
||||
|
||||
func apiServicesToRegister(delegateAPIServer genericapiserver.DelegationTarget, registration autoregister.AutoAPIServiceRegistration) []*v1.APIService { |
||||
apiServices := []*v1.APIService{} |
||||
|
||||
for _, curr := range delegateAPIServer.ListedPaths() { |
||||
if curr == "/api/v1" { |
||||
apiService := makeAPIService(schema.GroupVersion{Group: "", Version: "v1"}) |
||||
registration.AddAPIServiceToSyncOnStart(apiService) |
||||
apiServices = append(apiServices, apiService) |
||||
continue |
||||
} |
||||
|
||||
if !strings.HasPrefix(curr, "/apis/") { |
||||
continue |
||||
} |
||||
// this comes back in a list that looks like /apis/rbac.authorization.k8s.io/v1alpha1
|
||||
tokens := strings.Split(curr, "/") |
||||
if len(tokens) != 4 { |
||||
continue |
||||
} |
||||
|
||||
apiService := makeAPIService(schema.GroupVersion{Group: tokens[2], Version: tokens[3]}) |
||||
if apiService == nil { |
||||
continue |
||||
} |
||||
registration.AddAPIServiceToSyncOnStart(apiService) |
||||
apiServices = append(apiServices, apiService) |
||||
} |
||||
|
||||
return apiServices |
||||
} |
||||
|
||||
// NOTE: below function imported from https://github.com/kubernetes/kubernetes/blob/master/cmd/kube-apiserver/app/server.go#L197
|
||||
// createProxyTransport creates the dialer infrastructure to connect to the api servers.
|
||||
func createProxyTransport() *http.Transport { |
||||
// NOTE: We don't set proxyDialerFn but the below SetTransportDefaults will
|
||||
// See https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/util/net/http.go#L109
|
||||
var proxyDialerFn utilnet.DialFunc |
||||
// Proxying to services is IP-based... don't expect to be able to verify the hostname
|
||||
proxyTLSClientConfig := &tls.Config{InsecureSkipVerify: true} |
||||
proxyTransport := utilnet.SetTransportDefaults(&http.Transport{ |
||||
DialContext: proxyDialerFn, |
||||
TLSClientConfig: proxyTLSClientConfig, |
||||
}) |
||||
return proxyTransport |
||||
} |
@ -0,0 +1,466 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Provenance-includes-location: https://github.com/kubernetes/kube-aggregator/blob/master/pkg/controllers/status/available_controller.go
|
||||
// Provenance-includes-license: Apache-2.0
|
||||
// Provenance-includes-copyright: The Kubernetes Authors.
|
||||
|
||||
package aggregator |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"net/http" |
||||
"net/url" |
||||
"reflect" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
informersservicev0alpha1 "github.com/grafana/grafana/pkg/generated/informers/externalversions/service/v0alpha1" |
||||
listersservicev0alpha1 "github.com/grafana/grafana/pkg/generated/listers/service/v0alpha1" |
||||
|
||||
"k8s.io/apimachinery/pkg/api/equality" |
||||
apierrors "k8s.io/apimachinery/pkg/api/errors" |
||||
"k8s.io/apimachinery/pkg/api/meta" |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
"k8s.io/apimachinery/pkg/labels" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
||||
"k8s.io/apimachinery/pkg/util/wait" |
||||
"k8s.io/client-go/tools/cache" |
||||
"k8s.io/client-go/transport" |
||||
"k8s.io/client-go/util/workqueue" |
||||
"k8s.io/klog/v2" |
||||
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" |
||||
apiregistrationv1apihelper "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/helper" |
||||
apiregistrationclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1" |
||||
informers "k8s.io/kube-aggregator/pkg/client/informers/externalversions/apiregistration/v1" |
||||
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/v1" |
||||
"k8s.io/kube-aggregator/pkg/controllers" |
||||
) |
||||
|
||||
type certKeyFunc func() ([]byte, []byte) |
||||
|
||||
// ServiceResolver knows how to convert a service reference into an actual location.
|
||||
type ServiceResolver interface { |
||||
ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) |
||||
} |
||||
|
||||
// AvailableConditionController handles checking the availability of registered API services.
|
||||
type AvailableConditionController struct { |
||||
apiServiceClient apiregistrationclient.APIServicesGetter |
||||
|
||||
apiServiceLister listers.APIServiceLister |
||||
apiServiceSynced cache.InformerSynced |
||||
|
||||
// externalNameLister is used to get the IP to create the transport for
|
||||
externalNameLister listersservicev0alpha1.ExternalNameLister |
||||
servicesSynced cache.InformerSynced |
||||
|
||||
// proxyTransportDial specifies the dial function for creating unencrypted TCP connections.
|
||||
proxyTransportDial *transport.DialHolder |
||||
proxyCurrentCertKeyContent certKeyFunc |
||||
serviceResolver ServiceResolver |
||||
|
||||
// To allow injection for testing.
|
||||
syncFn func(key string) error |
||||
|
||||
queue workqueue.RateLimitingInterface |
||||
// map from service-namespace -> service-name -> apiservice names
|
||||
cache map[string]map[string][]string |
||||
// this lock protects operations on the above cache
|
||||
cacheLock sync.RWMutex |
||||
} |
||||
|
||||
// NewAvailableConditionController returns a new AvailableConditionController.
|
||||
func NewAvailableConditionController( |
||||
apiServiceInformer informers.APIServiceInformer, |
||||
externalNameInformer informersservicev0alpha1.ExternalNameInformer, |
||||
apiServiceClient apiregistrationclient.APIServicesGetter, |
||||
proxyTransportDial *transport.DialHolder, |
||||
proxyCurrentCertKeyContent certKeyFunc, |
||||
serviceResolver ServiceResolver, |
||||
) (*AvailableConditionController, error) { |
||||
c := &AvailableConditionController{ |
||||
apiServiceClient: apiServiceClient, |
||||
apiServiceLister: apiServiceInformer.Lister(), |
||||
externalNameLister: externalNameInformer.Lister(), |
||||
serviceResolver: serviceResolver, |
||||
queue: workqueue.NewNamedRateLimitingQueue( |
||||
// We want a fairly tight requeue time. The controller listens to the API, but because it relies on the routability of the
|
||||
// service network, it is possible for an external, non-watchable factor to affect availability. This keeps
|
||||
// the maximum disruption time to a minimum, but it does prevent hot loops.
|
||||
workqueue.NewItemExponentialFailureRateLimiter(5*time.Millisecond, 30*time.Second), |
||||
"AvailableConditionController"), |
||||
proxyTransportDial: proxyTransportDial, |
||||
proxyCurrentCertKeyContent: proxyCurrentCertKeyContent, |
||||
} |
||||
|
||||
// resync on this one because it is low cardinality and rechecking the actual discovery
|
||||
// allows us to detect health in a more timely fashion when network connectivity to
|
||||
// nodes is snipped, but the network still attempts to route there. See
|
||||
// https://github.com/openshift/origin/issues/17159#issuecomment-341798063
|
||||
apiServiceHandler, _ := apiServiceInformer.Informer().AddEventHandlerWithResyncPeriod( |
||||
cache.ResourceEventHandlerFuncs{ |
||||
AddFunc: c.addAPIService, |
||||
UpdateFunc: c.updateAPIService, |
||||
DeleteFunc: c.deleteAPIService, |
||||
}, |
||||
30*time.Second) |
||||
c.apiServiceSynced = apiServiceHandler.HasSynced |
||||
|
||||
serviceHandler, _ := externalNameInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ |
||||
AddFunc: c.addService, |
||||
UpdateFunc: c.updateService, |
||||
DeleteFunc: c.deleteService, |
||||
}) |
||||
c.servicesSynced = serviceHandler.HasSynced |
||||
|
||||
c.syncFn = c.sync |
||||
|
||||
return c, nil |
||||
} |
||||
|
||||
func (c *AvailableConditionController) sync(key string) error { |
||||
originalAPIService, err := c.apiServiceLister.Get(key) |
||||
if apierrors.IsNotFound(err) { |
||||
return nil |
||||
} |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// if a particular transport was specified, use that otherwise build one
|
||||
// construct an http client that will ignore TLS verification (if someone owns the network and messes with your status
|
||||
// that's not so bad) and sets a very short timeout. This is a best effort GET that provides no additional information
|
||||
transportConfig := &transport.Config{ |
||||
TLS: transport.TLSConfig{ |
||||
Insecure: true, |
||||
}, |
||||
DialHolder: c.proxyTransportDial, |
||||
} |
||||
|
||||
if c.proxyCurrentCertKeyContent != nil { |
||||
proxyClientCert, proxyClientKey := c.proxyCurrentCertKeyContent() |
||||
|
||||
transportConfig.TLS.CertData = proxyClientCert |
||||
transportConfig.TLS.KeyData = proxyClientKey |
||||
} |
||||
restTransport, err := transport.New(transportConfig) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
discoveryClient := &http.Client{ |
||||
Transport: restTransport, |
||||
// the request should happen quickly.
|
||||
Timeout: 5 * time.Second, |
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
||||
return http.ErrUseLastResponse |
||||
}, |
||||
} |
||||
|
||||
apiService := originalAPIService.DeepCopy() |
||||
|
||||
availableCondition := apiregistrationv1.APIServiceCondition{ |
||||
Type: apiregistrationv1.Available, |
||||
Status: apiregistrationv1.ConditionTrue, |
||||
LastTransitionTime: metav1.Now(), |
||||
} |
||||
|
||||
// local API services are always considered available
|
||||
if apiService.Spec.Service == nil { |
||||
apiregistrationv1apihelper.SetAPIServiceCondition(apiService, apiregistrationv1apihelper.NewLocalAvailableAPIServiceCondition()) |
||||
_, err := c.updateAPIServiceStatus(originalAPIService, apiService) |
||||
return err |
||||
} |
||||
|
||||
_, err = c.externalNameLister.ExternalNames(apiService.Spec.Service.Namespace).Get(apiService.Spec.Service.Name) |
||||
if apierrors.IsNotFound(err) { |
||||
availableCondition.Status = apiregistrationv1.ConditionFalse |
||||
availableCondition.Reason = "ServiceNotFound" |
||||
availableCondition.Message = fmt.Sprintf("service/%s in %q is not present", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace) |
||||
apiregistrationv1apihelper.SetAPIServiceCondition(apiService, availableCondition) |
||||
_, err := c.updateAPIServiceStatus(originalAPIService, apiService) |
||||
return err |
||||
} else if err != nil { |
||||
availableCondition.Status = apiregistrationv1.ConditionUnknown |
||||
availableCondition.Reason = "ServiceAccessError" |
||||
availableCondition.Message = fmt.Sprintf("service/%s in %q cannot be checked due to: %v", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace, err) |
||||
apiregistrationv1apihelper.SetAPIServiceCondition(apiService, availableCondition) |
||||
_, err := c.updateAPIServiceStatus(originalAPIService, apiService) |
||||
return err |
||||
} |
||||
|
||||
// actually try to hit the discovery endpoint when it isn't local and when we're routing as a service.
|
||||
if apiService.Spec.Service != nil && c.serviceResolver != nil { |
||||
attempts := 5 |
||||
results := make(chan error, attempts) |
||||
for i := 0; i < attempts; i++ { |
||||
go func() { |
||||
discoveryURL, err := c.serviceResolver.ResolveEndpoint(apiService.Spec.Service.Namespace, apiService.Spec.Service.Name, *apiService.Spec.Service.Port) |
||||
if err != nil { |
||||
results <- err |
||||
return |
||||
} |
||||
// render legacyAPIService health check path when it is delegated to a service
|
||||
if apiService.Name == "v1." { |
||||
discoveryURL.Path = "/api/" + apiService.Spec.Version |
||||
} else { |
||||
discoveryURL.Path = "/apis/" + apiService.Spec.Group + "/" + apiService.Spec.Version |
||||
} |
||||
|
||||
errCh := make(chan error, 1) |
||||
go func() { |
||||
// be sure to check a URL that the aggregated API server is required to serve
|
||||
newReq, err := http.NewRequest("GET", discoveryURL.String(), nil) |
||||
if err != nil { |
||||
errCh <- err |
||||
return |
||||
} |
||||
|
||||
// setting the system-masters identity ensures that we will always have access rights
|
||||
transport.SetAuthProxyHeaders(newReq, "system:kube-aggregator", []string{"system:masters"}, nil) |
||||
resp, err := discoveryClient.Do(newReq) |
||||
if resp != nil { |
||||
_ = resp.Body.Close() |
||||
// we should always been in the 200s or 300s
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { |
||||
errCh <- fmt.Errorf("bad status from %v: %v", discoveryURL, resp.StatusCode) |
||||
return |
||||
} |
||||
} |
||||
|
||||
errCh <- err |
||||
}() |
||||
|
||||
select { |
||||
case err = <-errCh: |
||||
if err != nil { |
||||
results <- fmt.Errorf("failing or missing response from %v: %v", discoveryURL, err) |
||||
return |
||||
} |
||||
|
||||
// we had trouble with slow dial and DNS responses causing us to wait too long.
|
||||
// we added this as insurance
|
||||
case <-time.After(6 * time.Second): |
||||
results <- fmt.Errorf("timed out waiting for %v", discoveryURL) |
||||
return |
||||
} |
||||
|
||||
results <- nil |
||||
}() |
||||
} |
||||
|
||||
var lastError error |
||||
for i := 0; i < attempts; i++ { |
||||
lastError = <-results |
||||
// if we had at least one success, we are successful overall and we can return now
|
||||
if lastError == nil { |
||||
break |
||||
} |
||||
} |
||||
|
||||
if lastError != nil { |
||||
availableCondition.Status = apiregistrationv1.ConditionFalse |
||||
availableCondition.Reason = "FailedDiscoveryCheck" |
||||
availableCondition.Message = lastError.Error() |
||||
apiregistrationv1apihelper.SetAPIServiceCondition(apiService, availableCondition) |
||||
_, updateErr := c.updateAPIServiceStatus(originalAPIService, apiService) |
||||
if updateErr != nil { |
||||
return updateErr |
||||
} |
||||
// force a requeue to make it very obvious that this will be retried at some point in the future
|
||||
// along with other requeues done via service change, endpoint change, and resync
|
||||
return lastError |
||||
} |
||||
} |
||||
|
||||
availableCondition.Reason = "Passed" |
||||
availableCondition.Message = "all checks passed" |
||||
apiregistrationv1apihelper.SetAPIServiceCondition(apiService, availableCondition) |
||||
_, err = c.updateAPIServiceStatus(originalAPIService, apiService) |
||||
return err |
||||
} |
||||
|
||||
// updateAPIServiceStatus only issues an update if a change is detected. We have a tight resync loop to quickly detect dead
|
||||
// apiservices. Doing that means we don't want to quickly issue no-op updates.
|
||||
func (c *AvailableConditionController) updateAPIServiceStatus(originalAPIService, newAPIService *apiregistrationv1.APIService) (*apiregistrationv1.APIService, error) { |
||||
if equality.Semantic.DeepEqual(originalAPIService.Status, newAPIService.Status) { |
||||
return newAPIService, nil |
||||
} |
||||
|
||||
orig := apiregistrationv1apihelper.GetAPIServiceConditionByType(originalAPIService, apiregistrationv1.Available) |
||||
now := apiregistrationv1apihelper.GetAPIServiceConditionByType(newAPIService, apiregistrationv1.Available) |
||||
unknown := apiregistrationv1.APIServiceCondition{ |
||||
Type: apiregistrationv1.Available, |
||||
Status: apiregistrationv1.ConditionUnknown, |
||||
} |
||||
if orig == nil { |
||||
orig = &unknown |
||||
} |
||||
if now == nil { |
||||
now = &unknown |
||||
} |
||||
if *orig != *now { |
||||
klog.V(2).InfoS("changing APIService availability", "name", newAPIService.Name, "oldStatus", orig.Status, "newStatus", now.Status, "message", now.Message, "reason", now.Reason) |
||||
} |
||||
|
||||
newAPIService, err := c.apiServiceClient.APIServices().UpdateStatus(context.TODO(), newAPIService, metav1.UpdateOptions{}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return newAPIService, nil |
||||
} |
||||
|
||||
// Run starts the AvailableConditionController loop which manages the availability condition of API services.
|
||||
func (c *AvailableConditionController) Run(workers int, stopCh <-chan struct{}) { |
||||
defer utilruntime.HandleCrash() |
||||
defer c.queue.ShutDown() |
||||
|
||||
klog.Info("Starting AvailableConditionController") |
||||
defer klog.Info("Shutting down AvailableConditionController") |
||||
|
||||
// This waits not just for the informers to sync, but for our handlers
|
||||
// to be called; since the handlers are three different ways of
|
||||
// enqueueing the same thing, waiting for this permits the queue to
|
||||
// maximally de-duplicate the entries.
|
||||
if !controllers.WaitForCacheSync("AvailableConditionCOverrideController", stopCh, c.apiServiceSynced, c.servicesSynced) { |
||||
return |
||||
} |
||||
|
||||
for i := 0; i < workers; i++ { |
||||
go wait.Until(c.runWorker, time.Second, stopCh) |
||||
} |
||||
|
||||
<-stopCh |
||||
} |
||||
|
||||
func (c *AvailableConditionController) runWorker() { |
||||
for c.processNextWorkItem() { |
||||
} |
||||
} |
||||
|
||||
// processNextWorkItem deals with one key off the queue. It returns false when it's time to quit.
|
||||
func (c *AvailableConditionController) processNextWorkItem() bool { |
||||
key, quit := c.queue.Get() |
||||
if quit { |
||||
return false |
||||
} |
||||
defer c.queue.Done(key) |
||||
|
||||
err := c.syncFn(key.(string)) |
||||
if err == nil { |
||||
c.queue.Forget(key) |
||||
return true |
||||
} |
||||
|
||||
utilruntime.HandleError(fmt.Errorf("%v failed with: %v", key, err)) |
||||
c.queue.AddRateLimited(key) |
||||
|
||||
return true |
||||
} |
||||
|
||||
func (c *AvailableConditionController) addAPIService(obj interface{}) { |
||||
castObj := obj.(*apiregistrationv1.APIService) |
||||
klog.V(4).Infof("Adding %s", castObj.Name) |
||||
if castObj.Spec.Service != nil { |
||||
c.rebuildAPIServiceCache() |
||||
} |
||||
c.queue.Add(castObj.Name) |
||||
} |
||||
|
||||
func (c *AvailableConditionController) updateAPIService(oldObj, newObj interface{}) { |
||||
castObj := newObj.(*apiregistrationv1.APIService) |
||||
oldCastObj := oldObj.(*apiregistrationv1.APIService) |
||||
klog.V(4).Infof("Updating %s", oldCastObj.Name) |
||||
if !reflect.DeepEqual(castObj.Spec.Service, oldCastObj.Spec.Service) { |
||||
c.rebuildAPIServiceCache() |
||||
} |
||||
c.queue.Add(oldCastObj.Name) |
||||
} |
||||
|
||||
func (c *AvailableConditionController) deleteAPIService(obj interface{}) { |
||||
castObj, ok := obj.(*apiregistrationv1.APIService) |
||||
if !ok { |
||||
tombstone, ok := obj.(cache.DeletedFinalStateUnknown) |
||||
if !ok { |
||||
klog.Errorf("Couldn't get object from tombstone %#v", obj) |
||||
return |
||||
} |
||||
castObj, ok = tombstone.Obj.(*apiregistrationv1.APIService) |
||||
if !ok { |
||||
klog.Errorf("Tombstone contained object that is not expected %#v", obj) |
||||
return |
||||
} |
||||
} |
||||
klog.V(4).Infof("Deleting %q", castObj.Name) |
||||
if castObj.Spec.Service != nil { |
||||
c.rebuildAPIServiceCache() |
||||
} |
||||
c.queue.Add(castObj.Name) |
||||
} |
||||
|
||||
func (c *AvailableConditionController) getAPIServicesFor(obj runtime.Object) []string { |
||||
metadata, err := meta.Accessor(obj) |
||||
if err != nil { |
||||
utilruntime.HandleError(err) |
||||
return nil |
||||
} |
||||
c.cacheLock.RLock() |
||||
defer c.cacheLock.RUnlock() |
||||
return c.cache[metadata.GetNamespace()][metadata.GetName()] |
||||
} |
||||
|
||||
// if the service/endpoint handler wins the race against the cache rebuilding, it may queue a no-longer-relevant apiservice
|
||||
// (which will get processed an extra time - this doesn't matter),
|
||||
// and miss a newly relevant apiservice (which will get queued by the apiservice handler)
|
||||
func (c *AvailableConditionController) rebuildAPIServiceCache() { |
||||
apiServiceList, _ := c.apiServiceLister.List(labels.Everything()) |
||||
newCache := map[string]map[string][]string{} |
||||
for _, apiService := range apiServiceList { |
||||
if apiService.Spec.Service == nil { |
||||
continue |
||||
} |
||||
if newCache[apiService.Spec.Service.Namespace] == nil { |
||||
newCache[apiService.Spec.Service.Namespace] = map[string][]string{} |
||||
} |
||||
newCache[apiService.Spec.Service.Namespace][apiService.Spec.Service.Name] = append(newCache[apiService.Spec.Service.Namespace][apiService.Spec.Service.Name], apiService.Name) |
||||
} |
||||
|
||||
c.cacheLock.Lock() |
||||
defer c.cacheLock.Unlock() |
||||
c.cache = newCache |
||||
} |
||||
|
||||
// TODO, think of a way to avoid checking on every service manipulation
|
||||
|
||||
func (c *AvailableConditionController) addService(obj interface{}) { |
||||
for _, apiService := range c.getAPIServicesFor(obj.(*v0alpha1.ExternalName)) { |
||||
c.queue.Add(apiService) |
||||
} |
||||
} |
||||
|
||||
func (c *AvailableConditionController) updateService(obj, _ interface{}) { |
||||
for _, apiService := range c.getAPIServicesFor(obj.(*v0alpha1.ExternalName)) { |
||||
c.queue.Add(apiService) |
||||
} |
||||
} |
||||
|
||||
func (c *AvailableConditionController) deleteService(obj interface{}) { |
||||
castObj, ok := obj.(*v0alpha1.ExternalName) |
||||
if !ok { |
||||
tombstone, ok := obj.(cache.DeletedFinalStateUnknown) |
||||
if !ok { |
||||
klog.Errorf("Couldn't get object from tombstone %#v", obj) |
||||
return |
||||
} |
||||
castObj, ok = tombstone.Obj.(*v0alpha1.ExternalName) |
||||
if !ok { |
||||
klog.Errorf("Tombstone contained object that is not expected %#v", obj) |
||||
return |
||||
} |
||||
} |
||||
for _, apiService := range c.getAPIServicesFor(castObj) { |
||||
c.queue.Add(apiService) |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
package aggregator |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"net/url" |
||||
|
||||
"k8s.io/kube-aggregator/pkg/apiserver" |
||||
|
||||
servicelistersv0alpha1 "github.com/grafana/grafana/pkg/generated/listers/service/v0alpha1" |
||||
) |
||||
|
||||
func NewExternalNameResolver(externalNames servicelistersv0alpha1.ExternalNameLister) apiserver.ServiceResolver { |
||||
return &externalNameResolver{ |
||||
externalNames: externalNames, |
||||
} |
||||
} |
||||
|
||||
type externalNameResolver struct { |
||||
externalNames servicelistersv0alpha1.ExternalNameLister |
||||
} |
||||
|
||||
func (r *externalNameResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) { |
||||
extName, err := r.externalNames.ExternalNames(namespace).Get(name) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &url.URL{ |
||||
Scheme: "https", |
||||
Host: net.JoinHostPort(extName.Spec.Host, fmt.Sprintf("%d", port)), |
||||
}, nil |
||||
} |
@ -0,0 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=service.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/service/v0alpha1"
|
@ -0,0 +1,50 @@ |
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/runtime/schema" |
||||
|
||||
"github.com/grafana/grafana/pkg/apis" |
||||
) |
||||
|
||||
const ( |
||||
GROUP = "service.grafana.app" |
||||
VERSION = "v0alpha1" |
||||
APIVERSION = GROUP + "/" + VERSION |
||||
) |
||||
|
||||
var ExternalNameResourceInfo = apis.NewResourceInfo(GROUP, VERSION, |
||||
"externalnames", "externalname", "ExternalName", |
||||
func() runtime.Object { return &ExternalName{} }, |
||||
func() runtime.Object { return &ExternalNameList{} }, |
||||
) |
||||
|
||||
var ( |
||||
// SchemeGroupVersion is group version used to register these objects
|
||||
SchemeGroupVersion = schema.GroupVersion{Group: GROUP, Version: VERSION} |
||||
|
||||
// SchemaBuilder is used by standard codegen
|
||||
SchemeBuilder runtime.SchemeBuilder |
||||
localSchemeBuilder = &SchemeBuilder |
||||
AddToScheme = localSchemeBuilder.AddToScheme |
||||
) |
||||
|
||||
func init() { |
||||
localSchemeBuilder.Register(addKnownTypes) |
||||
} |
||||
|
||||
// Adds the list of known types to the given scheme.
|
||||
func addKnownTypes(scheme *runtime.Scheme) error { |
||||
scheme.AddKnownTypes(SchemeGroupVersion, |
||||
&ExternalName{}, |
||||
&ExternalNameList{}, |
||||
) |
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion) |
||||
return nil |
||||
} |
||||
|
||||
// Resource takes an unqualified resource and returns a Group qualified GroupResource
|
||||
func Resource(resource string) schema.GroupResource { |
||||
return SchemeGroupVersion.WithResource(resource).GroupResource() |
||||
} |
@ -0,0 +1,27 @@ |
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
) |
||||
|
||||
// +genclient
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
type ExternalName struct { |
||||
metav1.TypeMeta `json:",inline"` |
||||
metav1.ObjectMeta `json:"metadata,omitempty"` |
||||
|
||||
Spec ExternalNameSpec `json:"spec,omitempty"` |
||||
} |
||||
|
||||
type ExternalNameSpec struct { |
||||
Host string `json:"host,omitempty"` |
||||
} |
||||
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
type ExternalNameList struct { |
||||
metav1.TypeMeta `json:",inline"` |
||||
// +optional
|
||||
metav1.ListMeta `json:"metadata,omitempty"` |
||||
|
||||
Items []ExternalName `json:"items,omitempty"` |
||||
} |
@ -0,0 +1,88 @@ |
||||
//go:build !ignore_autogenerated
|
||||
// +build !ignore_autogenerated
|
||||
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by deepcopy-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
) |
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ExternalName) DeepCopyInto(out *ExternalName) { |
||||
*out = *in |
||||
out.TypeMeta = in.TypeMeta |
||||
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) |
||||
out.Spec = in.Spec |
||||
return |
||||
} |
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExternalName.
|
||||
func (in *ExternalName) DeepCopy() *ExternalName { |
||||
if in == nil { |
||||
return nil |
||||
} |
||||
out := new(ExternalName) |
||||
in.DeepCopyInto(out) |
||||
return out |
||||
} |
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *ExternalName) DeepCopyObject() runtime.Object { |
||||
if c := in.DeepCopy(); c != nil { |
||||
return c |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ExternalNameList) DeepCopyInto(out *ExternalNameList) { |
||||
*out = *in |
||||
out.TypeMeta = in.TypeMeta |
||||
in.ListMeta.DeepCopyInto(&out.ListMeta) |
||||
if in.Items != nil { |
||||
in, out := &in.Items, &out.Items |
||||
*out = make([]ExternalName, len(*in)) |
||||
for i := range *in { |
||||
(*in)[i].DeepCopyInto(&(*out)[i]) |
||||
} |
||||
} |
||||
return |
||||
} |
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExternalNameList.
|
||||
func (in *ExternalNameList) DeepCopy() *ExternalNameList { |
||||
if in == nil { |
||||
return nil |
||||
} |
||||
out := new(ExternalNameList) |
||||
in.DeepCopyInto(out) |
||||
return out |
||||
} |
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *ExternalNameList) DeepCopyObject() runtime.Object { |
||||
if c := in.DeepCopy(); c != nil { |
||||
return c |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ExternalNameSpec) DeepCopyInto(out *ExternalNameSpec) { |
||||
*out = *in |
||||
return |
||||
} |
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExternalNameSpec.
|
||||
func (in *ExternalNameSpec) DeepCopy() *ExternalNameSpec { |
||||
if in == nil { |
||||
return nil |
||||
} |
||||
out := new(ExternalNameSpec) |
||||
in.DeepCopyInto(out) |
||||
return out |
||||
} |
@ -0,0 +1,19 @@ |
||||
//go:build !ignore_autogenerated
|
||||
// +build !ignore_autogenerated
|
||||
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by defaulter-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
) |
||||
|
||||
// RegisterDefaults adds defaulters functions to the given scheme.
|
||||
// Public to allow building arbitrary schemes.
|
||||
// All generated defaulters are covering - they call all nested defaulters.
|
||||
func RegisterDefaults(scheme *runtime.Scheme) error { |
||||
return nil |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,89 +0,0 @@ |
||||
# grafana apiserver (standalone) |
||||
|
||||
The example-apiserver closely resembles the |
||||
[sample-apiserver](https://github.com/kubernetes/sample-apiserver/tree/master) project in code and thus |
||||
allows the same |
||||
[CLI flags](https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/) as kube-apiserver. |
||||
It is currently used for testing our deployment pipelines for aggregated servers. You can optionally omit the |
||||
aggregation path altogether and just run this example apiserver as a standalone process. |
||||
|
||||
## Standalone Mode |
||||
|
||||
### Usage |
||||
|
||||
```shell |
||||
go run ./pkg/cmd/grafana apiserver example.grafana.app \ |
||||
--secure-port 8443 |
||||
``` |
||||
|
||||
### Verify that all works |
||||
|
||||
```shell |
||||
export KUBECONFIG=./example-apiserver/kubeconfig |
||||
|
||||
kubectl api-resources |
||||
NAME SHORTNAMES APIVERSION NAMESPACED KIND |
||||
dummy example.grafana.app/v0alpha1 true DummyResource |
||||
runtime example.grafana.app/v0alpha1 false RuntimeInfo |
||||
``` |
||||
|
||||
## Aggregated Mode |
||||
|
||||
### Prerequisites: |
||||
1. kind: you will need kind (or another local K8s setup) if you want to test aggregation. |
||||
``` |
||||
go install sigs.k8s.io/kind@v0.20.0 && kind create cluster |
||||
``` |
||||
|
||||
### Usage |
||||
|
||||
You can start the example-apiserver with an invocation as shown below. The Authn / Authz flags are set up so that the kind cluster |
||||
can be used as a root server for this example-apiserver (in aggregated mode). Here, it's assumed that you have a local |
||||
kind cluster and that you can provide its kubeconfig in the parameters to the example-apiserver. |
||||
|
||||
```shell |
||||
go run ./pkg/cmd/grafana apiserver example.grafana.app \ |
||||
--authentication-kubeconfig ~/.kube/config \ |
||||
--authorization-kubeconfig ~/.kube/config \ |
||||
--kubeconfig ~/.kube/config \ |
||||
--secure-port 8443 |
||||
``` |
||||
|
||||
Once, the `example-apiserver` is running, you can configure aggregation against your kind cluster |
||||
by applying a `APIService` and it's corresponding `Service` object. Sample kustomizations are provided |
||||
for local development on [Linux](./deploy/linux/kustomization.yaml) and [macOS](./deploy/darwin/kustomization.yaml). |
||||
|
||||
```shell |
||||
kubectl deploy -k ./deploy/darwin # or /linux |
||||
``` |
||||
|
||||
|
||||
### Verify that all works |
||||
|
||||
With kubectl configured against `kind-kind` context, you can run the following: |
||||
|
||||
```shell |
||||
kubectl get --raw /apis/example.grafana.app/v0alpha1 | jq -r |
||||
{ |
||||
"kind": "APIResourceList", |
||||
"apiVersion": "v1", |
||||
"groupVersion": "example.grafana.app/v0alpha1", |
||||
"resources": [ |
||||
{ |
||||
"name": "runtime", |
||||
"singularName": "runtime", |
||||
"namespaced": false, |
||||
"kind": "RuntimeInfo", |
||||
"verbs": [ |
||||
"list" |
||||
] |
||||
} |
||||
] |
||||
} |
||||
``` |
||||
|
||||
```shell |
||||
kubectl get apiservice v0alpha1.example.grafana.app |
||||
NAME SERVICE AVAILABLE AGE |
||||
v0alpha1.example.grafana.app grafana/example-apiserver True 4h1m |
||||
``` |
@ -0,0 +1,51 @@ |
||||
# grafana aggregator |
||||
|
||||
The `aggregator` command in this binary is our equivalent of what kube-apiserver does for aggregation using |
||||
the `kube-aggregator` pkg. Here, we enable only select controllers that are useful for aggregation in a Grafana |
||||
cloud context. In future, Grafana microservices (and even plugins) will run as separate API servers |
||||
hosting each their own APIs (with specific Group/Versions). The `aggregator` component here shall act similar to what |
||||
`kube-apiserver` does: doing healthchecks for `APIService` objects registered against it and acting as a proxy for |
||||
the specified `GroupVersion` therein. |
||||
|
||||
## How to get started |
||||
|
||||
1. Generate the PKI using `openssl` (for development purposes, we will use the CN of `system:masters`): |
||||
```shell |
||||
./hack/make-aggregator-pki.sh |
||||
``` |
||||
2. Start the aggregator: |
||||
```shell |
||||
# This will generate the kubeconfig which you can use in the extension apiservers for |
||||
# enforcing delegate authnz under $PWD/data/grafana-apiserver/aggregator.kubeconfig |
||||
go run ./pkg/cmd/grafana aggregator --secure-port 8443 \ |
||||
--proxy-client-cert-file $PWD/data/grafana-aggregator/client.crt \ |
||||
--proxy-client-key-file $PWD/data/grafana-aggregator/client.key |
||||
``` |
||||
3. Apply the manifests: |
||||
```shell |
||||
export KUBECONFIG=$PWD/data/grafana-apiserver/aggregator.kubeconfig |
||||
kubectl apply -k ./pkg/cmd/grafana/apiserver/deploy/aggregator-test |
||||
# SAMPLE OUTPUT |
||||
# apiservice.apiregistration.k8s.io/v0alpha1.example.grafana.app created |
||||
# externalname.service.grafana.app/example-apiserver created |
||||
|
||||
kubectl get apiservice |
||||
# SAMPLE OUTPUT |
||||
# NAME SERVICE AVAILABLE AGE |
||||
# v0alpha1.example.grafana.app grafana/example-apiserver False (FailedDiscoveryCheck) 29m |
||||
``` |
||||
4. In another tab, start the example microservice that will be aggregated by the parent apiserver: |
||||
```shell |
||||
go run ./pkg/cmd/grafana apiserver example.grafana.app \ |
||||
--kubeconfig $PWD/data/grafana-aggregator/aggregator.kubeconfig \ |
||||
--secure-port 7443 \ |
||||
--client-ca-file=$PWD/data/grafana-aggregator/ca.crt |
||||
``` |
||||
5. Check `APIService` again: |
||||
```shell |
||||
export KUBECONFIG=$PWD/data/grafana-apiserver/aggregator.kubeconfig |
||||
kubectl get apiservice |
||||
# SAMPLE OUTPUT |
||||
# NAME SERVICE AVAILABLE AGE |
||||
# v0alpha1.example.grafana.app grafana/example-apiserver True 30m |
||||
``` |
@ -0,0 +1,29 @@ |
||||
# grafana apiserver (standalone) |
||||
|
||||
The example-apiserver closely resembles the |
||||
[sample-apiserver](https://github.com/kubernetes/sample-apiserver/tree/master) project in code and thus |
||||
allows the same |
||||
[CLI flags](https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/) as kube-apiserver. |
||||
It is currently used for testing our deployment pipelines for aggregated servers. You can optionally omit the |
||||
aggregation path altogether and just run this example apiserver as a standalone process. |
||||
|
||||
## Standalone Mode |
||||
|
||||
### Usage |
||||
|
||||
```shell |
||||
go run ./pkg/cmd/grafana apiserver example.grafana.app \ |
||||
--secure-port 7443 |
||||
``` |
||||
|
||||
### Verify that all works |
||||
|
||||
```shell |
||||
export KUBECONFIG=./example-apiserver/kubeconfig |
||||
|
||||
kubectl api-resources |
||||
NAME SHORTNAMES APIVERSION NAMESPACED KIND |
||||
dummy example.grafana.app/v0alpha1 true DummyResource |
||||
runtime example.grafana.app/v0alpha1 false RuntimeInfo |
||||
``` |
||||
|
@ -0,0 +1,8 @@ |
||||
apiVersion: service.grafana.app/v0alpha1 |
||||
kind: ExternalName |
||||
metadata: |
||||
name: example-apiserver |
||||
namespace: grafana |
||||
spec: |
||||
host: localhost |
||||
|
@ -1,3 +1,3 @@ |
||||
resources: |
||||
- namespace.yaml |
||||
- apiservice.yaml |
||||
- externalname.yaml |
@ -1,4 +0,0 @@ |
||||
apiVersion: v1 |
||||
kind: Namespace |
||||
metadata: |
||||
name: grafana |
@ -1,5 +0,0 @@ |
||||
namespace: grafana |
||||
|
||||
resources: |
||||
- ../base |
||||
- service.yaml |
@ -1,10 +0,0 @@ |
||||
apiVersion: v1 |
||||
kind: Service |
||||
metadata: |
||||
name: example-apiserver |
||||
spec: |
||||
type: ExternalName |
||||
externalName: host.docker.internal |
||||
ports: |
||||
- port: 8443 |
||||
name: https |
@ -1,5 +0,0 @@ |
||||
namespace: grafana |
||||
|
||||
resources: |
||||
- ../base |
||||
- service.yaml |
@ -1,23 +0,0 @@ |
||||
--- |
||||
apiVersion: v1 |
||||
kind: Endpoints |
||||
metadata: |
||||
name: example-apiserver |
||||
subsets: |
||||
- addresses: |
||||
- ip: 172.17.0.1 # this is the gateway IP in the "bridge" docker network |
||||
ports: |
||||
- appProtocol: https |
||||
port: 8443 |
||||
protocol: TCP |
||||
--- |
||||
apiVersion: v1 |
||||
kind: Service |
||||
metadata: |
||||
name: example-apiserver |
||||
spec: |
||||
ports: |
||||
- protocol: TCP |
||||
appProtocol: https |
||||
port: 8443 |
||||
targetPort: 8443 |
@ -0,0 +1,48 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by applyconfiguration-gen. DO NOT EDIT.
|
||||
|
||||
package internal |
||||
|
||||
import ( |
||||
"fmt" |
||||
"sync" |
||||
|
||||
typed "sigs.k8s.io/structured-merge-diff/v4/typed" |
||||
) |
||||
|
||||
func Parser() *typed.Parser { |
||||
parserOnce.Do(func() { |
||||
var err error |
||||
parser, err = typed.NewParser(schemaYAML) |
||||
if err != nil { |
||||
panic(fmt.Sprintf("Failed to parse schema: %v", err)) |
||||
} |
||||
}) |
||||
return parser |
||||
} |
||||
|
||||
var parserOnce sync.Once |
||||
var parser *typed.Parser |
||||
var schemaYAML = typed.YAMLObject(`types: |
||||
- name: __untyped_atomic_ |
||||
scalar: untyped |
||||
list: |
||||
elementType: |
||||
namedType: __untyped_atomic_ |
||||
elementRelationship: atomic |
||||
map: |
||||
elementType: |
||||
namedType: __untyped_atomic_ |
||||
elementRelationship: atomic |
||||
- name: __untyped_deduced_ |
||||
scalar: untyped |
||||
list: |
||||
elementType: |
||||
namedType: __untyped_atomic_ |
||||
elementRelationship: atomic |
||||
map: |
||||
elementType: |
||||
namedType: __untyped_deduced_ |
||||
elementRelationship: separable |
||||
`) |
@ -0,0 +1,196 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by applyconfiguration-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
types "k8s.io/apimachinery/pkg/types" |
||||
v1 "k8s.io/client-go/applyconfigurations/meta/v1" |
||||
) |
||||
|
||||
// ExternalNameApplyConfiguration represents an declarative configuration of the ExternalName type for use
|
||||
// with apply.
|
||||
type ExternalNameApplyConfiguration struct { |
||||
v1.TypeMetaApplyConfiguration `json:",inline"` |
||||
*v1.ObjectMetaApplyConfiguration `json:"metadata,omitempty"` |
||||
Spec *ExternalNameSpecApplyConfiguration `json:"spec,omitempty"` |
||||
} |
||||
|
||||
// ExternalName constructs an declarative configuration of the ExternalName type for use with
|
||||
// apply.
|
||||
func ExternalName(name, namespace string) *ExternalNameApplyConfiguration { |
||||
b := &ExternalNameApplyConfiguration{} |
||||
b.WithName(name) |
||||
b.WithNamespace(namespace) |
||||
b.WithKind("ExternalName") |
||||
b.WithAPIVersion("service.grafana.app/v0alpha1") |
||||
return b |
||||
} |
||||
|
||||
// WithKind sets the Kind field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Kind field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithKind(value string) *ExternalNameApplyConfiguration { |
||||
b.Kind = &value |
||||
return b |
||||
} |
||||
|
||||
// WithAPIVersion sets the APIVersion field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the APIVersion field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithAPIVersion(value string) *ExternalNameApplyConfiguration { |
||||
b.APIVersion = &value |
||||
return b |
||||
} |
||||
|
||||
// WithName sets the Name field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Name field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithName(value string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.Name = &value |
||||
return b |
||||
} |
||||
|
||||
// WithGenerateName sets the GenerateName field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the GenerateName field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithGenerateName(value string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.GenerateName = &value |
||||
return b |
||||
} |
||||
|
||||
// WithNamespace sets the Namespace field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Namespace field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithNamespace(value string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.Namespace = &value |
||||
return b |
||||
} |
||||
|
||||
// WithUID sets the UID field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the UID field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithUID(value types.UID) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.UID = &value |
||||
return b |
||||
} |
||||
|
||||
// WithResourceVersion sets the ResourceVersion field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the ResourceVersion field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithResourceVersion(value string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.ResourceVersion = &value |
||||
return b |
||||
} |
||||
|
||||
// WithGeneration sets the Generation field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Generation field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithGeneration(value int64) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.Generation = &value |
||||
return b |
||||
} |
||||
|
||||
// WithCreationTimestamp sets the CreationTimestamp field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the CreationTimestamp field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithCreationTimestamp(value metav1.Time) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.CreationTimestamp = &value |
||||
return b |
||||
} |
||||
|
||||
// WithDeletionTimestamp sets the DeletionTimestamp field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the DeletionTimestamp field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithDeletionTimestamp(value metav1.Time) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.DeletionTimestamp = &value |
||||
return b |
||||
} |
||||
|
||||
// WithDeletionGracePeriodSeconds sets the DeletionGracePeriodSeconds field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the DeletionGracePeriodSeconds field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithDeletionGracePeriodSeconds(value int64) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
b.DeletionGracePeriodSeconds = &value |
||||
return b |
||||
} |
||||
|
||||
// WithLabels puts the entries into the Labels field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, the entries provided by each call will be put on the Labels field,
|
||||
// overwriting an existing map entries in Labels field with the same key.
|
||||
func (b *ExternalNameApplyConfiguration) WithLabels(entries map[string]string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
if b.Labels == nil && len(entries) > 0 { |
||||
b.Labels = make(map[string]string, len(entries)) |
||||
} |
||||
for k, v := range entries { |
||||
b.Labels[k] = v |
||||
} |
||||
return b |
||||
} |
||||
|
||||
// WithAnnotations puts the entries into the Annotations field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, the entries provided by each call will be put on the Annotations field,
|
||||
// overwriting an existing map entries in Annotations field with the same key.
|
||||
func (b *ExternalNameApplyConfiguration) WithAnnotations(entries map[string]string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
if b.Annotations == nil && len(entries) > 0 { |
||||
b.Annotations = make(map[string]string, len(entries)) |
||||
} |
||||
for k, v := range entries { |
||||
b.Annotations[k] = v |
||||
} |
||||
return b |
||||
} |
||||
|
||||
// WithOwnerReferences adds the given value to the OwnerReferences field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, values provided by each call will be appended to the OwnerReferences field.
|
||||
func (b *ExternalNameApplyConfiguration) WithOwnerReferences(values ...*v1.OwnerReferenceApplyConfiguration) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
for i := range values { |
||||
if values[i] == nil { |
||||
panic("nil value passed to WithOwnerReferences") |
||||
} |
||||
b.OwnerReferences = append(b.OwnerReferences, *values[i]) |
||||
} |
||||
return b |
||||
} |
||||
|
||||
// WithFinalizers adds the given value to the Finalizers field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, values provided by each call will be appended to the Finalizers field.
|
||||
func (b *ExternalNameApplyConfiguration) WithFinalizers(values ...string) *ExternalNameApplyConfiguration { |
||||
b.ensureObjectMetaApplyConfigurationExists() |
||||
for i := range values { |
||||
b.Finalizers = append(b.Finalizers, values[i]) |
||||
} |
||||
return b |
||||
} |
||||
|
||||
func (b *ExternalNameApplyConfiguration) ensureObjectMetaApplyConfigurationExists() { |
||||
if b.ObjectMetaApplyConfiguration == nil { |
||||
b.ObjectMetaApplyConfiguration = &v1.ObjectMetaApplyConfiguration{} |
||||
} |
||||
} |
||||
|
||||
// WithSpec sets the Spec field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Spec field is set to the value of the last call.
|
||||
func (b *ExternalNameApplyConfiguration) WithSpec(value *ExternalNameSpecApplyConfiguration) *ExternalNameApplyConfiguration { |
||||
b.Spec = value |
||||
return b |
||||
} |
@ -0,0 +1,25 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by applyconfiguration-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
// ExternalNameSpecApplyConfiguration represents an declarative configuration of the ExternalNameSpec type for use
|
||||
// with apply.
|
||||
type ExternalNameSpecApplyConfiguration struct { |
||||
Host *string `json:"host,omitempty"` |
||||
} |
||||
|
||||
// ExternalNameSpecApplyConfiguration constructs an declarative configuration of the ExternalNameSpec type for use with
|
||||
// apply.
|
||||
func ExternalNameSpec() *ExternalNameSpecApplyConfiguration { |
||||
return &ExternalNameSpecApplyConfiguration{} |
||||
} |
||||
|
||||
// WithHost sets the Host field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Host field is set to the value of the last call.
|
||||
func (b *ExternalNameSpecApplyConfiguration) WithHost(value string) *ExternalNameSpecApplyConfiguration { |
||||
b.Host = &value |
||||
return b |
||||
} |
@ -0,0 +1,25 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by applyconfiguration-gen. DO NOT EDIT.
|
||||
|
||||
package applyconfiguration |
||||
|
||||
import ( |
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/generated/applyconfiguration/service/v0alpha1" |
||||
schema "k8s.io/apimachinery/pkg/runtime/schema" |
||||
) |
||||
|
||||
// ForKind returns an apply configuration type for the given GroupVersionKind, or nil if no
|
||||
// apply configuration type exists for the given GroupVersionKind.
|
||||
func ForKind(kind schema.GroupVersionKind) interface{} { |
||||
switch kind { |
||||
// Group=service.grafana.app, Version=v0alpha1
|
||||
case v0alpha1.SchemeGroupVersion.WithKind("ExternalName"): |
||||
return &servicev0alpha1.ExternalNameApplyConfiguration{} |
||||
case v0alpha1.SchemeGroupVersion.WithKind("ExternalNameSpec"): |
||||
return &servicev0alpha1.ExternalNameSpecApplyConfiguration{} |
||||
|
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,106 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package versioned |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net/http" |
||||
|
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/generated/clientset/versioned/typed/service/v0alpha1" |
||||
discovery "k8s.io/client-go/discovery" |
||||
rest "k8s.io/client-go/rest" |
||||
flowcontrol "k8s.io/client-go/util/flowcontrol" |
||||
) |
||||
|
||||
type Interface interface { |
||||
Discovery() discovery.DiscoveryInterface |
||||
ServiceV0alpha1() servicev0alpha1.ServiceV0alpha1Interface |
||||
} |
||||
|
||||
// Clientset contains the clients for groups.
|
||||
type Clientset struct { |
||||
*discovery.DiscoveryClient |
||||
serviceV0alpha1 *servicev0alpha1.ServiceV0alpha1Client |
||||
} |
||||
|
||||
// ServiceV0alpha1 retrieves the ServiceV0alpha1Client
|
||||
func (c *Clientset) ServiceV0alpha1() servicev0alpha1.ServiceV0alpha1Interface { |
||||
return c.serviceV0alpha1 |
||||
} |
||||
|
||||
// Discovery retrieves the DiscoveryClient
|
||||
func (c *Clientset) Discovery() discovery.DiscoveryInterface { |
||||
if c == nil { |
||||
return nil |
||||
} |
||||
return c.DiscoveryClient |
||||
} |
||||
|
||||
// NewForConfig creates a new Clientset for the given config.
|
||||
// If config's RateLimiter is not set and QPS and Burst are acceptable,
|
||||
// NewForConfig will generate a rate-limiter in configShallowCopy.
|
||||
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
|
||||
// where httpClient was generated with rest.HTTPClientFor(c).
|
||||
func NewForConfig(c *rest.Config) (*Clientset, error) { |
||||
configShallowCopy := *c |
||||
|
||||
if configShallowCopy.UserAgent == "" { |
||||
configShallowCopy.UserAgent = rest.DefaultKubernetesUserAgent() |
||||
} |
||||
|
||||
// share the transport between all clients
|
||||
httpClient, err := rest.HTTPClientFor(&configShallowCopy) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return NewForConfigAndClient(&configShallowCopy, httpClient) |
||||
} |
||||
|
||||
// NewForConfigAndClient creates a new Clientset for the given config and http client.
|
||||
// Note the http client provided takes precedence over the configured transport values.
|
||||
// If config's RateLimiter is not set and QPS and Burst are acceptable,
|
||||
// NewForConfigAndClient will generate a rate-limiter in configShallowCopy.
|
||||
func NewForConfigAndClient(c *rest.Config, httpClient *http.Client) (*Clientset, error) { |
||||
configShallowCopy := *c |
||||
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 { |
||||
if configShallowCopy.Burst <= 0 { |
||||
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0") |
||||
} |
||||
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst) |
||||
} |
||||
|
||||
var cs Clientset |
||||
var err error |
||||
cs.serviceV0alpha1, err = servicev0alpha1.NewForConfigAndClient(&configShallowCopy, httpClient) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
cs.DiscoveryClient, err = discovery.NewDiscoveryClientForConfigAndClient(&configShallowCopy, httpClient) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &cs, nil |
||||
} |
||||
|
||||
// NewForConfigOrDie creates a new Clientset for the given config and
|
||||
// panics if there is an error in the config.
|
||||
func NewForConfigOrDie(c *rest.Config) *Clientset { |
||||
cs, err := NewForConfig(c) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return cs |
||||
} |
||||
|
||||
// New creates a new Clientset for the given RESTClient.
|
||||
func New(c rest.Interface) *Clientset { |
||||
var cs Clientset |
||||
cs.serviceV0alpha1 = servicev0alpha1.New(c) |
||||
|
||||
cs.DiscoveryClient = discovery.NewDiscoveryClient(c) |
||||
return &cs |
||||
} |
@ -0,0 +1,71 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package fake |
||||
|
||||
import ( |
||||
clientset "github.com/grafana/grafana/pkg/generated/clientset/versioned" |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/generated/clientset/versioned/typed/service/v0alpha1" |
||||
fakeservicev0alpha1 "github.com/grafana/grafana/pkg/generated/clientset/versioned/typed/service/v0alpha1/fake" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/watch" |
||||
"k8s.io/client-go/discovery" |
||||
fakediscovery "k8s.io/client-go/discovery/fake" |
||||
"k8s.io/client-go/testing" |
||||
) |
||||
|
||||
// NewSimpleClientset returns a clientset that will respond with the provided objects.
|
||||
// It's backed by a very simple object tracker that processes creates, updates and deletions as-is,
|
||||
// without applying any validations and/or defaults. It shouldn't be considered a replacement
|
||||
// for a real clientset and is mostly useful in simple unit tests.
|
||||
func NewSimpleClientset(objects ...runtime.Object) *Clientset { |
||||
o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder()) |
||||
for _, obj := range objects { |
||||
if err := o.Add(obj); err != nil { |
||||
panic(err) |
||||
} |
||||
} |
||||
|
||||
cs := &Clientset{tracker: o} |
||||
cs.discovery = &fakediscovery.FakeDiscovery{Fake: &cs.Fake} |
||||
cs.AddReactor("*", "*", testing.ObjectReaction(o)) |
||||
cs.AddWatchReactor("*", func(action testing.Action) (handled bool, ret watch.Interface, err error) { |
||||
gvr := action.GetResource() |
||||
ns := action.GetNamespace() |
||||
watch, err := o.Watch(gvr, ns) |
||||
if err != nil { |
||||
return false, nil, err |
||||
} |
||||
return true, watch, nil |
||||
}) |
||||
|
||||
return cs |
||||
} |
||||
|
||||
// Clientset implements clientset.Interface. Meant to be embedded into a
|
||||
// struct to get a default implementation. This makes faking out just the method
|
||||
// you want to test easier.
|
||||
type Clientset struct { |
||||
testing.Fake |
||||
discovery *fakediscovery.FakeDiscovery |
||||
tracker testing.ObjectTracker |
||||
} |
||||
|
||||
func (c *Clientset) Discovery() discovery.DiscoveryInterface { |
||||
return c.discovery |
||||
} |
||||
|
||||
func (c *Clientset) Tracker() testing.ObjectTracker { |
||||
return c.tracker |
||||
} |
||||
|
||||
var ( |
||||
_ clientset.Interface = &Clientset{} |
||||
_ testing.FakeClient = &Clientset{} |
||||
) |
||||
|
||||
// ServiceV0alpha1 retrieves the ServiceV0alpha1Client
|
||||
func (c *Clientset) ServiceV0alpha1() servicev0alpha1.ServiceV0alpha1Interface { |
||||
return &fakeservicev0alpha1.FakeServiceV0alpha1{Fake: &c.Fake} |
||||
} |
@ -0,0 +1,6 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
// This package has the automatically generated fake clientset.
|
||||
package fake |
@ -0,0 +1,42 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package fake |
||||
|
||||
import ( |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
schema "k8s.io/apimachinery/pkg/runtime/schema" |
||||
serializer "k8s.io/apimachinery/pkg/runtime/serializer" |
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
||||
) |
||||
|
||||
var scheme = runtime.NewScheme() |
||||
var codecs = serializer.NewCodecFactory(scheme) |
||||
|
||||
var localSchemeBuilder = runtime.SchemeBuilder{ |
||||
servicev0alpha1.AddToScheme, |
||||
} |
||||
|
||||
// AddToScheme adds all types of this clientset into the given scheme. This allows composition
|
||||
// of clientsets, like in:
|
||||
//
|
||||
// import (
|
||||
// "k8s.io/client-go/kubernetes"
|
||||
// clientsetscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
|
||||
// )
|
||||
//
|
||||
// kclientset, _ := kubernetes.NewForConfig(c)
|
||||
// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
|
||||
//
|
||||
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
|
||||
// correctly.
|
||||
var AddToScheme = localSchemeBuilder.AddToScheme |
||||
|
||||
func init() { |
||||
v1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"}) |
||||
utilruntime.Must(AddToScheme(scheme)) |
||||
} |
@ -0,0 +1,6 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
// This package contains the scheme of the automatically generated clientset.
|
||||
package scheme |
@ -0,0 +1,42 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package scheme |
||||
|
||||
import ( |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
schema "k8s.io/apimachinery/pkg/runtime/schema" |
||||
serializer "k8s.io/apimachinery/pkg/runtime/serializer" |
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
||||
) |
||||
|
||||
var Scheme = runtime.NewScheme() |
||||
var Codecs = serializer.NewCodecFactory(Scheme) |
||||
var ParameterCodec = runtime.NewParameterCodec(Scheme) |
||||
var localSchemeBuilder = runtime.SchemeBuilder{ |
||||
servicev0alpha1.AddToScheme, |
||||
} |
||||
|
||||
// AddToScheme adds all types of this clientset into the given scheme. This allows composition
|
||||
// of clientsets, like in:
|
||||
//
|
||||
// import (
|
||||
// "k8s.io/client-go/kubernetes"
|
||||
// clientsetscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
|
||||
// )
|
||||
//
|
||||
// kclientset, _ := kubernetes.NewForConfig(c)
|
||||
// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
|
||||
//
|
||||
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
|
||||
// correctly.
|
||||
var AddToScheme = localSchemeBuilder.AddToScheme |
||||
|
||||
func init() { |
||||
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"}) |
||||
utilruntime.Must(AddToScheme(Scheme)) |
||||
} |
@ -0,0 +1,6 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
// This package has the automatically generated typed clients.
|
||||
package v0alpha1 |
@ -0,0 +1,194 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
"context" |
||||
json "encoding/json" |
||||
"fmt" |
||||
"time" |
||||
|
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/generated/applyconfiguration/service/v0alpha1" |
||||
scheme "github.com/grafana/grafana/pkg/generated/clientset/versioned/scheme" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
types "k8s.io/apimachinery/pkg/types" |
||||
watch "k8s.io/apimachinery/pkg/watch" |
||||
rest "k8s.io/client-go/rest" |
||||
) |
||||
|
||||
// ExternalNamesGetter has a method to return a ExternalNameInterface.
|
||||
// A group's client should implement this interface.
|
||||
type ExternalNamesGetter interface { |
||||
ExternalNames(namespace string) ExternalNameInterface |
||||
} |
||||
|
||||
// ExternalNameInterface has methods to work with ExternalName resources.
|
||||
type ExternalNameInterface interface { |
||||
Create(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.CreateOptions) (*v0alpha1.ExternalName, error) |
||||
Update(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.UpdateOptions) (*v0alpha1.ExternalName, error) |
||||
Delete(ctx context.Context, name string, opts v1.DeleteOptions) error |
||||
DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error |
||||
Get(ctx context.Context, name string, opts v1.GetOptions) (*v0alpha1.ExternalName, error) |
||||
List(ctx context.Context, opts v1.ListOptions) (*v0alpha1.ExternalNameList, error) |
||||
Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) |
||||
Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v0alpha1.ExternalName, err error) |
||||
Apply(ctx context.Context, externalName *servicev0alpha1.ExternalNameApplyConfiguration, opts v1.ApplyOptions) (result *v0alpha1.ExternalName, err error) |
||||
ExternalNameExpansion |
||||
} |
||||
|
||||
// externalNames implements ExternalNameInterface
|
||||
type externalNames struct { |
||||
client rest.Interface |
||||
ns string |
||||
} |
||||
|
||||
// newExternalNames returns a ExternalNames
|
||||
func newExternalNames(c *ServiceV0alpha1Client, namespace string) *externalNames { |
||||
return &externalNames{ |
||||
client: c.RESTClient(), |
||||
ns: namespace, |
||||
} |
||||
} |
||||
|
||||
// Get takes name of the externalName, and returns the corresponding externalName object, and an error if there is any.
|
||||
func (c *externalNames) Get(ctx context.Context, name string, options v1.GetOptions) (result *v0alpha1.ExternalName, err error) { |
||||
result = &v0alpha1.ExternalName{} |
||||
err = c.client.Get(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
Name(name). |
||||
VersionedParams(&options, scheme.ParameterCodec). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
||||
|
||||
// List takes label and field selectors, and returns the list of ExternalNames that match those selectors.
|
||||
func (c *externalNames) List(ctx context.Context, opts v1.ListOptions) (result *v0alpha1.ExternalNameList, err error) { |
||||
var timeout time.Duration |
||||
if opts.TimeoutSeconds != nil { |
||||
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second |
||||
} |
||||
result = &v0alpha1.ExternalNameList{} |
||||
err = c.client.Get(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
VersionedParams(&opts, scheme.ParameterCodec). |
||||
Timeout(timeout). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
||||
|
||||
// Watch returns a watch.Interface that watches the requested externalNames.
|
||||
func (c *externalNames) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { |
||||
var timeout time.Duration |
||||
if opts.TimeoutSeconds != nil { |
||||
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second |
||||
} |
||||
opts.Watch = true |
||||
return c.client.Get(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
VersionedParams(&opts, scheme.ParameterCodec). |
||||
Timeout(timeout). |
||||
Watch(ctx) |
||||
} |
||||
|
||||
// Create takes the representation of a externalName and creates it. Returns the server's representation of the externalName, and an error, if there is any.
|
||||
func (c *externalNames) Create(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.CreateOptions) (result *v0alpha1.ExternalName, err error) { |
||||
result = &v0alpha1.ExternalName{} |
||||
err = c.client.Post(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
VersionedParams(&opts, scheme.ParameterCodec). |
||||
Body(externalName). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
||||
|
||||
// Update takes the representation of a externalName and updates it. Returns the server's representation of the externalName, and an error, if there is any.
|
||||
func (c *externalNames) Update(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.UpdateOptions) (result *v0alpha1.ExternalName, err error) { |
||||
result = &v0alpha1.ExternalName{} |
||||
err = c.client.Put(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
Name(externalName.Name). |
||||
VersionedParams(&opts, scheme.ParameterCodec). |
||||
Body(externalName). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
||||
|
||||
// Delete takes name of the externalName and deletes it. Returns an error if one occurs.
|
||||
func (c *externalNames) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { |
||||
return c.client.Delete(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
Name(name). |
||||
Body(&opts). |
||||
Do(ctx). |
||||
Error() |
||||
} |
||||
|
||||
// DeleteCollection deletes a collection of objects.
|
||||
func (c *externalNames) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { |
||||
var timeout time.Duration |
||||
if listOpts.TimeoutSeconds != nil { |
||||
timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second |
||||
} |
||||
return c.client.Delete(). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
VersionedParams(&listOpts, scheme.ParameterCodec). |
||||
Timeout(timeout). |
||||
Body(&opts). |
||||
Do(ctx). |
||||
Error() |
||||
} |
||||
|
||||
// Patch applies the patch and returns the patched externalName.
|
||||
func (c *externalNames) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v0alpha1.ExternalName, err error) { |
||||
result = &v0alpha1.ExternalName{} |
||||
err = c.client.Patch(pt). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
Name(name). |
||||
SubResource(subresources...). |
||||
VersionedParams(&opts, scheme.ParameterCodec). |
||||
Body(data). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
||||
|
||||
// Apply takes the given apply declarative configuration, applies it and returns the applied externalName.
|
||||
func (c *externalNames) Apply(ctx context.Context, externalName *servicev0alpha1.ExternalNameApplyConfiguration, opts v1.ApplyOptions) (result *v0alpha1.ExternalName, err error) { |
||||
if externalName == nil { |
||||
return nil, fmt.Errorf("externalName provided to Apply must not be nil") |
||||
} |
||||
patchOpts := opts.ToPatchOptions() |
||||
data, err := json.Marshal(externalName) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
name := externalName.Name |
||||
if name == nil { |
||||
return nil, fmt.Errorf("externalName.Name must be provided to Apply") |
||||
} |
||||
result = &v0alpha1.ExternalName{} |
||||
err = c.client.Patch(types.ApplyPatchType). |
||||
Namespace(c.ns). |
||||
Resource("externalnames"). |
||||
Name(*name). |
||||
VersionedParams(&patchOpts, scheme.ParameterCodec). |
||||
Body(data). |
||||
Do(ctx). |
||||
Into(result) |
||||
return |
||||
} |
@ -0,0 +1,6 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
// Package fake has the automatically generated clients.
|
||||
package fake |
@ -0,0 +1,140 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package fake |
||||
|
||||
import ( |
||||
"context" |
||||
json "encoding/json" |
||||
"fmt" |
||||
|
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/generated/applyconfiguration/service/v0alpha1" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
labels "k8s.io/apimachinery/pkg/labels" |
||||
types "k8s.io/apimachinery/pkg/types" |
||||
watch "k8s.io/apimachinery/pkg/watch" |
||||
testing "k8s.io/client-go/testing" |
||||
) |
||||
|
||||
// FakeExternalNames implements ExternalNameInterface
|
||||
type FakeExternalNames struct { |
||||
Fake *FakeServiceV0alpha1 |
||||
ns string |
||||
} |
||||
|
||||
var externalnamesResource = v0alpha1.SchemeGroupVersion.WithResource("externalnames") |
||||
|
||||
var externalnamesKind = v0alpha1.SchemeGroupVersion.WithKind("ExternalName") |
||||
|
||||
// Get takes name of the externalName, and returns the corresponding externalName object, and an error if there is any.
|
||||
func (c *FakeExternalNames) Get(ctx context.Context, name string, options v1.GetOptions) (result *v0alpha1.ExternalName, err error) { |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewGetAction(externalnamesResource, c.ns, name), &v0alpha1.ExternalName{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), err |
||||
} |
||||
|
||||
// List takes label and field selectors, and returns the list of ExternalNames that match those selectors.
|
||||
func (c *FakeExternalNames) List(ctx context.Context, opts v1.ListOptions) (result *v0alpha1.ExternalNameList, err error) { |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewListAction(externalnamesResource, externalnamesKind, c.ns, opts), &v0alpha1.ExternalNameList{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
|
||||
label, _, _ := testing.ExtractFromListOptions(opts) |
||||
if label == nil { |
||||
label = labels.Everything() |
||||
} |
||||
list := &v0alpha1.ExternalNameList{ListMeta: obj.(*v0alpha1.ExternalNameList).ListMeta} |
||||
for _, item := range obj.(*v0alpha1.ExternalNameList).Items { |
||||
if label.Matches(labels.Set(item.Labels)) { |
||||
list.Items = append(list.Items, item) |
||||
} |
||||
} |
||||
return list, err |
||||
} |
||||
|
||||
// Watch returns a watch.Interface that watches the requested externalNames.
|
||||
func (c *FakeExternalNames) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { |
||||
return c.Fake. |
||||
InvokesWatch(testing.NewWatchAction(externalnamesResource, c.ns, opts)) |
||||
|
||||
} |
||||
|
||||
// Create takes the representation of a externalName and creates it. Returns the server's representation of the externalName, and an error, if there is any.
|
||||
func (c *FakeExternalNames) Create(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.CreateOptions) (result *v0alpha1.ExternalName, err error) { |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewCreateAction(externalnamesResource, c.ns, externalName), &v0alpha1.ExternalName{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), err |
||||
} |
||||
|
||||
// Update takes the representation of a externalName and updates it. Returns the server's representation of the externalName, and an error, if there is any.
|
||||
func (c *FakeExternalNames) Update(ctx context.Context, externalName *v0alpha1.ExternalName, opts v1.UpdateOptions) (result *v0alpha1.ExternalName, err error) { |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewUpdateAction(externalnamesResource, c.ns, externalName), &v0alpha1.ExternalName{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), err |
||||
} |
||||
|
||||
// Delete takes name of the externalName and deletes it. Returns an error if one occurs.
|
||||
func (c *FakeExternalNames) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { |
||||
_, err := c.Fake. |
||||
Invokes(testing.NewDeleteActionWithOptions(externalnamesResource, c.ns, name, opts), &v0alpha1.ExternalName{}) |
||||
|
||||
return err |
||||
} |
||||
|
||||
// DeleteCollection deletes a collection of objects.
|
||||
func (c *FakeExternalNames) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { |
||||
action := testing.NewDeleteCollectionAction(externalnamesResource, c.ns, listOpts) |
||||
|
||||
_, err := c.Fake.Invokes(action, &v0alpha1.ExternalNameList{}) |
||||
return err |
||||
} |
||||
|
||||
// Patch applies the patch and returns the patched externalName.
|
||||
func (c *FakeExternalNames) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v0alpha1.ExternalName, err error) { |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewPatchSubresourceAction(externalnamesResource, c.ns, name, pt, data, subresources...), &v0alpha1.ExternalName{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), err |
||||
} |
||||
|
||||
// Apply takes the given apply declarative configuration, applies it and returns the applied externalName.
|
||||
func (c *FakeExternalNames) Apply(ctx context.Context, externalName *servicev0alpha1.ExternalNameApplyConfiguration, opts v1.ApplyOptions) (result *v0alpha1.ExternalName, err error) { |
||||
if externalName == nil { |
||||
return nil, fmt.Errorf("externalName provided to Apply must not be nil") |
||||
} |
||||
data, err := json.Marshal(externalName) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
name := externalName.Name |
||||
if name == nil { |
||||
return nil, fmt.Errorf("externalName.Name must be provided to Apply") |
||||
} |
||||
obj, err := c.Fake. |
||||
Invokes(testing.NewPatchSubresourceAction(externalnamesResource, c.ns, *name, types.ApplyPatchType, data), &v0alpha1.ExternalName{}) |
||||
|
||||
if obj == nil { |
||||
return nil, err |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), err |
||||
} |
@ -0,0 +1,26 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package fake |
||||
|
||||
import ( |
||||
v0alpha1 "github.com/grafana/grafana/pkg/generated/clientset/versioned/typed/service/v0alpha1" |
||||
rest "k8s.io/client-go/rest" |
||||
testing "k8s.io/client-go/testing" |
||||
) |
||||
|
||||
type FakeServiceV0alpha1 struct { |
||||
*testing.Fake |
||||
} |
||||
|
||||
func (c *FakeServiceV0alpha1) ExternalNames(namespace string) v0alpha1.ExternalNameInterface { |
||||
return &FakeExternalNames{c, namespace} |
||||
} |
||||
|
||||
// RESTClient returns a RESTClient that is used to communicate
|
||||
// with API server by this client implementation.
|
||||
func (c *FakeServiceV0alpha1) RESTClient() rest.Interface { |
||||
var ret *rest.RESTClient |
||||
return ret |
||||
} |
@ -0,0 +1,7 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
type ExternalNameExpansion interface{} |
@ -0,0 +1,93 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by client-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
"github.com/grafana/grafana/pkg/generated/clientset/versioned/scheme" |
||||
rest "k8s.io/client-go/rest" |
||||
) |
||||
|
||||
type ServiceV0alpha1Interface interface { |
||||
RESTClient() rest.Interface |
||||
ExternalNamesGetter |
||||
} |
||||
|
||||
// ServiceV0alpha1Client is used to interact with features provided by the service.grafana.app group.
|
||||
type ServiceV0alpha1Client struct { |
||||
restClient rest.Interface |
||||
} |
||||
|
||||
func (c *ServiceV0alpha1Client) ExternalNames(namespace string) ExternalNameInterface { |
||||
return newExternalNames(c, namespace) |
||||
} |
||||
|
||||
// NewForConfig creates a new ServiceV0alpha1Client for the given config.
|
||||
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
|
||||
// where httpClient was generated with rest.HTTPClientFor(c).
|
||||
func NewForConfig(c *rest.Config) (*ServiceV0alpha1Client, error) { |
||||
config := *c |
||||
if err := setConfigDefaults(&config); err != nil { |
||||
return nil, err |
||||
} |
||||
httpClient, err := rest.HTTPClientFor(&config) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return NewForConfigAndClient(&config, httpClient) |
||||
} |
||||
|
||||
// NewForConfigAndClient creates a new ServiceV0alpha1Client for the given config and http client.
|
||||
// Note the http client provided takes precedence over the configured transport values.
|
||||
func NewForConfigAndClient(c *rest.Config, h *http.Client) (*ServiceV0alpha1Client, error) { |
||||
config := *c |
||||
if err := setConfigDefaults(&config); err != nil { |
||||
return nil, err |
||||
} |
||||
client, err := rest.RESTClientForConfigAndClient(&config, h) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &ServiceV0alpha1Client{client}, nil |
||||
} |
||||
|
||||
// NewForConfigOrDie creates a new ServiceV0alpha1Client for the given config and
|
||||
// panics if there is an error in the config.
|
||||
func NewForConfigOrDie(c *rest.Config) *ServiceV0alpha1Client { |
||||
client, err := NewForConfig(c) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return client |
||||
} |
||||
|
||||
// New creates a new ServiceV0alpha1Client for the given RESTClient.
|
||||
func New(c rest.Interface) *ServiceV0alpha1Client { |
||||
return &ServiceV0alpha1Client{c} |
||||
} |
||||
|
||||
func setConfigDefaults(config *rest.Config) error { |
||||
gv := v0alpha1.SchemeGroupVersion |
||||
config.GroupVersion = &gv |
||||
config.APIPath = "/apis" |
||||
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion() |
||||
|
||||
if config.UserAgent == "" { |
||||
config.UserAgent = rest.DefaultKubernetesUserAgent() |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// RESTClient returns a RESTClient that is used to communicate
|
||||
// with API server by this client implementation.
|
||||
func (c *ServiceV0alpha1Client) RESTClient() rest.Interface { |
||||
if c == nil { |
||||
return nil |
||||
} |
||||
return c.restClient |
||||
} |
@ -0,0 +1,237 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package externalversions |
||||
|
||||
import ( |
||||
reflect "reflect" |
||||
sync "sync" |
||||
time "time" |
||||
|
||||
versioned "github.com/grafana/grafana/pkg/generated/clientset/versioned" |
||||
internalinterfaces "github.com/grafana/grafana/pkg/generated/informers/externalversions/internalinterfaces" |
||||
service "github.com/grafana/grafana/pkg/generated/informers/externalversions/service" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
schema "k8s.io/apimachinery/pkg/runtime/schema" |
||||
cache "k8s.io/client-go/tools/cache" |
||||
) |
||||
|
||||
// SharedInformerOption defines the functional option type for SharedInformerFactory.
|
||||
type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory |
||||
|
||||
type sharedInformerFactory struct { |
||||
client versioned.Interface |
||||
namespace string |
||||
tweakListOptions internalinterfaces.TweakListOptionsFunc |
||||
lock sync.Mutex |
||||
defaultResync time.Duration |
||||
customResync map[reflect.Type]time.Duration |
||||
|
||||
informers map[reflect.Type]cache.SharedIndexInformer |
||||
// startedInformers is used for tracking which informers have been started.
|
||||
// This allows Start() to be called multiple times safely.
|
||||
startedInformers map[reflect.Type]bool |
||||
// wg tracks how many goroutines were started.
|
||||
wg sync.WaitGroup |
||||
// shuttingDown is true when Shutdown has been called. It may still be running
|
||||
// because it needs to wait for goroutines.
|
||||
shuttingDown bool |
||||
} |
||||
|
||||
// WithCustomResyncConfig sets a custom resync period for the specified informer types.
|
||||
func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption { |
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory { |
||||
for k, v := range resyncConfig { |
||||
factory.customResync[reflect.TypeOf(k)] = v |
||||
} |
||||
return factory |
||||
} |
||||
} |
||||
|
||||
// WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
|
||||
func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption { |
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory { |
||||
factory.tweakListOptions = tweakListOptions |
||||
return factory |
||||
} |
||||
} |
||||
|
||||
// WithNamespace limits the SharedInformerFactory to the specified namespace.
|
||||
func WithNamespace(namespace string) SharedInformerOption { |
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory { |
||||
factory.namespace = namespace |
||||
return factory |
||||
} |
||||
} |
||||
|
||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
|
||||
func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory { |
||||
return NewSharedInformerFactoryWithOptions(client, defaultResync) |
||||
} |
||||
|
||||
// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
|
||||
// Listers obtained via this SharedInformerFactory will be subject to the same filters
|
||||
// as specified here.
|
||||
// Deprecated: Please use NewSharedInformerFactoryWithOptions instead
|
||||
func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { |
||||
return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions)) |
||||
} |
||||
|
||||
// NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
|
||||
func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory { |
||||
factory := &sharedInformerFactory{ |
||||
client: client, |
||||
namespace: v1.NamespaceAll, |
||||
defaultResync: defaultResync, |
||||
informers: make(map[reflect.Type]cache.SharedIndexInformer), |
||||
startedInformers: make(map[reflect.Type]bool), |
||||
customResync: make(map[reflect.Type]time.Duration), |
||||
} |
||||
|
||||
// Apply all options
|
||||
for _, opt := range options { |
||||
factory = opt(factory) |
||||
} |
||||
|
||||
return factory |
||||
} |
||||
|
||||
func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) { |
||||
f.lock.Lock() |
||||
defer f.lock.Unlock() |
||||
|
||||
if f.shuttingDown { |
||||
return |
||||
} |
||||
|
||||
for informerType, informer := range f.informers { |
||||
if !f.startedInformers[informerType] { |
||||
f.wg.Add(1) |
||||
// We need a new variable in each loop iteration,
|
||||
// otherwise the goroutine would use the loop variable
|
||||
// and that keeps changing.
|
||||
informer := informer |
||||
go func() { |
||||
defer f.wg.Done() |
||||
informer.Run(stopCh) |
||||
}() |
||||
f.startedInformers[informerType] = true |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (f *sharedInformerFactory) Shutdown() { |
||||
f.lock.Lock() |
||||
f.shuttingDown = true |
||||
f.lock.Unlock() |
||||
|
||||
// Will return immediately if there is nothing to wait for.
|
||||
f.wg.Wait() |
||||
} |
||||
|
||||
func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool { |
||||
informers := func() map[reflect.Type]cache.SharedIndexInformer { |
||||
f.lock.Lock() |
||||
defer f.lock.Unlock() |
||||
|
||||
informers := map[reflect.Type]cache.SharedIndexInformer{} |
||||
for informerType, informer := range f.informers { |
||||
if f.startedInformers[informerType] { |
||||
informers[informerType] = informer |
||||
} |
||||
} |
||||
return informers |
||||
}() |
||||
|
||||
res := map[reflect.Type]bool{} |
||||
for informType, informer := range informers { |
||||
res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// InternalInformerFor returns the SharedIndexInformer for obj using an internal
|
||||
// client.
|
||||
func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { |
||||
f.lock.Lock() |
||||
defer f.lock.Unlock() |
||||
|
||||
informerType := reflect.TypeOf(obj) |
||||
informer, exists := f.informers[informerType] |
||||
if exists { |
||||
return informer |
||||
} |
||||
|
||||
resyncPeriod, exists := f.customResync[informerType] |
||||
if !exists { |
||||
resyncPeriod = f.defaultResync |
||||
} |
||||
|
||||
informer = newFunc(f.client, resyncPeriod) |
||||
f.informers[informerType] = informer |
||||
|
||||
return informer |
||||
} |
||||
|
||||
// SharedInformerFactory provides shared informers for resources in all known
|
||||
// API group versions.
|
||||
//
|
||||
// It is typically used like this:
|
||||
//
|
||||
// ctx, cancel := context.Background()
|
||||
// defer cancel()
|
||||
// factory := NewSharedInformerFactory(client, resyncPeriod)
|
||||
// defer factory.WaitForStop() // Returns immediately if nothing was started.
|
||||
// genericInformer := factory.ForResource(resource)
|
||||
// typedInformer := factory.SomeAPIGroup().V1().SomeType()
|
||||
// factory.Start(ctx.Done()) // Start processing these informers.
|
||||
// synced := factory.WaitForCacheSync(ctx.Done())
|
||||
// for v, ok := range synced {
|
||||
// if !ok {
|
||||
// fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Creating informers can also be created after Start, but then
|
||||
// // Start must be called again:
|
||||
// anotherGenericInformer := factory.ForResource(resource)
|
||||
// factory.Start(ctx.Done())
|
||||
type SharedInformerFactory interface { |
||||
internalinterfaces.SharedInformerFactory |
||||
|
||||
// Start initializes all requested informers. They are handled in goroutines
|
||||
// which run until the stop channel gets closed.
|
||||
Start(stopCh <-chan struct{}) |
||||
|
||||
// Shutdown marks a factory as shutting down. At that point no new
|
||||
// informers can be started anymore and Start will return without
|
||||
// doing anything.
|
||||
//
|
||||
// In addition, Shutdown blocks until all goroutines have terminated. For that
|
||||
// to happen, the close channel(s) that they were started with must be closed,
|
||||
// either before Shutdown gets called or while it is waiting.
|
||||
//
|
||||
// Shutdown may be called multiple times, even concurrently. All such calls will
|
||||
// block until all goroutines have terminated.
|
||||
Shutdown() |
||||
|
||||
// WaitForCacheSync blocks until all started informers' caches were synced
|
||||
// or the stop channel gets closed.
|
||||
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool |
||||
|
||||
// ForResource gives generic access to a shared informer of the matching type.
|
||||
ForResource(resource schema.GroupVersionResource) (GenericInformer, error) |
||||
|
||||
// InternalInformerFor returns the SharedIndexInformer for obj using an internal
|
||||
// client.
|
||||
InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer |
||||
|
||||
Service() service.Interface |
||||
} |
||||
|
||||
func (f *sharedInformerFactory) Service() service.Interface { |
||||
return service.New(f, f.namespace, f.tweakListOptions) |
||||
} |
@ -0,0 +1,48 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package externalversions |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
schema "k8s.io/apimachinery/pkg/runtime/schema" |
||||
cache "k8s.io/client-go/tools/cache" |
||||
) |
||||
|
||||
// GenericInformer is type of SharedIndexInformer which will locate and delegate to other
|
||||
// sharedInformers based on type
|
||||
type GenericInformer interface { |
||||
Informer() cache.SharedIndexInformer |
||||
Lister() cache.GenericLister |
||||
} |
||||
|
||||
type genericInformer struct { |
||||
informer cache.SharedIndexInformer |
||||
resource schema.GroupResource |
||||
} |
||||
|
||||
// Informer returns the SharedIndexInformer.
|
||||
func (f *genericInformer) Informer() cache.SharedIndexInformer { |
||||
return f.informer |
||||
} |
||||
|
||||
// Lister returns the GenericLister.
|
||||
func (f *genericInformer) Lister() cache.GenericLister { |
||||
return cache.NewGenericLister(f.Informer().GetIndexer(), f.resource) |
||||
} |
||||
|
||||
// ForResource gives generic access to a shared informer of the matching type
|
||||
// TODO extend this to unknown resources with a client pool
|
||||
func (f *sharedInformerFactory) ForResource(resource schema.GroupVersionResource) (GenericInformer, error) { |
||||
switch resource { |
||||
// Group=service.grafana.app, Version=v0alpha1
|
||||
case v0alpha1.SchemeGroupVersion.WithResource("externalnames"): |
||||
return &genericInformer{resource: resource.GroupResource(), informer: f.Service().V0alpha1().ExternalNames().Informer()}, nil |
||||
|
||||
} |
||||
|
||||
return nil, fmt.Errorf("no informer found for %v", resource) |
||||
} |
@ -0,0 +1,26 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package internalinterfaces |
||||
|
||||
import ( |
||||
time "time" |
||||
|
||||
versioned "github.com/grafana/grafana/pkg/generated/clientset/versioned" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
cache "k8s.io/client-go/tools/cache" |
||||
) |
||||
|
||||
// NewInformerFunc takes versioned.Interface and time.Duration to return a SharedIndexInformer.
|
||||
type NewInformerFunc func(versioned.Interface, time.Duration) cache.SharedIndexInformer |
||||
|
||||
// SharedInformerFactory a small interface to allow for adding an informer without an import cycle
|
||||
type SharedInformerFactory interface { |
||||
Start(stopCh <-chan struct{}) |
||||
InformerFor(obj runtime.Object, newFunc NewInformerFunc) cache.SharedIndexInformer |
||||
} |
||||
|
||||
// TweakListOptionsFunc is a function that transforms a v1.ListOptions.
|
||||
type TweakListOptionsFunc func(*v1.ListOptions) |
@ -0,0 +1,32 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package service |
||||
|
||||
import ( |
||||
internalinterfaces "github.com/grafana/grafana/pkg/generated/informers/externalversions/internalinterfaces" |
||||
v0alpha1 "github.com/grafana/grafana/pkg/generated/informers/externalversions/service/v0alpha1" |
||||
) |
||||
|
||||
// Interface provides access to each of this group's versions.
|
||||
type Interface interface { |
||||
// V0alpha1 provides access to shared informers for resources in V0alpha1.
|
||||
V0alpha1() v0alpha1.Interface |
||||
} |
||||
|
||||
type group struct { |
||||
factory internalinterfaces.SharedInformerFactory |
||||
namespace string |
||||
tweakListOptions internalinterfaces.TweakListOptionsFunc |
||||
} |
||||
|
||||
// New returns a new Interface.
|
||||
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { |
||||
return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} |
||||
} |
||||
|
||||
// V0alpha1 returns a new v0alpha1.Interface.
|
||||
func (g *group) V0alpha1() v0alpha1.Interface { |
||||
return v0alpha1.New(g.factory, g.namespace, g.tweakListOptions) |
||||
} |
@ -0,0 +1,76 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
"context" |
||||
time "time" |
||||
|
||||
servicev0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
versioned "github.com/grafana/grafana/pkg/generated/clientset/versioned" |
||||
internalinterfaces "github.com/grafana/grafana/pkg/generated/informers/externalversions/internalinterfaces" |
||||
v0alpha1 "github.com/grafana/grafana/pkg/generated/listers/service/v0alpha1" |
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
runtime "k8s.io/apimachinery/pkg/runtime" |
||||
watch "k8s.io/apimachinery/pkg/watch" |
||||
cache "k8s.io/client-go/tools/cache" |
||||
) |
||||
|
||||
// ExternalNameInformer provides access to a shared informer and lister for
|
||||
// ExternalNames.
|
||||
type ExternalNameInformer interface { |
||||
Informer() cache.SharedIndexInformer |
||||
Lister() v0alpha1.ExternalNameLister |
||||
} |
||||
|
||||
type externalNameInformer struct { |
||||
factory internalinterfaces.SharedInformerFactory |
||||
tweakListOptions internalinterfaces.TweakListOptionsFunc |
||||
namespace string |
||||
} |
||||
|
||||
// NewExternalNameInformer constructs a new informer for ExternalName type.
|
||||
// Always prefer using an informer factory to get a shared informer instead of getting an independent
|
||||
// one. This reduces memory footprint and number of connections to the server.
|
||||
func NewExternalNameInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { |
||||
return NewFilteredExternalNameInformer(client, namespace, resyncPeriod, indexers, nil) |
||||
} |
||||
|
||||
// NewFilteredExternalNameInformer constructs a new informer for ExternalName type.
|
||||
// Always prefer using an informer factory to get a shared informer instead of getting an independent
|
||||
// one. This reduces memory footprint and number of connections to the server.
|
||||
func NewFilteredExternalNameInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { |
||||
return cache.NewSharedIndexInformer( |
||||
&cache.ListWatch{ |
||||
ListFunc: func(options v1.ListOptions) (runtime.Object, error) { |
||||
if tweakListOptions != nil { |
||||
tweakListOptions(&options) |
||||
} |
||||
return client.ServiceV0alpha1().ExternalNames(namespace).List(context.TODO(), options) |
||||
}, |
||||
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { |
||||
if tweakListOptions != nil { |
||||
tweakListOptions(&options) |
||||
} |
||||
return client.ServiceV0alpha1().ExternalNames(namespace).Watch(context.TODO(), options) |
||||
}, |
||||
}, |
||||
&servicev0alpha1.ExternalName{}, |
||||
resyncPeriod, |
||||
indexers, |
||||
) |
||||
} |
||||
|
||||
func (f *externalNameInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { |
||||
return NewFilteredExternalNameInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) |
||||
} |
||||
|
||||
func (f *externalNameInformer) Informer() cache.SharedIndexInformer { |
||||
return f.factory.InformerFor(&servicev0alpha1.ExternalName{}, f.defaultInformer) |
||||
} |
||||
|
||||
func (f *externalNameInformer) Lister() v0alpha1.ExternalNameLister { |
||||
return v0alpha1.NewExternalNameLister(f.Informer().GetIndexer()) |
||||
} |
@ -0,0 +1,31 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by informer-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
internalinterfaces "github.com/grafana/grafana/pkg/generated/informers/externalversions/internalinterfaces" |
||||
) |
||||
|
||||
// Interface provides access to all the informers in this group version.
|
||||
type Interface interface { |
||||
// ExternalNames returns a ExternalNameInformer.
|
||||
ExternalNames() ExternalNameInformer |
||||
} |
||||
|
||||
type version struct { |
||||
factory internalinterfaces.SharedInformerFactory |
||||
namespace string |
||||
tweakListOptions internalinterfaces.TweakListOptionsFunc |
||||
} |
||||
|
||||
// New returns a new Interface.
|
||||
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { |
||||
return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} |
||||
} |
||||
|
||||
// ExternalNames returns a ExternalNameInformer.
|
||||
func (v *version) ExternalNames() ExternalNameInformer { |
||||
return &externalNameInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} |
||||
} |
@ -0,0 +1,13 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by lister-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
// ExternalNameListerExpansion allows custom methods to be added to
|
||||
// ExternalNameLister.
|
||||
type ExternalNameListerExpansion interface{} |
||||
|
||||
// ExternalNameNamespaceListerExpansion allows custom methods to be added to
|
||||
// ExternalNameNamespaceLister.
|
||||
type ExternalNameNamespaceListerExpansion interface{} |
@ -0,0 +1,85 @@ |
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by lister-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1 |
||||
|
||||
import ( |
||||
v0alpha1 "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
"k8s.io/apimachinery/pkg/api/errors" |
||||
"k8s.io/apimachinery/pkg/labels" |
||||
"k8s.io/client-go/tools/cache" |
||||
) |
||||
|
||||
// ExternalNameLister helps list ExternalNames.
|
||||
// All objects returned here must be treated as read-only.
|
||||
type ExternalNameLister interface { |
||||
// List lists all ExternalNames in the indexer.
|
||||
// Objects returned here must be treated as read-only.
|
||||
List(selector labels.Selector) (ret []*v0alpha1.ExternalName, err error) |
||||
// ExternalNames returns an object that can list and get ExternalNames.
|
||||
ExternalNames(namespace string) ExternalNameNamespaceLister |
||||
ExternalNameListerExpansion |
||||
} |
||||
|
||||
// externalNameLister implements the ExternalNameLister interface.
|
||||
type externalNameLister struct { |
||||
indexer cache.Indexer |
||||
} |
||||
|
||||
// NewExternalNameLister returns a new ExternalNameLister.
|
||||
func NewExternalNameLister(indexer cache.Indexer) ExternalNameLister { |
||||
return &externalNameLister{indexer: indexer} |
||||
} |
||||
|
||||
// List lists all ExternalNames in the indexer.
|
||||
func (s *externalNameLister) List(selector labels.Selector) (ret []*v0alpha1.ExternalName, err error) { |
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) { |
||||
ret = append(ret, m.(*v0alpha1.ExternalName)) |
||||
}) |
||||
return ret, err |
||||
} |
||||
|
||||
// ExternalNames returns an object that can list and get ExternalNames.
|
||||
func (s *externalNameLister) ExternalNames(namespace string) ExternalNameNamespaceLister { |
||||
return externalNameNamespaceLister{indexer: s.indexer, namespace: namespace} |
||||
} |
||||
|
||||
// ExternalNameNamespaceLister helps list and get ExternalNames.
|
||||
// All objects returned here must be treated as read-only.
|
||||
type ExternalNameNamespaceLister interface { |
||||
// List lists all ExternalNames in the indexer for a given namespace.
|
||||
// Objects returned here must be treated as read-only.
|
||||
List(selector labels.Selector) (ret []*v0alpha1.ExternalName, err error) |
||||
// Get retrieves the ExternalName from the indexer for a given namespace and name.
|
||||
// Objects returned here must be treated as read-only.
|
||||
Get(name string) (*v0alpha1.ExternalName, error) |
||||
ExternalNameNamespaceListerExpansion |
||||
} |
||||
|
||||
// externalNameNamespaceLister implements the ExternalNameNamespaceLister
|
||||
// interface.
|
||||
type externalNameNamespaceLister struct { |
||||
indexer cache.Indexer |
||||
namespace string |
||||
} |
||||
|
||||
// List lists all ExternalNames in the indexer for a given namespace.
|
||||
func (s externalNameNamespaceLister) List(selector labels.Selector) (ret []*v0alpha1.ExternalName, err error) { |
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) { |
||||
ret = append(ret, m.(*v0alpha1.ExternalName)) |
||||
}) |
||||
return ret, err |
||||
} |
||||
|
||||
// Get retrieves the ExternalName from the indexer for a given namespace and name.
|
||||
func (s externalNameNamespaceLister) Get(name string) (*v0alpha1.ExternalName, error) { |
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if !exists { |
||||
return nil, errors.NewNotFound(v0alpha1.Resource("externalname"), name) |
||||
} |
||||
return obj.(*v0alpha1.ExternalName), nil |
||||
} |
@ -0,0 +1,88 @@ |
||||
package service |
||||
|
||||
import ( |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/runtime/schema" |
||||
"k8s.io/apimachinery/pkg/runtime/serializer" |
||||
"k8s.io/apiserver/pkg/authorization/authorizer" |
||||
"k8s.io/apiserver/pkg/registry/generic" |
||||
"k8s.io/apiserver/pkg/registry/rest" |
||||
genericapiserver "k8s.io/apiserver/pkg/server" |
||||
"k8s.io/kube-openapi/pkg/common" |
||||
|
||||
service "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
"github.com/grafana/grafana/pkg/services/featuremgmt" |
||||
grafanaapiserver "github.com/grafana/grafana/pkg/services/grafana-apiserver" |
||||
) |
||||
|
||||
var _ grafanaapiserver.APIGroupBuilder = (*ServiceAPIBuilder)(nil) |
||||
|
||||
// This is used just so wire has something unique to return
|
||||
type ServiceAPIBuilder struct{} |
||||
|
||||
func NewServiceAPIBuilder() *ServiceAPIBuilder { |
||||
return &ServiceAPIBuilder{} |
||||
} |
||||
|
||||
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration grafanaapiserver.APIRegistrar) *ServiceAPIBuilder { |
||||
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) { |
||||
return nil // skip registration unless opting into experimental apis
|
||||
} |
||||
builder := NewServiceAPIBuilder() |
||||
apiregistration.RegisterAPI(NewServiceAPIBuilder()) |
||||
return builder |
||||
} |
||||
|
||||
func (b *ServiceAPIBuilder) GetAuthorizer() authorizer.Authorizer { |
||||
return nil // default authorizer is fine
|
||||
} |
||||
|
||||
func (b *ServiceAPIBuilder) GetGroupVersion() schema.GroupVersion { |
||||
return service.SchemeGroupVersion |
||||
} |
||||
|
||||
func (b *ServiceAPIBuilder) InstallSchema(scheme *runtime.Scheme) error { |
||||
gv := service.SchemeGroupVersion |
||||
err := service.AddToScheme(scheme) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Link this version to the internal representation.
|
||||
// This is used for server-side-apply (PATCH), and avoids the error:
|
||||
// "no kind is registered for the type"
|
||||
// addKnownTypes(scheme, schema.GroupVersion{
|
||||
// Group: service.GROUP,
|
||||
// Version: runtime.APIVersionInternal,
|
||||
// })
|
||||
metav1.AddToGroupVersion(scheme, gv) |
||||
return scheme.SetVersionPriority(gv) |
||||
} |
||||
|
||||
func (b *ServiceAPIBuilder) GetAPIGroupInfo( |
||||
scheme *runtime.Scheme, |
||||
codecs serializer.CodecFactory, |
||||
optsGetter generic.RESTOptionsGetter, |
||||
) (*genericapiserver.APIGroupInfo, error) { |
||||
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(service.GROUP, scheme, metav1.ParameterCodec, codecs) |
||||
|
||||
resourceInfo := service.ExternalNameResourceInfo |
||||
storage := map[string]rest.Storage{} |
||||
serviceStorage, err := newStorage(scheme, optsGetter) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
storage[resourceInfo.StoragePath()] = serviceStorage |
||||
apiGroupInfo.VersionedResourcesStorageMap[service.VERSION] = storage |
||||
return &apiGroupInfo, nil |
||||
} |
||||
|
||||
func (b *ServiceAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions { |
||||
return service.GetOpenAPIDefinitions |
||||
} |
||||
|
||||
// Register additional routes with the server
|
||||
func (b *ServiceAPIBuilder) GetAPIRoutes() *grafanaapiserver.APIRoutes { |
||||
return nil |
||||
} |
@ -0,0 +1,62 @@ |
||||
package service |
||||
|
||||
import ( |
||||
"fmt" |
||||
"time" |
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apiserver/pkg/registry/generic" |
||||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" |
||||
|
||||
service "github.com/grafana/grafana/pkg/apis/service/v0alpha1" |
||||
grafanaregistry "github.com/grafana/grafana/pkg/services/grafana-apiserver/registry/generic" |
||||
grafanarest "github.com/grafana/grafana/pkg/services/grafana-apiserver/rest" |
||||
"github.com/grafana/grafana/pkg/services/grafana-apiserver/utils" |
||||
) |
||||
|
||||
var _ grafanarest.Storage = (*storage)(nil) |
||||
|
||||
type storage struct { |
||||
*genericregistry.Store |
||||
} |
||||
|
||||
func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) { |
||||
strategy := grafanaregistry.NewStrategy(scheme) |
||||
|
||||
resourceInfo := service.ExternalNameResourceInfo |
||||
store := &genericregistry.Store{ |
||||
NewFunc: resourceInfo.NewFunc, |
||||
NewListFunc: resourceInfo.NewListFunc, |
||||
PredicateFunc: grafanaregistry.Matcher, |
||||
DefaultQualifiedResource: resourceInfo.GroupResource(), |
||||
SingularQualifiedResource: resourceInfo.SingularGroupResource(), |
||||
TableConvertor: utils.NewTableConverter( |
||||
resourceInfo.GroupResource(), |
||||
[]metav1.TableColumnDefinition{ |
||||
{Name: "Name", Type: "string", Format: "name"}, |
||||
{Name: "Host", Type: "string", Format: "string", Description: "The service host"}, |
||||
{Name: "Created At", Type: "date"}, |
||||
}, |
||||
func(obj any) ([]interface{}, error) { |
||||
m, ok := obj.(*service.ExternalName) |
||||
if !ok { |
||||
return nil, fmt.Errorf("expected playlist") |
||||
} |
||||
return []interface{}{ |
||||
m.Name, |
||||
m.Spec.Host, |
||||
m.CreationTimestamp.UTC().Format(time.RFC3339), |
||||
}, nil |
||||
}, |
||||
), |
||||
CreateStrategy: strategy, |
||||
UpdateStrategy: strategy, |
||||
DeleteStrategy: strategy, |
||||
} |
||||
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} |
||||
if err := store.CompleteWithOptions(options); err != nil { |
||||
return nil, err |
||||
} |
||||
return &storage{Store: store}, nil |
||||
} |
Loading…
Reference in new issue