Skip to content

Commit

Permalink
Return Config instance instead of interface (#10)
Browse files Browse the repository at this point in the history
Also return rest.Config
  • Loading branch information
yalosev authored Oct 13, 2023
1 parent 1602a8a commit b093b9e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 286 deletions.
81 changes: 29 additions & 52 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,13 @@ const (
kubeNamespaceFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)

type Client interface {
kubernetes.Interface

WithContextName(contextName string)
WithConfigPath(configPath string)
WithServer(server string)
WithRateLimiterSettings(qps float32, burst int)
WithTimeout(time time.Duration)
WithMetricStorage(metricStorage MetricStorage)
WithMetricLabels(labels map[string]string)

Init() error

DefaultNamespace() string
Dynamic() dynamic.Interface
ApiExt() apixv1client.ApiextensionsV1Interface
Metadata() metadata.Interface

APIResourceList(apiVersion string) ([]*metav1.APIResourceList, error)
APIResource(apiVersion, kind string) (*metav1.APIResource, error)
GroupVersionResource(apiVersion, kind string) (schema.GroupVersionResource, error)

// ReloadDynamic reloads dynamic fake client.
// It is the only way to provide List operations on custom resources in client-go 1.20+.
// See https://github.com/kubernetes/client-go/issues/949#issuecomment-811154420
ReloadDynamic(gvrList map[schema.GroupVersionResource]string)
}

func New() Client {
return &client{}
func New() *Client {
return &Client{}
}

func NewFake(gvr map[schema.GroupVersionResource]string) Client {
func NewFake(gvr map[schema.GroupVersionResource]string) *Client {
sc := runtime.NewScheme()
return &client{
return &Client{
Interface: fake.NewSimpleClientset(),
defaultNamespace: "default",
dynamicClient: fakedynamic.NewSimpleDynamicClientWithCustomListKinds(sc, gvr),
Expand All @@ -76,9 +48,7 @@ func NewFake(gvr map[schema.GroupVersionResource]string) Client {
}
}

var _ Client = &client{}

type client struct {
type Client struct {
kubernetes.Interface
cachedDiscovery discovery.CachedDiscoveryInterface
contextName string
Expand All @@ -94,59 +64,65 @@ type client struct {
metricStorage MetricStorage
metricLabels map[string]string
schema *runtime.Scheme
restConfig *rest.Config
}

// ReloadDynamic creates new dynamic client with the new set of CRDs.
func (c *client) ReloadDynamic(gvrList map[schema.GroupVersionResource]string) {
func (c *Client) ReloadDynamic(gvrList map[schema.GroupVersionResource]string) {
c.dynamicClient = fakedynamic.NewSimpleDynamicClientWithCustomListKinds(c.schema, gvrList)
}

func (c *client) WithServer(server string) {
func (c *Client) WithServer(server string) {
c.server = server
}

func (c *client) WithContextName(name string) {
func (c *Client) WithContextName(name string) {
c.contextName = name
}

func (c *client) WithConfigPath(path string) {
func (c *Client) WithConfigPath(path string) {
c.configPath = path
}

func (c *client) WithRateLimiterSettings(qps float32, burst int) {
func (c *Client) WithRateLimiterSettings(qps float32, burst int) {
c.qps = qps
c.burst = burst
}

func (c *client) WithTimeout(timeout time.Duration) {
func (c *Client) WithTimeout(timeout time.Duration) {
c.timeout = timeout
}

func (c *client) WithMetricStorage(metricStorage MetricStorage) {
func (c *Client) WithMetricStorage(metricStorage MetricStorage) {
c.metricStorage = metricStorage
}

func (c *client) WithMetricLabels(labels map[string]string) {
func (c *Client) WithMetricLabels(labels map[string]string) {
c.metricLabels = labels
}

func (c *client) DefaultNamespace() string {
func (c *Client) DefaultNamespace() string {
return c.defaultNamespace
}

func (c *client) Dynamic() dynamic.Interface {
func (c *Client) Dynamic() dynamic.Interface {
return c.dynamicClient
}

func (c *client) ApiExt() apixv1client.ApiextensionsV1Interface {
func (c *Client) ApiExt() apixv1client.ApiextensionsV1Interface {
return c.apiExtClient
}

func (c *client) Metadata() metadata.Interface {
func (c *Client) Metadata() metadata.Interface {
return c.metadataClient
}

func (c *client) Init() error {
// RestConfig returns kubernetes Config with the common attributes that was passed on initialization.
func (c *Client) RestConfig() *rest.Config {
return c.restConfig
}

func (c *Client) Init() error {
logEntry := log.WithField("operator.component", "KubernetesAPIClient")

var err error
Expand Down Expand Up @@ -247,6 +223,7 @@ func (c *client) Init() error {
return err
}

c.restConfig = config
logEntry.Infof("Kubernetes client is configured successfully with '%s' config", configType)

return nil
Expand Down Expand Up @@ -348,7 +325,7 @@ func getInClusterConfig() (config *rest.Config, defaultNs string, err error) {
//
// NOTE that fetching all preferred resources can give errors if there are non-working
// api controllers in cluster.
func (c *client) APIResourceList(apiVersion string) (lists []*metav1.APIResourceList, err error) {
func (c *Client) APIResourceList(apiVersion string) (lists []*metav1.APIResourceList, err error) {
if apiVersion == "" {
// Get all preferred resources.
// Can return errors if api controllers are not available.
Expand Down Expand Up @@ -393,7 +370,7 @@ func (c *client) APIResourceList(apiVersion string) (lists []*metav1.APIResource
//
// NOTE that fetching with empty apiVersion can give errors if there are non-working
// api controllers in cluster.
func (c *client) APIResource(apiVersion, kind string) (res *metav1.APIResource, err error) {
func (c *Client) APIResource(apiVersion, kind string) (res *metav1.APIResource, err error) {
lists, err := c.APIResourceList(apiVersion)
if err != nil && len(lists) == 0 {
// apiVersion is defined and there is a ServerResourcesForGroupVersion error
Expand Down Expand Up @@ -427,7 +404,7 @@ func (c *client) APIResource(apiVersion, kind string) (res *metav1.APIResource,
//
// This method is borrowed from kubectl and kubedog. The difference are:
// - lower case comparison with kind, name and all short names
func (c *client) GroupVersionResource(apiVersion, kind string) (gvr schema.GroupVersionResource, err error) {
func (c *Client) GroupVersionResource(apiVersion, kind string) (gvr schema.GroupVersionResource, err error) {
apiRes, err := c.APIResource(apiVersion, kind)
if err != nil {
return
Expand All @@ -440,7 +417,7 @@ func (c *client) GroupVersionResource(apiVersion, kind string) (gvr schema.Group
}, nil
}

func (c *client) discovery() discovery.DiscoveryInterface {
func (c *Client) discovery() discovery.DiscoveryInterface {
if c.cachedDiscovery != nil {
return c.cachedDiscovery
}
Expand Down
2 changes: 1 addition & 1 deletion fake/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type Cluster struct {
Client klient.Client
Client *klient.Client

Discovery *fakediscovery.FakeDiscovery
gvrList map[schema.GroupVersionResource]string
Expand Down
49 changes: 23 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,63 @@ module github.com/flant/kube-client
go 1.19

require (
github.com/onsi/gomega v1.20.1
github.com/prometheus/client_golang v1.12.1
github.com/onsi/gomega v1.23.0
github.com/prometheus/client_golang v1.14.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
k8s.io/api v0.25.4
k8s.io/apiextensions-apiserver v0.25.4
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
k8s.io/klog/v2 v2.70.1
sigs.k8s.io/yaml v1.2.0
k8s.io/api v0.26.9
k8s.io/apiextensions-apiserver v0.26.9
k8s.io/apimachinery v0.26.9
k8s.io/client-go v0.26.9
k8s.io/klog/v2 v2.80.1
sigs.k8s.io/yaml v1.3.0
)

require (
cloud.google.com/go v0.97.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit b093b9e

Please sign in to comment.