diff --git a/api/v1alpha2/linodemachine_types.go b/api/v1alpha2/linodemachine_types.go index 703895692..2b4d7b076 100644 --- a/api/v1alpha2/linodemachine_types.go +++ b/api/v1alpha2/linodemachine_types.go @@ -17,6 +17,8 @@ limitations under the License. package v1alpha2 import ( + "slices" + "github.com/linode/linodego" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -631,11 +633,9 @@ func (lm *LinodeMachine) GetCondition(condType string) *metav1.Condition { } func (lm *LinodeMachine) DeleteCondition(condType string) { - for i := range lm.Status.Conditions { - if lm.Status.Conditions[i].Type == condType { - lm.Status.Conditions = append(lm.Status.Conditions[:i], lm.Status.Conditions[i+1:]...) - } - } + lm.Status.Conditions = slices.DeleteFunc(lm.Status.Conditions, func(c metav1.Condition) bool { + return c.Type == condType + }) } func (lm *LinodeMachine) IsPaused() bool { diff --git a/api/v1alpha2/linodemachine_types_test.go b/api/v1alpha2/linodemachine_types_test.go new file mode 100644 index 000000000..13a45a082 --- /dev/null +++ b/api/v1alpha2/linodemachine_types_test.go @@ -0,0 +1,62 @@ +/* +Copyright 2023 Akamai Technologies, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha2 + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestDeleteCondition(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + length int + delta int + del string + }{ + {name: "empty", length: 0, del: "type0"}, + {name: "delete-only", length: 1, del: "type0", delta: -1}, + {name: "delete-first", length: 3, del: "type0", delta: -1}, + {name: "delete-last", length: 3, del: "type2", delta: -1}, + {name: "delete-missing", length: 3, del: "type3"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + conds := make([]metav1.Condition, tc.length) + for i := 0; i < tc.length; i++ { + conds[i] = metav1.Condition{Type: fmt.Sprintf("type%d", i)} + } + lm := &LinodeMachine{ + Status: LinodeMachineStatus{ + Conditions: conds, + }, + } + + lm.DeleteCondition(tc.del) + assert.NotContains(t, lm.Status.Conditions, tc.del) + assert.Len(t, lm.Status.Conditions, tc.length+tc.delta) + }) + } +}