-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from robpblake/ocm-5027-common-kubeletclient-a…
…lternative OCM-5027 | feat: Added KubeletConfigClient and mock to be shared between ROSA and Terraform
- Loading branch information
Showing
16 changed files
with
1,486 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package test | ||
|
||
// Custom Gomega/GoMock Matchers that make it easier to assert interactions with the OCM API | ||
|
||
import v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
|
||
const failureMessage = "Expected KubeletConfig Does Not Match" | ||
const negatedFailureMessage = "Expected KubeletConfig Not to Match" | ||
|
||
type KubeletConfigMatcher struct { | ||
expected *v1.KubeletConfig | ||
} | ||
|
||
// MatchKubeletConfig returns a Matcher that asserts that the received KubeletConfig | ||
// matches the expected KubeletConfig | ||
func MatchKubeletConfig(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 failureMessage | ||
} | ||
|
||
func (k KubeletConfigMatcher) Match(actual interface{}) (success bool, err error) { | ||
return k.Matches(actual), nil | ||
} | ||
|
||
func (k KubeletConfigMatcher) FailureMessage(_ interface{}) (message string) { | ||
return failureMessage | ||
} | ||
|
||
func (k KubeletConfigMatcher) NegatedFailureMessage(_ interface{}) (message string) { | ||
return negatedFailureMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
) | ||
|
||
var _ = Describe("Matchers Testing", func() { | ||
|
||
Context("KubeletConfigMatcher", func() { | ||
|
||
kubeletConfig := func(k *v1.KubeletConfigBuilder) { | ||
k.PodPidsLimit(5000) | ||
} | ||
kubeletConfig2 := func(k *v1.KubeletConfigBuilder) { | ||
k.PodPidsLimit(10000) | ||
} | ||
|
||
It("Matches when KubeletConfig are the same", func() { | ||
config, err := NewKubeletConfig(kubeletConfig) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
config2, err := NewKubeletConfig(kubeletConfig) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(config).To(MatchKubeletConfig(config2)) | ||
}) | ||
|
||
It("Does not match when KubeletConfigs are different", func() { | ||
config, err := NewKubeletConfig(kubeletConfig) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
config2, err := NewKubeletConfig(kubeletConfig2) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(config).NotTo(MatchKubeletConfig(config2)) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.