Skip to content

Commit

Permalink
dataclients/kubernetes: export ClusterClient
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov committed Feb 12, 2024
1 parent 38d2bbb commit d1e0b27
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
36 changes: 18 additions & 18 deletions dataclients/kubernetes/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
const RouteGroupsNotInstalledMessage = `RouteGroups CRD is not installed in the cluster.
See: https://opensource.zalando.com/skipper/kubernetes/routegroups/#installation`

type clusterClient struct {
type ClusterClient struct {
ingressesURI string
routeGroupsURI string
servicesURI string
Expand Down Expand Up @@ -152,7 +152,7 @@ func buildHTTPClient(certFilePath string, quit <-chan struct{}) (*http.Client, e
}, nil
}

func newClusterClient(o Options) (*clusterClient, error) {
func NewClusterClient(o Options) (*ClusterClient, error) {
apiURL, err := buildAPIURL(&o)
if err != nil {
return nil, err
Expand Down Expand Up @@ -189,7 +189,7 @@ func newClusterClient(o Options) (*clusterClient, error) {
return nil, err
}

c := &clusterClient{
c := &ClusterClient{
ingressesURI: IngressesV1ClusterURI,
routeGroupsURI: RouteGroupsClusterURI,
servicesURI: ServicesClusterURI,
Expand Down Expand Up @@ -264,7 +264,7 @@ func toLabelSelectorQuery(selectors map[string]string) string {
return fmt.Sprintf(labelSelectorQueryFmt, url.QueryEscape(strings.Join(strs, ",")))
}

func (c *clusterClient) setNamespace(namespace string) {
func (c *ClusterClient) setNamespace(namespace string) {
c.ingressesURI = fmt.Sprintf(IngressesV1NamespaceFmt, namespace)
c.routeGroupsURI = fmt.Sprintf(RouteGroupsNamespaceFmt, namespace)
c.servicesURI = fmt.Sprintf(ServicesNamespaceFmt, namespace)
Expand All @@ -273,7 +273,7 @@ func (c *clusterClient) setNamespace(namespace string) {
c.secretsURI = fmt.Sprintf(SecretsNamespaceFmt, namespace)
}

func (c *clusterClient) createRequest(uri string, body io.Reader) (*http.Request, error) {
func (c *ClusterClient) createRequest(uri string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest("GET", c.apiURL+uri, body)
if err != nil {
return nil, err
Expand All @@ -290,7 +290,7 @@ func (c *clusterClient) createRequest(uri string, body io.Reader) (*http.Request
return req, nil
}

func (c *clusterClient) getJSON(uri string, a interface{}) error {
func (c *ClusterClient) getJSON(uri string, a interface{}) error {
log.Tracef("making request to: %s", uri)

req, err := c.createRequest(uri, nil)
Expand Down Expand Up @@ -330,7 +330,7 @@ func (c *clusterClient) getJSON(uri string, a interface{}) error {
return err
}

func (c *clusterClient) clusterHasRouteGroups() (bool, error) {
func (c *ClusterClient) clusterHasRouteGroups() (bool, error) {
var crl ClusterResourceList
if err := c.getJSON(ZalandoResourcesClusterURI, &crl); err != nil { // it probably should bounce once
return false, err
Expand All @@ -345,7 +345,7 @@ func (c *clusterClient) clusterHasRouteGroups() (bool, error) {
return false, nil
}

func (c *clusterClient) ingressClassMissmatch(m *definitions.Metadata) bool {
func (c *ClusterClient) ingressClassMissmatch(m *definitions.Metadata) bool {
// No Metadata is the same as no annotations for us
if m != nil {
cls, ok := m.Annotations[ingressClassKey]
Expand All @@ -357,7 +357,7 @@ func (c *clusterClient) ingressClassMissmatch(m *definitions.Metadata) bool {

// filterIngressesV1ByClass will filter only the ingresses that have the valid class, these are
// the defined one, empty string class or not class at all
func (c *clusterClient) filterIngressesV1ByClass(items []*definitions.IngressV1Item) []*definitions.IngressV1Item {
func (c *ClusterClient) filterIngressesV1ByClass(items []*definitions.IngressV1Item) []*definitions.IngressV1Item {
validIngs := []*definitions.IngressV1Item{}

for _, ing := range items {
Expand Down Expand Up @@ -393,7 +393,7 @@ func sortByMetadata(slice interface{}, getMetadata func(int) *definitions.Metada
})
}

func (c *clusterClient) loadIngressesV1() ([]*definitions.IngressV1Item, error) {
func (c *ClusterClient) loadIngressesV1() ([]*definitions.IngressV1Item, error) {
var il definitions.IngressV1List
if err := c.getJSON(c.ingressesURI+c.ingressLabelSelectors, &il); err != nil {
log.Debugf("requesting all ingresses failed: %v", err)
Expand All @@ -418,7 +418,7 @@ func (c *clusterClient) loadIngressesV1() ([]*definitions.IngressV1Item, error)
return validatedItems, nil
}

func (c *clusterClient) LoadRouteGroups() ([]*definitions.RouteGroupItem, error) {
func (c *ClusterClient) LoadRouteGroups() ([]*definitions.RouteGroupItem, error) {
var rgl definitions.RouteGroupList
if err := c.getJSON(c.routeGroupsURI+c.routeGroupsLabelSelectors, &rgl); err != nil {
return nil, err
Expand Down Expand Up @@ -452,7 +452,7 @@ func (c *clusterClient) LoadRouteGroups() ([]*definitions.RouteGroupItem, error)
return rgs, nil
}

func (c *clusterClient) loadServices() (map[definitions.ResourceID]*service, error) {
func (c *ClusterClient) loadServices() (map[definitions.ResourceID]*service, error) {
var services serviceList
if err := c.getJSON(c.servicesURI+c.servicesLabelSelectors, &services); err != nil {
log.Debugf("requesting all services failed: %v", err)
Expand All @@ -478,7 +478,7 @@ func (c *clusterClient) loadServices() (map[definitions.ResourceID]*service, err
return result, nil
}

func (c *clusterClient) loadSecrets() (map[definitions.ResourceID]*secret, error) {
func (c *ClusterClient) loadSecrets() (map[definitions.ResourceID]*secret, error) {
var secrets secretList
if err := c.getJSON(c.secretsURI+c.secretsLabelSelectors, &secrets); err != nil {
log.Debugf("requesting all secrets failed: %v", err)
Expand All @@ -498,7 +498,7 @@ func (c *clusterClient) loadSecrets() (map[definitions.ResourceID]*secret, error
return result, nil
}

func (c *clusterClient) loadEndpoints() (map[definitions.ResourceID]*endpoint, error) {
func (c *ClusterClient) loadEndpoints() (map[definitions.ResourceID]*endpoint, error) {
var endpoints endpointList
if err := c.getJSON(c.endpointsURI+c.endpointsLabelSelectors, &endpoints); err != nil {
log.Debugf("requesting all endpoints failed: %v", err)
Expand All @@ -524,7 +524,7 @@ func (c *clusterClient) loadEndpoints() (map[definitions.ResourceID]*endpoint, e
// members. The returned map will return the full list of ready
// non-terminating endpoints that should be in the load balancer of a
// given service, check endpointSlice.ToResourceID().
func (c *clusterClient) loadEndpointSlices() (map[definitions.ResourceID]*skipperEndpointSlice, error) {
func (c *ClusterClient) loadEndpointSlices() (map[definitions.ResourceID]*skipperEndpointSlice, error) {
var endpointSlices endpointSliceList
if err := c.getJSON(c.endpointSlicesURI+c.endpointSlicesLabelSelectors, &endpointSlices); err != nil {
log.Debugf("requesting all endpointslices failed: %v", err)
Expand Down Expand Up @@ -589,7 +589,7 @@ func (c *clusterClient) loadEndpointSlices() (map[definitions.ResourceID]*skippe
return result, nil
}

func (c *clusterClient) logMissingRouteGroupsOnce() {
func (c *ClusterClient) logMissingRouteGroupsOnce() {
if c.loggedMissingRouteGroups {
return
}
Expand All @@ -598,7 +598,7 @@ func (c *clusterClient) logMissingRouteGroupsOnce() {
log.Warn(RouteGroupsNotInstalledMessage)
}

func (c *clusterClient) fetchClusterState() (*clusterState, error) {
func (c *ClusterClient) fetchClusterState() (*clusterState, error) {
var (
err error
ingressesV1 []*definitions.IngressV1Item
Expand Down Expand Up @@ -656,7 +656,7 @@ func (c *clusterClient) fetchClusterState() (*clusterState, error) {
return state, nil
}

func (c *clusterClient) close() {
func (c *ClusterClient) Close() {
if c.quit != nil {
close(c.quit)
c.quit = nil
Expand Down
4 changes: 2 additions & 2 deletions dataclients/kubernetes/clusterclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ spec:
s := httptest.NewServer(a)
defer s.Close()

c, err := kubernetes.New(kubernetes.Options{KubernetesURL: s.URL, RouteGroupClass: tt.rgClass})
c, err := kubernetes.NewClusterClient(kubernetes.Options{KubernetesURL: s.URL, RouteGroupClass: tt.rgClass})
if err != nil {
t.Error(err)
}
defer c.Close()

rgs, err := c.ClusterClient.LoadRouteGroups()
rgs, err := c.LoadRouteGroups()
if err != nil {
t.Error(err)
}
Expand Down
14 changes: 7 additions & 7 deletions dataclients/kubernetes/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ type Options struct {
// Client is a Skipper DataClient implementation used to create routes based on Kubernetes Ingress settings.
type Client struct {
mu sync.Mutex
ClusterClient *clusterClient
clusterClient *ClusterClient
ingress *ingress
routeGroups *routeGroups
provideHealthcheck bool
Expand All @@ -263,7 +263,7 @@ func New(o Options) (*Client, error) {
log.Warning("OriginMarker is deprecated")
}

clusterClient, err := newClusterClient(o)
clusterClient, err := NewClusterClient(o)
if err != nil {
return nil, fmt.Errorf("failed to create cluster client: %v", err)
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func New(o Options) (*Client, error) {
rg := newRouteGroups(o)

return &Client{
ClusterClient: clusterClient,
clusterClient: clusterClient,
ingress: ing,
routeGroups: rg,
provideHealthcheck: o.ProvideHealthcheck,
Expand Down Expand Up @@ -361,7 +361,7 @@ func mapRoutes(routes []*eskip.Route) (map[string]*eskip.Route, []*eskip.Route)

func (c *Client) loadAndConvert() ([]*eskip.Route, error) {
c.mu.Lock()
state, err := c.ClusterClient.fetchClusterState()
state, err := c.clusterClient.fetchClusterState()
if err != nil {
c.mu.Unlock()
return nil, err
Expand All @@ -376,12 +376,12 @@ func (c *Client) loadAndConvert() ([]*eskip.Route, error) {

defaultFilters := c.fetchDefaultFilterConfigs()

ri, err := c.ingress.convert(state, defaultFilters, c.ClusterClient.certificateRegistry, loggingEnabled)
ri, err := c.ingress.convert(state, defaultFilters, c.clusterClient.certificateRegistry, loggingEnabled)
if err != nil {
return nil, err
}

rg, err := c.routeGroups.convert(state, defaultFilters, loggingEnabled, c.ClusterClient.certificateRegistry)
rg, err := c.routeGroups.convert(state, defaultFilters, loggingEnabled, c.clusterClient.certificateRegistry)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -512,7 +512,7 @@ func (c *Client) LoadUpdate() ([]*eskip.Route, []string, error) {
}

func (c *Client) Close() {
c.ClusterClient.close()
c.clusterClient.Close()
}

func (c *Client) fetchDefaultFilterConfigs() defaultFilters {
Expand Down
20 changes: 10 additions & 10 deletions dataclients/kubernetes/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ func TestIngressClassFilter(t *testing.T) {
return
}

c := &clusterClient{
c := &ClusterClient{
ingressClass: clsRx,
}

Expand Down Expand Up @@ -2006,7 +2006,7 @@ func TestCreateRequest(t *testing.T) {
)
rc := io.NopCloser(&buf)

client := &clusterClient{}
client := &ClusterClient{}

url = "A%"
_, err = client.createRequest(url, rc)
Expand Down Expand Up @@ -2126,7 +2126,7 @@ func TestBuildHTTPClient(t *testing.T) {
}

func TestScoping(t *testing.T) {
client := &clusterClient{}
client := &ClusterClient{}

client.setNamespace("test")
assert.Equal(t, "/apis/networking.k8s.io/v1/namespaces/test/ingresses", client.ingressesURI)
Expand Down Expand Up @@ -2243,11 +2243,11 @@ func TestLabelSelectorsSet(t *testing.T) {
client, err := New(test.options)
require.NoError(t, err)

assert.Equal(t, sortedSlice(test.expectedIngressSelector), sortedSlice(client.ClusterClient.ingressLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedServicesSelector), sortedSlice(client.ClusterClient.servicesLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedEndpointsSelector), sortedSlice(client.ClusterClient.endpointsLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedSecretsSelector), sortedSlice(client.ClusterClient.secretsLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedRouteGroupsSelector), sortedSlice(client.ClusterClient.routeGroupsLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedIngressSelector), sortedSlice(client.clusterClient.ingressLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedServicesSelector), sortedSlice(client.clusterClient.servicesLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedEndpointsSelector), sortedSlice(client.clusterClient.endpointsLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedSecretsSelector), sortedSlice(client.clusterClient.secretsLabelSelectors))
assert.Equal(t, sortedSlice(test.expectedRouteGroupsSelector), sortedSlice(client.clusterClient.routeGroupsLabelSelectors))
})
}
}
Expand Down Expand Up @@ -3088,7 +3088,7 @@ func TestCertificateRegistry(t *testing.T) {

defer dc.Close()

state, err := dc.ClusterClient.fetchClusterState()
state, err := dc.clusterClient.fetchClusterState()
if err != nil {
t.Error(err)
}
Expand All @@ -3107,7 +3107,7 @@ func TestCertificateRegistry(t *testing.T) {

defer dc.Close()

state, err := dc.ClusterClient.fetchClusterState()
state, err := dc.clusterClient.fetchClusterState()
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit d1e0b27

Please sign in to comment.