Skip to content

Commit

Permalink
Group resourcebuilders by group/version
Browse files Browse the repository at this point in the history
  • Loading branch information
RadekManak committed Jan 11, 2023
1 parent ca770ea commit 399c89b
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 94 deletions.
4 changes: 2 additions & 2 deletions pkg/testutils/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/onsi/gomega"

"github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder"
corev1resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder/core/v1"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -127,7 +127,7 @@ func removeNamespace(g gomega.Gomega, ctx context.Context, cfg *rest.Config, k8s
coreClient, err := coreclient.NewForConfig(cfg)
g.Expect(err).ToNot(gomega.HaveOccurred())

nsBuilder := resourcebuilder.Namespace().WithName(namespace)
nsBuilder := corev1resourcebuilder.Namespace().WithName(namespace)

// Delete the namespace
ns := nsBuilder.Build()
Expand Down
13 changes: 7 additions & 6 deletions pkg/testutils/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
. "github.com/onsi/gomega"

machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder"
corev1resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder/core/v1"
machinev1beta1resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder/machine/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand All @@ -34,12 +35,12 @@ var _ = Describe("Cleanup", func() {

BeforeEach(func() {
By("Setting up a namespace for the test")
ns := resourcebuilder.Namespace().WithGenerateName("test-utils-").Build()
ns := corev1resourcebuilder.Namespace().WithGenerateName("test-utils-").Build()
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
namespaceName = ns.GetName()

// Creating some Machines to cleanup
machineBuilder := resourcebuilder.Machine().
machineBuilder := machinev1beta1resourcebuilder.Machine().
WithGenerateName("cleanup-resources-test-").
WithNamespace(namespaceName)

Expand All @@ -60,7 +61,7 @@ var _ = Describe("Cleanup", func() {
&machinev1beta1.Machine{},
)

ns := resourcebuilder.Namespace().WithName(namespaceName).Build()
ns := corev1resourcebuilder.Namespace().WithName(namespaceName).Build()
namespaceNotFound := apierrors.NewNotFound(schema.GroupResource{Resource: "namespaces"}, namespaceName)

Expect(komega.Get(ns)()).To(MatchError(namespaceNotFound))
Expand All @@ -73,11 +74,11 @@ var _ = Describe("Cleanup", func() {
})

It("should ignore resources in another namespace", func() {
ns := resourcebuilder.Namespace().WithGenerateName("test-utils-").Build()
ns := corev1resourcebuilder.Namespace().WithGenerateName("test-utils-").Build()
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

// Creating some Machines to cleanup
machineBuilder := resourcebuilder.Machine().
machineBuilder := machinev1beta1resourcebuilder.Machine().
WithGenerateName("cleanup-resources-test-").
WithNamespace(ns.GetName())

Expand Down
9 changes: 8 additions & 1 deletion pkg/testutils/resourcebuilder/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@ const (
// Machine API namespace.
OpenshiftMachineAPINamespaceName = "openshift-machine-api"

testClusterIDValue = "cpms-cluster-test-id"
// TestClusterIDValue is the clusterID in the test environment.
TestClusterIDValue = "cluster-test-id"

// MachineRoleLabelName is the name for the machine role label.
MachineRoleLabelName = "machine.openshift.io/cluster-api-machine-role"

// MachineTypeLabelName is the name for the machine type label.
MachineTypeLabelName = "machine.openshift.io/cluster-api-machine-type"
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package resourcebuilder
import (
configv1 "github.com/openshift/api/config/v1"
machinev1 "github.com/openshift/api/machine/v1"
"k8s.io/utils/pointer"
)

// AWSFailureDomains creates a new failure domains builder for AWS.
Expand All @@ -28,17 +29,17 @@ func AWSFailureDomains() AWSFailureDomainsBuilder {
AWSFailureDomain().WithAvailabilityZone("us-east-1a").
WithSubnet(machinev1.AWSResourceReference{
Type: machinev1.AWSIDReferenceType,
ID: stringPtr("subenet-us-east-1a"),
ID: pointer.String("subenet-us-east-1a"),
}),
AWSFailureDomain().WithAvailabilityZone("us-east-1b").
WithSubnet(machinev1.AWSResourceReference{
Type: machinev1.AWSIDReferenceType,
ID: stringPtr("subenet-us-east-1b"),
ID: pointer.String("subenet-us-east-1b"),
}),
AWSFailureDomain().WithAvailabilityZone("us-east-1c").
WithSubnet(machinev1.AWSResourceReference{
Type: machinev1.AWSIDReferenceType,
ID: stringPtr("subenet-us-east-1c"),
ID: pointer.String("subenet-us-east-1c"),
}),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1

import (
machinev1 "github.com/openshift/api/machine/v1"
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder"
machinev1beta1resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder/machine/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

const (
Expand All @@ -46,16 +49,16 @@ func (c *ControlPlaneMachineSetFuncs) Build() *machinev1.ControlPlaneMachineSet
// ControlPlaneMachineSet creates a new controlplanemachineset builder.
func ControlPlaneMachineSet() ControlPlaneMachineSetBuilder {
return ControlPlaneMachineSetBuilder{
machineTemplateBuilder: OpenShiftMachineV1Beta1Template(),
machineTemplateBuilder: machinev1beta1resourcebuilder.OpenShiftMachineV1Beta1Template(),
name: ControlPlaneMachineSetName,
namespace: OpenshiftMachineAPINamespaceName,
namespace: resourcebuilder.OpenshiftMachineAPINamespaceName,
replicas: 3,
state: machinev1.ControlPlaneMachineSetStateActive,
selector: metav1.LabelSelector{
MatchLabels: map[string]string{
machineRoleLabelName: "master",
machineTypeLabelName: "master",
machinev1beta1.MachineClusterIDLabel: testClusterIDValue,
resourcebuilder.MachineRoleLabelName: "master",
resourcebuilder.MachineTypeLabelName: "master",
machinev1beta1.MachineClusterIDLabel: resourcebuilder.TestClusterIDValue,
},
},
strategyType: machinev1.RollingUpdate,
Expand All @@ -65,7 +68,7 @@ func ControlPlaneMachineSet() ControlPlaneMachineSetBuilder {
// ControlPlaneMachineSetBuilder is used to build out a controlplanemachineset object.
type ControlPlaneMachineSetBuilder struct {
generation int64
machineTemplateBuilder ControlPlaneMachineSetTemplateBuilder
machineTemplateBuilder resourcebuilder.ControlPlaneMachineSetTemplateBuilder
name string
namespace string
replicas int32
Expand All @@ -84,7 +87,7 @@ func (m ControlPlaneMachineSetBuilder) Build() *machinev1.ControlPlaneMachineSet
Generation: m.generation,
},
Spec: machinev1.ControlPlaneMachineSetSpec{
Replicas: int32Ptr(m.replicas),
Replicas: pointer.Int32(m.replicas),
Selector: m.selector,
State: m.state,
Strategy: machinev1.ControlPlaneMachineSetStrategy{
Expand All @@ -104,7 +107,7 @@ func (m ControlPlaneMachineSetBuilder) Build() *machinev1.ControlPlaneMachineSet
}

// WithMachineTemplateBuilder sets the machine template builder for the controlplanemachineset builder.
func (m ControlPlaneMachineSetBuilder) WithMachineTemplateBuilder(builder ControlPlaneMachineSetTemplateBuilder) ControlPlaneMachineSetBuilder {
func (m ControlPlaneMachineSetBuilder) WithMachineTemplateBuilder(builder resourcebuilder.ControlPlaneMachineSetTemplateBuilder) ControlPlaneMachineSetBuilder {
m.machineTemplateBuilder = builder
return m
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
// While the builders are almost identical, we need to keep them separate because they build different objects.
//
//nolint:dupl
package resourcebuilder
package v1

import (
configv1 "github.com/openshift/api/config/v1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1beta1

import (
"encoding/json"
Expand All @@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

// AWSProviderSpec creates a new AWS machine config builder.
Expand Down Expand Up @@ -71,22 +72,22 @@ func (m AWSProviderSpecBuilder) Build() *machinev1beta1.AWSMachineProviderConfig
Kind: "AWSMachineProviderConfig",
},
AMI: machinev1beta1.AWSResourceReference{
ID: stringPtr("aws-ami-12345678"),
ID: pointer.String("aws-ami-12345678"),
},
BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{
{
EBS: &machinev1beta1.EBSBlockDeviceSpec{
Encrypted: boolPtr(true),
VolumeSize: int64Ptr(120),
VolumeType: stringPtr("gp3"),
Encrypted: pointer.Bool(true),
VolumeSize: pointer.Int64(120),
VolumeType: pointer.String("gp3"),
},
},
},
CredentialsSecret: &corev1.LocalObjectReference{
Name: "aws-cloud-credentials",
},
IAMInstanceProfile: &machinev1beta1.AWSResourceReference{
ID: stringPtr("aws-iam-instance-profile-12345678"),
ID: pointer.String("aws-iam-instance-profile-12345678"),
},
InstanceType: m.instanceType,
LoadBalancers: []machinev1beta1.LoadBalancerReference{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1beta1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1beta1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1beta1

import (
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
machineRoleLabelName = "machine.openshift.io/cluster-api-machine-role"
machineTypeLabelName = "machine.openshift.io/cluster-api-machine-type"
)
const ()

// Machine creates a new machine builder.
func Machine() MachineBuilder {
Expand All @@ -39,7 +37,7 @@ type MachineBuilder struct {
namespace string
labels map[string]string
creationTimestamp metav1.Time
providerSpecBuilder RawExtensionBuilder
providerSpecBuilder resourcebuilder.RawExtensionBuilder

// status fields
errorMessage *string
Expand Down Expand Up @@ -68,23 +66,23 @@ func (m MachineBuilder) Build() *machinev1beta1.Machine {
machine.Spec.ProviderSpec.Value = m.providerSpecBuilder.BuildRawExtension()
}

m.WithLabel(machinev1beta1.MachineClusterIDLabel, testClusterIDValue)
m.WithLabel(machinev1beta1.MachineClusterIDLabel, resourcebuilder.TestClusterIDValue)

return machine
}

// AsWorker sets the worker role and type on the machine labels for the machine builder.
func (m MachineBuilder) AsWorker() MachineBuilder {
return m.
WithLabel(machineRoleLabelName, "worker").
WithLabel(machineTypeLabelName, "worker")
WithLabel(resourcebuilder.MachineRoleLabelName, "worker").
WithLabel(resourcebuilder.MachineTypeLabelName, "worker")
}

// AsMaster sets the master role and type on the machine labels for the machine builder.
func (m MachineBuilder) AsMaster() MachineBuilder {
return m.
WithLabel(machineRoleLabelName, "master").
WithLabel(machineTypeLabelName, "master")
WithLabel(resourcebuilder.MachineRoleLabelName, "master").
WithLabel(resourcebuilder.MachineTypeLabelName, "master")
}

// WithCreationTimestamp sets the creationTimestamp for the machine builder.
Expand Down Expand Up @@ -129,7 +127,7 @@ func (m MachineBuilder) WithNamespace(namespace string) MachineBuilder {
}

// WithProviderSpecBuilder sets the providerSpec builder for the machine builder.
func (m MachineBuilder) WithProviderSpecBuilder(builder RawExtensionBuilder) MachineBuilder {
func (m MachineBuilder) WithProviderSpecBuilder(builder resourcebuilder.RawExtensionBuilder) MachineBuilder {
m.providerSpecBuilder = builder
return m
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcebuilder
package v1beta1

import (
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-api-actuator-pkg/pkg/testutils/resourcebuilder"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -39,7 +40,7 @@ type MachineSetBuilder struct {
replicas int32
labels map[string]string
creationTimestamp metav1.Time
providerSpecBuilder RawExtensionBuilder
providerSpecBuilder resourcebuilder.RawExtensionBuilder

// status fields
errorMessage *string
Expand Down Expand Up @@ -75,7 +76,7 @@ func (m MachineSetBuilder) Build() *machinev1beta1.MachineSet {
machineSet.Spec.Template.Spec.ProviderSpec.Value = m.providerSpecBuilder.BuildRawExtension()
}

m.WithLabel(machinev1beta1.MachineClusterIDLabel, testClusterIDValue)
m.WithLabel(machinev1beta1.MachineClusterIDLabel, resourcebuilder.TestClusterIDValue)

return machineSet
}
Expand Down Expand Up @@ -129,7 +130,7 @@ func (m MachineSetBuilder) WithNamespace(namespace string) MachineSetBuilder {
}

// WithProviderSpecBuilder sets the providerSpec builder for the machineSet builder.
func (m MachineSetBuilder) WithProviderSpecBuilder(builder RawExtensionBuilder) MachineSetBuilder {
func (m MachineSetBuilder) WithProviderSpecBuilder(builder resourcebuilder.RawExtensionBuilder) MachineSetBuilder {
m.providerSpecBuilder = builder
return m
}
Expand Down
Loading

0 comments on commit 399c89b

Please sign in to comment.