Skip to content

Commit

Permalink
OCM-5027 | feat: Added KubeletConfigClient and mock to be shared betw…
Browse files Browse the repository at this point in the history
…een Terraform and ROSA CLI
  • Loading branch information
robpblake committed Dec 7, 2023
1 parent 489a03b commit 0fd7aec
Show file tree
Hide file tree
Showing 13 changed files with 1,375 additions and 0 deletions.
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,35 @@ require (
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/microcosm-cc/bluemonday v1.0.18 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/openshift-online/ocm-sdk-go v0.1.388 // indirect
github.com/prometheus/client_golang v1.12.1 // 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/stretchr/testify v1.7.0 // indirect
go.uber.org/mock v0.3.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
469 changes: 469 additions & 0 deletions go.sum

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions pkg/ocm/client/clusterautoscaler_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import (
"context"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

//go:generate mockgen -source=clusterautoscaler_client.go -package=testing -destination=testing/mock_clusterautoscaler_client.go
type ClusterAutoscalerClient interface {
SingleClusterSubResource[v1.ClusterAutoscaler]
}

func NewClusterAutoscalerClient(collection *v1.ClustersClient) ClusterAutoscalerClient {
return &SingleClusterSubResourceImpl[v1.ClusterAutoscaler]{
getFunc: func(ctx context.Context, clusterId string) (OcmInstanceResponse[v1.ClusterAutoscaler], error) {
return collection.Cluster(clusterId).Autoscaler().Get().SendContext(ctx)
},
updateFunc: func(ctx context.Context, clusterId string, instance *v1.ClusterAutoscaler) (OcmInstanceResponse[v1.ClusterAutoscaler], error) {
return collection.Cluster(clusterId).Autoscaler().Update().Body(instance).SendContext(ctx)
},
createFunc: func(ctx context.Context, clusterId string, instance *v1.ClusterAutoscaler) (OcmInstanceResponse[v1.ClusterAutoscaler], error) {
return collection.Cluster(clusterId).Autoscaler().Post().Request(instance).SendContext(ctx)
},
deleteFunc: func(ctx context.Context, clusterId string) (OcmResponse, error) {
return collection.Cluster(clusterId).Autoscaler().Delete().SendContext(ctx)
},
}
}
31 changes: 31 additions & 0 deletions pkg/ocm/client/kubeletconfig_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package client

import (
"context"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

// KubeletConfigClient wraps the OCM SDK to provide a more unit testable friendly way of
// interacting with the OCM API
//
//go:generate mockgen -source=kubeletconfig_client.go -package=testing -destination=testing/mock_kubeletconfig_client.go
type KubeletConfigClient interface {
SingleClusterSubResource[v1.KubeletConfig]
}

func NewKubeletConfigClient(collection *v1.ClustersClient) KubeletConfigClient {
return &SingleClusterSubResourceImpl[v1.KubeletConfig]{
getFunc: func(ctx context.Context, clusterId string) (OcmInstanceResponse[v1.KubeletConfig], error) {
return collection.Cluster(clusterId).KubeletConfig().Get().SendContext(ctx)
},
updateFunc: func(ctx context.Context, clusterId string, instance *v1.KubeletConfig) (OcmInstanceResponse[v1.KubeletConfig], error) {
return collection.Cluster(clusterId).KubeletConfig().Update().Body(instance).SendContext(ctx)
},
createFunc: func(ctx context.Context, clusterId string, instance *v1.KubeletConfig) (OcmInstanceResponse[v1.KubeletConfig], error) {
return collection.Cluster(clusterId).KubeletConfig().Post().Body(instance).SendContext(ctx)
},
deleteFunc: func(ctx context.Context, clusterId string) (OcmResponse, error) {
return collection.Cluster(clusterId).KubeletConfig().Delete().SendContext(ctx)
},
}
}
35 changes: 35 additions & 0 deletions pkg/ocm/client/machinepool_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"context"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

//go:generate mockgen -source=machinepool_client.go -package=testing -destination=testing/mock_machinepool_client.go
type MachinePoolClient interface {
CollectionClusterSubResource[v1.MachinePool, string]
}

func NewMachinePoolClient(collection *v1.ClustersClient) MachinePoolClient {
return &CollectionClusterSubResourceImpl[v1.MachinePool, string]{
getFunc: func(ctx context.Context, clusterId string, instanceId string) (OcmInstanceResponse[v1.MachinePool], error) {
return collection.Cluster(clusterId).MachinePools().MachinePool(instanceId).Get().SendContext(ctx)
},
updateFunc: func(ctx context.Context, clusterId string, instance *v1.MachinePool) (OcmInstanceResponse[v1.MachinePool], error) {
return collection.Cluster(clusterId).MachinePools().MachinePool(instance.ID()).Update().Body(instance).SendContext(ctx)
},
createFunc: func(ctx context.Context, clusterId string, instance *v1.MachinePool) (OcmInstanceResponse[v1.MachinePool], error) {
return collection.Cluster(clusterId).MachinePools().Add().Body(instance).SendContext(ctx)
},
deleteFunc: func(ctx context.Context, clusterId string, instanceId string) (OcmResponse, error) {
return collection.Cluster(clusterId).MachinePools().MachinePool(instanceId).Delete().SendContext(ctx)
},
listFunc: func(ctx context.Context, clusterId string, paging Paging) (OcmListResponse[v1.MachinePool], error) {
resp, err := collection.Cluster(clusterId).MachinePools().List().Size(paging.size).Page(paging.page).SendContext(ctx)
if err != nil {
return nil, err
}
return NewListResponse(resp.Status(), resp.Items().Slice()), nil
},
}
}
35 changes: 35 additions & 0 deletions pkg/ocm/client/nodepool_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"context"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

//go:generate mockgen -source=nodepool_client.go -package=testing -destination=testing/mock_nodepool_client.go
type NodePoolClient interface {
CollectionClusterSubResource[v1.NodePool, string]
}

func NewNodePoolClient(collection *v1.ClustersClient) NodePoolClient {
return &CollectionClusterSubResourceImpl[v1.NodePool, string]{
getFunc: func(ctx context.Context, clusterId string, instanceId string) (OcmInstanceResponse[v1.NodePool], error) {
return collection.Cluster(clusterId).NodePools().NodePool(instanceId).Get().SendContext(ctx)
},
updateFunc: func(ctx context.Context, clusterId string, instance *v1.NodePool) (OcmInstanceResponse[v1.NodePool], error) {
return collection.Cluster(clusterId).NodePools().NodePool(instance.ID()).Update().Body(instance).SendContext(ctx)
},
createFunc: func(ctx context.Context, clusterId string, instance *v1.NodePool) (OcmInstanceResponse[v1.NodePool], error) {
return collection.Cluster(clusterId).NodePools().Add().Body(instance).SendContext(ctx)
},
deleteFunc: func(ctx context.Context, clusterId string, instanceId string) (OcmResponse, error) {
return collection.Cluster(clusterId).NodePools().NodePool(instanceId).Delete().SendContext(ctx)
},
listFunc: func(ctx context.Context, clusterId string, paging Paging) (OcmListResponse[v1.NodePool], error) {
response, err := collection.Cluster(clusterId).NodePools().List().Size(paging.size).Page(paging.page).SendContext(ctx)
if err != nil {
return nil, err
}
return NewListResponse(response.Status(), response.Items().Slice()), nil
},
}
}
28 changes: 28 additions & 0 deletions pkg/ocm/client/testing/matchers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testing

// Custom Gomega Matchers that make it easier to assert interactions with the OCM API

import v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

type KubeletConfigMatcher struct {
expected *v1.KubeletConfig
}

// KubeletConfigMatches returns a Matcher that asserts that the received KubeletConfig
// matches the expected KubeletConfig
func KubeletConfigMatches(expected *v1.KubeletConfig) KubeletConfigMatcher {
return KubeletConfigMatcher{
expected: expected,
}
}

func (k KubeletConfigMatcher) Matches(x interface{}) bool {
if kubeletConfig, ok := x.(*v1.KubeletConfig); ok {
return k.expected.PodPidsLimit() == kubeletConfig.PodPidsLimit()
}
return false
}

func (k KubeletConfigMatcher) String() string {
return "Expected KubeletConfig Does Not Match"
}
115 changes: 115 additions & 0 deletions pkg/ocm/client/testing/mock_clusterautoscaler_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0fd7aec

Please sign in to comment.