Skip to content

Commit

Permalink
Delete() funcs should return nil if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed May 20, 2024
1 parent 1be12d6 commit c4915c4
Show file tree
Hide file tree
Showing 77 changed files with 243 additions and 216 deletions.
4 changes: 3 additions & 1 deletion pkg/assisted/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ func (builder *agentBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("agent cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/assisted/agentclusterinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ func (builder *AgentClusterInstallBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("agentclusterinstall cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/assisted/agentserviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ func (builder *AgentServiceConfigBuilder) Delete() error {
builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("agentserviceconfig cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/assisted/infraenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,9 @@ func (builder *InfraEnvBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("infraenv cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/bmh/baremetalhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ func (builder *BmhBuilder) Delete() (*BmhBuilder, error) {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return builder, fmt.Errorf("bmh cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
6 changes: 3 additions & 3 deletions pkg/bmh/baremetalhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestBareMetalHostDelete(t *testing.T) {
},
{
testBmHost: buildValidBmHostBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("bmh cannot be deleted because it does not exist"),
expectedError: nil,
},
{
testBmHost: buildInValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject()),
Expand Down Expand Up @@ -943,7 +943,7 @@ func TestBareMetalHostDeleteAndWaitUntilDeleted(t *testing.T) {
},
{
testBmHost: buildValidBmHostBuilder(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("bmh cannot be deleted because it does not exist"),
expectedError: nil,
},
{
testBmHost: buildInValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject()),
Expand All @@ -952,7 +952,7 @@ func TestBareMetalHostDeleteAndWaitUntilDeleted(t *testing.T) {
}

for _, testCase := range testCases {
builder, err := testCase.testBmHost.DeleteAndWaitUntilDeleted(2 * time.Second)
builder, err := testCase.testBmHost.DeleteAndWaitUntilDeleted(5 * time.Second)
assert.Equal(t, testCase.expectedError, err)

if testCase.expectedError == nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cgu/cgu.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ func (builder *CguBuilder) Delete() (*CguBuilder, error) {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return builder, fmt.Errorf("cgu cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.RanV1alpha1().ClusterGroupUpgrades(builder.Definition.Namespace).Delete(
Expand Down
39 changes: 10 additions & 29 deletions pkg/cgu/cgu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,39 +331,24 @@ func TestCguExist(t *testing.T) {

func TestCguUpdate(t *testing.T) {
testCases := []struct {
alreadyExists bool
force bool
force bool
}{
{
alreadyExists: false,
force: false,
force: true,
},
{
alreadyExists: true,
force: false,
},
{
alreadyExists: false,
force: true,
},
{
alreadyExists: true,
force: true,
force: false,
},
}

for _, testCase := range testCases {
testBuilder := buildValidCguTestBuilder(clients.GetTestClients(clients.TestClientParams{}))

// Create the builder rather than just adding it to the client so that the proper metadata is added and
// the update will not fail.
if testCase.alreadyExists {
var err error
var err error

testBuilder = buildValidCguTestBuilder(clients.GetTestClients(clients.TestClientParams{}))
testBuilder, err = testBuilder.Create()
assert.Nil(t, err)
}
testBuilder := buildValidCguTestBuilder(clients.GetTestClients(clients.TestClientParams{}))
testBuilder, err = testBuilder.Create()
assert.Nil(t, err)

assert.NotNil(t, testBuilder.Definition)
assert.False(t, testBuilder.Definition.Spec.Backup)
Expand All @@ -373,13 +358,9 @@ func TestCguUpdate(t *testing.T) {
cguBuilder, err := testBuilder.Update(testCase.force)
assert.NotNil(t, testBuilder.Definition)

if testCase.alreadyExists {
assert.Nil(t, err)
assert.Equal(t, testBuilder.Definition.Name, cguBuilder.Definition.Name)
assert.Equal(t, testBuilder.Definition.Spec.Backup, cguBuilder.Definition.Spec.Backup)
} else {
assert.NotNil(t, err)
}
assert.Nil(t, err)
assert.Equal(t, testBuilder.Definition.Name, cguBuilder.Definition.Name)
assert.Equal(t, testBuilder.Definition.Spec.Backup, cguBuilder.Definition.Spec.Backup)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/clusterlogging/clusterlogforwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ func (builder *ClusterLogForwarderBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("clusterlogforwarder cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
9 changes: 5 additions & 4 deletions pkg/clusterlogging/clusterlogforwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestClusterLogForwarderDelete(t *testing.T) {
},
{
testClusterLogForwarder: buildInValidClusterLogForwarderBuilder(buildClusterLogForwarderClientWithDummyObject()),
expectedError: fmt.Errorf("clusterlogforwarder cannot be deleted because it does not exist"),
expectedError: nil,
},
{
testClusterLogForwarder: buildValidClusterLogForwarderBuilder(clients.GetTestClients(clients.TestClientParams{})),
Expand Down Expand Up @@ -286,9 +286,10 @@ func TestClusterLogForwarderUpdate(t *testing.T) {
},
{
testClusterLogForwarder: buildInValidClusterLogForwarderBuilder(buildClusterLogForwarderClientWithDummyObject()),
expectedError: "clusterlogforwarder cannot be deleted because it does not exist",
outputs: newOutputs,
pipelines: newPipelines,
//nolint:lll
expectedError: `ClusterLogForwarder.logging.openshift.io "" is invalid: metadata.name: Required value: name is required`,
outputs: newOutputs,
pipelines: newPipelines,
},
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/clusterlogging/clusterlogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func (builder *Builder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("clusterLogging cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 0 additions & 4 deletions pkg/clusterlogging/clusterlogging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ func TestClusterLoggingDelete(t *testing.T) {
clusterLogging: buildValidClusterLogging(buildClusterLoggingTestClientWithDummyObject()),
expectedError: nil,
},
{
clusterLogging: buildValidClusterLogging(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("clusterLogging cannot be deleted because it does not exist"),
},
{
clusterLogging: buildInValidClusterLogging(clients.GetTestClients(clients.TestClientParams{})),
expectedError: fmt.Errorf("the clusterLogging 'name' cannot be empty"),
Expand Down
2 changes: 2 additions & 0 deletions pkg/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (builder *Builder) Delete() error {
glog.V(100).Infof("Deleting the configmap %s from namespace %s", builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
builder.Object = nil

return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func (builder *Builder) Delete() error {
glog.V(100).Infof("Deleting the console object %s", builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("console cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Consoles().Delete(context.TODO(), builder.Definition.Name, metav1.DeleteOptions{})
Expand Down
2 changes: 2 additions & 0 deletions pkg/daemonset/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func (builder *Builder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
builder.Object = nil

return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/hive/clusterdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ func (builder *ClusterDeploymentBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("clusterdeployment cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/hive/clusterimageset.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ func (builder *ClusterImageSetBuilder) Delete() error {
glog.V(100).Infof("Deleting the clusterimageset %s", builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("clusterimageset cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/hive/hiveconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func (builder *HiveConfigBuilder) Delete() error {
glog.V(100).Infof("Deleting the HiveConfig %s", builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("hiveconfig cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
2 changes: 2 additions & 0 deletions pkg/icsp/icsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func (builder *ICSPBuilder) Delete() error {
glog.V(100).Infof("Deleting ImageContentSourcePolicy %s", builder.Definition.Name)

if !builder.Exists() {
builder.Object = nil

return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/kmm/managedclustermodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ func (builder *ManagedClusterModuleBuilder) Delete() (*ManagedClusterModuleBuild
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return builder, fmt.Errorf("managedclustermodule cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/kmm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ func (builder *ModuleBuilder) Delete() (*ModuleBuilder, error) {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return builder, fmt.Errorf("module cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/kmm/preflightvalidationocp.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ func (builder *PreflightValidationOCPBuilder) Delete() (*PreflightValidationOCPB
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return builder, fmt.Errorf("preflightvalidationocp cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/lca/imagebasedupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func (builder *ImageBasedUpgradeBuilder) Delete() (*ImageBasedUpgradeBuilder, er
builder.Definition.Name)

if !builder.Exists() {
return builder, fmt.Errorf("imagebasedupgrade cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/lca/seedgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (builder *SeedGeneratorBuilder) Delete() (*SeedGeneratorBuilder, error) {
builder.Definition.Name)

if !builder.Exists() {
return builder, fmt.Errorf("seedgenerator cannot be deleted because it does not exist")
builder.Object = nil

return builder, nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/lso/localvolumediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (builder *LocalVolumeDiscoveryBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("localVolumeDiscovery cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/lso/localvolumeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func (builder *LocalVolumeSetBuilder) Delete() error {
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return fmt.Errorf("localVolumeSet cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)
Expand Down
4 changes: 3 additions & 1 deletion pkg/machine/machineset.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func (builder *SetBuilder) Delete() error {
builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("machineSet cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.MachineSets(builder.Object.Namespace).Delete(
Expand Down
4 changes: 3 additions & 1 deletion pkg/mco/kubeletconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ func (builder *KubeletConfigBuilder) Delete() error {
glog.V(100).Infof("Deleting the kubeletconfig object %s", builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("kubeletconfig cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.KubeletConfigs().Delete(
Expand Down
4 changes: 3 additions & 1 deletion pkg/mco/machineconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ func (builder *MCBuilder) Delete() error {
glog.V(100).Infof("Deleting the MachineConfig object %s", builder.Definition.Name)

if !builder.Exists() {
return fmt.Errorf("MachineConfig cannot be deleted because it does not exist")
builder.Object = nil

return nil
}

err := builder.apiClient.MachineConfigs().Delete(
Expand Down
Loading

0 comments on commit c4915c4

Please sign in to comment.